blob: d279e04729dc4fbb5fbc54ca7a963b48ab14f877 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt();
173 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000318 // Since AVX is a superset of SSE3, only check for SSE here.
319 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 // Expand FP_TO_UINT into a select.
321 // FIXME: We would like to use a Custom expander here eventually to do
322 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000325 // With SSE3 we can use fisttpll to convert to a signed i64; without
326 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329
Chris Lattner399610a2006-12-05 18:22:22 +0000330 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000331 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
333 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000334 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000336 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000338 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000339 }
Chris Lattner21f66852005-12-23 05:15:23 +0000340
Dan Gohmanb00ee212008-02-18 19:34:53 +0000341 // Scalar integer divide and remainder are lowered to use operations that
342 // produce two results, to match the available instructions. This exposes
343 // the two-result form to trivial CSE, which is able to combine x/y and x%y
344 // into a single instruction.
345 //
346 // Scalar integer multiply-high is also lowered to use two-result
347 // operations, to match the available instructions. However, plain multiply
348 // (low) operations are left as Legal, as there are single-result
349 // instructions for this in x86. Using the two-result multiply instructions
350 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 for (unsigned i = 0, e = 4; i != e; ++i) {
352 MVT VT = IntVTs[i];
353 setOperationAction(ISD::MULHS, VT, Expand);
354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::SDIV, VT, Expand);
356 setOperationAction(ISD::UDIV, VT, Expand);
357 setOperationAction(ISD::SREM, VT, Expand);
358 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000359
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000360 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000361 setOperationAction(ISD::ADDC, VT, Custom);
362 setOperationAction(ISD::ADDE, VT, Custom);
363 setOperationAction(ISD::SUBC, VT, Custom);
364 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000365 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000366
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
368 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
369 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
370 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
376 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f32 , Expand);
378 setOperationAction(ISD::FREM , MVT::f64 , Expand);
379 setOperationAction(ISD::FREM , MVT::f80 , Expand);
380 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
383 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000384 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
385 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000388 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
390 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000391 }
392
Benjamin Kramer1292c222010-12-04 20:32:23 +0000393 if (Subtarget->hasPOPCNT()) {
394 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
395 } else {
396 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
397 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
398 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
399 if (Subtarget->is64Bit())
400 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
401 }
402
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
404 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000405
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000406 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000407 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000408 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000409 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000410 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
412 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
413 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
414 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
415 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000416 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
418 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
419 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
420 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000423 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000426
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000427 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
429 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
430 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
431 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000432 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
434 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000435 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000436 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
438 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
439 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
440 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000441 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000442 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000443 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
445 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
446 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000447 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
449 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
450 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000451 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000453 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000455
Eric Christopher9a9d2752010-07-22 02:48:34 +0000456 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000457 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000458
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000459 // On X86 and X86-64, atomic operations are lowered to locked instructions.
460 // Locked instructions, in turn, have implicit fence semantics (all memory
461 // operations are flushed before issuing the locked instruction, and they
462 // are not buffered), so we can fold away the common pattern of
463 // fence-atomic-fence.
464 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000465
Mon P Wang63307c32008-05-05 19:05:59 +0000466 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000467 for (unsigned i = 0, e = 4; i != e; ++i) {
468 MVT VT = IntVTs[i];
469 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
470 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000471 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000472 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000473
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000474 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000475 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
480 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
481 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
482 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000483 }
484
Eli Friedman43f51ae2011-08-26 21:21:21 +0000485 if (Subtarget->hasCmpxchg16b()) {
486 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
487 }
488
Evan Cheng3c992d22006-03-07 02:02:57 +0000489 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000490 if (!Subtarget->isTargetDarwin() &&
491 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000492 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000494 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000495
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
497 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
498 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
499 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000500 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000501 setExceptionPointerRegister(X86::RAX);
502 setExceptionSelectorRegister(X86::RDX);
503 } else {
504 setExceptionPointerRegister(X86::EAX);
505 setExceptionSelectorRegister(X86::EDX);
506 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
508 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000509
Duncan Sands4a544a72011-09-06 13:37:06 +0000510 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
511 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000512
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000514
Nate Begemanacc398c2006-01-25 18:21:52 +0000515 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::VASTART , MVT::Other, Custom);
517 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000518 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::VAARG , MVT::Other, Custom);
520 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000521 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::VAARG , MVT::Other, Expand);
523 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000524 }
Evan Chengae642192007-03-02 23:16:35 +0000525
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
527 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000528
529 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
530 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
531 MVT::i64 : MVT::i32, Custom);
532 else if (EnableSegmentedStacks)
533 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
534 MVT::i64 : MVT::i32, Custom);
535 else
536 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
537 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000538
Evan Chengc7ce29b2009-02-13 22:36:38 +0000539 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000540 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000541 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
543 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000544
Evan Cheng223547a2006-01-31 22:28:30 +0000545 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::FABS , MVT::f64, Custom);
547 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000548
549 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::FNEG , MVT::f64, Custom);
551 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000552
Evan Cheng68c47cb2007-01-05 07:55:56 +0000553 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
555 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000556
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000557 // Lower this to FGETSIGNx86 plus an AND.
558 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
559 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
560
Evan Chengd25e9e82006-02-02 00:28:23 +0000561 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FSIN , MVT::f64, Expand);
563 setOperationAction(ISD::FCOS , MVT::f64, Expand);
564 setOperationAction(ISD::FSIN , MVT::f32, Expand);
565 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000566
Chris Lattnera54aa942006-01-29 06:26:08 +0000567 // Expand FP immediates into loads from the stack, except for the special
568 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000569 addLegalFPImmediate(APFloat(+0.0)); // xorpd
570 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000571 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000572 // Use SSE for f32, x87 for f64.
573 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
575 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000576
577 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000579
580 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000582
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
587 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
589 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::FSIN , MVT::f32, Expand);
591 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
Nate Begemane1795842008-02-14 08:57:00 +0000593 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000594 addLegalFPImmediate(APFloat(+0.0f)); // xorps
595 addLegalFPImmediate(APFloat(+0.0)); // FLD0
596 addLegalFPImmediate(APFloat(+1.0)); // FLD1
597 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
598 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
599
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
602 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000604 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000605 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000606 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
608 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000609
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
611 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
612 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
613 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000614
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000615 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
617 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000618 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000619 addLegalFPImmediate(APFloat(+0.0)); // FLD0
620 addLegalFPImmediate(APFloat(+1.0)); // FLD1
621 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
622 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
624 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
625 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
626 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000627 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000628
Cameron Zwarich33390842011-07-08 21:39:21 +0000629 // We don't support FMA.
630 setOperationAction(ISD::FMA, MVT::f64, Expand);
631 setOperationAction(ISD::FMA, MVT::f32, Expand);
632
Dale Johannesen59a58732007-08-05 18:49:15 +0000633 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000634 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
636 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
637 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000638 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000639 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000640 addLegalFPImmediate(TmpFlt); // FLD0
641 TmpFlt.changeSign();
642 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000643
644 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000645 APFloat TmpFlt2(+1.0);
646 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
647 &ignored);
648 addLegalFPImmediate(TmpFlt2); // FLD1
649 TmpFlt2.changeSign();
650 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
651 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000652
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
655 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000657
658 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000659 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000660
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000661 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
663 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
664 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000665
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FLOG, MVT::f80, Expand);
667 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
668 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
669 setOperationAction(ISD::FEXP, MVT::f80, Expand);
670 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000671
Mon P Wangf007a8b2008-11-06 05:31:54 +0000672 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000673 // (for widening) or expand (for scalarization). Then we will selectively
674 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
676 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
677 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000693 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
694 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000695 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000716 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000726 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000727 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000731 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000732 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
733 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
734 setTruncStoreAction((MVT::SimpleValueType)VT,
735 (MVT::SimpleValueType)InnerVT, Expand);
736 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
737 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
738 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000739 }
740
Evan Chengc7ce29b2009-02-13 22:36:38 +0000741 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
742 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000743 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000744 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000745 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000746 }
747
Dale Johannesen0488fb62010-09-30 23:57:10 +0000748 // MMX-sized vectors (other than x86mmx) are expected to be expanded
749 // into smaller operations.
750 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
751 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
752 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
753 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
754 setOperationAction(ISD::AND, MVT::v8i8, Expand);
755 setOperationAction(ISD::AND, MVT::v4i16, Expand);
756 setOperationAction(ISD::AND, MVT::v2i32, Expand);
757 setOperationAction(ISD::AND, MVT::v1i64, Expand);
758 setOperationAction(ISD::OR, MVT::v8i8, Expand);
759 setOperationAction(ISD::OR, MVT::v4i16, Expand);
760 setOperationAction(ISD::OR, MVT::v2i32, Expand);
761 setOperationAction(ISD::OR, MVT::v1i64, Expand);
762 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
763 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
764 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
765 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
766 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
767 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
768 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
769 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
771 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
772 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
773 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
774 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000775 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
776 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
777 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
778 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000779
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000780 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000782
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
784 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
785 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
786 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
787 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
788 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
789 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
790 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
791 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
792 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
793 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000794 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795 }
796
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000797 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000799
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000800 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
801 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
803 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
804 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
805 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000806
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
808 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
809 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
810 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
811 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
812 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
813 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
814 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
815 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
816 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
817 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
818 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
819 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
820 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
821 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
822 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000823
Duncan Sands28b77e92011-09-06 19:07:46 +0000824 setOperationAction(ISD::SETCC, MVT::v2f64, Custom);
825 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
826 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
827 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
830 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
833 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000834
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000835 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
836 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
837 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
838 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
839 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
840
Evan Cheng2c3ae372006-04-12 21:21:57 +0000841 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
843 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000844 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000845 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000846 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000847 // Do not attempt to custom lower non-128-bit vectors
848 if (!VT.is128BitVector())
849 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 setOperationAction(ISD::BUILD_VECTOR,
851 VT.getSimpleVT().SimpleTy, Custom);
852 setOperationAction(ISD::VECTOR_SHUFFLE,
853 VT.getSimpleVT().SimpleTy, Custom);
854 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
855 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000856 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000857
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
859 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000864
Nate Begemancdd1eec2008-02-12 22:51:28 +0000865 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000868 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000870 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
872 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000873 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000874
875 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000876 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000877 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000878
Owen Andersond6662ad2009-08-10 20:46:15 +0000879 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000881 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000883 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000885 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000887 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000889 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000890
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000892
Evan Cheng2c3ae372006-04-12 21:21:57 +0000893 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
895 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
896 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
897 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
900 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000901 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000902
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000903 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000904 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
905 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
906 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
907 setOperationAction(ISD::FRINT, MVT::f32, Legal);
908 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
909 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
910 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
911 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
912 setOperationAction(ISD::FRINT, MVT::f64, Legal);
913 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
914
Nate Begeman14d12ca2008-02-11 04:19:36 +0000915 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000917
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000918 // Can turn SHL into an integer multiply.
919 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000920 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000921
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000922 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
923 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
924 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
925 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
926 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 // i8 and i16 vectors are custom , because the source register and source
929 // source memory operand types are not the same width. f32 vectors are
930 // custom since the immediate controlling the insert encodes additional
931 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
934 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
935 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000936
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
939 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000941
942 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945 }
946 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000947
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000948 if (Subtarget->hasXMMInt()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000949 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
950 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
951 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000952 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000953
954 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
955 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
956 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
957
958 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
959 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
960 }
961
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000962 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000963 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000964
David Greene9b9838d2009-06-29 16:47:10 +0000965 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000966 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
967 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
968 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
969 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
970 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
971 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000972
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
975 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000976
Owen Anderson825b72b2009-08-11 20:47:22 +0000977 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
978 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
979 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
980 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
981 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
982 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000983
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
985 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
986 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
987 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
988 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
989 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000990
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000991 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
992 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000993 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000994
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
999 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1000 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1001
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001002 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1003 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1004 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1005 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1006
1007 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1008 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1009 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1010 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1011
1012 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1013 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1014
Duncan Sands28b77e92011-09-06 19:07:46 +00001015 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1016 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1017 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1018 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001019
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001020 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1021 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1022 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1023
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001024 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1025 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1026 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1027 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001028
Craig Topper13894fa2011-08-24 06:14:18 +00001029 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1030 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1031 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1032 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1033
1034 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1035 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1036 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1037 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1038
1039 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1040 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1041 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1042 // Don't lower v32i8 because there is no 128-bit byte mul
1043
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001044 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001045 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001046 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1047 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1048 EVT VT = SVT;
1049
1050 // Extract subvector is special because the value type
1051 // (result) is 128-bit but the source is 256-bit wide.
1052 if (VT.is128BitVector())
1053 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1054
1055 // Do not attempt to custom lower other non-256-bit vectors
1056 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001057 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001058
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001059 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1060 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1061 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1062 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001063 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001064 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001065 }
1066
David Greene54d8eba2011-01-27 22:38:56 +00001067 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001068 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1069 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1070 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001071
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001072 // Do not attempt to promote non-256-bit vectors
1073 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001074 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001075
1076 setOperationAction(ISD::AND, SVT, Promote);
1077 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1078 setOperationAction(ISD::OR, SVT, Promote);
1079 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1080 setOperationAction(ISD::XOR, SVT, Promote);
1081 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1082 setOperationAction(ISD::LOAD, SVT, Promote);
1083 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1084 setOperationAction(ISD::SELECT, SVT, Promote);
1085 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001086 }
David Greene9b9838d2009-06-29 16:47:10 +00001087 }
1088
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001089 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1090 // of this type with custom code.
1091 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1092 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1093 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1094 }
1095
Evan Cheng6be2c582006-04-05 23:38:46 +00001096 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001097 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001098
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001099
Eli Friedman962f5492010-06-02 19:35:46 +00001100 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1101 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001102 //
Eli Friedman962f5492010-06-02 19:35:46 +00001103 // FIXME: We really should do custom legalization for addition and
1104 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1105 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001106 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1107 // Add/Sub/Mul with overflow operations are custom lowered.
1108 MVT VT = IntVTs[i];
1109 setOperationAction(ISD::SADDO, VT, Custom);
1110 setOperationAction(ISD::UADDO, VT, Custom);
1111 setOperationAction(ISD::SSUBO, VT, Custom);
1112 setOperationAction(ISD::USUBO, VT, Custom);
1113 setOperationAction(ISD::SMULO, VT, Custom);
1114 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001115 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001116
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001117 // There are no 8-bit 3-address imul/mul instructions
1118 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1119 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001120
Evan Chengd54f2d52009-03-31 19:38:51 +00001121 if (!Subtarget->is64Bit()) {
1122 // These libcalls are not available in 32-bit.
1123 setLibcallName(RTLIB::SHL_I128, 0);
1124 setLibcallName(RTLIB::SRL_I128, 0);
1125 setLibcallName(RTLIB::SRA_I128, 0);
1126 }
1127
Evan Cheng206ee9d2006-07-07 08:33:52 +00001128 // We have target-specific dag combine patterns for the following nodes:
1129 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001130 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001131 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001132 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001133 setTargetDAGCombine(ISD::SHL);
1134 setTargetDAGCombine(ISD::SRA);
1135 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001136 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001137 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001138 setTargetDAGCombine(ISD::ADD);
1139 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001140 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001141 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001142 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001143 if (Subtarget->is64Bit())
1144 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001145
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001146 computeRegisterProperties();
1147
Evan Cheng05219282011-01-06 06:52:41 +00001148 // On Darwin, -Os means optimize for size without hurting performance,
1149 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001150 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001151 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001152 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001153 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1154 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1155 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001156 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001157 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001158
1159 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001160}
1161
Scott Michel5b8f82e2008-03-10 15:42:14 +00001162
Duncan Sands28b77e92011-09-06 19:07:46 +00001163EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1164 if (!VT.isVector()) return MVT::i8;
1165 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001166}
1167
1168
Evan Cheng29286502008-01-23 23:17:41 +00001169/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1170/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001171static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001172 if (MaxAlign == 16)
1173 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001174 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001175 if (VTy->getBitWidth() == 128)
1176 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001177 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001178 unsigned EltAlign = 0;
1179 getMaxByValAlign(ATy->getElementType(), EltAlign);
1180 if (EltAlign > MaxAlign)
1181 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001182 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001183 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1184 unsigned EltAlign = 0;
1185 getMaxByValAlign(STy->getElementType(i), EltAlign);
1186 if (EltAlign > MaxAlign)
1187 MaxAlign = EltAlign;
1188 if (MaxAlign == 16)
1189 break;
1190 }
1191 }
1192 return;
1193}
1194
1195/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1196/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001197/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1198/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001199unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001200 if (Subtarget->is64Bit()) {
1201 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001202 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001203 if (TyAlign > 8)
1204 return TyAlign;
1205 return 8;
1206 }
1207
Evan Cheng29286502008-01-23 23:17:41 +00001208 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001209 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001210 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001211 return Align;
1212}
Chris Lattner2b02a442007-02-25 08:29:00 +00001213
Evan Chengf0df0312008-05-15 08:39:06 +00001214/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001215/// and store operations as a result of memset, memcpy, and memmove
1216/// lowering. If DstAlign is zero that means it's safe to destination
1217/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1218/// means there isn't a need to check it against alignment requirement,
1219/// probably because the source does not need to be loaded. If
1220/// 'NonScalarIntSafe' is true, that means it's safe to return a
1221/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1222/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1223/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001224/// It returns EVT::Other if the type should be determined using generic
1225/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001226EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001227X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1228 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001229 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001230 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001231 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001232 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1233 // linux. This is because the stack realignment code can't handle certain
1234 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001235 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001236 if (NonScalarIntSafe &&
1237 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001238 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001239 (Subtarget->isUnalignedMemAccessFast() ||
1240 ((DstAlign == 0 || DstAlign >= 16) &&
1241 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001242 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001243 if (Subtarget->hasAVX() &&
1244 Subtarget->getStackAlignment() >= 32)
1245 return MVT::v8f32;
1246 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001247 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001248 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001249 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001250 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001251 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001252 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001253 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001254 // Do not use f64 to lower memcpy if source is string constant. It's
1255 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001256 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001257 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001258 }
Evan Chengf0df0312008-05-15 08:39:06 +00001259 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001260 return MVT::i64;
1261 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001262}
1263
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001264/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1265/// current function. The returned value is a member of the
1266/// MachineJumpTableInfo::JTEntryKind enum.
1267unsigned X86TargetLowering::getJumpTableEncoding() const {
1268 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1269 // symbol.
1270 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1271 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001272 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001273
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001274 // Otherwise, use the normal jump table encoding heuristics.
1275 return TargetLowering::getJumpTableEncoding();
1276}
1277
Chris Lattnerc64daab2010-01-26 05:02:42 +00001278const MCExpr *
1279X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1280 const MachineBasicBlock *MBB,
1281 unsigned uid,MCContext &Ctx) const{
1282 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1283 Subtarget->isPICStyleGOT());
1284 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1285 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001286 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1287 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001288}
1289
Evan Chengcc415862007-11-09 01:32:10 +00001290/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1291/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001292SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001293 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001294 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001295 // This doesn't have DebugLoc associated with it, but is not really the
1296 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001297 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001298 return Table;
1299}
1300
Chris Lattner589c6f62010-01-26 06:28:43 +00001301/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1302/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1303/// MCExpr.
1304const MCExpr *X86TargetLowering::
1305getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1306 MCContext &Ctx) const {
1307 // X86-64 uses RIP relative addressing based on the jump table label.
1308 if (Subtarget->isPICStyleRIPRel())
1309 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1310
1311 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001312 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001313}
1314
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001315// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001316std::pair<const TargetRegisterClass*, uint8_t>
1317X86TargetLowering::findRepresentativeClass(EVT VT) const{
1318 const TargetRegisterClass *RRC = 0;
1319 uint8_t Cost = 1;
1320 switch (VT.getSimpleVT().SimpleTy) {
1321 default:
1322 return TargetLowering::findRepresentativeClass(VT);
1323 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1324 RRC = (Subtarget->is64Bit()
1325 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1326 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001327 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001328 RRC = X86::VR64RegisterClass;
1329 break;
1330 case MVT::f32: case MVT::f64:
1331 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1332 case MVT::v4f32: case MVT::v2f64:
1333 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1334 case MVT::v4f64:
1335 RRC = X86::VR128RegisterClass;
1336 break;
1337 }
1338 return std::make_pair(RRC, Cost);
1339}
1340
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001341bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1342 unsigned &Offset) const {
1343 if (!Subtarget->isTargetLinux())
1344 return false;
1345
1346 if (Subtarget->is64Bit()) {
1347 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1348 Offset = 0x28;
1349 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1350 AddressSpace = 256;
1351 else
1352 AddressSpace = 257;
1353 } else {
1354 // %gs:0x14 on i386
1355 Offset = 0x14;
1356 AddressSpace = 256;
1357 }
1358 return true;
1359}
1360
1361
Chris Lattner2b02a442007-02-25 08:29:00 +00001362//===----------------------------------------------------------------------===//
1363// Return Value Calling Convention Implementation
1364//===----------------------------------------------------------------------===//
1365
Chris Lattner59ed56b2007-02-28 04:55:35 +00001366#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001367
Michael J. Spencerec38de22010-10-10 22:04:20 +00001368bool
Eric Christopher471e4222011-06-08 23:55:35 +00001369X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1370 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001371 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001372 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001373 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001374 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001375 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001376 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001377}
1378
Dan Gohman98ca4f22009-08-05 01:29:28 +00001379SDValue
1380X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001381 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001382 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001383 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001384 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001385 MachineFunction &MF = DAG.getMachineFunction();
1386 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001387
Chris Lattner9774c912007-02-27 05:28:59 +00001388 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001389 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390 RVLocs, *DAG.getContext());
1391 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001392
Evan Chengdcea1632010-02-04 02:40:39 +00001393 // Add the regs to the liveout set for the function.
1394 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1395 for (unsigned i = 0; i != RVLocs.size(); ++i)
1396 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1397 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001398
Dan Gohman475871a2008-07-27 21:46:04 +00001399 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001400
Dan Gohman475871a2008-07-27 21:46:04 +00001401 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001402 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1403 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001404 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1405 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001406
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001407 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001408 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1409 CCValAssign &VA = RVLocs[i];
1410 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001411 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001412 EVT ValVT = ValToCopy.getValueType();
1413
Dale Johannesenc4510512010-09-24 19:05:48 +00001414 // If this is x86-64, and we disabled SSE, we can't return FP values,
1415 // or SSE or MMX vectors.
1416 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1417 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001418 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001419 report_fatal_error("SSE register return with SSE disabled");
1420 }
1421 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1422 // llvm-gcc has never done it right and no one has noticed, so this
1423 // should be OK for now.
1424 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001425 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001426 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001427
Chris Lattner447ff682008-03-11 03:23:40 +00001428 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1429 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001430 if (VA.getLocReg() == X86::ST0 ||
1431 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001432 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1433 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001434 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001435 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001436 RetOps.push_back(ValToCopy);
1437 // Don't emit a copytoreg.
1438 continue;
1439 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001440
Evan Cheng242b38b2009-02-23 09:03:22 +00001441 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1442 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001443 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001444 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001445 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001446 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001447 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1448 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001449 // If we don't have SSE2 available, convert to v4f32 so the generated
1450 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001451 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001452 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001453 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001454 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001455 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001456
Dale Johannesendd64c412009-02-04 00:33:20 +00001457 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001458 Flag = Chain.getValue(1);
1459 }
Dan Gohman61a92132008-04-21 23:59:07 +00001460
1461 // The x86-64 ABI for returning structs by value requires that we copy
1462 // the sret argument into %rax for the return. We saved the argument into
1463 // a virtual register in the entry block, so now we copy the value out
1464 // and into %rax.
1465 if (Subtarget->is64Bit() &&
1466 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1467 MachineFunction &MF = DAG.getMachineFunction();
1468 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1469 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001470 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001471 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001472 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001473
Dale Johannesendd64c412009-02-04 00:33:20 +00001474 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001475 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001476
1477 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001478 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001479 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001480
Chris Lattner447ff682008-03-11 03:23:40 +00001481 RetOps[0] = Chain; // Update chain.
1482
1483 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001484 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001485 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001486
1487 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001488 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001489}
1490
Evan Cheng3d2125c2010-11-30 23:55:39 +00001491bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1492 if (N->getNumValues() != 1)
1493 return false;
1494 if (!N->hasNUsesOfValue(1, 0))
1495 return false;
1496
1497 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001498 if (Copy->getOpcode() != ISD::CopyToReg &&
1499 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001500 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001501
1502 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001503 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001504 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001505 if (UI->getOpcode() != X86ISD::RET_FLAG)
1506 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001507 HasRet = true;
1508 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001509
Evan Cheng1bf891a2010-12-01 22:59:46 +00001510 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001511}
1512
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001513EVT
1514X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001515 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001516 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001517 // TODO: Is this also valid on 32-bit?
1518 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001519 ReturnMVT = MVT::i8;
1520 else
1521 ReturnMVT = MVT::i32;
1522
1523 EVT MinVT = getRegisterType(Context, ReturnMVT);
1524 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001525}
1526
Dan Gohman98ca4f22009-08-05 01:29:28 +00001527/// LowerCallResult - Lower the result values of a call into the
1528/// appropriate copies out of appropriate physical registers.
1529///
1530SDValue
1531X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001532 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001533 const SmallVectorImpl<ISD::InputArg> &Ins,
1534 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001535 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001536
Chris Lattnere32bbf62007-02-28 07:09:55 +00001537 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001538 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001539 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001540 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1541 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Chris Lattner3085e152007-02-25 08:59:22 +00001544 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001545 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001546 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001547 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001548
Torok Edwin3f142c32009-02-01 18:15:56 +00001549 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001550 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001551 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001552 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001553 }
1554
Evan Cheng79fb3b42009-02-20 20:43:02 +00001555 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001556
1557 // If this is a call to a function that returns an fp value on the floating
1558 // point stack, we must guarantee the the value is popped from the stack, so
1559 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001560 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001561 // instead.
1562 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1563 // If we prefer to use the value in xmm registers, copy it out as f80 and
1564 // use a truncate to move it from fp stack reg to xmm reg.
1565 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001566 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001567 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1568 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001569 Val = Chain.getValue(0);
1570
1571 // Round the f80 to the right size, which also moves it to the appropriate
1572 // xmm register.
1573 if (CopyVT != VA.getValVT())
1574 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1575 // This truncation won't change the value.
1576 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001577 } else {
1578 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1579 CopyVT, InFlag).getValue(1);
1580 Val = Chain.getValue(0);
1581 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001582 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001583 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001584 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001585
Dan Gohman98ca4f22009-08-05 01:29:28 +00001586 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001587}
1588
1589
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001590//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001591// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001592//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001593// StdCall calling convention seems to be standard for many Windows' API
1594// routines and around. It differs from C calling convention just a little:
1595// callee should clean up the stack, not caller. Symbols should be also
1596// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001597// For info on fast calling convention see Fast Calling Convention (tail call)
1598// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001599
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001601/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001602static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1603 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001604 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001605
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001607}
1608
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001609/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001610/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611static bool
1612ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1613 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001614 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001615
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001617}
1618
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001619/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1620/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001621/// the specific parameter attribute. The copy will be passed as a byval
1622/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001623static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001624CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001625 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1626 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001627 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001628
Dale Johannesendd64c412009-02-04 00:33:20 +00001629 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001630 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001631 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001632}
1633
Chris Lattner29689432010-03-11 00:22:57 +00001634/// IsTailCallConvention - Return true if the calling convention is one that
1635/// supports tail call optimization.
1636static bool IsTailCallConvention(CallingConv::ID CC) {
1637 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1638}
1639
Evan Cheng485fafc2011-03-21 01:19:09 +00001640bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1641 if (!CI->isTailCall())
1642 return false;
1643
1644 CallSite CS(CI);
1645 CallingConv::ID CalleeCC = CS.getCallingConv();
1646 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1647 return false;
1648
1649 return true;
1650}
1651
Evan Cheng0c439eb2010-01-27 00:07:07 +00001652/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1653/// a tailcall target by changing its ABI.
1654static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001655 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001656}
1657
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658SDValue
1659X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001660 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001661 const SmallVectorImpl<ISD::InputArg> &Ins,
1662 DebugLoc dl, SelectionDAG &DAG,
1663 const CCValAssign &VA,
1664 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001665 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001666 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001667 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001668 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001669 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001670 EVT ValVT;
1671
1672 // If value is passed by pointer we have address passed instead of the value
1673 // itself.
1674 if (VA.getLocInfo() == CCValAssign::Indirect)
1675 ValVT = VA.getLocVT();
1676 else
1677 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001678
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001679 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001680 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001681 // In case of tail call optimization mark all arguments mutable. Since they
1682 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001683 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001684 unsigned Bytes = Flags.getByValSize();
1685 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1686 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001687 return DAG.getFrameIndex(FI, getPointerTy());
1688 } else {
1689 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001690 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001691 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1692 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001693 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001694 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001695 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001696}
1697
Dan Gohman475871a2008-07-27 21:46:04 +00001698SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001699X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001700 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001701 bool isVarArg,
1702 const SmallVectorImpl<ISD::InputArg> &Ins,
1703 DebugLoc dl,
1704 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001705 SmallVectorImpl<SDValue> &InVals)
1706 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001707 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001709
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 const Function* Fn = MF.getFunction();
1711 if (Fn->hasExternalLinkage() &&
1712 Subtarget->isTargetCygMing() &&
1713 Fn->getName() == "main")
1714 FuncInfo->setForceFramePointer(true);
1715
Evan Cheng1bc78042006-04-26 01:20:17 +00001716 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001717 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001718 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001719
Chris Lattner29689432010-03-11 00:22:57 +00001720 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1721 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001722
Chris Lattner638402b2007-02-28 07:00:42 +00001723 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001724 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001725 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001727
1728 // Allocate shadow area for Win64
1729 if (IsWin64) {
1730 CCInfo.AllocateStack(32, 8);
1731 }
1732
Duncan Sands45907662010-10-31 13:21:44 +00001733 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001734
Chris Lattnerf39f7712007-02-28 05:46:49 +00001735 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001736 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001737 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1738 CCValAssign &VA = ArgLocs[i];
1739 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1740 // places.
1741 assert(VA.getValNo() != LastVal &&
1742 "Don't support value assigned to multiple locs yet");
1743 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001744
Chris Lattnerf39f7712007-02-28 05:46:49 +00001745 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001746 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001747 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001748 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001749 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001750 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001751 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001753 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001755 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001756 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1757 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001758 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001759 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001760 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001761 RC = X86::VR64RegisterClass;
1762 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001763 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001764
Devang Patel68e6bee2011-02-21 23:21:26 +00001765 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001766 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001767
Chris Lattnerf39f7712007-02-28 05:46:49 +00001768 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1769 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1770 // right size.
1771 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001772 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001773 DAG.getValueType(VA.getValVT()));
1774 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001775 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001776 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001777 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001778 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001779
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001780 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001781 // Handle MMX values passed in XMM regs.
1782 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001783 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1784 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001785 } else
1786 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001787 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001788 } else {
1789 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001791 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001792
1793 // If value is passed via pointer - do a load.
1794 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001795 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1796 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001797
Dan Gohman98ca4f22009-08-05 01:29:28 +00001798 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001799 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001800
Dan Gohman61a92132008-04-21 23:59:07 +00001801 // The x86-64 ABI for returning structs by value requires that we copy
1802 // the sret argument into %rax for the return. Save the argument into
1803 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001804 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001805 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1806 unsigned Reg = FuncInfo->getSRetReturnReg();
1807 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001808 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001809 FuncInfo->setSRetReturnReg(Reg);
1810 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001811 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001813 }
1814
Chris Lattnerf39f7712007-02-28 05:46:49 +00001815 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001816 // Align stack specially for tail calls.
1817 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001818 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001819
Evan Cheng1bc78042006-04-26 01:20:17 +00001820 // If the function takes variable number of arguments, make a frame index for
1821 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001822 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001823 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1824 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001825 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001826 }
1827 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001828 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1829
1830 // FIXME: We should really autogenerate these arrays
1831 static const unsigned GPR64ArgRegsWin64[] = {
1832 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001833 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001834 static const unsigned GPR64ArgRegs64Bit[] = {
1835 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1836 };
1837 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001838 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1839 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1840 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001841 const unsigned *GPR64ArgRegs;
1842 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001843
1844 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001845 // The XMM registers which might contain var arg parameters are shadowed
1846 // in their paired GPR. So we only need to save the GPR to their home
1847 // slots.
1848 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001849 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001850 } else {
1851 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1852 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001853
1854 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001855 }
1856 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1857 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001858
Devang Patel578efa92009-06-05 21:57:13 +00001859 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001860 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001861 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001862 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001863 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001864 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001865 // Kernel mode asks for SSE to be disabled, so don't push them
1866 // on the stack.
1867 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001868
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001869 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001870 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001871 // Get to the caller-allocated home save location. Add 8 to account
1872 // for the return address.
1873 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001874 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001875 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001876 // Fixup to set vararg frame on shadow area (4 x i64).
1877 if (NumIntRegs < 4)
1878 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001879 } else {
1880 // For X86-64, if there are vararg parameters that are passed via
1881 // registers, then we must store them to their spots on the stack so they
1882 // may be loaded by deferencing the result of va_next.
1883 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1884 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1885 FuncInfo->setRegSaveFrameIndex(
1886 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001887 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001888 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001889
Gordon Henriksen86737662008-01-05 16:56:59 +00001890 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001891 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001892 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1893 getPointerTy());
1894 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001895 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001896 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1897 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001898 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001899 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001901 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001902 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001903 MachinePointerInfo::getFixedStack(
1904 FuncInfo->getRegSaveFrameIndex(), Offset),
1905 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001906 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001907 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001908 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001909
Dan Gohmanface41a2009-08-16 21:24:25 +00001910 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1911 // Now store the XMM (fp + vector) parameter registers.
1912 SmallVector<SDValue, 11> SaveXMMOps;
1913 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001914
Devang Patel68e6bee2011-02-21 23:21:26 +00001915 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001916 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1917 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001918
Dan Gohman1e93df62010-04-17 14:41:14 +00001919 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1920 FuncInfo->getRegSaveFrameIndex()));
1921 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1922 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001923
Dan Gohmanface41a2009-08-16 21:24:25 +00001924 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001925 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001926 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001927 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1928 SaveXMMOps.push_back(Val);
1929 }
1930 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1931 MVT::Other,
1932 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001933 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001934
1935 if (!MemOps.empty())
1936 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1937 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001938 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001939 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001940
Gordon Henriksen86737662008-01-05 16:56:59 +00001941 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001942 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001943 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001944 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001945 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001946 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001947 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001948 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001949 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001950
Gordon Henriksen86737662008-01-05 16:56:59 +00001951 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001952 // RegSaveFrameIndex is X86-64 only.
1953 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001954 if (CallConv == CallingConv::X86_FastCall ||
1955 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001956 // fastcc functions can't have varargs.
1957 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001958 }
Evan Cheng25caf632006-05-23 21:06:34 +00001959
Rafael Espindola76927d752011-08-30 19:39:58 +00001960 FuncInfo->setArgumentStackSize(StackSize);
1961
Dan Gohman98ca4f22009-08-05 01:29:28 +00001962 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001963}
1964
Dan Gohman475871a2008-07-27 21:46:04 +00001965SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1967 SDValue StackPtr, SDValue Arg,
1968 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001969 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001970 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001971 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001972 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001973 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001974 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001975 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001976
1977 return DAG.getStore(Chain, dl, Arg, PtrOff,
1978 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001979 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001980}
1981
Bill Wendling64e87322009-01-16 19:25:27 +00001982/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001983/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001984SDValue
1985X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001986 SDValue &OutRetAddr, SDValue Chain,
1987 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001988 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001989 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001990 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001991 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001992
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001993 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001994 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1995 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001996 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001997}
1998
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001999/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002000/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002001static SDValue
2002EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002003 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002004 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002005 // Store the return address to the appropriate stack slot.
2006 if (!FPDiff) return Chain;
2007 // Calculate the new stack slot for the return address.
2008 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002009 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002010 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002012 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002013 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002014 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002015 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002016 return Chain;
2017}
2018
Dan Gohman98ca4f22009-08-05 01:29:28 +00002019SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002020X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002021 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002022 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002023 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002024 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002025 const SmallVectorImpl<ISD::InputArg> &Ins,
2026 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002027 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002028 MachineFunction &MF = DAG.getMachineFunction();
2029 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002030 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002031 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002032 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002033
Evan Cheng5f941932010-02-05 02:21:12 +00002034 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002035 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002036 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2037 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002038 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002039
2040 // Sibcalls are automatically detected tailcalls which do not require
2041 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002042 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002043 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002044
2045 if (isTailCall)
2046 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002047 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002048
Chris Lattner29689432010-03-11 00:22:57 +00002049 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2050 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002051
Chris Lattner638402b2007-02-28 07:00:42 +00002052 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002053 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002054 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002055 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002056
2057 // Allocate shadow area for Win64
2058 if (IsWin64) {
2059 CCInfo.AllocateStack(32, 8);
2060 }
2061
Duncan Sands45907662010-10-31 13:21:44 +00002062 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002063
Chris Lattner423c5f42007-02-28 05:31:48 +00002064 // Get a count of how many bytes are to be pushed on the stack.
2065 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002066 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002067 // This is a sibcall. The memory operands are available in caller's
2068 // own caller's stack.
2069 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002070 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002071 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002072
Gordon Henriksen86737662008-01-05 16:56:59 +00002073 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002074 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002075 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002076 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2078 FPDiff = NumBytesCallerPushed - NumBytes;
2079
2080 // Set the delta of movement of the returnaddr stackslot.
2081 // But only set if delta is greater than previous delta.
2082 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2083 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2084 }
2085
Evan Chengf22f9b32010-02-06 03:28:46 +00002086 if (!IsSibcall)
2087 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002088
Dan Gohman475871a2008-07-27 21:46:04 +00002089 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002090 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002091 if (isTailCall && FPDiff)
2092 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2093 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002094
Dan Gohman475871a2008-07-27 21:46:04 +00002095 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2096 SmallVector<SDValue, 8> MemOpChains;
2097 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002098
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002099 // Walk the register/memloc assignments, inserting copies/loads. In the case
2100 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002101 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2102 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002103 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002104 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002105 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002106 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002107
Chris Lattner423c5f42007-02-28 05:31:48 +00002108 // Promote the value if needed.
2109 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002110 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002111 case CCValAssign::Full: break;
2112 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002113 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002114 break;
2115 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002116 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002117 break;
2118 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002119 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2120 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002121 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002122 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2123 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002124 } else
2125 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2126 break;
2127 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002128 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002129 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002130 case CCValAssign::Indirect: {
2131 // Store the argument.
2132 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002133 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002134 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002135 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002136 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002137 Arg = SpillSlot;
2138 break;
2139 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002140 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002141
Chris Lattner423c5f42007-02-28 05:31:48 +00002142 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002143 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2144 if (isVarArg && IsWin64) {
2145 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2146 // shadow reg if callee is a varargs function.
2147 unsigned ShadowReg = 0;
2148 switch (VA.getLocReg()) {
2149 case X86::XMM0: ShadowReg = X86::RCX; break;
2150 case X86::XMM1: ShadowReg = X86::RDX; break;
2151 case X86::XMM2: ShadowReg = X86::R8; break;
2152 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002153 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002154 if (ShadowReg)
2155 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002156 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002157 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002158 assert(VA.isMemLoc());
2159 if (StackPtr.getNode() == 0)
2160 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2161 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2162 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002163 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002164 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002165
Evan Cheng32fe1032006-05-25 00:59:30 +00002166 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002167 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002168 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002169
Evan Cheng347d5f72006-04-28 21:29:37 +00002170 // Build a sequence of copy-to-reg nodes chained together with token chain
2171 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002172 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002173 // Tail call byval lowering might overwrite argument registers so in case of
2174 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002176 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002177 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002178 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002179 InFlag = Chain.getValue(1);
2180 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002181
Chris Lattner88e1fd52009-07-09 04:24:46 +00002182 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002183 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2184 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002185 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002186 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2187 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002188 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002189 InFlag);
2190 InFlag = Chain.getValue(1);
2191 } else {
2192 // If we are tail calling and generating PIC/GOT style code load the
2193 // address of the callee into ECX. The value in ecx is used as target of
2194 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2195 // for tail calls on PIC/GOT architectures. Normally we would just put the
2196 // address of GOT into ebx and then call target@PLT. But for tail calls
2197 // ebx would be restored (since ebx is callee saved) before jumping to the
2198 // target@PLT.
2199
2200 // Note: The actual moving to ECX is done further down.
2201 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2202 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2203 !G->getGlobal()->hasProtectedVisibility())
2204 Callee = LowerGlobalAddress(Callee, DAG);
2205 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002206 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002207 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002208 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002209
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002210 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002211 // From AMD64 ABI document:
2212 // For calls that may call functions that use varargs or stdargs
2213 // (prototype-less calls or calls to functions containing ellipsis (...) in
2214 // the declaration) %al is used as hidden argument to specify the number
2215 // of SSE registers used. The contents of %al do not need to match exactly
2216 // the number of registers, but must be an ubound on the number of SSE
2217 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002218
Gordon Henriksen86737662008-01-05 16:56:59 +00002219 // Count the number of XMM registers allocated.
2220 static const unsigned XMMArgRegs[] = {
2221 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2222 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2223 };
2224 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002225 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002226 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002227
Dale Johannesendd64c412009-02-04 00:33:20 +00002228 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002229 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002230 InFlag = Chain.getValue(1);
2231 }
2232
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002233
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002234 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002235 if (isTailCall) {
2236 // Force all the incoming stack arguments to be loaded from the stack
2237 // before any new outgoing arguments are stored to the stack, because the
2238 // outgoing stack slots may alias the incoming argument stack slots, and
2239 // the alias isn't otherwise explicit. This is slightly more conservative
2240 // than necessary, because it means that each store effectively depends
2241 // on every argument instead of just those arguments it would clobber.
2242 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2243
Dan Gohman475871a2008-07-27 21:46:04 +00002244 SmallVector<SDValue, 8> MemOpChains2;
2245 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002246 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002247 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002248 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002249 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002250 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2251 CCValAssign &VA = ArgLocs[i];
2252 if (VA.isRegLoc())
2253 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002254 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002255 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002256 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002257 // Create frame index.
2258 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002259 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002260 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002261 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002262
Duncan Sands276dcbd2008-03-21 09:14:45 +00002263 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002264 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002265 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002266 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002267 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002268 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002269 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002270
Dan Gohman98ca4f22009-08-05 01:29:28 +00002271 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2272 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002273 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002274 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002275 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002276 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002277 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002278 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002279 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002280 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002281 }
2282 }
2283
2284 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002285 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002286 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002287
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002288 // Copy arguments to their registers.
2289 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002290 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002291 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002292 InFlag = Chain.getValue(1);
2293 }
Dan Gohman475871a2008-07-27 21:46:04 +00002294 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002295
Gordon Henriksen86737662008-01-05 16:56:59 +00002296 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002297 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002298 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002299 }
2300
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002301 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2302 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2303 // In the 64-bit large code model, we have to make all calls
2304 // through a register, since the call instruction's 32-bit
2305 // pc-relative offset may not be large enough to hold the whole
2306 // address.
2307 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002308 // If the callee is a GlobalAddress node (quite common, every direct call
2309 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2310 // it.
2311
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002312 // We should use extra load for direct calls to dllimported functions in
2313 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002314 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002315 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002316 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002317 bool ExtraLoad = false;
2318 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002319
Chris Lattner48a7d022009-07-09 05:02:21 +00002320 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2321 // external symbols most go through the PLT in PIC mode. If the symbol
2322 // has hidden or protected visibility, or if it is static or local, then
2323 // we don't need to use the PLT - we can directly call it.
2324 if (Subtarget->isTargetELF() &&
2325 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002326 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002327 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002328 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002329 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002330 (!Subtarget->getTargetTriple().isMacOSX() ||
2331 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002332 // PC-relative references to external symbols should go through $stub,
2333 // unless we're building with the leopard linker or later, which
2334 // automatically synthesizes these stubs.
2335 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002336 } else if (Subtarget->isPICStyleRIPRel() &&
2337 isa<Function>(GV) &&
2338 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2339 // If the function is marked as non-lazy, generate an indirect call
2340 // which loads from the GOT directly. This avoids runtime overhead
2341 // at the cost of eager binding (and one extra byte of encoding).
2342 OpFlags = X86II::MO_GOTPCREL;
2343 WrapperKind = X86ISD::WrapperRIP;
2344 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002345 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002346
Devang Patel0d881da2010-07-06 22:08:15 +00002347 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002348 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002349
2350 // Add a wrapper if needed.
2351 if (WrapperKind != ISD::DELETED_NODE)
2352 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2353 // Add extra indirection if needed.
2354 if (ExtraLoad)
2355 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2356 MachinePointerInfo::getGOT(),
2357 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002358 }
Bill Wendling056292f2008-09-16 21:48:12 +00002359 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002360 unsigned char OpFlags = 0;
2361
Evan Cheng1bf891a2010-12-01 22:59:46 +00002362 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2363 // external symbols should go through the PLT.
2364 if (Subtarget->isTargetELF() &&
2365 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2366 OpFlags = X86II::MO_PLT;
2367 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002368 (!Subtarget->getTargetTriple().isMacOSX() ||
2369 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002370 // PC-relative references to external symbols should go through $stub,
2371 // unless we're building with the leopard linker or later, which
2372 // automatically synthesizes these stubs.
2373 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002374 }
Eric Christopherfd179292009-08-27 18:07:15 +00002375
Chris Lattner48a7d022009-07-09 05:02:21 +00002376 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2377 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002378 }
2379
Chris Lattnerd96d0722007-02-25 06:40:16 +00002380 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002381 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002382 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002383
Evan Chengf22f9b32010-02-06 03:28:46 +00002384 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002385 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2386 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002387 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002388 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002389
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002390 Ops.push_back(Chain);
2391 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002392
Dan Gohman98ca4f22009-08-05 01:29:28 +00002393 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002394 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002395
Gordon Henriksen86737662008-01-05 16:56:59 +00002396 // Add argument registers to the end of the list so that they are known live
2397 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002398 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2399 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2400 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002401
Evan Cheng586ccac2008-03-18 23:36:35 +00002402 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002403 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002404 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2405
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002406 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002407 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002408 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002409
Gabor Greifba36cb52008-08-28 21:40:38 +00002410 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002411 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002412
Dan Gohman98ca4f22009-08-05 01:29:28 +00002413 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002414 // We used to do:
2415 //// If this is the first return lowered for this function, add the regs
2416 //// to the liveout set for the function.
2417 // This isn't right, although it's probably harmless on x86; liveouts
2418 // should be computed from returns not tail calls. Consider a void
2419 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002420 return DAG.getNode(X86ISD::TC_RETURN, dl,
2421 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002422 }
2423
Dale Johannesenace16102009-02-03 19:33:06 +00002424 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002425 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002426
Chris Lattner2d297092006-05-23 18:50:38 +00002427 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002428 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002429 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002430 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002431 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002432 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002433 // pops the hidden struct pointer, so we have to push it back.
2434 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002435 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002436 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002437 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002438
Gordon Henriksenae636f82008-01-03 16:47:34 +00002439 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002440 if (!IsSibcall) {
2441 Chain = DAG.getCALLSEQ_END(Chain,
2442 DAG.getIntPtrConstant(NumBytes, true),
2443 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2444 true),
2445 InFlag);
2446 InFlag = Chain.getValue(1);
2447 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002448
Chris Lattner3085e152007-02-25 08:59:22 +00002449 // Handle result values, copying them out of physregs into vregs that we
2450 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002451 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2452 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002453}
2454
Evan Cheng25ab6902006-09-08 06:48:29 +00002455
2456//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002457// Fast Calling Convention (tail call) implementation
2458//===----------------------------------------------------------------------===//
2459
2460// Like std call, callee cleans arguments, convention except that ECX is
2461// reserved for storing the tail called function address. Only 2 registers are
2462// free for argument passing (inreg). Tail call optimization is performed
2463// provided:
2464// * tailcallopt is enabled
2465// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002466// On X86_64 architecture with GOT-style position independent code only local
2467// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002468// To keep the stack aligned according to platform abi the function
2469// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2470// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002471// If a tail called function callee has more arguments than the caller the
2472// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002473// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002474// original REtADDR, but before the saved framepointer or the spilled registers
2475// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2476// stack layout:
2477// arg1
2478// arg2
2479// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002480// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002481// move area ]
2482// (possible EBP)
2483// ESI
2484// EDI
2485// local1 ..
2486
2487/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2488/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002489unsigned
2490X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2491 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002492 MachineFunction &MF = DAG.getMachineFunction();
2493 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002494 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002495 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002496 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002497 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002498 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002499 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2500 // Number smaller than 12 so just add the difference.
2501 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2502 } else {
2503 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002504 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002505 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002506 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002507 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002508}
2509
Evan Cheng5f941932010-02-05 02:21:12 +00002510/// MatchingStackOffset - Return true if the given stack call argument is
2511/// already available in the same position (relatively) of the caller's
2512/// incoming argument stack.
2513static
2514bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2515 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2516 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002517 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2518 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002519 if (Arg.getOpcode() == ISD::CopyFromReg) {
2520 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002521 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002522 return false;
2523 MachineInstr *Def = MRI->getVRegDef(VR);
2524 if (!Def)
2525 return false;
2526 if (!Flags.isByVal()) {
2527 if (!TII->isLoadFromStackSlot(Def, FI))
2528 return false;
2529 } else {
2530 unsigned Opcode = Def->getOpcode();
2531 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2532 Def->getOperand(1).isFI()) {
2533 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002534 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002535 } else
2536 return false;
2537 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002538 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2539 if (Flags.isByVal())
2540 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002541 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002542 // define @foo(%struct.X* %A) {
2543 // tail call @bar(%struct.X* byval %A)
2544 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002545 return false;
2546 SDValue Ptr = Ld->getBasePtr();
2547 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2548 if (!FINode)
2549 return false;
2550 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002551 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002552 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002553 FI = FINode->getIndex();
2554 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002555 } else
2556 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002557
Evan Cheng4cae1332010-03-05 08:38:04 +00002558 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002559 if (!MFI->isFixedObjectIndex(FI))
2560 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002561 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002562}
2563
Dan Gohman98ca4f22009-08-05 01:29:28 +00002564/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2565/// for tail call optimization. Targets which want to do tail call
2566/// optimization should implement this function.
2567bool
2568X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002569 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002570 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002571 bool isCalleeStructRet,
2572 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002573 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002574 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002575 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002576 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002577 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002578 CalleeCC != CallingConv::C)
2579 return false;
2580
Evan Cheng7096ae42010-01-29 06:45:59 +00002581 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002582 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002583 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002584 CallingConv::ID CallerCC = CallerF->getCallingConv();
2585 bool CCMatch = CallerCC == CalleeCC;
2586
Dan Gohman1797ed52010-02-08 20:27:50 +00002587 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002588 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002589 return true;
2590 return false;
2591 }
2592
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002593 // Look for obvious safe cases to perform tail call optimization that do not
2594 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002595
Evan Cheng2c12cb42010-03-26 16:26:03 +00002596 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2597 // emit a special epilogue.
2598 if (RegInfo->needsStackRealignment(MF))
2599 return false;
2600
Evan Chenga375d472010-03-15 18:54:48 +00002601 // Also avoid sibcall optimization if either caller or callee uses struct
2602 // return semantics.
2603 if (isCalleeStructRet || isCallerStructRet)
2604 return false;
2605
Chad Rosier2416da32011-06-24 21:15:36 +00002606 // An stdcall caller is expected to clean up its arguments; the callee
2607 // isn't going to do that.
2608 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2609 return false;
2610
Chad Rosier871f6642011-05-18 19:59:50 +00002611 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002612 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002613 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002614
2615 // Optimizing for varargs on Win64 is unlikely to be safe without
2616 // additional testing.
2617 if (Subtarget->isTargetWin64())
2618 return false;
2619
Chad Rosier871f6642011-05-18 19:59:50 +00002620 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002621 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2622 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002623
Chad Rosier871f6642011-05-18 19:59:50 +00002624 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2625 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2626 if (!ArgLocs[i].isRegLoc())
2627 return false;
2628 }
2629
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002630 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2631 // Therefore if it's not used by the call it is not safe to optimize this into
2632 // a sibcall.
2633 bool Unused = false;
2634 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2635 if (!Ins[i].Used) {
2636 Unused = true;
2637 break;
2638 }
2639 }
2640 if (Unused) {
2641 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002642 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2643 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002644 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002645 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002646 CCValAssign &VA = RVLocs[i];
2647 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2648 return false;
2649 }
2650 }
2651
Evan Cheng13617962010-04-30 01:12:32 +00002652 // If the calling conventions do not match, then we'd better make sure the
2653 // results are returned in the same way as what the caller expects.
2654 if (!CCMatch) {
2655 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002656 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2657 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002658 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2659
2660 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002661 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2662 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002663 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2664
2665 if (RVLocs1.size() != RVLocs2.size())
2666 return false;
2667 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2668 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2669 return false;
2670 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2671 return false;
2672 if (RVLocs1[i].isRegLoc()) {
2673 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2674 return false;
2675 } else {
2676 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2677 return false;
2678 }
2679 }
2680 }
2681
Evan Chenga6bff982010-01-30 01:22:00 +00002682 // If the callee takes no arguments then go on to check the results of the
2683 // call.
2684 if (!Outs.empty()) {
2685 // Check if stack adjustment is needed. For now, do not do this if any
2686 // argument is passed on the stack.
2687 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002688 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2689 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002690
2691 // Allocate shadow area for Win64
2692 if (Subtarget->isTargetWin64()) {
2693 CCInfo.AllocateStack(32, 8);
2694 }
2695
Duncan Sands45907662010-10-31 13:21:44 +00002696 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002697 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002698 MachineFunction &MF = DAG.getMachineFunction();
2699 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2700 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002701
2702 // Check if the arguments are already laid out in the right way as
2703 // the caller's fixed stack objects.
2704 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002705 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2706 const X86InstrInfo *TII =
2707 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002708 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2709 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002710 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002711 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002712 if (VA.getLocInfo() == CCValAssign::Indirect)
2713 return false;
2714 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002715 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2716 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002717 return false;
2718 }
2719 }
2720 }
Evan Cheng9c044672010-05-29 01:35:22 +00002721
2722 // If the tailcall address may be in a register, then make sure it's
2723 // possible to register allocate for it. In 32-bit, the call address can
2724 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002725 // callee-saved registers are restored. These happen to be the same
2726 // registers used to pass 'inreg' arguments so watch out for those.
2727 if (!Subtarget->is64Bit() &&
2728 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002729 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002730 unsigned NumInRegs = 0;
2731 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2732 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002733 if (!VA.isRegLoc())
2734 continue;
2735 unsigned Reg = VA.getLocReg();
2736 switch (Reg) {
2737 default: break;
2738 case X86::EAX: case X86::EDX: case X86::ECX:
2739 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002740 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002741 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002742 }
2743 }
2744 }
Evan Chenga6bff982010-01-30 01:22:00 +00002745 }
Evan Chengb1712452010-01-27 06:25:16 +00002746
Evan Cheng86809cc2010-02-03 03:28:02 +00002747 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002748}
2749
Dan Gohman3df24e62008-09-03 23:12:08 +00002750FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002751X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2752 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002753}
2754
2755
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002756//===----------------------------------------------------------------------===//
2757// Other Lowering Hooks
2758//===----------------------------------------------------------------------===//
2759
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002760static bool MayFoldLoad(SDValue Op) {
2761 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2762}
2763
2764static bool MayFoldIntoStore(SDValue Op) {
2765 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2766}
2767
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002768static bool isTargetShuffle(unsigned Opcode) {
2769 switch(Opcode) {
2770 default: return false;
2771 case X86ISD::PSHUFD:
2772 case X86ISD::PSHUFHW:
2773 case X86ISD::PSHUFLW:
2774 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002775 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002776 case X86ISD::SHUFPS:
2777 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002778 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002779 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002780 case X86ISD::MOVLPS:
2781 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002782 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002783 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002784 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002785 case X86ISD::MOVSS:
2786 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002787 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002788 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002789 case X86ISD::VUNPCKLPSY:
2790 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002791 case X86ISD::PUNPCKLWD:
2792 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002793 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002794 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002795 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002796 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002797 case X86ISD::VUNPCKHPSY:
2798 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002799 case X86ISD::PUNPCKHWD:
2800 case X86ISD::PUNPCKHBW:
2801 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002802 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002803 case X86ISD::VPERMILPS:
2804 case X86ISD::VPERMILPSY:
2805 case X86ISD::VPERMILPD:
2806 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002807 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002808 return true;
2809 }
2810 return false;
2811}
2812
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002813static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002814 SDValue V1, SelectionDAG &DAG) {
2815 switch(Opc) {
2816 default: llvm_unreachable("Unknown x86 shuffle node");
2817 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002818 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002819 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002820 return DAG.getNode(Opc, dl, VT, V1);
2821 }
2822
2823 return SDValue();
2824}
2825
2826static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002827 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002828 switch(Opc) {
2829 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002830 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002831 case X86ISD::PSHUFHW:
2832 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002833 case X86ISD::VPERMILPS:
2834 case X86ISD::VPERMILPSY:
2835 case X86ISD::VPERMILPD:
2836 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002837 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2838 }
2839
2840 return SDValue();
2841}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002842
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002843static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2844 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2845 switch(Opc) {
2846 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002847 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002848 case X86ISD::SHUFPD:
2849 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002850 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002851 return DAG.getNode(Opc, dl, VT, V1, V2,
2852 DAG.getConstant(TargetMask, MVT::i8));
2853 }
2854 return SDValue();
2855}
2856
2857static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2858 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2859 switch(Opc) {
2860 default: llvm_unreachable("Unknown x86 shuffle node");
2861 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002862 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002863 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002864 case X86ISD::MOVLPS:
2865 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002866 case X86ISD::MOVSS:
2867 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002868 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002869 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002870 case X86ISD::VUNPCKLPSY:
2871 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002872 case X86ISD::PUNPCKLWD:
2873 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002874 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002875 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002876 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002877 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002878 case X86ISD::VUNPCKHPSY:
2879 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002880 case X86ISD::PUNPCKHWD:
2881 case X86ISD::PUNPCKHBW:
2882 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002883 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002884 return DAG.getNode(Opc, dl, VT, V1, V2);
2885 }
2886 return SDValue();
2887}
2888
Dan Gohmand858e902010-04-17 15:26:15 +00002889SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002890 MachineFunction &MF = DAG.getMachineFunction();
2891 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2892 int ReturnAddrIndex = FuncInfo->getRAIndex();
2893
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002894 if (ReturnAddrIndex == 0) {
2895 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002896 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002897 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002898 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002899 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002900 }
2901
Evan Cheng25ab6902006-09-08 06:48:29 +00002902 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002903}
2904
2905
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002906bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2907 bool hasSymbolicDisplacement) {
2908 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002909 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002910 return false;
2911
2912 // If we don't have a symbolic displacement - we don't have any extra
2913 // restrictions.
2914 if (!hasSymbolicDisplacement)
2915 return true;
2916
2917 // FIXME: Some tweaks might be needed for medium code model.
2918 if (M != CodeModel::Small && M != CodeModel::Kernel)
2919 return false;
2920
2921 // For small code model we assume that latest object is 16MB before end of 31
2922 // bits boundary. We may also accept pretty large negative constants knowing
2923 // that all objects are in the positive half of address space.
2924 if (M == CodeModel::Small && Offset < 16*1024*1024)
2925 return true;
2926
2927 // For kernel code model we know that all object resist in the negative half
2928 // of 32bits address space. We may not accept negative offsets, since they may
2929 // be just off and we may accept pretty large positive ones.
2930 if (M == CodeModel::Kernel && Offset > 0)
2931 return true;
2932
2933 return false;
2934}
2935
Evan Chengef41ff62011-06-23 17:54:54 +00002936/// isCalleePop - Determines whether the callee is required to pop its
2937/// own arguments. Callee pop is necessary to support tail calls.
2938bool X86::isCalleePop(CallingConv::ID CallingConv,
2939 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2940 if (IsVarArg)
2941 return false;
2942
2943 switch (CallingConv) {
2944 default:
2945 return false;
2946 case CallingConv::X86_StdCall:
2947 return !is64Bit;
2948 case CallingConv::X86_FastCall:
2949 return !is64Bit;
2950 case CallingConv::X86_ThisCall:
2951 return !is64Bit;
2952 case CallingConv::Fast:
2953 return TailCallOpt;
2954 case CallingConv::GHC:
2955 return TailCallOpt;
2956 }
2957}
2958
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002959/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2960/// specific condition code, returning the condition code and the LHS/RHS of the
2961/// comparison to make.
2962static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2963 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002964 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002965 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2966 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2967 // X > -1 -> X == 0, jump !sign.
2968 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002969 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002970 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2971 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002972 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002973 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002974 // X < 1 -> X <= 0
2975 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002976 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002977 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002978 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002979
Evan Chengd9558e02006-01-06 00:43:03 +00002980 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002981 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002982 case ISD::SETEQ: return X86::COND_E;
2983 case ISD::SETGT: return X86::COND_G;
2984 case ISD::SETGE: return X86::COND_GE;
2985 case ISD::SETLT: return X86::COND_L;
2986 case ISD::SETLE: return X86::COND_LE;
2987 case ISD::SETNE: return X86::COND_NE;
2988 case ISD::SETULT: return X86::COND_B;
2989 case ISD::SETUGT: return X86::COND_A;
2990 case ISD::SETULE: return X86::COND_BE;
2991 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002992 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002993 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002994
Chris Lattner4c78e022008-12-23 23:42:27 +00002995 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002996
Chris Lattner4c78e022008-12-23 23:42:27 +00002997 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002998 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2999 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003000 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3001 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003002 }
3003
Chris Lattner4c78e022008-12-23 23:42:27 +00003004 switch (SetCCOpcode) {
3005 default: break;
3006 case ISD::SETOLT:
3007 case ISD::SETOLE:
3008 case ISD::SETUGT:
3009 case ISD::SETUGE:
3010 std::swap(LHS, RHS);
3011 break;
3012 }
3013
3014 // On a floating point condition, the flags are set as follows:
3015 // ZF PF CF op
3016 // 0 | 0 | 0 | X > Y
3017 // 0 | 0 | 1 | X < Y
3018 // 1 | 0 | 0 | X == Y
3019 // 1 | 1 | 1 | unordered
3020 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003021 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003022 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003023 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003024 case ISD::SETOLT: // flipped
3025 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003026 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003027 case ISD::SETOLE: // flipped
3028 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003029 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003030 case ISD::SETUGT: // flipped
3031 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003032 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003033 case ISD::SETUGE: // flipped
3034 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003035 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003036 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003037 case ISD::SETNE: return X86::COND_NE;
3038 case ISD::SETUO: return X86::COND_P;
3039 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003040 case ISD::SETOEQ:
3041 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003042 }
Evan Chengd9558e02006-01-06 00:43:03 +00003043}
3044
Evan Cheng4a460802006-01-11 00:33:36 +00003045/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3046/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003047/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003048static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003049 switch (X86CC) {
3050 default:
3051 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003052 case X86::COND_B:
3053 case X86::COND_BE:
3054 case X86::COND_E:
3055 case X86::COND_P:
3056 case X86::COND_A:
3057 case X86::COND_AE:
3058 case X86::COND_NE:
3059 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003060 return true;
3061 }
3062}
3063
Evan Chengeb2f9692009-10-27 19:56:55 +00003064/// isFPImmLegal - Returns true if the target can instruction select the
3065/// specified FP immediate natively. If false, the legalizer will
3066/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003067bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003068 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3069 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3070 return true;
3071 }
3072 return false;
3073}
3074
Nate Begeman9008ca62009-04-27 18:41:29 +00003075/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3076/// the specified range (L, H].
3077static bool isUndefOrInRange(int Val, int Low, int Hi) {
3078 return (Val < 0) || (Val >= Low && Val < Hi);
3079}
3080
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003081/// isUndefOrInRange - Return true if every element in Mask, begining
3082/// from position Pos and ending in Pos+Size, falls within the specified
3083/// range (L, L+Pos]. or is undef.
3084static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3085 int Pos, int Size, int Low, int Hi) {
3086 for (int i = Pos, e = Pos+Size; i != e; ++i)
3087 if (!isUndefOrInRange(Mask[i], Low, Hi))
3088 return false;
3089 return true;
3090}
3091
Nate Begeman9008ca62009-04-27 18:41:29 +00003092/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3093/// specified value.
3094static bool isUndefOrEqual(int Val, int CmpVal) {
3095 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003096 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003097 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003098}
3099
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003100/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3101/// from position Pos and ending in Pos+Size, falls within the specified
3102/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003103static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3104 int Pos, int Size, int Low) {
3105 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3106 if (!isUndefOrEqual(Mask[i], Low))
3107 return false;
3108 return true;
3109}
3110
Nate Begeman9008ca62009-04-27 18:41:29 +00003111/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3112/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3113/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003114static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003115 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003116 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003117 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003118 return (Mask[0] < 2 && Mask[1] < 2);
3119 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003120}
3121
Nate Begeman9008ca62009-04-27 18:41:29 +00003122bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003123 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003124 N->getMask(M);
3125 return ::isPSHUFDMask(M, N->getValueType(0));
3126}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003127
Nate Begeman9008ca62009-04-27 18:41:29 +00003128/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3129/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003130static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003132 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003133
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 // Lower quadword copied in order or undef.
3135 for (int i = 0; i != 4; ++i)
3136 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003137 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003138
Evan Cheng506d3df2006-03-29 23:07:14 +00003139 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003140 for (int i = 4; i != 8; ++i)
3141 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003142 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003143
Evan Cheng506d3df2006-03-29 23:07:14 +00003144 return true;
3145}
3146
Nate Begeman9008ca62009-04-27 18:41:29 +00003147bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003148 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003149 N->getMask(M);
3150 return ::isPSHUFHWMask(M, N->getValueType(0));
3151}
Evan Cheng506d3df2006-03-29 23:07:14 +00003152
Nate Begeman9008ca62009-04-27 18:41:29 +00003153/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3154/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003155static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003156 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003157 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003158
Rafael Espindola15684b22009-04-24 12:40:33 +00003159 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003160 for (int i = 4; i != 8; ++i)
3161 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003162 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003163
Rafael Espindola15684b22009-04-24 12:40:33 +00003164 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 for (int i = 0; i != 4; ++i)
3166 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003167 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003168
Rafael Espindola15684b22009-04-24 12:40:33 +00003169 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003170}
3171
Nate Begeman9008ca62009-04-27 18:41:29 +00003172bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003173 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 N->getMask(M);
3175 return ::isPSHUFLWMask(M, N->getValueType(0));
3176}
3177
Nate Begemana09008b2009-10-19 02:17:23 +00003178/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3179/// is suitable for input to PALIGNR.
3180static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003181 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003182 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003183 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3184 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003185
Nate Begemana09008b2009-10-19 02:17:23 +00003186 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003187 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003188 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003189
Nate Begemana09008b2009-10-19 02:17:23 +00003190 for (i = 0; i != e; ++i)
3191 if (Mask[i] >= 0)
3192 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003193
Nate Begemana09008b2009-10-19 02:17:23 +00003194 // All undef, not a palignr.
3195 if (i == e)
3196 return false;
3197
Eli Friedman63f8dde2011-07-25 21:36:45 +00003198 // Make sure we're shifting in the right direction.
3199 if (Mask[i] <= i)
3200 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003201
3202 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003203
Nate Begemana09008b2009-10-19 02:17:23 +00003204 // Check the rest of the elements to see if they are consecutive.
3205 for (++i; i != e; ++i) {
3206 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003207 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003208 return false;
3209 }
3210 return true;
3211}
3212
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003213/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3214/// specifies a shuffle of elements that is suitable for input to 256-bit
3215/// VSHUFPSY.
3216static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3217 const X86Subtarget *Subtarget) {
3218 int NumElems = VT.getVectorNumElements();
3219
3220 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3221 return false;
3222
3223 if (NumElems != 8)
3224 return false;
3225
3226 // VSHUFPSY divides the resulting vector into 4 chunks.
3227 // The sources are also splitted into 4 chunks, and each destination
3228 // chunk must come from a different source chunk.
3229 //
3230 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3231 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3232 //
3233 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3234 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3235 //
3236 int QuarterSize = NumElems/4;
3237 int HalfSize = QuarterSize*2;
3238 for (int i = 0; i < QuarterSize; ++i)
3239 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3240 return false;
3241 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3242 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3243 return false;
3244
3245 // The mask of the second half must be the same as the first but with
3246 // the appropriate offsets. This works in the same way as VPERMILPS
3247 // works with masks.
3248 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3249 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
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 for (int i = QuarterSize*3; i < NumElems; ++i) {
3258 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3259 return false;
3260 int FstHalfIdx = i-HalfSize;
3261 if (Mask[FstHalfIdx] < 0)
3262 continue;
3263 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3264 return false;
3265
3266 }
3267
3268 return true;
3269}
3270
3271/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3272/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3273static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3274 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3275 EVT VT = SVOp->getValueType(0);
3276 int NumElems = VT.getVectorNumElements();
3277
3278 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3279 "Only supports v8i32 and v8f32 types");
3280
3281 int HalfSize = NumElems/2;
3282 unsigned Mask = 0;
3283 for (int i = 0; i != NumElems ; ++i) {
3284 if (SVOp->getMaskElt(i) < 0)
3285 continue;
3286 // The mask of the first half must be equal to the second one.
3287 unsigned Shamt = (i%HalfSize)*2;
3288 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3289 Mask |= Elt << Shamt;
3290 }
3291
3292 return Mask;
3293}
3294
3295/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3296/// specifies a shuffle of elements that is suitable for input to 256-bit
3297/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3298/// version and the mask of the second half isn't binded with the first
3299/// one.
3300static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3301 const X86Subtarget *Subtarget) {
3302 int NumElems = VT.getVectorNumElements();
3303
3304 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3305 return false;
3306
3307 if (NumElems != 4)
3308 return false;
3309
3310 // VSHUFPSY divides the resulting vector into 4 chunks.
3311 // The sources are also splitted into 4 chunks, and each destination
3312 // chunk must come from a different source chunk.
3313 //
3314 // SRC1 => X3 X2 X1 X0
3315 // SRC2 => Y3 Y2 Y1 Y0
3316 //
3317 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3318 //
3319 int QuarterSize = NumElems/4;
3320 int HalfSize = QuarterSize*2;
3321 for (int i = 0; i < QuarterSize; ++i)
3322 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3323 return false;
3324 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3325 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3326 return false;
3327 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3328 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3329 return false;
3330 for (int i = QuarterSize*3; i < NumElems; ++i)
3331 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3332 return false;
3333
3334 return true;
3335}
3336
3337/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3338/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3339static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3340 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3341 EVT VT = SVOp->getValueType(0);
3342 int NumElems = VT.getVectorNumElements();
3343
3344 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3345 "Only supports v4i64 and v4f64 types");
3346
3347 int HalfSize = NumElems/2;
3348 unsigned Mask = 0;
3349 for (int i = 0; i != NumElems ; ++i) {
3350 if (SVOp->getMaskElt(i) < 0)
3351 continue;
3352 int Elt = SVOp->getMaskElt(i) % HalfSize;
3353 Mask |= Elt << i;
3354 }
3355
3356 return Mask;
3357}
3358
Evan Cheng14aed5e2006-03-24 01:18:28 +00003359/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003360/// specifies a shuffle of elements that is suitable for input to 128-bit
3361/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003362static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003363 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003364
3365 if (VT.getSizeInBits() != 128)
3366 return false;
3367
Nate Begeman9008ca62009-04-27 18:41:29 +00003368 if (NumElems != 2 && NumElems != 4)
3369 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003370
Nate Begeman9008ca62009-04-27 18:41:29 +00003371 int Half = NumElems / 2;
3372 for (int i = 0; i < Half; ++i)
3373 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003374 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 for (int i = Half; i < NumElems; ++i)
3376 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003377 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003378
Evan Cheng14aed5e2006-03-24 01:18:28 +00003379 return true;
3380}
3381
Nate Begeman9008ca62009-04-27 18:41:29 +00003382bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3383 SmallVector<int, 8> M;
3384 N->getMask(M);
3385 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003386}
3387
Evan Cheng213d2cf2007-05-17 18:45:50 +00003388/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003389/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3390/// half elements to come from vector 1 (which would equal the dest.) and
3391/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003392static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003394
3395 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003396 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003397
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 int Half = NumElems / 2;
3399 for (int i = 0; i < Half; ++i)
3400 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003401 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003402 for (int i = Half; i < NumElems; ++i)
3403 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003404 return false;
3405 return true;
3406}
3407
Nate Begeman9008ca62009-04-27 18:41:29 +00003408static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3409 SmallVector<int, 8> M;
3410 N->getMask(M);
3411 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003412}
3413
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003414/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3415/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003416bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003417 EVT VT = N->getValueType(0);
3418 unsigned NumElems = VT.getVectorNumElements();
3419
3420 if (VT.getSizeInBits() != 128)
3421 return false;
3422
3423 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003424 return false;
3425
Evan Cheng2064a2b2006-03-28 06:50:32 +00003426 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003427 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3428 isUndefOrEqual(N->getMaskElt(1), 7) &&
3429 isUndefOrEqual(N->getMaskElt(2), 2) &&
3430 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003431}
3432
Nate Begeman0b10b912009-11-07 23:17:15 +00003433/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3434/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3435/// <2, 3, 2, 3>
3436bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003437 EVT VT = N->getValueType(0);
3438 unsigned NumElems = VT.getVectorNumElements();
3439
3440 if (VT.getSizeInBits() != 128)
3441 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003442
Nate Begeman0b10b912009-11-07 23:17:15 +00003443 if (NumElems != 4)
3444 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003445
Nate Begeman0b10b912009-11-07 23:17:15 +00003446 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003447 isUndefOrEqual(N->getMaskElt(1), 3) &&
3448 isUndefOrEqual(N->getMaskElt(2), 2) &&
3449 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003450}
3451
Evan Cheng5ced1d82006-04-06 23:23:56 +00003452/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3453/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003454bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3455 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003456
Evan Cheng5ced1d82006-04-06 23:23:56 +00003457 if (NumElems != 2 && NumElems != 4)
3458 return false;
3459
Evan Chengc5cdff22006-04-07 21:53:05 +00003460 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003461 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003462 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003463
Evan Chengc5cdff22006-04-07 21:53:05 +00003464 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003466 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003467
3468 return true;
3469}
3470
Nate Begeman0b10b912009-11-07 23:17:15 +00003471/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3472/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3473bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003475
David Greenea20244d2011-03-02 17:23:43 +00003476 if ((NumElems != 2 && NumElems != 4)
3477 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003478 return false;
3479
Evan Chengc5cdff22006-04-07 21:53:05 +00003480 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003481 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003482 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003483
Nate Begeman9008ca62009-04-27 18:41:29 +00003484 for (unsigned i = 0; i < NumElems/2; ++i)
3485 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003486 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003487
3488 return true;
3489}
3490
Evan Cheng0038e592006-03-28 00:39:58 +00003491/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3492/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003493static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003494 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003496
3497 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3498 "Unsupported vector type for unpckh");
3499
3500 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003501 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003502
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003503 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3504 // independently on 128-bit lanes.
3505 unsigned NumLanes = VT.getSizeInBits()/128;
3506 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003507
3508 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003509 unsigned End = NumLaneElts;
3510 for (unsigned s = 0; s < NumLanes; ++s) {
3511 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003512 i != End;
3513 i += 2, ++j) {
3514 int BitI = Mask[i];
3515 int BitI1 = Mask[i+1];
3516 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003517 return false;
David Greenea20244d2011-03-02 17:23:43 +00003518 if (V2IsSplat) {
3519 if (!isUndefOrEqual(BitI1, NumElts))
3520 return false;
3521 } else {
3522 if (!isUndefOrEqual(BitI1, j + NumElts))
3523 return false;
3524 }
Evan Cheng39623da2006-04-20 08:58:49 +00003525 }
David Greenea20244d2011-03-02 17:23:43 +00003526 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003527 Start += NumLaneElts;
3528 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003529 }
David Greenea20244d2011-03-02 17:23:43 +00003530
Evan Cheng0038e592006-03-28 00:39:58 +00003531 return true;
3532}
3533
Nate Begeman9008ca62009-04-27 18:41:29 +00003534bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3535 SmallVector<int, 8> M;
3536 N->getMask(M);
3537 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003538}
3539
Evan Cheng4fcb9222006-03-28 02:43:26 +00003540/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3541/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003542static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003543 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003544 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003545
3546 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3547 "Unsupported vector type for unpckh");
3548
3549 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003550 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003551
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003552 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3553 // independently on 128-bit lanes.
3554 unsigned NumLanes = VT.getSizeInBits()/128;
3555 unsigned NumLaneElts = NumElts/NumLanes;
3556
3557 unsigned Start = 0;
3558 unsigned End = NumLaneElts;
3559 for (unsigned l = 0; l != NumLanes; ++l) {
3560 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3561 i != End; i += 2, ++j) {
3562 int BitI = Mask[i];
3563 int BitI1 = Mask[i+1];
3564 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003565 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003566 if (V2IsSplat) {
3567 if (isUndefOrEqual(BitI1, NumElts))
3568 return false;
3569 } else {
3570 if (!isUndefOrEqual(BitI1, j+NumElts))
3571 return false;
3572 }
Evan Cheng39623da2006-04-20 08:58:49 +00003573 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003574 // Process the next 128 bits.
3575 Start += NumLaneElts;
3576 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003577 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003578 return true;
3579}
3580
Nate Begeman9008ca62009-04-27 18:41:29 +00003581bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3582 SmallVector<int, 8> M;
3583 N->getMask(M);
3584 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003585}
3586
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003587/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3588/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3589/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003590static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003591 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003592 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003593 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003594
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003595 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3596 // FIXME: Need a better way to get rid of this, there's no latency difference
3597 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3598 // the former later. We should also remove the "_undef" special mask.
3599 if (NumElems == 4 && VT.getSizeInBits() == 256)
3600 return false;
3601
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003602 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3603 // independently on 128-bit lanes.
3604 unsigned NumLanes = VT.getSizeInBits() / 128;
3605 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003606
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003607 for (unsigned s = 0; s < NumLanes; ++s) {
3608 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3609 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003610 i += 2, ++j) {
3611 int BitI = Mask[i];
3612 int BitI1 = Mask[i+1];
3613
3614 if (!isUndefOrEqual(BitI, j))
3615 return false;
3616 if (!isUndefOrEqual(BitI1, j))
3617 return false;
3618 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003619 }
David Greenea20244d2011-03-02 17:23:43 +00003620
Rafael Espindola15684b22009-04-24 12:40:33 +00003621 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003622}
3623
Nate Begeman9008ca62009-04-27 18:41:29 +00003624bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3625 SmallVector<int, 8> M;
3626 N->getMask(M);
3627 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3628}
3629
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003630/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3631/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3632/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003633static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003634 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003635 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3636 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003637
Nate Begeman9008ca62009-04-27 18:41:29 +00003638 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3639 int BitI = Mask[i];
3640 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003641 if (!isUndefOrEqual(BitI, j))
3642 return false;
3643 if (!isUndefOrEqual(BitI1, j))
3644 return false;
3645 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003646 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003647}
3648
Nate Begeman9008ca62009-04-27 18:41:29 +00003649bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3650 SmallVector<int, 8> M;
3651 N->getMask(M);
3652 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3653}
3654
Evan Cheng017dcc62006-04-21 01:05:10 +00003655/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3656/// specifies a shuffle of elements that is suitable for input to MOVSS,
3657/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003658static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003659 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003660 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003661
3662 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003663
Nate Begeman9008ca62009-04-27 18:41:29 +00003664 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003665 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003666
Nate Begeman9008ca62009-04-27 18:41:29 +00003667 for (int i = 1; i < NumElts; ++i)
3668 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003669 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003670
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003671 return true;
3672}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003673
Nate Begeman9008ca62009-04-27 18:41:29 +00003674bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3675 SmallVector<int, 8> M;
3676 N->getMask(M);
3677 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003678}
3679
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003680/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3681/// as permutations between 128-bit chunks or halves. As an example: this
3682/// shuffle bellow:
3683/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3684/// The first half comes from the second half of V1 and the second half from the
3685/// the second half of V2.
3686static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3687 const X86Subtarget *Subtarget) {
3688 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3689 return false;
3690
3691 // The shuffle result is divided into half A and half B. In total the two
3692 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3693 // B must come from C, D, E or F.
3694 int HalfSize = VT.getVectorNumElements()/2;
3695 bool MatchA = false, MatchB = false;
3696
3697 // Check if A comes from one of C, D, E, F.
3698 for (int Half = 0; Half < 4; ++Half) {
3699 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3700 MatchA = true;
3701 break;
3702 }
3703 }
3704
3705 // Check if B comes from one of C, D, E, F.
3706 for (int Half = 0; Half < 4; ++Half) {
3707 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3708 MatchB = true;
3709 break;
3710 }
3711 }
3712
3713 return MatchA && MatchB;
3714}
3715
3716/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3717/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3718static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3719 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3720 EVT VT = SVOp->getValueType(0);
3721
3722 int HalfSize = VT.getVectorNumElements()/2;
3723
3724 int FstHalf = 0, SndHalf = 0;
3725 for (int i = 0; i < HalfSize; ++i) {
3726 if (SVOp->getMaskElt(i) > 0) {
3727 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3728 break;
3729 }
3730 }
3731 for (int i = HalfSize; i < HalfSize*2; ++i) {
3732 if (SVOp->getMaskElt(i) > 0) {
3733 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3734 break;
3735 }
3736 }
3737
3738 return (FstHalf | (SndHalf << 4));
3739}
3740
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003741/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3742/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3743/// Note that VPERMIL mask matching is different depending whether theunderlying
3744/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3745/// to the same elements of the low, but to the higher half of the source.
3746/// In VPERMILPD the two lanes could be shuffled independently of each other
3747/// with the same restriction that lanes can't be crossed.
3748static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3749 const X86Subtarget *Subtarget) {
3750 int NumElts = VT.getVectorNumElements();
3751 int NumLanes = VT.getSizeInBits()/128;
3752
3753 if (!Subtarget->hasAVX())
3754 return false;
3755
3756 // Match any permutation of 128-bit vector with 64-bit types
3757 if (NumLanes == 1 && NumElts != 2)
3758 return false;
3759
3760 // Only match 256-bit with 32 types
3761 if (VT.getSizeInBits() == 256 && NumElts != 4)
3762 return false;
3763
3764 // The mask on the high lane is independent of the low. Both can match
3765 // any element in inside its own lane, but can't cross.
3766 int LaneSize = NumElts/NumLanes;
3767 for (int l = 0; l < NumLanes; ++l)
3768 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3769 int LaneStart = l*LaneSize;
3770 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3771 return false;
3772 }
3773
3774 return true;
3775}
3776
3777/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3778/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3779/// Note that VPERMIL mask matching is different depending whether theunderlying
3780/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3781/// to the same elements of the low, but to the higher half of the source.
3782/// In VPERMILPD the two lanes could be shuffled independently of each other
3783/// with the same restriction that lanes can't be crossed.
3784static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3785 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003786 unsigned NumElts = VT.getVectorNumElements();
3787 unsigned NumLanes = VT.getSizeInBits()/128;
3788
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003789 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003790 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003791
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003792 // Match any permutation of 128-bit vector with 32-bit types
3793 if (NumLanes == 1 && NumElts != 4)
3794 return false;
3795
3796 // Only match 256-bit with 32 types
3797 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003798 return false;
3799
3800 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003801 // they can differ if any of the corresponding index in a lane is undef
3802 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003803 int LaneSize = NumElts/NumLanes;
3804 for (int i = 0; i < LaneSize; ++i) {
3805 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003806 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3807 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3808
3809 if (!HighValid || !LowValid)
3810 return false;
3811 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003812 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003813 if (Mask[HighElt]-Mask[i] != LaneSize)
3814 return false;
3815 }
3816
3817 return true;
3818}
3819
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003820/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3821/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3822static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003823 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3824 EVT VT = SVOp->getValueType(0);
3825
3826 int NumElts = VT.getVectorNumElements();
3827 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003828 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003829
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003830 // Although the mask is equal for both lanes do it twice to get the cases
3831 // where a mask will match because the same mask element is undef on the
3832 // first half but valid on the second. This would get pathological cases
3833 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003834 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003835 for (int l = 0; l < NumLanes; ++l) {
3836 for (int i = 0; i < LaneSize; ++i) {
3837 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3838 if (MaskElt < 0)
3839 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003840 if (MaskElt >= LaneSize)
3841 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003842 Mask |= MaskElt << (i*2);
3843 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003844 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003845
3846 return Mask;
3847}
3848
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003849/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3850/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3851static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3852 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3853 EVT VT = SVOp->getValueType(0);
3854
3855 int NumElts = VT.getVectorNumElements();
3856 int NumLanes = VT.getSizeInBits()/128;
3857
3858 unsigned Mask = 0;
3859 int LaneSize = NumElts/NumLanes;
3860 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003861 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3862 int MaskElt = SVOp->getMaskElt(i);
3863 if (MaskElt < 0)
3864 continue;
3865 Mask |= (MaskElt-l*LaneSize) << i;
3866 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003867
3868 return Mask;
3869}
3870
Evan Cheng017dcc62006-04-21 01:05:10 +00003871/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3872/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003873/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003874static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003875 bool V2IsSplat = false, bool V2IsUndef = false) {
3876 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003877 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003878 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003879
Nate Begeman9008ca62009-04-27 18:41:29 +00003880 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003881 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003882
Nate Begeman9008ca62009-04-27 18:41:29 +00003883 for (int i = 1; i < NumOps; ++i)
3884 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3885 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3886 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003887 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003888
Evan Cheng39623da2006-04-20 08:58:49 +00003889 return true;
3890}
3891
Nate Begeman9008ca62009-04-27 18:41:29 +00003892static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003893 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003894 SmallVector<int, 8> M;
3895 N->getMask(M);
3896 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003897}
3898
Evan Chengd9539472006-04-14 21:59:03 +00003899/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3900/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003901/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3902bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3903 const X86Subtarget *Subtarget) {
3904 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003905 return false;
3906
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003907 // The second vector must be undef
3908 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3909 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003910
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003911 EVT VT = N->getValueType(0);
3912 unsigned NumElems = VT.getVectorNumElements();
3913
3914 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3915 (VT.getSizeInBits() == 256 && NumElems != 8))
3916 return false;
3917
3918 // "i+1" is the value the indexed mask element must have
3919 for (unsigned i = 0; i < NumElems; i += 2)
3920 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3921 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003922 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003923
3924 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003925}
3926
3927/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3928/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003929/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3930bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3931 const X86Subtarget *Subtarget) {
3932 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003933 return false;
3934
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003935 // The second vector must be undef
3936 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3937 return false;
3938
3939 EVT VT = N->getValueType(0);
3940 unsigned NumElems = VT.getVectorNumElements();
3941
3942 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3943 (VT.getSizeInBits() == 256 && NumElems != 8))
3944 return false;
3945
3946 // "i" is the value the indexed mask element must have
3947 for (unsigned i = 0; i < NumElems; i += 2)
3948 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3949 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003951
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003952 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003953}
3954
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003955/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3956/// specifies a shuffle of elements that is suitable for input to 256-bit
3957/// version of MOVDDUP.
3958static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3959 const X86Subtarget *Subtarget) {
3960 EVT VT = N->getValueType(0);
3961 int NumElts = VT.getVectorNumElements();
3962 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3963
3964 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3965 !V2IsUndef || NumElts != 4)
3966 return false;
3967
3968 for (int i = 0; i != NumElts/2; ++i)
3969 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3970 return false;
3971 for (int i = NumElts/2; i != NumElts; ++i)
3972 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3973 return false;
3974 return true;
3975}
3976
Evan Cheng0b457f02008-09-25 20:50:48 +00003977/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003978/// specifies a shuffle of elements that is suitable for input to 128-bit
3979/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003980bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003981 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003982
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003983 if (VT.getSizeInBits() != 128)
3984 return false;
3985
3986 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003987 for (int i = 0; i < e; ++i)
3988 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003989 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003990 for (int i = 0; i < e; ++i)
3991 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003992 return false;
3993 return true;
3994}
3995
David Greenec38a03e2011-02-03 15:50:00 +00003996/// isVEXTRACTF128Index - Return true if the specified
3997/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3998/// suitable for input to VEXTRACTF128.
3999bool X86::isVEXTRACTF128Index(SDNode *N) {
4000 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4001 return false;
4002
4003 // The index should be aligned on a 128-bit boundary.
4004 uint64_t Index =
4005 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4006
4007 unsigned VL = N->getValueType(0).getVectorNumElements();
4008 unsigned VBits = N->getValueType(0).getSizeInBits();
4009 unsigned ElSize = VBits / VL;
4010 bool Result = (Index * ElSize) % 128 == 0;
4011
4012 return Result;
4013}
4014
David Greeneccacdc12011-02-04 16:08:29 +00004015/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4016/// operand specifies a subvector insert that is suitable for input to
4017/// VINSERTF128.
4018bool X86::isVINSERTF128Index(SDNode *N) {
4019 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4020 return false;
4021
4022 // The index should be aligned on a 128-bit boundary.
4023 uint64_t Index =
4024 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4025
4026 unsigned VL = N->getValueType(0).getVectorNumElements();
4027 unsigned VBits = N->getValueType(0).getSizeInBits();
4028 unsigned ElSize = VBits / VL;
4029 bool Result = (Index * ElSize) % 128 == 0;
4030
4031 return Result;
4032}
4033
Evan Cheng63d33002006-03-22 08:01:21 +00004034/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004035/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004036unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004037 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4038 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4039
Evan Chengb9df0ca2006-03-22 02:53:00 +00004040 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4041 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004042 for (int i = 0; i < NumOperands; ++i) {
4043 int Val = SVOp->getMaskElt(NumOperands-i-1);
4044 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004045 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004046 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004047 if (i != NumOperands - 1)
4048 Mask <<= Shift;
4049 }
Evan Cheng63d33002006-03-22 08:01:21 +00004050 return Mask;
4051}
4052
Evan Cheng506d3df2006-03-29 23:07:14 +00004053/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004054/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004055unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004057 unsigned Mask = 0;
4058 // 8 nodes, but we only care about the last 4.
4059 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004060 int Val = SVOp->getMaskElt(i);
4061 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004062 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004063 if (i != 4)
4064 Mask <<= 2;
4065 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004066 return Mask;
4067}
4068
4069/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004070/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004071unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004072 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004073 unsigned Mask = 0;
4074 // 8 nodes, but we only care about the first 4.
4075 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004076 int Val = SVOp->getMaskElt(i);
4077 if (Val >= 0)
4078 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004079 if (i != 0)
4080 Mask <<= 2;
4081 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004082 return Mask;
4083}
4084
Nate Begemana09008b2009-10-19 02:17:23 +00004085/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4086/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4087unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4088 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4089 EVT VVT = N->getValueType(0);
4090 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4091 int Val = 0;
4092
4093 unsigned i, e;
4094 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4095 Val = SVOp->getMaskElt(i);
4096 if (Val >= 0)
4097 break;
4098 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004099 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004100 return (Val - i) * EltSize;
4101}
4102
David Greenec38a03e2011-02-03 15:50:00 +00004103/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4104/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4105/// instructions.
4106unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4107 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4108 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4109
4110 uint64_t Index =
4111 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4112
4113 EVT VecVT = N->getOperand(0).getValueType();
4114 EVT ElVT = VecVT.getVectorElementType();
4115
4116 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004117 return Index / NumElemsPerChunk;
4118}
4119
David Greeneccacdc12011-02-04 16:08:29 +00004120/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4121/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4122/// instructions.
4123unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4124 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4125 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4126
4127 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004128 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004129
4130 EVT VecVT = N->getValueType(0);
4131 EVT ElVT = VecVT.getVectorElementType();
4132
4133 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004134 return Index / NumElemsPerChunk;
4135}
4136
Evan Cheng37b73872009-07-30 08:33:02 +00004137/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4138/// constant +0.0.
4139bool X86::isZeroNode(SDValue Elt) {
4140 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004141 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004142 (isa<ConstantFPSDNode>(Elt) &&
4143 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4144}
4145
Nate Begeman9008ca62009-04-27 18:41:29 +00004146/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4147/// their permute mask.
4148static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4149 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004150 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004151 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004152 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004153
Nate Begeman5a5ca152009-04-29 05:20:52 +00004154 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004155 int idx = SVOp->getMaskElt(i);
4156 if (idx < 0)
4157 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004158 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004159 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004160 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004161 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004162 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004163 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4164 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004165}
4166
Evan Cheng779ccea2007-12-07 21:30:01 +00004167/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4168/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004169static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004170 unsigned NumElems = VT.getVectorNumElements();
4171 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004172 int idx = Mask[i];
4173 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004174 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004175 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004176 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004177 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004178 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004179 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004180}
4181
Evan Cheng533a0aa2006-04-19 20:35:22 +00004182/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4183/// match movhlps. The lower half elements should come from upper half of
4184/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004185/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004186static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004187 EVT VT = Op->getValueType(0);
4188 if (VT.getSizeInBits() != 128)
4189 return false;
4190 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004191 return false;
4192 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004193 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004194 return false;
4195 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004196 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004197 return false;
4198 return true;
4199}
4200
Evan Cheng5ced1d82006-04-06 23:23:56 +00004201/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004202/// is promoted to a vector. It also returns the LoadSDNode by reference if
4203/// required.
4204static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004205 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4206 return false;
4207 N = N->getOperand(0).getNode();
4208 if (!ISD::isNON_EXTLoad(N))
4209 return false;
4210 if (LD)
4211 *LD = cast<LoadSDNode>(N);
4212 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004213}
4214
Evan Cheng533a0aa2006-04-19 20:35:22 +00004215/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4216/// match movlp{s|d}. The lower half elements should come from lower half of
4217/// V1 (and in order), and the upper half elements should come from the upper
4218/// half of V2 (and in order). And since V1 will become the source of the
4219/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004220static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4221 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004222 EVT VT = Op->getValueType(0);
4223 if (VT.getSizeInBits() != 128)
4224 return false;
4225
Evan Cheng466685d2006-10-09 20:57:25 +00004226 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004227 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004228 // Is V2 is a vector load, don't do this transformation. We will try to use
4229 // load folding shufps op.
4230 if (ISD::isNON_EXTLoad(V2))
4231 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004232
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004233 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004234
Evan Cheng533a0aa2006-04-19 20:35:22 +00004235 if (NumElems != 2 && NumElems != 4)
4236 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004237 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004238 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004239 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004240 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004242 return false;
4243 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004244}
4245
Evan Cheng39623da2006-04-20 08:58:49 +00004246/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4247/// all the same.
4248static bool isSplatVector(SDNode *N) {
4249 if (N->getOpcode() != ISD::BUILD_VECTOR)
4250 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004251
Dan Gohman475871a2008-07-27 21:46:04 +00004252 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004253 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4254 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004255 return false;
4256 return true;
4257}
4258
Evan Cheng213d2cf2007-05-17 18:45:50 +00004259/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004260/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004261/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004262static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004263 SDValue V1 = N->getOperand(0);
4264 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004265 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4266 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004267 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004268 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004269 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004270 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4271 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004272 if (Opc != ISD::BUILD_VECTOR ||
4273 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004274 return false;
4275 } else if (Idx >= 0) {
4276 unsigned Opc = V1.getOpcode();
4277 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4278 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004279 if (Opc != ISD::BUILD_VECTOR ||
4280 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004281 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004282 }
4283 }
4284 return true;
4285}
4286
4287/// getZeroVector - Returns a vector of specified type with all zero elements.
4288///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004289static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004290 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004291 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004292
Dale Johannesen0488fb62010-09-30 23:57:10 +00004293 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004294 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004295 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004296 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004297 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004298 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4299 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4300 } else { // SSE1
4301 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4302 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4303 }
4304 } else if (VT.getSizeInBits() == 256) { // AVX
4305 // 256-bit logic and arithmetic instructions in AVX are
4306 // all floating-point, no support for integer ops. Default
4307 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004308 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004309 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4310 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004311 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004312 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004313}
4314
Chris Lattner8a594482007-11-25 00:24:49 +00004315/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004316/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4317/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4318/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004319static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004320 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004321 assert((VT.is128BitVector() || VT.is256BitVector())
4322 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004323
Owen Anderson825b72b2009-08-11 20:47:22 +00004324 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004325 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4326 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004327
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004328 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004329 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4330 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4331 Vec = Insert128BitVector(InsV, Vec,
4332 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4333 }
4334
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004335 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004336}
4337
Evan Cheng39623da2006-04-20 08:58:49 +00004338/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4339/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004340static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004341 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004342 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004343
Evan Cheng39623da2006-04-20 08:58:49 +00004344 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004345 SmallVector<int, 8> MaskVec;
4346 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004347
Nate Begeman5a5ca152009-04-29 05:20:52 +00004348 for (unsigned i = 0; i != NumElems; ++i) {
4349 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004350 MaskVec[i] = NumElems;
4351 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004352 }
Evan Cheng39623da2006-04-20 08:58:49 +00004353 }
Evan Cheng39623da2006-04-20 08:58:49 +00004354 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004355 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4356 SVOp->getOperand(1), &MaskVec[0]);
4357 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004358}
4359
Evan Cheng017dcc62006-04-21 01:05:10 +00004360/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4361/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004362static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004363 SDValue V2) {
4364 unsigned NumElems = VT.getVectorNumElements();
4365 SmallVector<int, 8> Mask;
4366 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004367 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 Mask.push_back(i);
4369 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004370}
4371
Nate Begeman9008ca62009-04-27 18:41:29 +00004372/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004373static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 SDValue V2) {
4375 unsigned NumElems = VT.getVectorNumElements();
4376 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004377 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004378 Mask.push_back(i);
4379 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004380 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004382}
4383
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004384/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004385static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 SDValue V2) {
4387 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004388 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004390 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004391 Mask.push_back(i + Half);
4392 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004393 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004394 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004395}
4396
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004397// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004398// a generic shuffle instruction because the target has no such instructions.
4399// Generate shuffles which repeat i16 and i8 several times until they can be
4400// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004401static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004402 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004403 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004404 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004405
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 while (NumElems > 4) {
4407 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004408 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004410 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004411 EltNo -= NumElems/2;
4412 }
4413 NumElems >>= 1;
4414 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004415 return V;
4416}
Eric Christopherfd179292009-08-27 18:07:15 +00004417
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004418/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4419static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4420 EVT VT = V.getValueType();
4421 DebugLoc dl = V.getDebugLoc();
4422 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4423 && "Vector size not supported");
4424
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004425 if (VT.getSizeInBits() == 128) {
4426 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004427 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004428 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4429 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004430 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004431 // To use VPERMILPS to splat scalars, the second half of indicies must
4432 // refer to the higher part, which is a duplication of the lower one,
4433 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004434 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4435 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004436
4437 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4438 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4439 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004440 }
4441
4442 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4443}
4444
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004445/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004446static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4447 EVT SrcVT = SV->getValueType(0);
4448 SDValue V1 = SV->getOperand(0);
4449 DebugLoc dl = SV->getDebugLoc();
4450
4451 int EltNo = SV->getSplatIndex();
4452 int NumElems = SrcVT.getVectorNumElements();
4453 unsigned Size = SrcVT.getSizeInBits();
4454
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004455 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4456 "Unknown how to promote splat for type");
4457
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004458 // Extract the 128-bit part containing the splat element and update
4459 // the splat element index when it refers to the higher register.
4460 if (Size == 256) {
4461 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4462 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4463 if (Idx > 0)
4464 EltNo -= NumElems/2;
4465 }
4466
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004467 // All i16 and i8 vector types can't be used directly by a generic shuffle
4468 // instruction because the target has no such instruction. Generate shuffles
4469 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004470 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004471 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004472 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004473 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004474
4475 // Recreate the 256-bit vector and place the same 128-bit vector
4476 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004477 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004478 if (Size == 256) {
4479 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4480 DAG.getConstant(0, MVT::i32), DAG, dl);
4481 V1 = Insert128BitVector(InsV, V1,
4482 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4483 }
4484
4485 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004486}
4487
Evan Chengba05f722006-04-21 23:03:30 +00004488/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004489/// vector of zero or undef vector. This produces a shuffle where the low
4490/// element of V2 is swizzled into the zero/undef vector, landing at element
4491/// 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 +00004492static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004493 bool isZero, bool HasXMMInt,
4494 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004495 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004496 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004497 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 unsigned NumElems = VT.getVectorNumElements();
4499 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004500 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004501 // If this is the insertion idx, put the low elt of V2 here.
4502 MaskVec.push_back(i == Idx ? NumElems : i);
4503 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004504}
4505
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004506/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4507/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004508static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4509 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004510 if (Depth == 6)
4511 return SDValue(); // Limit search depth.
4512
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004513 SDValue V = SDValue(N, 0);
4514 EVT VT = V.getValueType();
4515 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004516
4517 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4518 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4519 Index = SV->getMaskElt(Index);
4520
4521 if (Index < 0)
4522 return DAG.getUNDEF(VT.getVectorElementType());
4523
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004524 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004525 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004526 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004527 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004528
4529 // Recurse into target specific vector shuffles to find scalars.
4530 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004531 int NumElems = VT.getVectorNumElements();
4532 SmallVector<unsigned, 16> ShuffleMask;
4533 SDValue ImmN;
4534
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004535 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004536 case X86ISD::SHUFPS:
4537 case X86ISD::SHUFPD:
4538 ImmN = N->getOperand(N->getNumOperands()-1);
4539 DecodeSHUFPSMask(NumElems,
4540 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4541 ShuffleMask);
4542 break;
4543 case X86ISD::PUNPCKHBW:
4544 case X86ISD::PUNPCKHWD:
4545 case X86ISD::PUNPCKHDQ:
4546 case X86ISD::PUNPCKHQDQ:
4547 DecodePUNPCKHMask(NumElems, ShuffleMask);
4548 break;
4549 case X86ISD::UNPCKHPS:
4550 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004551 case X86ISD::VUNPCKHPSY:
4552 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004553 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4554 break;
4555 case X86ISD::PUNPCKLBW:
4556 case X86ISD::PUNPCKLWD:
4557 case X86ISD::PUNPCKLDQ:
4558 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004559 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004560 break;
4561 case X86ISD::UNPCKLPS:
4562 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004563 case X86ISD::VUNPCKLPSY:
4564 case X86ISD::VUNPCKLPDY:
4565 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004566 break;
4567 case X86ISD::MOVHLPS:
4568 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4569 break;
4570 case X86ISD::MOVLHPS:
4571 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4572 break;
4573 case X86ISD::PSHUFD:
4574 ImmN = N->getOperand(N->getNumOperands()-1);
4575 DecodePSHUFMask(NumElems,
4576 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4577 ShuffleMask);
4578 break;
4579 case X86ISD::PSHUFHW:
4580 ImmN = N->getOperand(N->getNumOperands()-1);
4581 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4582 ShuffleMask);
4583 break;
4584 case X86ISD::PSHUFLW:
4585 ImmN = N->getOperand(N->getNumOperands()-1);
4586 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4587 ShuffleMask);
4588 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004589 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004590 case X86ISD::MOVSD: {
4591 // The index 0 always comes from the first element of the second source,
4592 // this is why MOVSS and MOVSD are used in the first place. The other
4593 // elements come from the other positions of the first source vector.
4594 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004595 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4596 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004597 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004598 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004599 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004600 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004601 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004602 break;
4603 case X86ISD::VPERMILPSY:
4604 ImmN = N->getOperand(N->getNumOperands()-1);
4605 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4606 ShuffleMask);
4607 break;
4608 case X86ISD::VPERMILPD:
4609 ImmN = N->getOperand(N->getNumOperands()-1);
4610 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4611 ShuffleMask);
4612 break;
4613 case X86ISD::VPERMILPDY:
4614 ImmN = N->getOperand(N->getNumOperands()-1);
4615 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4616 ShuffleMask);
4617 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004618 case X86ISD::VPERM2F128:
4619 ImmN = N->getOperand(N->getNumOperands()-1);
4620 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4621 ShuffleMask);
4622 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004623 case X86ISD::MOVDDUP:
4624 case X86ISD::MOVLHPD:
4625 case X86ISD::MOVLPD:
4626 case X86ISD::MOVLPS:
4627 case X86ISD::MOVSHDUP:
4628 case X86ISD::MOVSLDUP:
4629 case X86ISD::PALIGN:
4630 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004631 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004632 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004633 return SDValue();
4634 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004635
4636 Index = ShuffleMask[Index];
4637 if (Index < 0)
4638 return DAG.getUNDEF(VT.getVectorElementType());
4639
4640 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4641 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4642 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004643 }
4644
4645 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004646 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004647 V = V.getOperand(0);
4648 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004649 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004650
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004651 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004652 return SDValue();
4653 }
4654
4655 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4656 return (Index == 0) ? V.getOperand(0)
4657 : DAG.getUNDEF(VT.getVectorElementType());
4658
4659 if (V.getOpcode() == ISD::BUILD_VECTOR)
4660 return V.getOperand(Index);
4661
4662 return SDValue();
4663}
4664
4665/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4666/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004667/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004668static
4669unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4670 bool ZerosFromLeft, SelectionDAG &DAG) {
4671 int i = 0;
4672
4673 while (i < NumElems) {
4674 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004675 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004676 if (!(Elt.getNode() &&
4677 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4678 break;
4679 ++i;
4680 }
4681
4682 return i;
4683}
4684
4685/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4686/// MaskE correspond consecutively to elements from one of the vector operands,
4687/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4688static
4689bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4690 int OpIdx, int NumElems, unsigned &OpNum) {
4691 bool SeenV1 = false;
4692 bool SeenV2 = false;
4693
4694 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4695 int Idx = SVOp->getMaskElt(i);
4696 // Ignore undef indicies
4697 if (Idx < 0)
4698 continue;
4699
4700 if (Idx < NumElems)
4701 SeenV1 = true;
4702 else
4703 SeenV2 = true;
4704
4705 // Only accept consecutive elements from the same vector
4706 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4707 return false;
4708 }
4709
4710 OpNum = SeenV1 ? 0 : 1;
4711 return true;
4712}
4713
4714/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4715/// logical left shift of a vector.
4716static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4717 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4718 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4719 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4720 false /* check zeros from right */, DAG);
4721 unsigned OpSrc;
4722
4723 if (!NumZeros)
4724 return false;
4725
4726 // Considering the elements in the mask that are not consecutive zeros,
4727 // check if they consecutively come from only one of the source vectors.
4728 //
4729 // V1 = {X, A, B, C} 0
4730 // \ \ \ /
4731 // vector_shuffle V1, V2 <1, 2, 3, X>
4732 //
4733 if (!isShuffleMaskConsecutive(SVOp,
4734 0, // Mask Start Index
4735 NumElems-NumZeros-1, // Mask End Index
4736 NumZeros, // Where to start looking in the src vector
4737 NumElems, // Number of elements in vector
4738 OpSrc)) // Which source operand ?
4739 return false;
4740
4741 isLeft = false;
4742 ShAmt = NumZeros;
4743 ShVal = SVOp->getOperand(OpSrc);
4744 return true;
4745}
4746
4747/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4748/// logical left shift of a vector.
4749static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4750 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4751 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4752 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4753 true /* check zeros from left */, DAG);
4754 unsigned OpSrc;
4755
4756 if (!NumZeros)
4757 return false;
4758
4759 // Considering the elements in the mask that are not consecutive zeros,
4760 // check if they consecutively come from only one of the source vectors.
4761 //
4762 // 0 { A, B, X, X } = V2
4763 // / \ / /
4764 // vector_shuffle V1, V2 <X, X, 4, 5>
4765 //
4766 if (!isShuffleMaskConsecutive(SVOp,
4767 NumZeros, // Mask Start Index
4768 NumElems-1, // Mask End Index
4769 0, // Where to start looking in the src vector
4770 NumElems, // Number of elements in vector
4771 OpSrc)) // Which source operand ?
4772 return false;
4773
4774 isLeft = true;
4775 ShAmt = NumZeros;
4776 ShVal = SVOp->getOperand(OpSrc);
4777 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004778}
4779
4780/// isVectorShift - Returns true if the shuffle can be implemented as a
4781/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004782static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004783 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004784 // Although the logic below support any bitwidth size, there are no
4785 // shift instructions which handle more than 128-bit vectors.
4786 if (SVOp->getValueType(0).getSizeInBits() > 128)
4787 return false;
4788
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004789 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4790 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4791 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004792
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004793 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004794}
4795
Evan Chengc78d3b42006-04-24 18:01:45 +00004796/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4797///
Dan Gohman475871a2008-07-27 21:46:04 +00004798static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004799 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004800 SelectionDAG &DAG,
4801 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004802 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004803 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004804
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004805 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004806 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004807 bool First = true;
4808 for (unsigned i = 0; i < 16; ++i) {
4809 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4810 if (ThisIsNonZero && First) {
4811 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004813 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004814 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004815 First = false;
4816 }
4817
4818 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004819 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004820 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4821 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004822 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004823 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004824 }
4825 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4827 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4828 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004829 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004830 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004831 } else
4832 ThisElt = LastElt;
4833
Gabor Greifba36cb52008-08-28 21:40:38 +00004834 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004836 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004837 }
4838 }
4839
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004840 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004841}
4842
Bill Wendlinga348c562007-03-22 18:42:45 +00004843/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004844///
Dan Gohman475871a2008-07-27 21:46:04 +00004845static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004846 unsigned NumNonZero, unsigned NumZero,
4847 SelectionDAG &DAG,
4848 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004849 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004850 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004851
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004852 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004853 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004854 bool First = true;
4855 for (unsigned i = 0; i < 8; ++i) {
4856 bool isNonZero = (NonZeros & (1 << i)) != 0;
4857 if (isNonZero) {
4858 if (First) {
4859 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004861 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004863 First = false;
4864 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004865 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004867 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004868 }
4869 }
4870
4871 return V;
4872}
4873
Evan Chengf26ffe92008-05-29 08:22:04 +00004874/// getVShift - Return a vector logical shift node.
4875///
Owen Andersone50ed302009-08-10 22:56:29 +00004876static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004877 unsigned NumBits, SelectionDAG &DAG,
4878 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004879 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004880 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004881 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004882 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4883 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004884 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004885 DAG.getConstant(NumBits,
4886 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004887}
4888
Dan Gohman475871a2008-07-27 21:46:04 +00004889SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004890X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004891 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004892
Evan Chengc3630942009-12-09 21:00:30 +00004893 // Check if the scalar load can be widened into a vector load. And if
4894 // the address is "base + cst" see if the cst can be "absorbed" into
4895 // the shuffle mask.
4896 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4897 SDValue Ptr = LD->getBasePtr();
4898 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4899 return SDValue();
4900 EVT PVT = LD->getValueType(0);
4901 if (PVT != MVT::i32 && PVT != MVT::f32)
4902 return SDValue();
4903
4904 int FI = -1;
4905 int64_t Offset = 0;
4906 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4907 FI = FINode->getIndex();
4908 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004909 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004910 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4911 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4912 Offset = Ptr.getConstantOperandVal(1);
4913 Ptr = Ptr.getOperand(0);
4914 } else {
4915 return SDValue();
4916 }
4917
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004918 // FIXME: 256-bit vector instructions don't require a strict alignment,
4919 // improve this code to support it better.
4920 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004921 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004922 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004923 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004924 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004925 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004926 // Can't change the alignment. FIXME: It's possible to compute
4927 // the exact stack offset and reference FI + adjust offset instead.
4928 // If someone *really* cares about this. That's the way to implement it.
4929 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004930 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004931 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004932 }
4933 }
4934
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004935 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004936 // Ptr + (Offset & ~15).
4937 if (Offset < 0)
4938 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004939 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004940 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004941 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004942 if (StartOffset)
4943 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4944 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4945
4946 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004947 int NumElems = VT.getVectorNumElements();
4948
4949 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4950 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4951 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004952 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004953 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004954
4955 // Canonicalize it to a v4i32 or v8i32 shuffle.
4956 SmallVector<int, 8> Mask;
4957 for (int i = 0; i < NumElems; ++i)
4958 Mask.push_back(EltNo);
4959
4960 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4961 return DAG.getNode(ISD::BITCAST, dl, NVT,
4962 DAG.getVectorShuffle(CanonVT, dl, V1,
4963 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004964 }
4965
4966 return SDValue();
4967}
4968
Michael J. Spencerec38de22010-10-10 22:04:20 +00004969/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4970/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004971/// load which has the same value as a build_vector whose operands are 'elts'.
4972///
4973/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004974///
Nate Begeman1449f292010-03-24 22:19:06 +00004975/// FIXME: we'd also like to handle the case where the last elements are zero
4976/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4977/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004978static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004979 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004980 EVT EltVT = VT.getVectorElementType();
4981 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004982
Nate Begemanfdea31a2010-03-24 20:49:50 +00004983 LoadSDNode *LDBase = NULL;
4984 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004985
Nate Begeman1449f292010-03-24 22:19:06 +00004986 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004987 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004988 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004989 for (unsigned i = 0; i < NumElems; ++i) {
4990 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004991
Nate Begemanfdea31a2010-03-24 20:49:50 +00004992 if (!Elt.getNode() ||
4993 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4994 return SDValue();
4995 if (!LDBase) {
4996 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4997 return SDValue();
4998 LDBase = cast<LoadSDNode>(Elt.getNode());
4999 LastLoadedElt = i;
5000 continue;
5001 }
5002 if (Elt.getOpcode() == ISD::UNDEF)
5003 continue;
5004
5005 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5006 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5007 return SDValue();
5008 LastLoadedElt = i;
5009 }
Nate Begeman1449f292010-03-24 22:19:06 +00005010
5011 // If we have found an entire vector of loads and undefs, then return a large
5012 // load of the entire vector width starting at the base pointer. If we found
5013 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005014 if (LastLoadedElt == NumElems - 1) {
5015 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005016 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005017 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005018 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005019 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005020 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005021 LDBase->isVolatile(), LDBase->isNonTemporal(),
5022 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005023 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5024 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005025 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5026 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005027 SDValue ResNode =
5028 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5029 LDBase->getPointerInfo(),
5030 LDBase->getAlignment(),
5031 false/*isVolatile*/, true/*ReadMem*/,
5032 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005033 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005034 }
5035 return SDValue();
5036}
5037
Evan Chengc3630942009-12-09 21:00:30 +00005038SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005039X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005040 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005041
David Greenef125a292011-02-08 19:04:41 +00005042 EVT VT = Op.getValueType();
5043 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005044 unsigned NumElems = Op.getNumOperands();
5045
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005046 // Vectors containing all zeros can be matched by pxor and xorps later
5047 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5048 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5049 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005050 if (Op.getValueType() == MVT::v4i32 ||
5051 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005052 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005053
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005054 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005055 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005056
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005057 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5058 // vectors or broken into v4i32 operations on 256-bit vectors.
5059 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5060 if (Op.getValueType() == MVT::v4i32)
5061 return Op;
5062
5063 return getOnesVector(Op.getValueType(), DAG, dl);
5064 }
5065
Owen Andersone50ed302009-08-10 22:56:29 +00005066 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005067
Evan Cheng0db9fe62006-04-25 20:13:52 +00005068 unsigned NumZero = 0;
5069 unsigned NumNonZero = 0;
5070 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005071 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005072 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005073 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005074 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005075 if (Elt.getOpcode() == ISD::UNDEF)
5076 continue;
5077 Values.insert(Elt);
5078 if (Elt.getOpcode() != ISD::Constant &&
5079 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005080 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005081 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005082 NumZero++;
5083 else {
5084 NonZeros |= (1 << i);
5085 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005086 }
5087 }
5088
Chris Lattner97a2a562010-08-26 05:24:29 +00005089 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5090 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005091 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005092
Chris Lattner67f453a2008-03-09 05:42:06 +00005093 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005094 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005095 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005096 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005097
Chris Lattner62098042008-03-09 01:05:04 +00005098 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5099 // the value are obviously zero, truncate the value to i32 and do the
5100 // insertion that way. Only do this if the value is non-constant or if the
5101 // value is a constant being inserted into element 0. It is cheaper to do
5102 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005103 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005104 (!IsAllConstants || Idx == 0)) {
5105 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005106 // Handle SSE only.
5107 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5108 EVT VecVT = MVT::v4i32;
5109 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005110
Chris Lattner62098042008-03-09 01:05:04 +00005111 // Truncate the value (which may itself be a constant) to i32, and
5112 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005113 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005114 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005115 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005116 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005117
Chris Lattner62098042008-03-09 01:05:04 +00005118 // Now we have our 32-bit value zero extended in the low element of
5119 // a vector. If Idx != 0, swizzle it into place.
5120 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005121 SmallVector<int, 4> Mask;
5122 Mask.push_back(Idx);
5123 for (unsigned i = 1; i != VecElts; ++i)
5124 Mask.push_back(i);
5125 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005126 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005127 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005128 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005129 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005130 }
5131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005132
Chris Lattner19f79692008-03-08 22:59:52 +00005133 // If we have a constant or non-constant insertion into the low element of
5134 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5135 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005136 // depending on what the source datatype is.
5137 if (Idx == 0) {
5138 if (NumZero == 0) {
5139 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005140 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5141 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005142 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5143 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005144 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005145 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005146 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5147 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005148 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5149 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005150 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5151 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005152 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005153 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005154 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005155 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005156
5157 // Is it a vector logical left shift?
5158 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005159 X86::isZeroNode(Op.getOperand(0)) &&
5160 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005161 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005162 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005163 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005164 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005165 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005166 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005167
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005168 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005169 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005170
Chris Lattner19f79692008-03-08 22:59:52 +00005171 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5172 // is a non-constant being inserted into an element other than the low one,
5173 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5174 // movd/movss) to move this into the low element, then shuffle it into
5175 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005176 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005177 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005178
Evan Cheng0db9fe62006-04-25 20:13:52 +00005179 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005180 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005181 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005182 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005183 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005184 MaskVec.push_back(i == Idx ? 0 : 1);
5185 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005186 }
5187 }
5188
Chris Lattner67f453a2008-03-09 05:42:06 +00005189 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005190 if (Values.size() == 1) {
5191 if (EVTBits == 32) {
5192 // Instead of a shuffle like this:
5193 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5194 // Check if it's possible to issue this instead.
5195 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5196 unsigned Idx = CountTrailingZeros_32(NonZeros);
5197 SDValue Item = Op.getOperand(Idx);
5198 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5199 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5200 }
Dan Gohman475871a2008-07-27 21:46:04 +00005201 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005202 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005203
Dan Gohmana3941172007-07-24 22:55:08 +00005204 // A vector full of immediates; various special cases are already
5205 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005206 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005207 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005208
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005209 // For AVX-length vectors, build the individual 128-bit pieces and use
5210 // shuffles to put them in place.
5211 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5212 SmallVector<SDValue, 32> V;
5213 for (unsigned i = 0; i < NumElems; ++i)
5214 V.push_back(Op.getOperand(i));
5215
5216 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5217
5218 // Build both the lower and upper subvector.
5219 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5220 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5221 NumElems/2);
5222
5223 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005224 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5225 DAG.getConstant(0, MVT::i32), DAG, dl);
5226 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005227 DAG, dl);
5228 }
5229
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005230 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005231 if (EVTBits == 64) {
5232 if (NumNonZero == 1) {
5233 // One half is zero or undef.
5234 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005235 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005236 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005237 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005238 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005239 }
Dan Gohman475871a2008-07-27 21:46:04 +00005240 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005241 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005242
5243 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005244 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005245 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005246 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005247 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005248 }
5249
Bill Wendling826f36f2007-03-28 00:57:11 +00005250 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005251 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005252 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005253 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005254 }
5255
5256 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005257 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005258 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005259 if (NumElems == 4 && NumZero > 0) {
5260 for (unsigned i = 0; i < 4; ++i) {
5261 bool isZero = !(NonZeros & (1 << i));
5262 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005263 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005264 else
Dale Johannesenace16102009-02-03 19:33:06 +00005265 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005266 }
5267
5268 for (unsigned i = 0; i < 2; ++i) {
5269 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5270 default: break;
5271 case 0:
5272 V[i] = V[i*2]; // Must be a zero vector.
5273 break;
5274 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005275 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005276 break;
5277 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005278 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005279 break;
5280 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005281 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005282 break;
5283 }
5284 }
5285
Nate Begeman9008ca62009-04-27 18:41:29 +00005286 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005287 bool Reverse = (NonZeros & 0x3) == 2;
5288 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005289 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005290 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5291 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005292 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5293 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005294 }
5295
Nate Begemanfdea31a2010-03-24 20:49:50 +00005296 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5297 // Check for a build vector of consecutive loads.
5298 for (unsigned i = 0; i < NumElems; ++i)
5299 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005300
Nate Begemanfdea31a2010-03-24 20:49:50 +00005301 // Check for elements which are consecutive loads.
5302 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5303 if (LD.getNode())
5304 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005305
5306 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005307 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005308 SDValue Result;
5309 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5310 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5311 else
5312 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005313
Chris Lattner24faf612010-08-28 17:59:08 +00005314 for (unsigned i = 1; i < NumElems; ++i) {
5315 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5316 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005317 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005318 }
5319 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005320 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005321
Chris Lattner6e80e442010-08-28 17:15:43 +00005322 // Otherwise, expand into a number of unpckl*, start by extending each of
5323 // our (non-undef) elements to the full vector width with the element in the
5324 // bottom slot of the vector (which generates no code for SSE).
5325 for (unsigned i = 0; i < NumElems; ++i) {
5326 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5327 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5328 else
5329 V[i] = DAG.getUNDEF(VT);
5330 }
5331
5332 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005333 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5334 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5335 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005336 unsigned EltStride = NumElems >> 1;
5337 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005338 for (unsigned i = 0; i < EltStride; ++i) {
5339 // If V[i+EltStride] is undef and this is the first round of mixing,
5340 // then it is safe to just drop this shuffle: V[i] is already in the
5341 // right place, the one element (since it's the first round) being
5342 // inserted as undef can be dropped. This isn't safe for successive
5343 // rounds because they will permute elements within both vectors.
5344 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5345 EltStride == NumElems/2)
5346 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005347
Chris Lattner6e80e442010-08-28 17:15:43 +00005348 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005349 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005350 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005351 }
5352 return V[0];
5353 }
Dan Gohman475871a2008-07-27 21:46:04 +00005354 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005355}
5356
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005357// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5358// them in a MMX register. This is better than doing a stack convert.
5359static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005360 DebugLoc dl = Op.getDebugLoc();
5361 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005362
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005363 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5364 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5365 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005366 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005367 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5368 InVec = Op.getOperand(1);
5369 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5370 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005371 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005372 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5373 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5374 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005375 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005376 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5377 Mask[0] = 0; Mask[1] = 2;
5378 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5379 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005380 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005381}
5382
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005383// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5384// to create 256-bit vectors from two other 128-bit ones.
5385static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5386 DebugLoc dl = Op.getDebugLoc();
5387 EVT ResVT = Op.getValueType();
5388
5389 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5390
5391 SDValue V1 = Op.getOperand(0);
5392 SDValue V2 = Op.getOperand(1);
5393 unsigned NumElems = ResVT.getVectorNumElements();
5394
5395 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5396 DAG.getConstant(0, MVT::i32), DAG, dl);
5397 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5398 DAG, dl);
5399}
5400
5401SDValue
5402X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005403 EVT ResVT = Op.getValueType();
5404
5405 assert(Op.getNumOperands() == 2);
5406 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5407 "Unsupported CONCAT_VECTORS for value type");
5408
5409 // We support concatenate two MMX registers and place them in a MMX register.
5410 // This is better than doing a stack convert.
5411 if (ResVT.is128BitVector())
5412 return LowerMMXCONCAT_VECTORS(Op, DAG);
5413
5414 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5415 // from two other 128-bit ones.
5416 return LowerAVXCONCAT_VECTORS(Op, DAG);
5417}
5418
Nate Begemanb9a47b82009-02-23 08:49:38 +00005419// v8i16 shuffles - Prefer shuffles in the following order:
5420// 1. [all] pshuflw, pshufhw, optional move
5421// 2. [ssse3] 1 x pshufb
5422// 3. [ssse3] 2 x pshufb + 1 x por
5423// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005424SDValue
5425X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5426 SelectionDAG &DAG) const {
5427 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005428 SDValue V1 = SVOp->getOperand(0);
5429 SDValue V2 = SVOp->getOperand(1);
5430 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005431 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005432
Nate Begemanb9a47b82009-02-23 08:49:38 +00005433 // Determine if more than 1 of the words in each of the low and high quadwords
5434 // of the result come from the same quadword of one of the two inputs. Undef
5435 // mask values count as coming from any quadword, for better codegen.
5436 SmallVector<unsigned, 4> LoQuad(4);
5437 SmallVector<unsigned, 4> HiQuad(4);
5438 BitVector InputQuads(4);
5439 for (unsigned i = 0; i < 8; ++i) {
5440 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005441 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005442 MaskVals.push_back(EltIdx);
5443 if (EltIdx < 0) {
5444 ++Quad[0];
5445 ++Quad[1];
5446 ++Quad[2];
5447 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005448 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005449 }
5450 ++Quad[EltIdx / 4];
5451 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005452 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005453
Nate Begemanb9a47b82009-02-23 08:49:38 +00005454 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005455 unsigned MaxQuad = 1;
5456 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005457 if (LoQuad[i] > MaxQuad) {
5458 BestLoQuad = i;
5459 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005460 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005461 }
5462
Nate Begemanb9a47b82009-02-23 08:49:38 +00005463 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005464 MaxQuad = 1;
5465 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005466 if (HiQuad[i] > MaxQuad) {
5467 BestHiQuad = i;
5468 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005469 }
5470 }
5471
Nate Begemanb9a47b82009-02-23 08:49:38 +00005472 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005473 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005474 // single pshufb instruction is necessary. If There are more than 2 input
5475 // quads, disable the next transformation since it does not help SSSE3.
5476 bool V1Used = InputQuads[0] || InputQuads[1];
5477 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005478 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005479 if (InputQuads.count() == 2 && V1Used && V2Used) {
5480 BestLoQuad = InputQuads.find_first();
5481 BestHiQuad = InputQuads.find_next(BestLoQuad);
5482 }
5483 if (InputQuads.count() > 2) {
5484 BestLoQuad = -1;
5485 BestHiQuad = -1;
5486 }
5487 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005488
Nate Begemanb9a47b82009-02-23 08:49:38 +00005489 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5490 // the shuffle mask. If a quad is scored as -1, that means that it contains
5491 // words from all 4 input quadwords.
5492 SDValue NewV;
5493 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005494 SmallVector<int, 8> MaskV;
5495 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5496 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005497 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005498 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5499 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5500 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005501
Nate Begemanb9a47b82009-02-23 08:49:38 +00005502 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5503 // source words for the shuffle, to aid later transformations.
5504 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005505 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005506 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005507 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005508 if (idx != (int)i)
5509 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005510 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005511 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005512 AllWordsInNewV = false;
5513 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005514 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005515
Nate Begemanb9a47b82009-02-23 08:49:38 +00005516 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5517 if (AllWordsInNewV) {
5518 for (int i = 0; i != 8; ++i) {
5519 int idx = MaskVals[i];
5520 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005521 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005522 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005523 if ((idx != i) && idx < 4)
5524 pshufhw = false;
5525 if ((idx != i) && idx > 3)
5526 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005527 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005528 V1 = NewV;
5529 V2Used = false;
5530 BestLoQuad = 0;
5531 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005532 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005533
Nate Begemanb9a47b82009-02-23 08:49:38 +00005534 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5535 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005536 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005537 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5538 unsigned TargetMask = 0;
5539 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005540 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005541 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5542 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5543 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005544 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005545 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005546 }
Eric Christopherfd179292009-08-27 18:07:15 +00005547
Nate Begemanb9a47b82009-02-23 08:49:38 +00005548 // If we have SSSE3, and all words of the result are from 1 input vector,
5549 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5550 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005551 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005552 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005553
Nate Begemanb9a47b82009-02-23 08:49:38 +00005554 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005555 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005556 // mask, and elements that come from V1 in the V2 mask, so that the two
5557 // results can be OR'd together.
5558 bool TwoInputs = V1Used && V2Used;
5559 for (unsigned i = 0; i != 8; ++i) {
5560 int EltIdx = MaskVals[i] * 2;
5561 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5563 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005564 continue;
5565 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005566 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5567 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005568 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005569 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005570 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005571 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005573 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005574 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005575
Nate Begemanb9a47b82009-02-23 08:49:38 +00005576 // Calculate the shuffle mask for the second input, shuffle it, and
5577 // OR it with the first shuffled input.
5578 pshufbMask.clear();
5579 for (unsigned i = 0; i != 8; ++i) {
5580 int EltIdx = MaskVals[i] * 2;
5581 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005582 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5583 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005584 continue;
5585 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005586 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5587 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005588 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005589 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005590 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005591 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005592 MVT::v16i8, &pshufbMask[0], 16));
5593 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005594 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005595 }
5596
5597 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5598 // and update MaskVals with new element order.
5599 BitVector InOrder(8);
5600 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005601 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005602 for (int i = 0; i != 4; ++i) {
5603 int idx = MaskVals[i];
5604 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005605 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005606 InOrder.set(i);
5607 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005608 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005609 InOrder.set(i);
5610 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005611 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005612 }
5613 }
5614 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005615 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005616 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005617 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005618
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005619 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5620 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005621 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5622 NewV.getOperand(0),
5623 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5624 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005625 }
Eric Christopherfd179292009-08-27 18:07:15 +00005626
Nate Begemanb9a47b82009-02-23 08:49:38 +00005627 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5628 // and update MaskVals with the new element order.
5629 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005630 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005631 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005632 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005633 for (unsigned i = 4; i != 8; ++i) {
5634 int idx = MaskVals[i];
5635 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005636 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 InOrder.set(i);
5638 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005639 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005640 InOrder.set(i);
5641 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005642 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005643 }
5644 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005645 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005646 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005647
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005648 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5649 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005650 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5651 NewV.getOperand(0),
5652 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5653 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005654 }
Eric Christopherfd179292009-08-27 18:07:15 +00005655
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 // In case BestHi & BestLo were both -1, which means each quadword has a word
5657 // from each of the four input quadwords, calculate the InOrder bitvector now
5658 // before falling through to the insert/extract cleanup.
5659 if (BestLoQuad == -1 && BestHiQuad == -1) {
5660 NewV = V1;
5661 for (int i = 0; i != 8; ++i)
5662 if (MaskVals[i] < 0 || MaskVals[i] == i)
5663 InOrder.set(i);
5664 }
Eric Christopherfd179292009-08-27 18:07:15 +00005665
Nate Begemanb9a47b82009-02-23 08:49:38 +00005666 // The other elements are put in the right place using pextrw and pinsrw.
5667 for (unsigned i = 0; i != 8; ++i) {
5668 if (InOrder[i])
5669 continue;
5670 int EltIdx = MaskVals[i];
5671 if (EltIdx < 0)
5672 continue;
5673 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005674 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005676 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005678 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 DAG.getIntPtrConstant(i));
5680 }
5681 return NewV;
5682}
5683
5684// v16i8 shuffles - Prefer shuffles in the following order:
5685// 1. [ssse3] 1 x pshufb
5686// 2. [ssse3] 2 x pshufb + 1 x por
5687// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5688static
Nate Begeman9008ca62009-04-27 18:41:29 +00005689SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005690 SelectionDAG &DAG,
5691 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005692 SDValue V1 = SVOp->getOperand(0);
5693 SDValue V2 = SVOp->getOperand(1);
5694 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005695 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005696 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005697
Nate Begemanb9a47b82009-02-23 08:49:38 +00005698 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005699 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700 // present, fall back to case 3.
5701 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5702 bool V1Only = true;
5703 bool V2Only = true;
5704 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005705 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 if (EltIdx < 0)
5707 continue;
5708 if (EltIdx < 16)
5709 V2Only = false;
5710 else
5711 V1Only = false;
5712 }
Eric Christopherfd179292009-08-27 18:07:15 +00005713
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005715 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005716 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005717
Nate Begemanb9a47b82009-02-23 08:49:38 +00005718 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005719 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 //
5721 // Otherwise, we have elements from both input vectors, and must zero out
5722 // elements that come from V2 in the first mask, and V1 in the second mask
5723 // so that we can OR them together.
5724 bool TwoInputs = !(V1Only || V2Only);
5725 for (unsigned i = 0; i != 16; ++i) {
5726 int EltIdx = MaskVals[i];
5727 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 continue;
5730 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005731 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 }
5733 // If all the elements are from V2, assign it to V1 and return after
5734 // building the first pshufb.
5735 if (V2Only)
5736 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005737 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005738 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005739 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740 if (!TwoInputs)
5741 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005742
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743 // Calculate the shuffle mask for the second input, shuffle it, and
5744 // OR it with the first shuffled input.
5745 pshufbMask.clear();
5746 for (unsigned i = 0; i != 16; ++i) {
5747 int EltIdx = MaskVals[i];
5748 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005749 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 continue;
5751 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005754 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005755 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005756 MVT::v16i8, &pshufbMask[0], 16));
5757 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 }
Eric Christopherfd179292009-08-27 18:07:15 +00005759
Nate Begemanb9a47b82009-02-23 08:49:38 +00005760 // No SSSE3 - Calculate in place words and then fix all out of place words
5761 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5762 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005763 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5764 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 SDValue NewV = V2Only ? V2 : V1;
5766 for (int i = 0; i != 8; ++i) {
5767 int Elt0 = MaskVals[i*2];
5768 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005769
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 // This word of the result is all undef, skip it.
5771 if (Elt0 < 0 && Elt1 < 0)
5772 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005773
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 // This word of the result is already in the correct place, skip it.
5775 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5776 continue;
5777 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5778 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005779
Nate Begemanb9a47b82009-02-23 08:49:38 +00005780 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5781 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5782 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005783
5784 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5785 // using a single extract together, load it and store it.
5786 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005787 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005788 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005790 DAG.getIntPtrConstant(i));
5791 continue;
5792 }
5793
Nate Begemanb9a47b82009-02-23 08:49:38 +00005794 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005795 // source byte is not also odd, shift the extracted word left 8 bits
5796 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005798 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 DAG.getIntPtrConstant(Elt1 / 2));
5800 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005801 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005802 DAG.getConstant(8,
5803 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005804 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005805 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5806 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 }
5808 // If Elt0 is defined, extract it from the appropriate source. If the
5809 // source byte is not also even, shift the extracted word right 8 bits. If
5810 // Elt1 was also defined, OR the extracted values together before
5811 // inserting them in the result.
5812 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005813 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5815 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005816 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005817 DAG.getConstant(8,
5818 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005819 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005820 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5821 DAG.getConstant(0x00FF, MVT::i16));
5822 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005823 : InsElt0;
5824 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005825 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 DAG.getIntPtrConstant(i));
5827 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005828 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005829}
5830
Evan Cheng7a831ce2007-12-15 03:00:47 +00005831/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005832/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005833/// done when every pair / quad of shuffle mask elements point to elements in
5834/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005835/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005836static
Nate Begeman9008ca62009-04-27 18:41:29 +00005837SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005838 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005839 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005840 SDValue V1 = SVOp->getOperand(0);
5841 SDValue V2 = SVOp->getOperand(1);
5842 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005843 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005844 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005845 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005846 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005847 case MVT::v4f32: NewVT = MVT::v2f64; break;
5848 case MVT::v4i32: NewVT = MVT::v2i64; break;
5849 case MVT::v8i16: NewVT = MVT::v4i32; break;
5850 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005851 }
5852
Nate Begeman9008ca62009-04-27 18:41:29 +00005853 int Scale = NumElems / NewWidth;
5854 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005855 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005856 int StartIdx = -1;
5857 for (int j = 0; j < Scale; ++j) {
5858 int EltIdx = SVOp->getMaskElt(i+j);
5859 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005860 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005861 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005862 StartIdx = EltIdx - (EltIdx % Scale);
5863 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005864 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005865 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005866 if (StartIdx == -1)
5867 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005868 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005869 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005870 }
5871
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005872 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5873 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005874 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005875}
5876
Evan Chengd880b972008-05-09 21:53:03 +00005877/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005878///
Owen Andersone50ed302009-08-10 22:56:29 +00005879static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005880 SDValue SrcOp, SelectionDAG &DAG,
5881 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005882 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005883 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005884 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005885 LD = dyn_cast<LoadSDNode>(SrcOp);
5886 if (!LD) {
5887 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5888 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005889 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005890 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005891 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005892 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005893 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005894 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005895 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005896 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005897 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5898 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5899 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005900 SrcOp.getOperand(0)
5901 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005902 }
5903 }
5904 }
5905
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005906 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005907 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005908 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005909 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005910}
5911
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005912/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5913/// shuffle node referes to only one lane in the sources.
5914static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5915 EVT VT = SVOp->getValueType(0);
5916 int NumElems = VT.getVectorNumElements();
5917 int HalfSize = NumElems/2;
5918 SmallVector<int, 16> M;
5919 SVOp->getMask(M);
5920 bool MatchA = false, MatchB = false;
5921
5922 for (int l = 0; l < NumElems*2; l += HalfSize) {
5923 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5924 MatchA = true;
5925 break;
5926 }
5927 }
5928
5929 for (int l = 0; l < NumElems*2; l += HalfSize) {
5930 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5931 MatchB = true;
5932 break;
5933 }
5934 }
5935
5936 return MatchA && MatchB;
5937}
5938
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005939/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5940/// which could not be matched by any known target speficic shuffle
5941static SDValue
5942LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005943 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5944 // If each half of a vector shuffle node referes to only one lane in the
5945 // source vectors, extract each used 128-bit lane and shuffle them using
5946 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5947 // the work to the legalizer.
5948 DebugLoc dl = SVOp->getDebugLoc();
5949 EVT VT = SVOp->getValueType(0);
5950 int NumElems = VT.getVectorNumElements();
5951 int HalfSize = NumElems/2;
5952
5953 // Extract the reference for each half
5954 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5955 int FstVecOpNum = 0, SndVecOpNum = 0;
5956 for (int i = 0; i < HalfSize; ++i) {
5957 int Elt = SVOp->getMaskElt(i);
5958 if (SVOp->getMaskElt(i) < 0)
5959 continue;
5960 FstVecOpNum = Elt/NumElems;
5961 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5962 break;
5963 }
5964 for (int i = HalfSize; i < NumElems; ++i) {
5965 int Elt = SVOp->getMaskElt(i);
5966 if (SVOp->getMaskElt(i) < 0)
5967 continue;
5968 SndVecOpNum = Elt/NumElems;
5969 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5970 break;
5971 }
5972
5973 // Extract the subvectors
5974 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5975 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5976 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5977 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5978
5979 // Generate 128-bit shuffles
5980 SmallVector<int, 16> MaskV1, MaskV2;
5981 for (int i = 0; i < HalfSize; ++i) {
5982 int Elt = SVOp->getMaskElt(i);
5983 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5984 }
5985 for (int i = HalfSize; i < NumElems; ++i) {
5986 int Elt = SVOp->getMaskElt(i);
5987 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5988 }
5989
5990 EVT NVT = V1.getValueType();
5991 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5992 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5993
5994 // Concatenate the result back
5995 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5996 DAG.getConstant(0, MVT::i32), DAG, dl);
5997 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5998 DAG, dl);
5999 }
6000
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006001 return SDValue();
6002}
6003
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006004/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6005/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006006static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006007LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006008 SDValue V1 = SVOp->getOperand(0);
6009 SDValue V2 = SVOp->getOperand(1);
6010 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006011 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006012
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006013 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6014
Evan Chengace3c172008-07-22 21:13:36 +00006015 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006016 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006017 SmallVector<int, 8> Mask1(4U, -1);
6018 SmallVector<int, 8> PermMask;
6019 SVOp->getMask(PermMask);
6020
Evan Chengace3c172008-07-22 21:13:36 +00006021 unsigned NumHi = 0;
6022 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006023 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006024 int Idx = PermMask[i];
6025 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006026 Locs[i] = std::make_pair(-1, -1);
6027 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006028 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6029 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006030 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006031 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006032 NumLo++;
6033 } else {
6034 Locs[i] = std::make_pair(1, NumHi);
6035 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006036 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006037 NumHi++;
6038 }
6039 }
6040 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006041
Evan Chengace3c172008-07-22 21:13:36 +00006042 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006043 // If no more than two elements come from either vector. This can be
6044 // implemented with two shuffles. First shuffle gather the elements.
6045 // The second shuffle, which takes the first shuffle as both of its
6046 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006047 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006048
Nate Begeman9008ca62009-04-27 18:41:29 +00006049 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006050
Evan Chengace3c172008-07-22 21:13:36 +00006051 for (unsigned i = 0; i != 4; ++i) {
6052 if (Locs[i].first == -1)
6053 continue;
6054 else {
6055 unsigned Idx = (i < 2) ? 0 : 4;
6056 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006057 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006058 }
6059 }
6060
Nate Begeman9008ca62009-04-27 18:41:29 +00006061 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006062 } else if (NumLo == 3 || NumHi == 3) {
6063 // Otherwise, we must have three elements from one vector, call it X, and
6064 // one element from the other, call it Y. First, use a shufps to build an
6065 // intermediate vector with the one element from Y and the element from X
6066 // that will be in the same half in the final destination (the indexes don't
6067 // matter). Then, use a shufps to build the final vector, taking the half
6068 // containing the element from Y from the intermediate, and the other half
6069 // from X.
6070 if (NumHi == 3) {
6071 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006072 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006073 std::swap(V1, V2);
6074 }
6075
6076 // Find the element from V2.
6077 unsigned HiIndex;
6078 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006079 int Val = PermMask[HiIndex];
6080 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006081 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006082 if (Val >= 4)
6083 break;
6084 }
6085
Nate Begeman9008ca62009-04-27 18:41:29 +00006086 Mask1[0] = PermMask[HiIndex];
6087 Mask1[1] = -1;
6088 Mask1[2] = PermMask[HiIndex^1];
6089 Mask1[3] = -1;
6090 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006091
6092 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006093 Mask1[0] = PermMask[0];
6094 Mask1[1] = PermMask[1];
6095 Mask1[2] = HiIndex & 1 ? 6 : 4;
6096 Mask1[3] = HiIndex & 1 ? 4 : 6;
6097 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006098 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 Mask1[0] = HiIndex & 1 ? 2 : 0;
6100 Mask1[1] = HiIndex & 1 ? 0 : 2;
6101 Mask1[2] = PermMask[2];
6102 Mask1[3] = PermMask[3];
6103 if (Mask1[2] >= 0)
6104 Mask1[2] += 4;
6105 if (Mask1[3] >= 0)
6106 Mask1[3] += 4;
6107 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006108 }
Evan Chengace3c172008-07-22 21:13:36 +00006109 }
6110
6111 // Break it into (shuffle shuffle_hi, shuffle_lo).
6112 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006113 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006114 SmallVector<int,8> LoMask(4U, -1);
6115 SmallVector<int,8> HiMask(4U, -1);
6116
6117 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006118 unsigned MaskIdx = 0;
6119 unsigned LoIdx = 0;
6120 unsigned HiIdx = 2;
6121 for (unsigned i = 0; i != 4; ++i) {
6122 if (i == 2) {
6123 MaskPtr = &HiMask;
6124 MaskIdx = 1;
6125 LoIdx = 0;
6126 HiIdx = 2;
6127 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006128 int Idx = PermMask[i];
6129 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006130 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006132 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006133 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006134 LoIdx++;
6135 } else {
6136 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006137 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006138 HiIdx++;
6139 }
6140 }
6141
Nate Begeman9008ca62009-04-27 18:41:29 +00006142 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6143 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6144 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006145 for (unsigned i = 0; i != 4; ++i) {
6146 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006147 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006148 } else {
6149 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006150 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006151 }
6152 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006153 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006154}
6155
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006156static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006157 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006158 V = V.getOperand(0);
6159 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6160 V = V.getOperand(0);
6161 if (MayFoldLoad(V))
6162 return true;
6163 return false;
6164}
6165
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006166// FIXME: the version above should always be used. Since there's
6167// a bug where several vector shuffles can't be folded because the
6168// DAG is not updated during lowering and a node claims to have two
6169// uses while it only has one, use this version, and let isel match
6170// another instruction if the load really happens to have more than
6171// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006172// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006173static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006174 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006175 V = V.getOperand(0);
6176 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6177 V = V.getOperand(0);
6178 if (ISD::isNormalLoad(V.getNode()))
6179 return true;
6180 return false;
6181}
6182
6183/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6184/// a vector extract, and if both can be later optimized into a single load.
6185/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6186/// here because otherwise a target specific shuffle node is going to be
6187/// emitted for this shuffle, and the optimization not done.
6188/// FIXME: This is probably not the best approach, but fix the problem
6189/// until the right path is decided.
6190static
6191bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6192 const TargetLowering &TLI) {
6193 EVT VT = V.getValueType();
6194 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6195
6196 // Be sure that the vector shuffle is present in a pattern like this:
6197 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6198 if (!V.hasOneUse())
6199 return false;
6200
6201 SDNode *N = *V.getNode()->use_begin();
6202 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6203 return false;
6204
6205 SDValue EltNo = N->getOperand(1);
6206 if (!isa<ConstantSDNode>(EltNo))
6207 return false;
6208
6209 // If the bit convert changed the number of elements, it is unsafe
6210 // to examine the mask.
6211 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006212 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006213 EVT SrcVT = V.getOperand(0).getValueType();
6214 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6215 return false;
6216 V = V.getOperand(0);
6217 HasShuffleIntoBitcast = true;
6218 }
6219
6220 // Select the input vector, guarding against out of range extract vector.
6221 unsigned NumElems = VT.getVectorNumElements();
6222 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6223 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6224 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6225
6226 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006227 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006228 V = V.getOperand(0);
6229
6230 if (ISD::isNormalLoad(V.getNode())) {
6231 // Is the original load suitable?
6232 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6233
6234 // FIXME: avoid the multi-use bug that is preventing lots of
6235 // of foldings to be detected, this is still wrong of course, but
6236 // give the temporary desired behavior, and if it happens that
6237 // the load has real more uses, during isel it will not fold, and
6238 // will generate poor code.
6239 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6240 return false;
6241
6242 if (!HasShuffleIntoBitcast)
6243 return true;
6244
6245 // If there's a bitcast before the shuffle, check if the load type and
6246 // alignment is valid.
6247 unsigned Align = LN0->getAlignment();
6248 unsigned NewAlign =
6249 TLI.getTargetData()->getABITypeAlignment(
6250 VT.getTypeForEVT(*DAG.getContext()));
6251
6252 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6253 return false;
6254 }
6255
6256 return true;
6257}
6258
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006259static
Evan Cheng835580f2010-10-07 20:50:20 +00006260SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6261 EVT VT = Op.getValueType();
6262
6263 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006264 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6265 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006266 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6267 V1, DAG));
6268}
6269
6270static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006271SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006272 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006273 SDValue V1 = Op.getOperand(0);
6274 SDValue V2 = Op.getOperand(1);
6275 EVT VT = Op.getValueType();
6276
6277 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6278
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006279 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006280 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6281
Evan Cheng0899f5c2011-08-31 02:05:24 +00006282 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6283 return DAG.getNode(ISD::BITCAST, dl, VT,
6284 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6285 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6286 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006287}
6288
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006289static
6290SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6291 SDValue V1 = Op.getOperand(0);
6292 SDValue V2 = Op.getOperand(1);
6293 EVT VT = Op.getValueType();
6294
6295 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6296 "unsupported shuffle type");
6297
6298 if (V2.getOpcode() == ISD::UNDEF)
6299 V2 = V1;
6300
6301 // v4i32 or v4f32
6302 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6303}
6304
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006305static inline unsigned getSHUFPOpcode(EVT VT) {
6306 switch(VT.getSimpleVT().SimpleTy) {
6307 case MVT::v8i32: // Use fp unit for int unpack.
6308 case MVT::v8f32:
6309 case MVT::v4i32: // Use fp unit for int unpack.
6310 case MVT::v4f32: return X86ISD::SHUFPS;
6311 case MVT::v4i64: // Use fp unit for int unpack.
6312 case MVT::v4f64:
6313 case MVT::v2i64: // Use fp unit for int unpack.
6314 case MVT::v2f64: return X86ISD::SHUFPD;
6315 default:
6316 llvm_unreachable("Unknown type for shufp*");
6317 }
6318 return 0;
6319}
6320
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006321static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006322SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006323 SDValue V1 = Op.getOperand(0);
6324 SDValue V2 = Op.getOperand(1);
6325 EVT VT = Op.getValueType();
6326 unsigned NumElems = VT.getVectorNumElements();
6327
6328 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6329 // operand of these instructions is only memory, so check if there's a
6330 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6331 // same masks.
6332 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006333
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006334 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006335 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006336 CanFoldLoad = true;
6337
6338 // When V1 is a load, it can be folded later into a store in isel, example:
6339 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6340 // turns into:
6341 // (MOVLPSmr addr:$src1, VR128:$src2)
6342 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006343 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006344 CanFoldLoad = true;
6345
Eric Christopher893a8822011-02-20 05:04:42 +00006346 // Both of them can't be memory operations though.
6347 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6348 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006349
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006350 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006351 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006352 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6353
6354 if (NumElems == 4)
6355 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6356 }
6357
6358 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6359 // movl and movlp will both match v2i64, but v2i64 is never matched by
6360 // movl earlier because we make it strict to avoid messing with the movlp load
6361 // folding logic (see the code above getMOVLP call). Match it here then,
6362 // this is horrible, but will stay like this until we move all shuffle
6363 // matching to x86 specific nodes. Note that for the 1st condition all
6364 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006365 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006366 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6367 // as to remove this logic from here, as much as possible
6368 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006369 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006370 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006371 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006372
6373 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6374
6375 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006376 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006377 X86::getShuffleSHUFImmediate(SVOp), DAG);
6378}
6379
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006380static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006381 switch(VT.getSimpleVT().SimpleTy) {
6382 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6383 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006384 case MVT::v4f32: return X86ISD::UNPCKLPS;
6385 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006386 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006387 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006388 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006389 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006390 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6391 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6392 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006393 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006394 }
6395 return 0;
6396}
6397
6398static inline unsigned getUNPCKHOpcode(EVT VT) {
6399 switch(VT.getSimpleVT().SimpleTy) {
6400 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6401 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6402 case MVT::v4f32: return X86ISD::UNPCKHPS;
6403 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006404 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006405 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006406 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006407 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006408 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6409 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6410 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006411 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006412 }
6413 return 0;
6414}
6415
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006416static inline unsigned getVPERMILOpcode(EVT VT) {
6417 switch(VT.getSimpleVT().SimpleTy) {
6418 case MVT::v4i32:
6419 case MVT::v4f32: return X86ISD::VPERMILPS;
6420 case MVT::v2i64:
6421 case MVT::v2f64: return X86ISD::VPERMILPD;
6422 case MVT::v8i32:
6423 case MVT::v8f32: return X86ISD::VPERMILPSY;
6424 case MVT::v4i64:
6425 case MVT::v4f64: return X86ISD::VPERMILPDY;
6426 default:
6427 llvm_unreachable("Unknown type for vpermil");
6428 }
6429 return 0;
6430}
6431
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006432/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6433/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6434/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6435static bool isVectorBroadcast(SDValue &Op) {
6436 EVT VT = Op.getValueType();
6437 bool Is256 = VT.getSizeInBits() == 256;
6438
6439 assert((VT.getSizeInBits() == 128 || Is256) &&
6440 "Unsupported type for vbroadcast node");
6441
6442 SDValue V = Op;
6443 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6444 V = V.getOperand(0);
6445
6446 if (Is256 && !(V.hasOneUse() &&
6447 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6448 V.getOperand(0).getOpcode() == ISD::UNDEF))
6449 return false;
6450
6451 if (Is256)
6452 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006453
6454 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006455 return false;
6456
6457 // Check the source scalar_to_vector type. 256-bit broadcasts are
6458 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6459 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006460 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6461 return false;
6462
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006463 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6464 if (ScalarSize != 32 && ScalarSize != 64)
6465 return false;
6466 if (!Is256 && ScalarSize == 64)
6467 return false;
6468
6469 V = V.getOperand(0);
6470 if (!MayFoldLoad(V))
6471 return false;
6472
6473 // Return the load node
6474 Op = V;
6475 return true;
6476}
6477
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006478static
6479SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006480 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006481 const X86Subtarget *Subtarget) {
6482 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6483 EVT VT = Op.getValueType();
6484 DebugLoc dl = Op.getDebugLoc();
6485 SDValue V1 = Op.getOperand(0);
6486 SDValue V2 = Op.getOperand(1);
6487
6488 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006489 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006490
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006491 // Handle splat operations
6492 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006493 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006494 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006495 // Special case, this is the only place now where it's allowed to return
6496 // a vector_shuffle operation without using a target specific node, because
6497 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6498 // this be moved to DAGCombine instead?
6499 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006500 return Op;
6501
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006502 // Use vbroadcast whenever the splat comes from a foldable load
6503 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6504 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6505
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006506 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006507 if ((Size == 128 && NumElem <= 4) ||
6508 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006509 return SDValue();
6510
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006511 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006512 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006513 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006514
6515 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6516 // do it!
6517 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6518 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6519 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006520 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006521 } else if ((VT == MVT::v4i32 ||
6522 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006523 // FIXME: Figure out a cleaner way to do this.
6524 // Try to make use of movq to zero out the top part.
6525 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6526 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6527 if (NewOp.getNode()) {
6528 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6529 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6530 DAG, Subtarget, dl);
6531 }
6532 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6533 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6534 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6535 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6536 DAG, Subtarget, dl);
6537 }
6538 }
6539 return SDValue();
6540}
6541
Dan Gohman475871a2008-07-27 21:46:04 +00006542SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006543X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006544 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006545 SDValue V1 = Op.getOperand(0);
6546 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006547 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006548 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006549 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006550 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006551 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6552 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006553 bool V1IsSplat = false;
6554 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006555 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006556 MachineFunction &MF = DAG.getMachineFunction();
6557 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006558
Dale Johannesen0488fb62010-09-30 23:57:10 +00006559 // Shuffle operations on MMX not supported.
6560 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006561 return Op;
6562
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006563 // Vector shuffle lowering takes 3 steps:
6564 //
6565 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6566 // narrowing and commutation of operands should be handled.
6567 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6568 // shuffle nodes.
6569 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6570 // so the shuffle can be broken into other shuffles and the legalizer can
6571 // try the lowering again.
6572 //
6573 // The general ideia is that no vector_shuffle operation should be left to
6574 // be matched during isel, all of them must be converted to a target specific
6575 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006576
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006577 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6578 // narrowing and commutation of operands should be handled. The actual code
6579 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006580 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006581 if (NewOp.getNode())
6582 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006583
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006584 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6585 // unpckh_undef). Only use pshufd if speed is more important than size.
6586 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006587 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006588 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006589 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006590
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006591 if (X86::isMOVDDUPMask(SVOp) &&
6592 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6593 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006594 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006595
Dale Johannesen0488fb62010-09-30 23:57:10 +00006596 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006597 return getMOVHighToLow(Op, dl, DAG);
6598
6599 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006600 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006601 (VT == MVT::v2f64 || VT == MVT::v2i64))
6602 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6603
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006604 if (X86::isPSHUFDMask(SVOp)) {
6605 // The actual implementation will match the mask in the if above and then
6606 // during isel it can match several different instructions, not only pshufd
6607 // as its name says, sad but true, emulate the behavior for now...
6608 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6609 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6610
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006611 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6612
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006613 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006614 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6615
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006616 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6617 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006618 }
Eric Christopherfd179292009-08-27 18:07:15 +00006619
Evan Chengf26ffe92008-05-29 08:22:04 +00006620 // Check if this can be converted into a logical shift.
6621 bool isLeft = false;
6622 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006623 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006624 bool isShift = getSubtarget()->hasXMMInt() &&
6625 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006626 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006627 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006628 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006629 EVT EltVT = VT.getVectorElementType();
6630 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006631 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006632 }
Eric Christopherfd179292009-08-27 18:07:15 +00006633
Nate Begeman9008ca62009-04-27 18:41:29 +00006634 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006635 if (V1IsUndef)
6636 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006637 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006638 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006639 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006640 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006641 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6642
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006643 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006644 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6645 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006646 }
Eric Christopherfd179292009-08-27 18:07:15 +00006647
Nate Begeman9008ca62009-04-27 18:41:29 +00006648 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006649 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006650 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006651
Dale Johannesen0488fb62010-09-30 23:57:10 +00006652 if (X86::isMOVHLPSMask(SVOp))
6653 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006654
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006655 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006656 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006657
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006658 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006659 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006660
Dale Johannesen0488fb62010-09-30 23:57:10 +00006661 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006662 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006663
Nate Begeman9008ca62009-04-27 18:41:29 +00006664 if (ShouldXformToMOVHLPS(SVOp) ||
6665 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6666 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006667
Evan Chengf26ffe92008-05-29 08:22:04 +00006668 if (isShift) {
6669 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006670 EVT EltVT = VT.getVectorElementType();
6671 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006672 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006673 }
Eric Christopherfd179292009-08-27 18:07:15 +00006674
Evan Cheng9eca5e82006-10-25 21:49:50 +00006675 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006676 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6677 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006678 V1IsSplat = isSplatVector(V1.getNode());
6679 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006680
Chris Lattner8a594482007-11-25 00:24:49 +00006681 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006682 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006683 Op = CommuteVectorShuffle(SVOp, DAG);
6684 SVOp = cast<ShuffleVectorSDNode>(Op);
6685 V1 = SVOp->getOperand(0);
6686 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006687 std::swap(V1IsSplat, V2IsSplat);
6688 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006689 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006690 }
6691
Nate Begeman9008ca62009-04-27 18:41:29 +00006692 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6693 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006694 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006695 return V1;
6696 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6697 // the instruction selector will not match, so get a canonical MOVL with
6698 // swapped operands to undo the commute.
6699 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006700 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006701
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006702 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006703 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006704
6705 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006706 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006707
Evan Cheng9bbbb982006-10-25 20:48:19 +00006708 if (V2IsSplat) {
6709 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006710 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006711 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006712 SDValue NewMask = NormalizeMask(SVOp, DAG);
6713 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6714 if (NSVOp != SVOp) {
6715 if (X86::isUNPCKLMask(NSVOp, true)) {
6716 return NewMask;
6717 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6718 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006719 }
6720 }
6721 }
6722
Evan Cheng9eca5e82006-10-25 21:49:50 +00006723 if (Commuted) {
6724 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006725 // FIXME: this seems wrong.
6726 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6727 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006728
6729 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006730 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006731
6732 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006733 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006734 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006735
Nate Begeman9008ca62009-04-27 18:41:29 +00006736 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006737 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006738 return CommuteVectorShuffle(SVOp, DAG);
6739
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006740 // The checks below are all present in isShuffleMaskLegal, but they are
6741 // inlined here right now to enable us to directly emit target specific
6742 // nodes, and remove one by one until they don't return Op anymore.
6743 SmallVector<int, 16> M;
6744 SVOp->getMask(M);
6745
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006746 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006747 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6748 X86::getShufflePALIGNRImmediate(SVOp),
6749 DAG);
6750
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006751 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6752 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006753 if (VT == MVT::v2f64)
6754 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006755 if (VT == MVT::v2i64)
6756 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6757 }
6758
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006759 if (isPSHUFHWMask(M, VT))
6760 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6761 X86::getShufflePSHUFHWImmediate(SVOp),
6762 DAG);
6763
6764 if (isPSHUFLWMask(M, VT))
6765 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6766 X86::getShufflePSHUFLWImmediate(SVOp),
6767 DAG);
6768
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006769 if (isSHUFPMask(M, VT))
6770 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6771 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006772
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006773 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006774 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006775 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006776 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006777
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006778 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006779 // Generate target specific nodes for 128 or 256-bit shuffles only
6780 // supported in the AVX instruction set.
6781 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006782
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006783 // Handle VMOVDDUPY permutations
6784 if (isMOVDDUPYMask(SVOp, Subtarget))
6785 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6786
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006787 // Handle VPERMILPS* permutations
6788 if (isVPERMILPSMask(M, VT, Subtarget))
6789 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6790 getShuffleVPERMILPSImmediate(SVOp), DAG);
6791
6792 // Handle VPERMILPD* permutations
6793 if (isVPERMILPDMask(M, VT, Subtarget))
6794 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6795 getShuffleVPERMILPDImmediate(SVOp), DAG);
6796
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006797 // Handle VPERM2F128 permutations
6798 if (isVPERM2F128Mask(M, VT, Subtarget))
6799 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6800 getShuffleVPERM2F128Immediate(SVOp), DAG);
6801
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006802 // Handle VSHUFPSY permutations
6803 if (isVSHUFPSYMask(M, VT, Subtarget))
6804 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6805 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6806
6807 // Handle VSHUFPDY permutations
6808 if (isVSHUFPDYMask(M, VT, Subtarget))
6809 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6810 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6811
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006812 //===--------------------------------------------------------------------===//
6813 // Since no target specific shuffle was selected for this generic one,
6814 // lower it into other known shuffles. FIXME: this isn't true yet, but
6815 // this is the plan.
6816 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006817
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006818 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6819 if (VT == MVT::v8i16) {
6820 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6821 if (NewOp.getNode())
6822 return NewOp;
6823 }
6824
6825 if (VT == MVT::v16i8) {
6826 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6827 if (NewOp.getNode())
6828 return NewOp;
6829 }
6830
6831 // Handle all 128-bit wide vectors with 4 elements, and match them with
6832 // several different shuffle types.
6833 if (NumElems == 4 && VT.getSizeInBits() == 128)
6834 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6835
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006836 // Handle general 256-bit shuffles
6837 if (VT.is256BitVector())
6838 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6839
Dan Gohman475871a2008-07-27 21:46:04 +00006840 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006841}
6842
Dan Gohman475871a2008-07-27 21:46:04 +00006843SDValue
6844X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006845 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006846 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006847 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006848
6849 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6850 return SDValue();
6851
Duncan Sands83ec4b62008-06-06 12:08:01 +00006852 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006853 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006854 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006856 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006857 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006858 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006859 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6860 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6861 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6863 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006864 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006865 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006866 Op.getOperand(0)),
6867 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006868 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006869 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006870 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006871 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006872 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006873 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006874 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6875 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006876 // result has a single use which is a store or a bitcast to i32. And in
6877 // the case of a store, it's not worth it if the index is a constant 0,
6878 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006879 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006880 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006881 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006882 if ((User->getOpcode() != ISD::STORE ||
6883 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6884 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006885 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006886 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006887 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006888 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006889 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006890 Op.getOperand(0)),
6891 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006892 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006893 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006894 // ExtractPS works with constant index.
6895 if (isa<ConstantSDNode>(Op.getOperand(1)))
6896 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006897 }
Dan Gohman475871a2008-07-27 21:46:04 +00006898 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006899}
6900
6901
Dan Gohman475871a2008-07-27 21:46:04 +00006902SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006903X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6904 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006905 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006906 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006907
David Greene74a579d2011-02-10 16:57:36 +00006908 SDValue Vec = Op.getOperand(0);
6909 EVT VecVT = Vec.getValueType();
6910
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006911 // If this is a 256-bit vector result, first extract the 128-bit vector and
6912 // then extract the element from the 128-bit vector.
6913 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006914 DebugLoc dl = Op.getNode()->getDebugLoc();
6915 unsigned NumElems = VecVT.getVectorNumElements();
6916 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006917 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6918
6919 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006920 bool Upper = IdxVal >= NumElems/2;
6921 Vec = Extract128BitVector(Vec,
6922 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006923
David Greene74a579d2011-02-10 16:57:36 +00006924 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006925 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006926 }
6927
6928 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6929
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006930 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006931 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006932 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006933 return Res;
6934 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006935
Owen Andersone50ed302009-08-10 22:56:29 +00006936 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006937 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006938 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006939 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006940 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006941 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006942 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006943 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6944 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006945 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006946 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006947 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006948 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006949 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006950 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006951 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006952 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006953 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006954 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006955 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006956 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006957 if (Idx == 0)
6958 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006959
Evan Cheng0db9fe62006-04-25 20:13:52 +00006960 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006961 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006962 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006963 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006964 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006965 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006966 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006967 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006968 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6969 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6970 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006971 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006972 if (Idx == 0)
6973 return Op;
6974
6975 // UNPCKHPD the element to the lowest double word, then movsd.
6976 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6977 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006978 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006979 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006980 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006981 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006982 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006983 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006984 }
6985
Dan Gohman475871a2008-07-27 21:46:04 +00006986 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006987}
6988
Dan Gohman475871a2008-07-27 21:46:04 +00006989SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006990X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6991 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006992 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006993 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006994 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006995
Dan Gohman475871a2008-07-27 21:46:04 +00006996 SDValue N0 = Op.getOperand(0);
6997 SDValue N1 = Op.getOperand(1);
6998 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006999
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007000 if (VT.getSizeInBits() == 256)
7001 return SDValue();
7002
Dan Gohman8a55ce42009-09-23 21:02:20 +00007003 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007004 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007005 unsigned Opc;
7006 if (VT == MVT::v8i16)
7007 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007008 else if (VT == MVT::v16i8)
7009 Opc = X86ISD::PINSRB;
7010 else
7011 Opc = X86ISD::PINSRB;
7012
Nate Begeman14d12ca2008-02-11 04:19:36 +00007013 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7014 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007015 if (N1.getValueType() != MVT::i32)
7016 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7017 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007018 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007019 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007020 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007021 // Bits [7:6] of the constant are the source select. This will always be
7022 // zero here. The DAG Combiner may combine an extract_elt index into these
7023 // bits. For example (insert (extract, 3), 2) could be matched by putting
7024 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007025 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007026 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007027 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007028 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007029 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007030 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007031 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007032 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007033 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007034 // PINSR* works with constant index.
7035 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007036 }
Dan Gohman475871a2008-07-27 21:46:04 +00007037 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007038}
7039
Dan Gohman475871a2008-07-27 21:46:04 +00007040SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007041X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007042 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007043 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007044
David Greene6b381262011-02-09 15:32:06 +00007045 DebugLoc dl = Op.getDebugLoc();
7046 SDValue N0 = Op.getOperand(0);
7047 SDValue N1 = Op.getOperand(1);
7048 SDValue N2 = Op.getOperand(2);
7049
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007050 // If this is a 256-bit vector result, first extract the 128-bit vector,
7051 // insert the element into the extracted half and then place it back.
7052 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007053 if (!isa<ConstantSDNode>(N2))
7054 return SDValue();
7055
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007056 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007057 unsigned NumElems = VT.getVectorNumElements();
7058 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007059 bool Upper = IdxVal >= NumElems/2;
7060 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7061 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007062
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007063 // Insert the element into the desired half.
7064 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7065 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007066
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007067 // Insert the changed part back to the 256-bit vector
7068 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007069 }
7070
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007071 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007072 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7073
Dan Gohman8a55ce42009-09-23 21:02:20 +00007074 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007075 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007076
Dan Gohman8a55ce42009-09-23 21:02:20 +00007077 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007078 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7079 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 if (N1.getValueType() != MVT::i32)
7081 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7082 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007083 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007084 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007085 }
Dan Gohman475871a2008-07-27 21:46:04 +00007086 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007087}
7088
Dan Gohman475871a2008-07-27 21:46:04 +00007089SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007090X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007091 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007092 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007093 EVT OpVT = Op.getValueType();
7094
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007095 // If this is a 256-bit vector result, first insert into a 128-bit
7096 // vector and then insert into the 256-bit vector.
7097 if (OpVT.getSizeInBits() > 128) {
7098 // Insert into a 128-bit vector.
7099 EVT VT128 = EVT::getVectorVT(*Context,
7100 OpVT.getVectorElementType(),
7101 OpVT.getVectorNumElements() / 2);
7102
7103 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7104
7105 // Insert the 128-bit vector.
7106 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7107 DAG.getConstant(0, MVT::i32),
7108 DAG, dl);
7109 }
7110
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007111 if (Op.getValueType() == MVT::v1i64 &&
7112 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007114
Owen Anderson825b72b2009-08-11 20:47:22 +00007115 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007116 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7117 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007118 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007119 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007120}
7121
David Greene91585092011-01-26 15:38:49 +00007122// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7123// a simple subregister reference or explicit instructions to grab
7124// upper bits of a vector.
7125SDValue
7126X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7127 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007128 DebugLoc dl = Op.getNode()->getDebugLoc();
7129 SDValue Vec = Op.getNode()->getOperand(0);
7130 SDValue Idx = Op.getNode()->getOperand(1);
7131
7132 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7133 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7134 return Extract128BitVector(Vec, Idx, DAG, dl);
7135 }
David Greene91585092011-01-26 15:38:49 +00007136 }
7137 return SDValue();
7138}
7139
David Greenecfe33c42011-01-26 19:13:22 +00007140// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7141// simple superregister reference or explicit instructions to insert
7142// the upper bits of a vector.
7143SDValue
7144X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7145 if (Subtarget->hasAVX()) {
7146 DebugLoc dl = Op.getNode()->getDebugLoc();
7147 SDValue Vec = Op.getNode()->getOperand(0);
7148 SDValue SubVec = Op.getNode()->getOperand(1);
7149 SDValue Idx = Op.getNode()->getOperand(2);
7150
7151 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7152 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007153 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007154 }
7155 }
7156 return SDValue();
7157}
7158
Bill Wendling056292f2008-09-16 21:48:12 +00007159// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7160// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7161// one of the above mentioned nodes. It has to be wrapped because otherwise
7162// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7163// be used to form addressing mode. These wrapped nodes will be selected
7164// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007165SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007166X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007167 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007168
Chris Lattner41621a22009-06-26 19:22:52 +00007169 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7170 // global base reg.
7171 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007172 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007173 CodeModel::Model M = getTargetMachine().getCodeModel();
7174
Chris Lattner4f066492009-07-11 20:29:19 +00007175 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007176 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007177 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007178 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007179 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007180 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007181 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007182
Evan Cheng1606e8e2009-03-13 07:51:59 +00007183 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007184 CP->getAlignment(),
7185 CP->getOffset(), OpFlag);
7186 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007187 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007188 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007189 if (OpFlag) {
7190 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007191 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007192 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007193 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007194 }
7195
7196 return Result;
7197}
7198
Dan Gohmand858e902010-04-17 15:26:15 +00007199SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007200 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007201
Chris Lattner18c59872009-06-27 04:16:01 +00007202 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7203 // global base reg.
7204 unsigned char OpFlag = 0;
7205 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007206 CodeModel::Model M = getTargetMachine().getCodeModel();
7207
Chris Lattner4f066492009-07-11 20:29:19 +00007208 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007209 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007210 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007211 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007212 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007213 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007214 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007215
Chris Lattner18c59872009-06-27 04:16:01 +00007216 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7217 OpFlag);
7218 DebugLoc DL = JT->getDebugLoc();
7219 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007220
Chris Lattner18c59872009-06-27 04:16:01 +00007221 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007222 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007223 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7224 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007225 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007226 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007227
Chris Lattner18c59872009-06-27 04:16:01 +00007228 return Result;
7229}
7230
7231SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007232X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007233 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007234
Chris Lattner18c59872009-06-27 04:16:01 +00007235 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7236 // global base reg.
7237 unsigned char OpFlag = 0;
7238 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007239 CodeModel::Model M = getTargetMachine().getCodeModel();
7240
Chris Lattner4f066492009-07-11 20:29:19 +00007241 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007242 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7243 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7244 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007245 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007246 } else if (Subtarget->isPICStyleGOT()) {
7247 OpFlag = X86II::MO_GOT;
7248 } else if (Subtarget->isPICStyleStubPIC()) {
7249 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7250 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7251 OpFlag = X86II::MO_DARWIN_NONLAZY;
7252 }
Eric Christopherfd179292009-08-27 18:07:15 +00007253
Chris Lattner18c59872009-06-27 04:16:01 +00007254 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007255
Chris Lattner18c59872009-06-27 04:16:01 +00007256 DebugLoc DL = Op.getDebugLoc();
7257 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007258
7259
Chris Lattner18c59872009-06-27 04:16:01 +00007260 // With PIC, the address is actually $g + Offset.
7261 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007262 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007263 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7264 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007265 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007266 Result);
7267 }
Eric Christopherfd179292009-08-27 18:07:15 +00007268
Eli Friedman586272d2011-08-11 01:48:05 +00007269 // For symbols that require a load from a stub to get the address, emit the
7270 // load.
7271 if (isGlobalStubReference(OpFlag))
7272 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7273 MachinePointerInfo::getGOT(), false, false, 0);
7274
Chris Lattner18c59872009-06-27 04:16:01 +00007275 return Result;
7276}
7277
Dan Gohman475871a2008-07-27 21:46:04 +00007278SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007279X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007280 // Create the TargetBlockAddressAddress node.
7281 unsigned char OpFlags =
7282 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007283 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007284 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007285 DebugLoc dl = Op.getDebugLoc();
7286 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7287 /*isTarget=*/true, OpFlags);
7288
Dan Gohmanf705adb2009-10-30 01:28:02 +00007289 if (Subtarget->isPICStyleRIPRel() &&
7290 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007291 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7292 else
7293 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007294
Dan Gohman29cbade2009-11-20 23:18:13 +00007295 // With PIC, the address is actually $g + Offset.
7296 if (isGlobalRelativeToPICBase(OpFlags)) {
7297 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7298 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7299 Result);
7300 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007301
7302 return Result;
7303}
7304
7305SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007306X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007307 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007308 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007309 // Create the TargetGlobalAddress node, folding in the constant
7310 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007311 unsigned char OpFlags =
7312 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007313 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007314 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007315 if (OpFlags == X86II::MO_NO_FLAG &&
7316 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007317 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007318 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007319 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007320 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007321 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007322 }
Eric Christopherfd179292009-08-27 18:07:15 +00007323
Chris Lattner4f066492009-07-11 20:29:19 +00007324 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007325 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007326 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7327 else
7328 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007329
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007330 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007331 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007332 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7333 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007334 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007335 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007336
Chris Lattner36c25012009-07-10 07:34:39 +00007337 // For globals that require a load from a stub to get the address, emit the
7338 // load.
7339 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007340 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007341 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007342
Dan Gohman6520e202008-10-18 02:06:02 +00007343 // If there was a non-zero offset that we didn't fold, create an explicit
7344 // addition for it.
7345 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007346 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007347 DAG.getConstant(Offset, getPointerTy()));
7348
Evan Cheng0db9fe62006-04-25 20:13:52 +00007349 return Result;
7350}
7351
Evan Chengda43bcf2008-09-24 00:05:32 +00007352SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007353X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007354 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007355 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007356 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007357}
7358
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007359static SDValue
7360GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007361 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007362 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007363 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007364 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007365 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007366 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007367 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007368 GA->getOffset(),
7369 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007370 if (InFlag) {
7371 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007372 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007373 } else {
7374 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007375 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007376 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007377
7378 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007379 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007380
Rafael Espindola15f1b662009-04-24 12:59:40 +00007381 SDValue Flag = Chain.getValue(1);
7382 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007383}
7384
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007385// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007386static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007387LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007388 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007389 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007390 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7391 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007392 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007393 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007394 InFlag = Chain.getValue(1);
7395
Chris Lattnerb903bed2009-06-26 21:20:29 +00007396 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007397}
7398
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007399// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007400static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007401LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007402 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007403 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7404 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007405}
7406
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007407// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7408// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007409static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007410 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007411 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007412 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007413
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007414 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7415 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7416 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007417
Michael J. Spencerec38de22010-10-10 22:04:20 +00007418 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007419 DAG.getIntPtrConstant(0),
7420 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007421
Chris Lattnerb903bed2009-06-26 21:20:29 +00007422 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007423 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7424 // initialexec.
7425 unsigned WrapperKind = X86ISD::Wrapper;
7426 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007427 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007428 } else if (is64Bit) {
7429 assert(model == TLSModel::InitialExec);
7430 OperandFlags = X86II::MO_GOTTPOFF;
7431 WrapperKind = X86ISD::WrapperRIP;
7432 } else {
7433 assert(model == TLSModel::InitialExec);
7434 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007435 }
Eric Christopherfd179292009-08-27 18:07:15 +00007436
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007437 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7438 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007439 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007440 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007441 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007442 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007443
Rafael Espindola9a580232009-02-27 13:37:18 +00007444 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007445 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007446 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007447
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007448 // The address of the thread local variable is the add of the thread
7449 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007450 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007451}
7452
Dan Gohman475871a2008-07-27 21:46:04 +00007453SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007454X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007455
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007456 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007457 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007458
Eric Christopher30ef0e52010-06-03 04:07:48 +00007459 if (Subtarget->isTargetELF()) {
7460 // TODO: implement the "local dynamic" model
7461 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007462
Eric Christopher30ef0e52010-06-03 04:07:48 +00007463 // If GV is an alias then use the aliasee for determining
7464 // thread-localness.
7465 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7466 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007467
7468 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007469 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007470
Eric Christopher30ef0e52010-06-03 04:07:48 +00007471 switch (model) {
7472 case TLSModel::GeneralDynamic:
7473 case TLSModel::LocalDynamic: // not implemented
7474 if (Subtarget->is64Bit())
7475 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7476 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007477
Eric Christopher30ef0e52010-06-03 04:07:48 +00007478 case TLSModel::InitialExec:
7479 case TLSModel::LocalExec:
7480 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7481 Subtarget->is64Bit());
7482 }
7483 } else if (Subtarget->isTargetDarwin()) {
7484 // Darwin only has one model of TLS. Lower to that.
7485 unsigned char OpFlag = 0;
7486 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7487 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007488
Eric Christopher30ef0e52010-06-03 04:07:48 +00007489 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7490 // global base reg.
7491 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7492 !Subtarget->is64Bit();
7493 if (PIC32)
7494 OpFlag = X86II::MO_TLVP_PIC_BASE;
7495 else
7496 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007497 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007498 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007499 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007500 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007501 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007502
Eric Christopher30ef0e52010-06-03 04:07:48 +00007503 // With PIC32, the address is actually $g + Offset.
7504 if (PIC32)
7505 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7506 DAG.getNode(X86ISD::GlobalBaseReg,
7507 DebugLoc(), getPointerTy()),
7508 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007509
Eric Christopher30ef0e52010-06-03 04:07:48 +00007510 // Lowering the machine isd will make sure everything is in the right
7511 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007512 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007513 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007514 SDValue Args[] = { Chain, Offset };
7515 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007516
Eric Christopher30ef0e52010-06-03 04:07:48 +00007517 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7518 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7519 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007520
Eric Christopher30ef0e52010-06-03 04:07:48 +00007521 // And our return value (tls address) is in the standard call return value
7522 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007523 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7524 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007525 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007526
Eric Christopher30ef0e52010-06-03 04:07:48 +00007527 assert(false &&
7528 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007529
Torok Edwinc23197a2009-07-14 16:55:14 +00007530 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007531 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007532}
7533
Evan Cheng0db9fe62006-04-25 20:13:52 +00007534
Nadav Rotem43012222011-05-11 08:12:09 +00007535/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007536/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007537SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007538 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007539 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007540 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007541 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007542 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007543 SDValue ShOpLo = Op.getOperand(0);
7544 SDValue ShOpHi = Op.getOperand(1);
7545 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007546 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007547 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007548 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007549
Dan Gohman475871a2008-07-27 21:46:04 +00007550 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007551 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007552 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7553 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007554 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007555 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7556 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007557 }
Evan Chenge3413162006-01-09 18:33:28 +00007558
Owen Anderson825b72b2009-08-11 20:47:22 +00007559 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7560 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007561 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007562 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007563
Dan Gohman475871a2008-07-27 21:46:04 +00007564 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007565 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007566 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7567 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007568
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007569 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007570 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7571 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007572 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007573 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7574 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007575 }
7576
Dan Gohman475871a2008-07-27 21:46:04 +00007577 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007578 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007579}
Evan Chenga3195e82006-01-12 22:54:21 +00007580
Dan Gohmand858e902010-04-17 15:26:15 +00007581SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7582 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007583 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007584
Dale Johannesen0488fb62010-09-30 23:57:10 +00007585 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007586 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007587
Owen Anderson825b72b2009-08-11 20:47:22 +00007588 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007589 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007590
Eli Friedman36df4992009-05-27 00:47:34 +00007591 // These are really Legal; return the operand so the caller accepts it as
7592 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007593 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007594 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007595 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007596 Subtarget->is64Bit()) {
7597 return Op;
7598 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007599
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007600 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007601 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007602 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007603 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007604 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007605 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007606 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007607 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007608 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007609 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7610}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007611
Owen Andersone50ed302009-08-10 22:56:29 +00007612SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007613 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007614 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007615 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007616 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007617 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007618 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007619 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007620 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007621 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007622 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007623
Chris Lattner492a43e2010-09-22 01:28:21 +00007624 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007625
Stuart Hastings84be9582011-06-02 15:57:11 +00007626 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7627 MachineMemOperand *MMO;
7628 if (FI) {
7629 int SSFI = FI->getIndex();
7630 MMO =
7631 DAG.getMachineFunction()
7632 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7633 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7634 } else {
7635 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7636 StackSlot = StackSlot.getOperand(1);
7637 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007638 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007639 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7640 X86ISD::FILD, DL,
7641 Tys, Ops, array_lengthof(Ops),
7642 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007643
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007644 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007645 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007646 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007647
7648 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7649 // shouldn't be necessary except that RFP cannot be live across
7650 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007651 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007652 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7653 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007654 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007655 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007656 SDValue Ops[] = {
7657 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7658 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007659 MachineMemOperand *MMO =
7660 DAG.getMachineFunction()
7661 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007662 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007663
Chris Lattner492a43e2010-09-22 01:28:21 +00007664 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7665 Ops, array_lengthof(Ops),
7666 Op.getValueType(), MMO);
7667 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007668 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007669 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007670 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007671
Evan Cheng0db9fe62006-04-25 20:13:52 +00007672 return Result;
7673}
7674
Bill Wendling8b8a6362009-01-17 03:56:04 +00007675// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007676SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7677 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007678 // This algorithm is not obvious. Here it is in C code, more or less:
7679 /*
7680 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7681 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7682 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007683
Bill Wendling8b8a6362009-01-17 03:56:04 +00007684 // Copy ints to xmm registers.
7685 __m128i xh = _mm_cvtsi32_si128( hi );
7686 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007687
Bill Wendling8b8a6362009-01-17 03:56:04 +00007688 // Combine into low half of a single xmm register.
7689 __m128i x = _mm_unpacklo_epi32( xh, xl );
7690 __m128d d;
7691 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007692
Bill Wendling8b8a6362009-01-17 03:56:04 +00007693 // Merge in appropriate exponents to give the integer bits the right
7694 // magnitude.
7695 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007696
Bill Wendling8b8a6362009-01-17 03:56:04 +00007697 // Subtract away the biases to deal with the IEEE-754 double precision
7698 // implicit 1.
7699 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007700
Bill Wendling8b8a6362009-01-17 03:56:04 +00007701 // All conversions up to here are exact. The correctly rounded result is
7702 // calculated using the current rounding mode using the following
7703 // horizontal add.
7704 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7705 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7706 // store doesn't really need to be here (except
7707 // maybe to zero the other double)
7708 return sd;
7709 }
7710 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007711
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007712 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007713 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007714
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007715 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007716 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007717 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7718 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7719 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7720 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007721 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007722 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007723
Bill Wendling8b8a6362009-01-17 03:56:04 +00007724 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007725 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007726 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007727 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007728 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007729 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007730 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007731
Owen Anderson825b72b2009-08-11 20:47:22 +00007732 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7733 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007734 Op.getOperand(0),
7735 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007736 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7737 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007738 Op.getOperand(0),
7739 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7741 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007742 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007743 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007744 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007745 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007746 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007747 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007748 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007749 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007750
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007751 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007752 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007753 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7754 DAG.getUNDEF(MVT::v2f64), ShufMask);
7755 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7756 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007757 DAG.getIntPtrConstant(0));
7758}
7759
Bill Wendling8b8a6362009-01-17 03:56:04 +00007760// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007761SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7762 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007763 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007764 // FP constant to bias correct the final result.
7765 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007766 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007767
7768 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007769 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007770 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007771
Eli Friedmanf3704762011-08-29 21:15:46 +00007772 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007773 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7774 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007775
Owen Anderson825b72b2009-08-11 20:47:22 +00007776 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007777 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007778 DAG.getIntPtrConstant(0));
7779
7780 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007781 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007782 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007783 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007784 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007785 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007786 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007787 MVT::v2f64, Bias)));
7788 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007789 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007790 DAG.getIntPtrConstant(0));
7791
7792 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007793 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007794
7795 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007796 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007797
Owen Anderson825b72b2009-08-11 20:47:22 +00007798 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007799 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007800 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007801 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007802 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007803 }
7804
7805 // Handle final rounding.
7806 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007807}
7808
Dan Gohmand858e902010-04-17 15:26:15 +00007809SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7810 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007811 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007812 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007813
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007814 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007815 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7816 // the optimization here.
7817 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007818 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007819
Owen Andersone50ed302009-08-10 22:56:29 +00007820 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007821 EVT DstVT = Op.getValueType();
7822 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007823 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007824 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007825 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007826
7827 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007828 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007829 if (SrcVT == MVT::i32) {
7830 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7831 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7832 getPointerTy(), StackSlot, WordOff);
7833 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007834 StackSlot, MachinePointerInfo(),
7835 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007836 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007837 OffsetSlot, MachinePointerInfo(),
7838 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007839 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7840 return Fild;
7841 }
7842
7843 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7844 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007845 StackSlot, MachinePointerInfo(),
7846 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007847 // For i64 source, we need to add the appropriate power of 2 if the input
7848 // was negative. This is the same as the optimization in
7849 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7850 // we must be careful to do the computation in x87 extended precision, not
7851 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007852 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7853 MachineMemOperand *MMO =
7854 DAG.getMachineFunction()
7855 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7856 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007857
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007858 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7859 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007860 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7861 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007862
7863 APInt FF(32, 0x5F800000ULL);
7864
7865 // Check whether the sign bit is set.
7866 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7867 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7868 ISD::SETLT);
7869
7870 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7871 SDValue FudgePtr = DAG.getConstantPool(
7872 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7873 getPointerTy());
7874
7875 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7876 SDValue Zero = DAG.getIntPtrConstant(0);
7877 SDValue Four = DAG.getIntPtrConstant(4);
7878 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7879 Zero, Four);
7880 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7881
7882 // Load the value out, extending it from f32 to f80.
7883 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007884 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007885 FudgePtr, MachinePointerInfo::getConstantPool(),
7886 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007887 // Extend everything to 80 bits to force it to be done on x87.
7888 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7889 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007890}
7891
Dan Gohman475871a2008-07-27 21:46:04 +00007892std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007893FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007894 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007895
Owen Andersone50ed302009-08-10 22:56:29 +00007896 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007897
7898 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007899 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7900 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007901 }
7902
Owen Anderson825b72b2009-08-11 20:47:22 +00007903 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7904 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007905 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007906
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007907 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007909 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007910 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007911 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007912 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007913 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007914 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007915
Evan Cheng87c89352007-10-15 20:11:21 +00007916 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7917 // stack slot.
7918 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007919 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007920 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007921 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007922
Michael J. Spencerec38de22010-10-10 22:04:20 +00007923
7924
Evan Cheng0db9fe62006-04-25 20:13:52 +00007925 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007926 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007927 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007928 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7929 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7930 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007931 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007932
Dan Gohman475871a2008-07-27 21:46:04 +00007933 SDValue Chain = DAG.getEntryNode();
7934 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007935 EVT TheVT = Op.getOperand(0).getValueType();
7936 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007937 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007938 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007939 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007940 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007941 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007942 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007943 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007944 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007945
Chris Lattner492a43e2010-09-22 01:28:21 +00007946 MachineMemOperand *MMO =
7947 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7948 MachineMemOperand::MOLoad, MemSize, MemSize);
7949 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7950 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007951 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007952 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007953 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7954 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007955
Chris Lattner07290932010-09-22 01:05:16 +00007956 MachineMemOperand *MMO =
7957 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7958 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007959
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007961 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007962 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7963 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007964
Chris Lattner27a6c732007-11-24 07:07:01 +00007965 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007966}
7967
Dan Gohmand858e902010-04-17 15:26:15 +00007968SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7969 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007970 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007971 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007972
Eli Friedman948e95a2009-05-23 09:59:16 +00007973 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007974 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007975 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7976 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007977
Chris Lattner27a6c732007-11-24 07:07:01 +00007978 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007979 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007980 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007981}
7982
Dan Gohmand858e902010-04-17 15:26:15 +00007983SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7984 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007985 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7986 SDValue FIST = Vals.first, StackSlot = Vals.second;
7987 assert(FIST.getNode() && "Unexpected failure");
7988
7989 // Load the result.
7990 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007991 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007992}
7993
Dan Gohmand858e902010-04-17 15:26:15 +00007994SDValue X86TargetLowering::LowerFABS(SDValue Op,
7995 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007996 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007997 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007998 EVT VT = Op.getValueType();
7999 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008000 if (VT.isVector())
8001 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008002 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008003 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008004 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008005 CV.push_back(C);
8006 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008007 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008008 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008009 CV.push_back(C);
8010 CV.push_back(C);
8011 CV.push_back(C);
8012 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008013 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008014 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008015 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008016 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008017 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008018 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008019 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008020}
8021
Dan Gohmand858e902010-04-17 15:26:15 +00008022SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008023 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008024 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008025 EVT VT = Op.getValueType();
8026 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008027 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008028 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008029 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008030 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008031 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008032 CV.push_back(C);
8033 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008034 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008035 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008036 CV.push_back(C);
8037 CV.push_back(C);
8038 CV.push_back(C);
8039 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008040 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008041 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008042 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008043 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008044 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008045 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008046 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008047 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008048 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008049 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008050 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008051 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008052 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008053 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008054 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008055}
8056
Dan Gohmand858e902010-04-17 15:26:15 +00008057SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008058 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008059 SDValue Op0 = Op.getOperand(0);
8060 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008061 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008062 EVT VT = Op.getValueType();
8063 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008064
8065 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008066 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008067 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008068 SrcVT = VT;
8069 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008070 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008071 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008072 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008073 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008074 }
8075
8076 // At this point the operands and the result should have the same
8077 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008078
Evan Cheng68c47cb2007-01-05 07:55:56 +00008079 // First get the sign bit of second operand.
8080 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008081 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008082 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8083 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008084 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008085 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8086 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8087 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8088 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008089 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008090 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008091 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008092 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008093 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008094 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008095 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008096
8097 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008098 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008099 // Op0 is MVT::f32, Op1 is MVT::f64.
8100 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8101 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8102 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008103 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008104 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008105 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008106 }
8107
Evan Cheng73d6cf12007-01-05 21:37:56 +00008108 // Clear first operand sign bit.
8109 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008110 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008111 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8112 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008113 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008114 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8115 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8116 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8117 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008118 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008119 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008120 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008121 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008122 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008123 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008124 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008125
8126 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008127 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008128}
8129
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008130SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8131 SDValue N0 = Op.getOperand(0);
8132 DebugLoc dl = Op.getDebugLoc();
8133 EVT VT = Op.getValueType();
8134
8135 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8136 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8137 DAG.getConstant(1, VT));
8138 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8139}
8140
Dan Gohman076aee32009-03-04 19:44:21 +00008141/// Emit nodes that will be selected as "test Op0,Op0", or something
8142/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008143SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008144 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008145 DebugLoc dl = Op.getDebugLoc();
8146
Dan Gohman31125812009-03-07 01:58:32 +00008147 // CF and OF aren't always set the way we want. Determine which
8148 // of these we need.
8149 bool NeedCF = false;
8150 bool NeedOF = false;
8151 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008152 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008153 case X86::COND_A: case X86::COND_AE:
8154 case X86::COND_B: case X86::COND_BE:
8155 NeedCF = true;
8156 break;
8157 case X86::COND_G: case X86::COND_GE:
8158 case X86::COND_L: case X86::COND_LE:
8159 case X86::COND_O: case X86::COND_NO:
8160 NeedOF = true;
8161 break;
Dan Gohman31125812009-03-07 01:58:32 +00008162 }
8163
Dan Gohman076aee32009-03-04 19:44:21 +00008164 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008165 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8166 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008167 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8168 // Emit a CMP with 0, which is the TEST pattern.
8169 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8170 DAG.getConstant(0, Op.getValueType()));
8171
8172 unsigned Opcode = 0;
8173 unsigned NumOperands = 0;
8174 switch (Op.getNode()->getOpcode()) {
8175 case ISD::ADD:
8176 // Due to an isel shortcoming, be conservative if this add is likely to be
8177 // selected as part of a load-modify-store instruction. When the root node
8178 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8179 // uses of other nodes in the match, such as the ADD in this case. This
8180 // leads to the ADD being left around and reselected, with the result being
8181 // two adds in the output. Alas, even if none our users are stores, that
8182 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8183 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8184 // climbing the DAG back to the root, and it doesn't seem to be worth the
8185 // effort.
8186 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008187 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008188 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8189 goto default_case;
8190
8191 if (ConstantSDNode *C =
8192 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8193 // An add of one will be selected as an INC.
8194 if (C->getAPIntValue() == 1) {
8195 Opcode = X86ISD::INC;
8196 NumOperands = 1;
8197 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008198 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008199
8200 // An add of negative one (subtract of one) will be selected as a DEC.
8201 if (C->getAPIntValue().isAllOnesValue()) {
8202 Opcode = X86ISD::DEC;
8203 NumOperands = 1;
8204 break;
8205 }
Dan Gohman076aee32009-03-04 19:44:21 +00008206 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008207
8208 // Otherwise use a regular EFLAGS-setting add.
8209 Opcode = X86ISD::ADD;
8210 NumOperands = 2;
8211 break;
8212 case ISD::AND: {
8213 // If the primary and result isn't used, don't bother using X86ISD::AND,
8214 // because a TEST instruction will be better.
8215 bool NonFlagUse = false;
8216 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8217 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8218 SDNode *User = *UI;
8219 unsigned UOpNo = UI.getOperandNo();
8220 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8221 // Look pass truncate.
8222 UOpNo = User->use_begin().getOperandNo();
8223 User = *User->use_begin();
8224 }
8225
8226 if (User->getOpcode() != ISD::BRCOND &&
8227 User->getOpcode() != ISD::SETCC &&
8228 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8229 NonFlagUse = true;
8230 break;
8231 }
Dan Gohman076aee32009-03-04 19:44:21 +00008232 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008233
8234 if (!NonFlagUse)
8235 break;
8236 }
8237 // FALL THROUGH
8238 case ISD::SUB:
8239 case ISD::OR:
8240 case ISD::XOR:
8241 // Due to the ISEL shortcoming noted above, be conservative if this op is
8242 // likely to be selected as part of a load-modify-store instruction.
8243 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8244 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8245 if (UI->getOpcode() == ISD::STORE)
8246 goto default_case;
8247
8248 // Otherwise use a regular EFLAGS-setting instruction.
8249 switch (Op.getNode()->getOpcode()) {
8250 default: llvm_unreachable("unexpected operator!");
8251 case ISD::SUB: Opcode = X86ISD::SUB; break;
8252 case ISD::OR: Opcode = X86ISD::OR; break;
8253 case ISD::XOR: Opcode = X86ISD::XOR; break;
8254 case ISD::AND: Opcode = X86ISD::AND; break;
8255 }
8256
8257 NumOperands = 2;
8258 break;
8259 case X86ISD::ADD:
8260 case X86ISD::SUB:
8261 case X86ISD::INC:
8262 case X86ISD::DEC:
8263 case X86ISD::OR:
8264 case X86ISD::XOR:
8265 case X86ISD::AND:
8266 return SDValue(Op.getNode(), 1);
8267 default:
8268 default_case:
8269 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008270 }
8271
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008272 if (Opcode == 0)
8273 // Emit a CMP with 0, which is the TEST pattern.
8274 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8275 DAG.getConstant(0, Op.getValueType()));
8276
8277 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8278 SmallVector<SDValue, 4> Ops;
8279 for (unsigned i = 0; i != NumOperands; ++i)
8280 Ops.push_back(Op.getOperand(i));
8281
8282 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8283 DAG.ReplaceAllUsesWith(Op, New);
8284 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008285}
8286
8287/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8288/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008289SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008290 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008291 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8292 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008293 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008294
8295 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008296 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008297}
8298
Evan Chengd40d03e2010-01-06 19:38:29 +00008299/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8300/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008301SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8302 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008303 SDValue Op0 = And.getOperand(0);
8304 SDValue Op1 = And.getOperand(1);
8305 if (Op0.getOpcode() == ISD::TRUNCATE)
8306 Op0 = Op0.getOperand(0);
8307 if (Op1.getOpcode() == ISD::TRUNCATE)
8308 Op1 = Op1.getOperand(0);
8309
Evan Chengd40d03e2010-01-06 19:38:29 +00008310 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008311 if (Op1.getOpcode() == ISD::SHL)
8312 std::swap(Op0, Op1);
8313 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008314 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8315 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008316 // If we looked past a truncate, check that it's only truncating away
8317 // known zeros.
8318 unsigned BitWidth = Op0.getValueSizeInBits();
8319 unsigned AndBitWidth = And.getValueSizeInBits();
8320 if (BitWidth > AndBitWidth) {
8321 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8322 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8323 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8324 return SDValue();
8325 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008326 LHS = Op1;
8327 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008328 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008329 } else if (Op1.getOpcode() == ISD::Constant) {
8330 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8331 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008332 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8333 LHS = AndLHS.getOperand(0);
8334 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008335 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008336 }
Evan Cheng0488db92007-09-25 01:57:46 +00008337
Evan Chengd40d03e2010-01-06 19:38:29 +00008338 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008339 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008340 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008341 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008342 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008343 // Also promote i16 to i32 for performance / code size reason.
8344 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008345 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008346 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008347
Evan Chengd40d03e2010-01-06 19:38:29 +00008348 // If the operand types disagree, extend the shift amount to match. Since
8349 // BT ignores high bits (like shifts) we can use anyextend.
8350 if (LHS.getValueType() != RHS.getValueType())
8351 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008352
Evan Chengd40d03e2010-01-06 19:38:29 +00008353 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8354 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8355 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8356 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008357 }
8358
Evan Cheng54de3ea2010-01-05 06:52:31 +00008359 return SDValue();
8360}
8361
Dan Gohmand858e902010-04-17 15:26:15 +00008362SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008363
8364 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8365
Evan Cheng54de3ea2010-01-05 06:52:31 +00008366 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8367 SDValue Op0 = Op.getOperand(0);
8368 SDValue Op1 = Op.getOperand(1);
8369 DebugLoc dl = Op.getDebugLoc();
8370 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8371
8372 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008373 // Lower (X & (1 << N)) == 0 to BT(X, N).
8374 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8375 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008376 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008377 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008378 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008379 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8380 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8381 if (NewSetCC.getNode())
8382 return NewSetCC;
8383 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008384
Chris Lattner481eebc2010-12-19 21:23:48 +00008385 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8386 // these.
8387 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008388 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008389 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8390 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008391
Chris Lattner481eebc2010-12-19 21:23:48 +00008392 // If the input is a setcc, then reuse the input setcc or use a new one with
8393 // the inverted condition.
8394 if (Op0.getOpcode() == X86ISD::SETCC) {
8395 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8396 bool Invert = (CC == ISD::SETNE) ^
8397 cast<ConstantSDNode>(Op1)->isNullValue();
8398 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008399
Evan Cheng2c755ba2010-02-27 07:36:59 +00008400 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008401 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8402 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8403 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008404 }
8405
Evan Chenge5b51ac2010-04-17 06:13:15 +00008406 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008407 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008408 if (X86CC == X86::COND_INVALID)
8409 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008410
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008411 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008412 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008413 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008414}
8415
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008416// Lower256IntVETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
8417// ones, and then concatenate the result back.
8418static SDValue Lower256IntVETCC(SDValue Op, SelectionDAG &DAG) {
8419 EVT VT = Op.getValueType();
8420
Duncan Sands28b77e92011-09-06 19:07:46 +00008421 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008422 "Unsupported value type for operation");
8423
8424 int NumElems = VT.getVectorNumElements();
8425 DebugLoc dl = Op.getDebugLoc();
8426 SDValue CC = Op.getOperand(2);
8427 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8428 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8429
8430 // Extract the LHS vectors
8431 SDValue LHS = Op.getOperand(0);
8432 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8433 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8434
8435 // Extract the RHS vectors
8436 SDValue RHS = Op.getOperand(1);
8437 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8438 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8439
8440 // Issue the operation on the smaller types and concatenate the result back
8441 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8442 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8443 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8444 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8445 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8446}
8447
8448
Dan Gohmand858e902010-04-17 15:26:15 +00008449SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008450 SDValue Cond;
8451 SDValue Op0 = Op.getOperand(0);
8452 SDValue Op1 = Op.getOperand(1);
8453 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008454 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008455 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8456 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008457 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008458
8459 if (isFP) {
8460 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008461 EVT EltVT = Op0.getValueType().getVectorElementType();
8462 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8463
8464 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008465 bool Swap = false;
8466
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008467 // SSE Condition code mapping:
8468 // 0 - EQ
8469 // 1 - LT
8470 // 2 - LE
8471 // 3 - UNORD
8472 // 4 - NEQ
8473 // 5 - NLT
8474 // 6 - NLE
8475 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008476 switch (SetCCOpcode) {
8477 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008478 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008479 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008480 case ISD::SETOGT:
8481 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008482 case ISD::SETLT:
8483 case ISD::SETOLT: SSECC = 1; break;
8484 case ISD::SETOGE:
8485 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008486 case ISD::SETLE:
8487 case ISD::SETOLE: SSECC = 2; break;
8488 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008489 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008490 case ISD::SETNE: SSECC = 4; break;
8491 case ISD::SETULE: Swap = true;
8492 case ISD::SETUGE: SSECC = 5; break;
8493 case ISD::SETULT: Swap = true;
8494 case ISD::SETUGT: SSECC = 6; break;
8495 case ISD::SETO: SSECC = 7; break;
8496 }
8497 if (Swap)
8498 std::swap(Op0, Op1);
8499
Nate Begemanfb8ead02008-07-25 19:05:58 +00008500 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008501 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008502 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008503 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008504 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8505 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008506 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008507 }
8508 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008509 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008510 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8511 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008512 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008513 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008514 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008515 }
8516 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008517 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008518 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008519
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008520 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008521 if (!isFP && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008522 return Lower256IntVETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008523
Nate Begeman30a0de92008-07-17 16:51:19 +00008524 // We are handling one of the integer comparisons here. Since SSE only has
8525 // GT and EQ comparisons for integer, swapping operands and multiple
8526 // operations may be required for some comparisons.
8527 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8528 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008529
Owen Anderson825b72b2009-08-11 20:47:22 +00008530 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008531 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008532 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008533 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008534 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8535 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008536 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008537
Nate Begeman30a0de92008-07-17 16:51:19 +00008538 switch (SetCCOpcode) {
8539 default: break;
8540 case ISD::SETNE: Invert = true;
8541 case ISD::SETEQ: Opc = EQOpc; break;
8542 case ISD::SETLT: Swap = true;
8543 case ISD::SETGT: Opc = GTOpc; break;
8544 case ISD::SETGE: Swap = true;
8545 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8546 case ISD::SETULT: Swap = true;
8547 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8548 case ISD::SETUGE: Swap = true;
8549 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8550 }
8551 if (Swap)
8552 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008553
Nate Begeman30a0de92008-07-17 16:51:19 +00008554 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8555 // bits of the inputs before performing those operations.
8556 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008557 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008558 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8559 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008560 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008561 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8562 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008563 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8564 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008566
Dale Johannesenace16102009-02-03 19:33:06 +00008567 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008568
8569 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008570 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008571 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008572
Nate Begeman30a0de92008-07-17 16:51:19 +00008573 return Result;
8574}
Evan Cheng0488db92007-09-25 01:57:46 +00008575
Evan Cheng370e5342008-12-03 08:38:43 +00008576// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008577static bool isX86LogicalCmp(SDValue Op) {
8578 unsigned Opc = Op.getNode()->getOpcode();
8579 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8580 return true;
8581 if (Op.getResNo() == 1 &&
8582 (Opc == X86ISD::ADD ||
8583 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008584 Opc == X86ISD::ADC ||
8585 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008586 Opc == X86ISD::SMUL ||
8587 Opc == X86ISD::UMUL ||
8588 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008589 Opc == X86ISD::DEC ||
8590 Opc == X86ISD::OR ||
8591 Opc == X86ISD::XOR ||
8592 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008593 return true;
8594
Chris Lattner9637d5b2010-12-05 07:49:54 +00008595 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8596 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008597
Dan Gohman076aee32009-03-04 19:44:21 +00008598 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008599}
8600
Chris Lattnera2b56002010-12-05 01:23:24 +00008601static bool isZero(SDValue V) {
8602 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8603 return C && C->isNullValue();
8604}
8605
Chris Lattner96908b12010-12-05 02:00:51 +00008606static bool isAllOnes(SDValue V) {
8607 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8608 return C && C->isAllOnesValue();
8609}
8610
Dan Gohmand858e902010-04-17 15:26:15 +00008611SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008612 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008613 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008614 SDValue Op1 = Op.getOperand(1);
8615 SDValue Op2 = Op.getOperand(2);
8616 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008617 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008618
Dan Gohman1a492952009-10-20 16:22:37 +00008619 if (Cond.getOpcode() == ISD::SETCC) {
8620 SDValue NewCond = LowerSETCC(Cond, DAG);
8621 if (NewCond.getNode())
8622 Cond = NewCond;
8623 }
Evan Cheng734503b2006-09-11 02:19:56 +00008624
Chris Lattnera2b56002010-12-05 01:23:24 +00008625 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008626 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008627 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008628 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008629 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008630 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8631 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008632 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008633
Chris Lattnera2b56002010-12-05 01:23:24 +00008634 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008635
8636 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008637 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8638 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008639
8640 SDValue CmpOp0 = Cmp.getOperand(0);
8641 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8642 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008643
Chris Lattner96908b12010-12-05 02:00:51 +00008644 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008645 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8646 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008647
Chris Lattner96908b12010-12-05 02:00:51 +00008648 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8649 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008650
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008651 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008652 if (N2C == 0 || !N2C->isNullValue())
8653 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8654 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008655 }
8656 }
8657
Chris Lattnera2b56002010-12-05 01:23:24 +00008658 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008659 if (Cond.getOpcode() == ISD::AND &&
8660 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8661 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008662 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008663 Cond = Cond.getOperand(0);
8664 }
8665
Evan Cheng3f41d662007-10-08 22:16:29 +00008666 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8667 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008668 if (Cond.getOpcode() == X86ISD::SETCC ||
8669 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008670 CC = Cond.getOperand(0);
8671
Dan Gohman475871a2008-07-27 21:46:04 +00008672 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008673 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008674 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008675
Evan Cheng3f41d662007-10-08 22:16:29 +00008676 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008677 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008678 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008679 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008680
Chris Lattnerd1980a52009-03-12 06:52:53 +00008681 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8682 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008683 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008684 addTest = false;
8685 }
8686 }
8687
8688 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008689 // Look pass the truncate.
8690 if (Cond.getOpcode() == ISD::TRUNCATE)
8691 Cond = Cond.getOperand(0);
8692
8693 // We know the result of AND is compared against zero. Try to match
8694 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008695 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008696 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008697 if (NewSetCC.getNode()) {
8698 CC = NewSetCC.getOperand(0);
8699 Cond = NewSetCC.getOperand(1);
8700 addTest = false;
8701 }
8702 }
8703 }
8704
8705 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008706 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008707 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008708 }
8709
Benjamin Kramere915ff32010-12-22 23:09:28 +00008710 // a < b ? -1 : 0 -> RES = ~setcc_carry
8711 // a < b ? 0 : -1 -> RES = setcc_carry
8712 // a >= b ? -1 : 0 -> RES = setcc_carry
8713 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8714 if (Cond.getOpcode() == X86ISD::CMP) {
8715 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8716
8717 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8718 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8719 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8720 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8721 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8722 return DAG.getNOT(DL, Res, Res.getValueType());
8723 return Res;
8724 }
8725 }
8726
Evan Cheng0488db92007-09-25 01:57:46 +00008727 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8728 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008729 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008730 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008731 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008732}
8733
Evan Cheng370e5342008-12-03 08:38:43 +00008734// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8735// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8736// from the AND / OR.
8737static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8738 Opc = Op.getOpcode();
8739 if (Opc != ISD::OR && Opc != ISD::AND)
8740 return false;
8741 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8742 Op.getOperand(0).hasOneUse() &&
8743 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8744 Op.getOperand(1).hasOneUse());
8745}
8746
Evan Cheng961d6d42009-02-02 08:19:07 +00008747// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8748// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008749static bool isXor1OfSetCC(SDValue Op) {
8750 if (Op.getOpcode() != ISD::XOR)
8751 return false;
8752 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8753 if (N1C && N1C->getAPIntValue() == 1) {
8754 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8755 Op.getOperand(0).hasOneUse();
8756 }
8757 return false;
8758}
8759
Dan Gohmand858e902010-04-17 15:26:15 +00008760SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008761 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008762 SDValue Chain = Op.getOperand(0);
8763 SDValue Cond = Op.getOperand(1);
8764 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008765 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008766 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008767
Dan Gohman1a492952009-10-20 16:22:37 +00008768 if (Cond.getOpcode() == ISD::SETCC) {
8769 SDValue NewCond = LowerSETCC(Cond, DAG);
8770 if (NewCond.getNode())
8771 Cond = NewCond;
8772 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008773#if 0
8774 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008775 else if (Cond.getOpcode() == X86ISD::ADD ||
8776 Cond.getOpcode() == X86ISD::SUB ||
8777 Cond.getOpcode() == X86ISD::SMUL ||
8778 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008779 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008780#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008781
Evan Chengad9c0a32009-12-15 00:53:42 +00008782 // Look pass (and (setcc_carry (cmp ...)), 1).
8783 if (Cond.getOpcode() == ISD::AND &&
8784 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8785 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008786 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008787 Cond = Cond.getOperand(0);
8788 }
8789
Evan Cheng3f41d662007-10-08 22:16:29 +00008790 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8791 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008792 if (Cond.getOpcode() == X86ISD::SETCC ||
8793 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008794 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008795
Dan Gohman475871a2008-07-27 21:46:04 +00008796 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008797 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008798 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008799 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008800 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008801 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008802 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008803 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008804 default: break;
8805 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008806 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008807 // These can only come from an arithmetic instruction with overflow,
8808 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008809 Cond = Cond.getNode()->getOperand(1);
8810 addTest = false;
8811 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008812 }
Evan Cheng0488db92007-09-25 01:57:46 +00008813 }
Evan Cheng370e5342008-12-03 08:38:43 +00008814 } else {
8815 unsigned CondOpc;
8816 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8817 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008818 if (CondOpc == ISD::OR) {
8819 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8820 // two branches instead of an explicit OR instruction with a
8821 // separate test.
8822 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008823 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008824 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008825 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008826 Chain, Dest, CC, Cmp);
8827 CC = Cond.getOperand(1).getOperand(0);
8828 Cond = Cmp;
8829 addTest = false;
8830 }
8831 } else { // ISD::AND
8832 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8833 // two branches instead of an explicit AND instruction with a
8834 // separate test. However, we only do this if this block doesn't
8835 // have a fall-through edge, because this requires an explicit
8836 // jmp when the condition is false.
8837 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008838 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008839 Op.getNode()->hasOneUse()) {
8840 X86::CondCode CCode =
8841 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8842 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008843 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008844 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008845 // Look for an unconditional branch following this conditional branch.
8846 // We need this because we need to reverse the successors in order
8847 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008848 if (User->getOpcode() == ISD::BR) {
8849 SDValue FalseBB = User->getOperand(1);
8850 SDNode *NewBR =
8851 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008852 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008853 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008854 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008855
Dale Johannesene4d209d2009-02-03 20:21:25 +00008856 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008857 Chain, Dest, CC, Cmp);
8858 X86::CondCode CCode =
8859 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8860 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008861 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008862 Cond = Cmp;
8863 addTest = false;
8864 }
8865 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008866 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008867 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8868 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8869 // It should be transformed during dag combiner except when the condition
8870 // is set by a arithmetics with overflow node.
8871 X86::CondCode CCode =
8872 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8873 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008874 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008875 Cond = Cond.getOperand(0).getOperand(1);
8876 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008877 }
Evan Cheng0488db92007-09-25 01:57:46 +00008878 }
8879
8880 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008881 // Look pass the truncate.
8882 if (Cond.getOpcode() == ISD::TRUNCATE)
8883 Cond = Cond.getOperand(0);
8884
8885 // We know the result of AND is compared against zero. Try to match
8886 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008887 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008888 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8889 if (NewSetCC.getNode()) {
8890 CC = NewSetCC.getOperand(0);
8891 Cond = NewSetCC.getOperand(1);
8892 addTest = false;
8893 }
8894 }
8895 }
8896
8897 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008898 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008899 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008900 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008901 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008902 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008903}
8904
Anton Korobeynikove060b532007-04-17 19:34:00 +00008905
8906// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8907// Calls to _alloca is needed to probe the stack when allocating more than 4k
8908// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8909// that the guard pages used by the OS virtual memory manager are allocated in
8910// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008911SDValue
8912X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008913 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008914 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8915 EnableSegmentedStacks) &&
8916 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008917 "are being used");
8918 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008919 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008920
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008921 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008922 SDValue Chain = Op.getOperand(0);
8923 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008924 // FIXME: Ensure alignment here
8925
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008926 bool Is64Bit = Subtarget->is64Bit();
8927 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008928
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008929 if (EnableSegmentedStacks) {
8930 MachineFunction &MF = DAG.getMachineFunction();
8931 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008932
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008933 if (Is64Bit) {
8934 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008935 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008936 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008937
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008938 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8939 I != E; I++)
8940 if (I->hasNestAttr())
8941 report_fatal_error("Cannot use segmented stacks with functions that "
8942 "have nested arguments.");
8943 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008944
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008945 const TargetRegisterClass *AddrRegClass =
8946 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8947 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8948 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8949 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8950 DAG.getRegister(Vreg, SPTy));
8951 SDValue Ops1[2] = { Value, Chain };
8952 return DAG.getMergeValues(Ops1, 2, dl);
8953 } else {
8954 SDValue Flag;
8955 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008956
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008957 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8958 Flag = Chain.getValue(1);
8959 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008960
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008961 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8962 Flag = Chain.getValue(1);
8963
8964 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8965
8966 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8967 return DAG.getMergeValues(Ops1, 2, dl);
8968 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008969}
8970
Dan Gohmand858e902010-04-17 15:26:15 +00008971SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008972 MachineFunction &MF = DAG.getMachineFunction();
8973 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8974
Dan Gohman69de1932008-02-06 22:27:42 +00008975 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008976 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008977
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008978 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008979 // vastart just stores the address of the VarArgsFrameIndex slot into the
8980 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008981 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8982 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008983 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8984 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008985 }
8986
8987 // __va_list_tag:
8988 // gp_offset (0 - 6 * 8)
8989 // fp_offset (48 - 48 + 8 * 16)
8990 // overflow_arg_area (point to parameters coming in memory).
8991 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008992 SmallVector<SDValue, 8> MemOps;
8993 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008994 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008995 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008996 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8997 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008998 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008999 MemOps.push_back(Store);
9000
9001 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009002 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009003 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009004 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009005 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9006 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009007 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009008 MemOps.push_back(Store);
9009
9010 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009011 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009012 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009013 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9014 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009015 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9016 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009017 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009018 MemOps.push_back(Store);
9019
9020 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009021 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009022 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009023 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9024 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009025 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9026 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009027 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009028 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009029 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009030}
9031
Dan Gohmand858e902010-04-17 15:26:15 +00009032SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009033 assert(Subtarget->is64Bit() &&
9034 "LowerVAARG only handles 64-bit va_arg!");
9035 assert((Subtarget->isTargetLinux() ||
9036 Subtarget->isTargetDarwin()) &&
9037 "Unhandled target in LowerVAARG");
9038 assert(Op.getNode()->getNumOperands() == 4);
9039 SDValue Chain = Op.getOperand(0);
9040 SDValue SrcPtr = Op.getOperand(1);
9041 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9042 unsigned Align = Op.getConstantOperandVal(3);
9043 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009044
Dan Gohman320afb82010-10-12 18:00:49 +00009045 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009046 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009047 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9048 uint8_t ArgMode;
9049
9050 // Decide which area this value should be read from.
9051 // TODO: Implement the AMD64 ABI in its entirety. This simple
9052 // selection mechanism works only for the basic types.
9053 if (ArgVT == MVT::f80) {
9054 llvm_unreachable("va_arg for f80 not yet implemented");
9055 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9056 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9057 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9058 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9059 } else {
9060 llvm_unreachable("Unhandled argument type in LowerVAARG");
9061 }
9062
9063 if (ArgMode == 2) {
9064 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009065 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009066 !(DAG.getMachineFunction()
9067 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009068 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009069 }
9070
9071 // Insert VAARG_64 node into the DAG
9072 // VAARG_64 returns two values: Variable Argument Address, Chain
9073 SmallVector<SDValue, 11> InstOps;
9074 InstOps.push_back(Chain);
9075 InstOps.push_back(SrcPtr);
9076 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9077 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9078 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9079 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9080 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9081 VTs, &InstOps[0], InstOps.size(),
9082 MVT::i64,
9083 MachinePointerInfo(SV),
9084 /*Align=*/0,
9085 /*Volatile=*/false,
9086 /*ReadMem=*/true,
9087 /*WriteMem=*/true);
9088 Chain = VAARG.getValue(1);
9089
9090 // Load the next argument and return it
9091 return DAG.getLoad(ArgVT, dl,
9092 Chain,
9093 VAARG,
9094 MachinePointerInfo(),
9095 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009096}
9097
Dan Gohmand858e902010-04-17 15:26:15 +00009098SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009099 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009100 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009101 SDValue Chain = Op.getOperand(0);
9102 SDValue DstPtr = Op.getOperand(1);
9103 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009104 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9105 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009106 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009107
Chris Lattnere72f2022010-09-21 05:40:29 +00009108 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009109 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009110 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009111 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009112}
9113
Dan Gohman475871a2008-07-27 21:46:04 +00009114SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009115X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009116 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009117 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009118 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009119 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009120 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009121 case Intrinsic::x86_sse_comieq_ss:
9122 case Intrinsic::x86_sse_comilt_ss:
9123 case Intrinsic::x86_sse_comile_ss:
9124 case Intrinsic::x86_sse_comigt_ss:
9125 case Intrinsic::x86_sse_comige_ss:
9126 case Intrinsic::x86_sse_comineq_ss:
9127 case Intrinsic::x86_sse_ucomieq_ss:
9128 case Intrinsic::x86_sse_ucomilt_ss:
9129 case Intrinsic::x86_sse_ucomile_ss:
9130 case Intrinsic::x86_sse_ucomigt_ss:
9131 case Intrinsic::x86_sse_ucomige_ss:
9132 case Intrinsic::x86_sse_ucomineq_ss:
9133 case Intrinsic::x86_sse2_comieq_sd:
9134 case Intrinsic::x86_sse2_comilt_sd:
9135 case Intrinsic::x86_sse2_comile_sd:
9136 case Intrinsic::x86_sse2_comigt_sd:
9137 case Intrinsic::x86_sse2_comige_sd:
9138 case Intrinsic::x86_sse2_comineq_sd:
9139 case Intrinsic::x86_sse2_ucomieq_sd:
9140 case Intrinsic::x86_sse2_ucomilt_sd:
9141 case Intrinsic::x86_sse2_ucomile_sd:
9142 case Intrinsic::x86_sse2_ucomigt_sd:
9143 case Intrinsic::x86_sse2_ucomige_sd:
9144 case Intrinsic::x86_sse2_ucomineq_sd: {
9145 unsigned Opc = 0;
9146 ISD::CondCode CC = ISD::SETCC_INVALID;
9147 switch (IntNo) {
9148 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009149 case Intrinsic::x86_sse_comieq_ss:
9150 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009151 Opc = X86ISD::COMI;
9152 CC = ISD::SETEQ;
9153 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009154 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009155 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009156 Opc = X86ISD::COMI;
9157 CC = ISD::SETLT;
9158 break;
9159 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009160 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009161 Opc = X86ISD::COMI;
9162 CC = ISD::SETLE;
9163 break;
9164 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009165 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009166 Opc = X86ISD::COMI;
9167 CC = ISD::SETGT;
9168 break;
9169 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009170 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009171 Opc = X86ISD::COMI;
9172 CC = ISD::SETGE;
9173 break;
9174 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009175 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009176 Opc = X86ISD::COMI;
9177 CC = ISD::SETNE;
9178 break;
9179 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009180 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009181 Opc = X86ISD::UCOMI;
9182 CC = ISD::SETEQ;
9183 break;
9184 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009185 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009186 Opc = X86ISD::UCOMI;
9187 CC = ISD::SETLT;
9188 break;
9189 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009190 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009191 Opc = X86ISD::UCOMI;
9192 CC = ISD::SETLE;
9193 break;
9194 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009195 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009196 Opc = X86ISD::UCOMI;
9197 CC = ISD::SETGT;
9198 break;
9199 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009200 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009201 Opc = X86ISD::UCOMI;
9202 CC = ISD::SETGE;
9203 break;
9204 case Intrinsic::x86_sse_ucomineq_ss:
9205 case Intrinsic::x86_sse2_ucomineq_sd:
9206 Opc = X86ISD::UCOMI;
9207 CC = ISD::SETNE;
9208 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009209 }
Evan Cheng734503b2006-09-11 02:19:56 +00009210
Dan Gohman475871a2008-07-27 21:46:04 +00009211 SDValue LHS = Op.getOperand(1);
9212 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009213 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009214 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009215 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9216 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9217 DAG.getConstant(X86CC, MVT::i8), Cond);
9218 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009219 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009220 // ptest and testp intrinsics. The intrinsic these come from are designed to
9221 // return an integer value, not just an instruction so lower it to the ptest
9222 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009223 case Intrinsic::x86_sse41_ptestz:
9224 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009225 case Intrinsic::x86_sse41_ptestnzc:
9226 case Intrinsic::x86_avx_ptestz_256:
9227 case Intrinsic::x86_avx_ptestc_256:
9228 case Intrinsic::x86_avx_ptestnzc_256:
9229 case Intrinsic::x86_avx_vtestz_ps:
9230 case Intrinsic::x86_avx_vtestc_ps:
9231 case Intrinsic::x86_avx_vtestnzc_ps:
9232 case Intrinsic::x86_avx_vtestz_pd:
9233 case Intrinsic::x86_avx_vtestc_pd:
9234 case Intrinsic::x86_avx_vtestnzc_pd:
9235 case Intrinsic::x86_avx_vtestz_ps_256:
9236 case Intrinsic::x86_avx_vtestc_ps_256:
9237 case Intrinsic::x86_avx_vtestnzc_ps_256:
9238 case Intrinsic::x86_avx_vtestz_pd_256:
9239 case Intrinsic::x86_avx_vtestc_pd_256:
9240 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9241 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009242 unsigned X86CC = 0;
9243 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009244 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009245 case Intrinsic::x86_avx_vtestz_ps:
9246 case Intrinsic::x86_avx_vtestz_pd:
9247 case Intrinsic::x86_avx_vtestz_ps_256:
9248 case Intrinsic::x86_avx_vtestz_pd_256:
9249 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009250 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009251 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009252 // ZF = 1
9253 X86CC = X86::COND_E;
9254 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009255 case Intrinsic::x86_avx_vtestc_ps:
9256 case Intrinsic::x86_avx_vtestc_pd:
9257 case Intrinsic::x86_avx_vtestc_ps_256:
9258 case Intrinsic::x86_avx_vtestc_pd_256:
9259 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009260 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009261 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009262 // CF = 1
9263 X86CC = X86::COND_B;
9264 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009265 case Intrinsic::x86_avx_vtestnzc_ps:
9266 case Intrinsic::x86_avx_vtestnzc_pd:
9267 case Intrinsic::x86_avx_vtestnzc_ps_256:
9268 case Intrinsic::x86_avx_vtestnzc_pd_256:
9269 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009270 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009271 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009272 // ZF and CF = 0
9273 X86CC = X86::COND_A;
9274 break;
9275 }
Eric Christopherfd179292009-08-27 18:07:15 +00009276
Eric Christopher71c67532009-07-29 00:28:05 +00009277 SDValue LHS = Op.getOperand(1);
9278 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009279 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9280 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009281 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9282 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9283 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009284 }
Evan Cheng5759f972008-05-04 09:15:50 +00009285
9286 // Fix vector shift instructions where the last operand is a non-immediate
9287 // i32 value.
9288 case Intrinsic::x86_sse2_pslli_w:
9289 case Intrinsic::x86_sse2_pslli_d:
9290 case Intrinsic::x86_sse2_pslli_q:
9291 case Intrinsic::x86_sse2_psrli_w:
9292 case Intrinsic::x86_sse2_psrli_d:
9293 case Intrinsic::x86_sse2_psrli_q:
9294 case Intrinsic::x86_sse2_psrai_w:
9295 case Intrinsic::x86_sse2_psrai_d:
9296 case Intrinsic::x86_mmx_pslli_w:
9297 case Intrinsic::x86_mmx_pslli_d:
9298 case Intrinsic::x86_mmx_pslli_q:
9299 case Intrinsic::x86_mmx_psrli_w:
9300 case Intrinsic::x86_mmx_psrli_d:
9301 case Intrinsic::x86_mmx_psrli_q:
9302 case Intrinsic::x86_mmx_psrai_w:
9303 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009304 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009305 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009306 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009307
9308 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009309 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009310 switch (IntNo) {
9311 case Intrinsic::x86_sse2_pslli_w:
9312 NewIntNo = Intrinsic::x86_sse2_psll_w;
9313 break;
9314 case Intrinsic::x86_sse2_pslli_d:
9315 NewIntNo = Intrinsic::x86_sse2_psll_d;
9316 break;
9317 case Intrinsic::x86_sse2_pslli_q:
9318 NewIntNo = Intrinsic::x86_sse2_psll_q;
9319 break;
9320 case Intrinsic::x86_sse2_psrli_w:
9321 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9322 break;
9323 case Intrinsic::x86_sse2_psrli_d:
9324 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9325 break;
9326 case Intrinsic::x86_sse2_psrli_q:
9327 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9328 break;
9329 case Intrinsic::x86_sse2_psrai_w:
9330 NewIntNo = Intrinsic::x86_sse2_psra_w;
9331 break;
9332 case Intrinsic::x86_sse2_psrai_d:
9333 NewIntNo = Intrinsic::x86_sse2_psra_d;
9334 break;
9335 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009336 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009337 switch (IntNo) {
9338 case Intrinsic::x86_mmx_pslli_w:
9339 NewIntNo = Intrinsic::x86_mmx_psll_w;
9340 break;
9341 case Intrinsic::x86_mmx_pslli_d:
9342 NewIntNo = Intrinsic::x86_mmx_psll_d;
9343 break;
9344 case Intrinsic::x86_mmx_pslli_q:
9345 NewIntNo = Intrinsic::x86_mmx_psll_q;
9346 break;
9347 case Intrinsic::x86_mmx_psrli_w:
9348 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9349 break;
9350 case Intrinsic::x86_mmx_psrli_d:
9351 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9352 break;
9353 case Intrinsic::x86_mmx_psrli_q:
9354 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9355 break;
9356 case Intrinsic::x86_mmx_psrai_w:
9357 NewIntNo = Intrinsic::x86_mmx_psra_w;
9358 break;
9359 case Intrinsic::x86_mmx_psrai_d:
9360 NewIntNo = Intrinsic::x86_mmx_psra_d;
9361 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009362 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009363 }
9364 break;
9365 }
9366 }
Mon P Wangefa42202009-09-03 19:56:25 +00009367
9368 // The vector shift intrinsics with scalars uses 32b shift amounts but
9369 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9370 // to be zero.
9371 SDValue ShOps[4];
9372 ShOps[0] = ShAmt;
9373 ShOps[1] = DAG.getConstant(0, MVT::i32);
9374 if (ShAmtVT == MVT::v4i32) {
9375 ShOps[2] = DAG.getUNDEF(MVT::i32);
9376 ShOps[3] = DAG.getUNDEF(MVT::i32);
9377 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9378 } else {
9379 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009380// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009381 }
9382
Owen Andersone50ed302009-08-10 22:56:29 +00009383 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009384 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009385 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009386 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009387 Op.getOperand(1), ShAmt);
9388 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009389 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009390}
Evan Cheng72261582005-12-20 06:22:03 +00009391
Dan Gohmand858e902010-04-17 15:26:15 +00009392SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9393 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009394 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9395 MFI->setReturnAddressIsTaken(true);
9396
Bill Wendling64e87322009-01-16 19:25:27 +00009397 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009398 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009399
9400 if (Depth > 0) {
9401 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9402 SDValue Offset =
9403 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009404 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009405 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009406 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009407 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009408 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009409 }
9410
9411 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009412 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009413 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009414 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009415}
9416
Dan Gohmand858e902010-04-17 15:26:15 +00009417SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009418 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9419 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009420
Owen Andersone50ed302009-08-10 22:56:29 +00009421 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009422 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009423 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9424 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009425 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009426 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009427 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9428 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009429 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009430 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009431}
9432
Dan Gohman475871a2008-07-27 21:46:04 +00009433SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009434 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009435 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009436}
9437
Dan Gohmand858e902010-04-17 15:26:15 +00009438SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009439 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009440 SDValue Chain = Op.getOperand(0);
9441 SDValue Offset = Op.getOperand(1);
9442 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009443 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009444
Dan Gohmand8816272010-08-11 18:14:00 +00009445 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9446 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9447 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009448 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009449
Dan Gohmand8816272010-08-11 18:14:00 +00009450 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9451 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009452 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009453 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9454 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009455 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009456 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009457
Dale Johannesene4d209d2009-02-03 20:21:25 +00009458 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009459 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009460 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009461}
9462
Duncan Sands4a544a72011-09-06 13:37:06 +00009463SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9464 SelectionDAG &DAG) const {
9465 return Op.getOperand(0);
9466}
9467
9468SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9469 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009470 SDValue Root = Op.getOperand(0);
9471 SDValue Trmp = Op.getOperand(1); // trampoline
9472 SDValue FPtr = Op.getOperand(2); // nested function
9473 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009474 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009475
Dan Gohman69de1932008-02-06 22:27:42 +00009476 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009477
9478 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009479 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009480
9481 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009482 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9483 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009484
Evan Cheng0e6a0522011-07-18 20:57:22 +00009485 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9486 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009487
9488 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9489
9490 // Load the pointer to the nested function into R11.
9491 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009492 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009493 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009494 Addr, MachinePointerInfo(TrmpAddr),
9495 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009496
Owen Anderson825b72b2009-08-11 20:47:22 +00009497 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9498 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009499 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9500 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009501 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009502
9503 // Load the 'nest' parameter value into R10.
9504 // R10 is specified in X86CallingConv.td
9505 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009506 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9507 DAG.getConstant(10, MVT::i64));
9508 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009509 Addr, MachinePointerInfo(TrmpAddr, 10),
9510 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009511
Owen Anderson825b72b2009-08-11 20:47:22 +00009512 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9513 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009514 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9515 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009516 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009517
9518 // Jump to the nested function.
9519 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009520 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9521 DAG.getConstant(20, MVT::i64));
9522 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009523 Addr, MachinePointerInfo(TrmpAddr, 20),
9524 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009525
9526 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009527 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9528 DAG.getConstant(22, MVT::i64));
9529 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009530 MachinePointerInfo(TrmpAddr, 22),
9531 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009532
Duncan Sands4a544a72011-09-06 13:37:06 +00009533 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009534 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009535 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009536 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009537 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009538 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009539
9540 switch (CC) {
9541 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009542 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009543 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009544 case CallingConv::X86_StdCall: {
9545 // Pass 'nest' parameter in ECX.
9546 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009547 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009548
9549 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009550 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009551 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009552
Chris Lattner58d74912008-03-12 17:45:29 +00009553 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009554 unsigned InRegCount = 0;
9555 unsigned Idx = 1;
9556
9557 for (FunctionType::param_iterator I = FTy->param_begin(),
9558 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009559 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009560 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009561 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009562
9563 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009564 report_fatal_error("Nest register in use - reduce number of inreg"
9565 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009566 }
9567 }
9568 break;
9569 }
9570 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009571 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009572 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009573 // Pass 'nest' parameter in EAX.
9574 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009575 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009576 break;
9577 }
9578
Dan Gohman475871a2008-07-27 21:46:04 +00009579 SDValue OutChains[4];
9580 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009581
Owen Anderson825b72b2009-08-11 20:47:22 +00009582 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9583 DAG.getConstant(10, MVT::i32));
9584 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009585
Chris Lattnera62fe662010-02-05 19:20:30 +00009586 // This is storing the opcode for MOV32ri.
9587 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009588 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009589 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009590 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009591 Trmp, MachinePointerInfo(TrmpAddr),
9592 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009593
Owen Anderson825b72b2009-08-11 20:47:22 +00009594 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9595 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009596 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9597 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009598 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009599
Chris Lattnera62fe662010-02-05 19:20:30 +00009600 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009601 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9602 DAG.getConstant(5, MVT::i32));
9603 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009604 MachinePointerInfo(TrmpAddr, 5),
9605 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009606
Owen Anderson825b72b2009-08-11 20:47:22 +00009607 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9608 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009609 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9610 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009611 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009612
Duncan Sands4a544a72011-09-06 13:37:06 +00009613 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009614 }
9615}
9616
Dan Gohmand858e902010-04-17 15:26:15 +00009617SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9618 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009619 /*
9620 The rounding mode is in bits 11:10 of FPSR, and has the following
9621 settings:
9622 00 Round to nearest
9623 01 Round to -inf
9624 10 Round to +inf
9625 11 Round to 0
9626
9627 FLT_ROUNDS, on the other hand, expects the following:
9628 -1 Undefined
9629 0 Round to 0
9630 1 Round to nearest
9631 2 Round to +inf
9632 3 Round to -inf
9633
9634 To perform the conversion, we do:
9635 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9636 */
9637
9638 MachineFunction &MF = DAG.getMachineFunction();
9639 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009640 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009641 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009642 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009643 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009644
9645 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009646 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009647 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009648
Michael J. Spencerec38de22010-10-10 22:04:20 +00009649
Chris Lattner2156b792010-09-22 01:11:26 +00009650 MachineMemOperand *MMO =
9651 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9652 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009653
Chris Lattner2156b792010-09-22 01:11:26 +00009654 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9655 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9656 DAG.getVTList(MVT::Other),
9657 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009658
9659 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009660 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009661 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009662
9663 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009664 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009665 DAG.getNode(ISD::SRL, DL, MVT::i16,
9666 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009667 CWD, DAG.getConstant(0x800, MVT::i16)),
9668 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009669 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009670 DAG.getNode(ISD::SRL, DL, MVT::i16,
9671 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009672 CWD, DAG.getConstant(0x400, MVT::i16)),
9673 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009674
Dan Gohman475871a2008-07-27 21:46:04 +00009675 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009676 DAG.getNode(ISD::AND, DL, MVT::i16,
9677 DAG.getNode(ISD::ADD, DL, MVT::i16,
9678 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009679 DAG.getConstant(1, MVT::i16)),
9680 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009681
9682
Duncan Sands83ec4b62008-06-06 12:08:01 +00009683 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009684 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009685}
9686
Dan Gohmand858e902010-04-17 15:26:15 +00009687SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009688 EVT VT = Op.getValueType();
9689 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009690 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009691 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009692
9693 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009694 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009695 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009696 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009697 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009698 }
Evan Cheng18efe262007-12-14 02:13:44 +00009699
Evan Cheng152804e2007-12-14 08:30:15 +00009700 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009701 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009702 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009703
9704 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009705 SDValue Ops[] = {
9706 Op,
9707 DAG.getConstant(NumBits+NumBits-1, OpVT),
9708 DAG.getConstant(X86::COND_E, MVT::i8),
9709 Op.getValue(1)
9710 };
9711 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009712
9713 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009714 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009715
Owen Anderson825b72b2009-08-11 20:47:22 +00009716 if (VT == MVT::i8)
9717 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009718 return Op;
9719}
9720
Dan Gohmand858e902010-04-17 15:26:15 +00009721SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009722 EVT VT = Op.getValueType();
9723 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009724 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009725 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009726
9727 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009728 if (VT == MVT::i8) {
9729 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009730 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009731 }
Evan Cheng152804e2007-12-14 08:30:15 +00009732
9733 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009734 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009735 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009736
9737 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009738 SDValue Ops[] = {
9739 Op,
9740 DAG.getConstant(NumBits, OpVT),
9741 DAG.getConstant(X86::COND_E, MVT::i8),
9742 Op.getValue(1)
9743 };
9744 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009745
Owen Anderson825b72b2009-08-11 20:47:22 +00009746 if (VT == MVT::i8)
9747 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009748 return Op;
9749}
9750
Craig Topper13894fa2011-08-24 06:14:18 +00009751// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9752// ones, and then concatenate the result back.
9753static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009754 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009755
9756 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9757 "Unsupported value type for operation");
9758
9759 int NumElems = VT.getVectorNumElements();
9760 DebugLoc dl = Op.getDebugLoc();
9761 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9762 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9763
9764 // Extract the LHS vectors
9765 SDValue LHS = Op.getOperand(0);
9766 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9767 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9768
9769 // Extract the RHS vectors
9770 SDValue RHS = Op.getOperand(1);
9771 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9772 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9773
9774 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9775 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9776
9777 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9778 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9779 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9780}
9781
9782SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9783 assert(Op.getValueType().getSizeInBits() == 256 &&
9784 Op.getValueType().isInteger() &&
9785 "Only handle AVX 256-bit vector integer operation");
9786 return Lower256IntArith(Op, DAG);
9787}
9788
9789SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9790 assert(Op.getValueType().getSizeInBits() == 256 &&
9791 Op.getValueType().isInteger() &&
9792 "Only handle AVX 256-bit vector integer operation");
9793 return Lower256IntArith(Op, DAG);
9794}
9795
9796SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9797 EVT VT = Op.getValueType();
9798
9799 // Decompose 256-bit ops into smaller 128-bit ops.
9800 if (VT.getSizeInBits() == 256)
9801 return Lower256IntArith(Op, DAG);
9802
Owen Anderson825b72b2009-08-11 20:47:22 +00009803 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009804 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009805
Mon P Wangaf9b9522008-12-18 21:42:19 +00009806 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9807 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9808 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9809 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9810 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9811 //
9812 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9813 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9814 // return AloBlo + AloBhi + AhiBlo;
9815
9816 SDValue A = Op.getOperand(0);
9817 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009818
Dale Johannesene4d209d2009-02-03 20:21:25 +00009819 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009820 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9821 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009822 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009823 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9824 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009825 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009826 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009827 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009828 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009829 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009830 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009831 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009832 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009833 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009834 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009835 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9836 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009837 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009838 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9839 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009840 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9841 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009842 return Res;
9843}
9844
Nadav Rotem43012222011-05-11 08:12:09 +00009845SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9846
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009847 EVT VT = Op.getValueType();
9848 DebugLoc dl = Op.getDebugLoc();
9849 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009850 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009851 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009852
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00009853 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009854 return SDValue();
9855
9856 // Decompose 256-bit shifts into smaller 128-bit shifts.
9857 if (VT.getSizeInBits() == 256) {
9858 int NumElems = VT.getVectorNumElements();
9859 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9860 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9861
9862 // Extract the two vectors
9863 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9864 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9865 DAG, dl);
9866
9867 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009868 SDValue Amt1, Amt2;
9869 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9870 // Constant shift amount
9871 SmallVector<SDValue, 4> Amt1Csts;
9872 SmallVector<SDValue, 4> Amt2Csts;
9873 for (int i = 0; i < NumElems/2; ++i)
9874 Amt1Csts.push_back(Amt->getOperand(i));
9875 for (int i = NumElems/2; i < NumElems; ++i)
9876 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009877
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009878 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9879 &Amt1Csts[0], NumElems/2);
9880 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9881 &Amt2Csts[0], NumElems/2);
9882 } else {
9883 // Variable shift amount
9884 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9885 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9886 DAG, dl);
9887 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009888
9889 // Issue new vector shifts for the smaller types
9890 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9891 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9892
9893 // Concatenate the result back
9894 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9895 }
Nate Begeman51409212010-07-28 00:21:48 +00009896
Nadav Rotem43012222011-05-11 08:12:09 +00009897 // Optimize shl/srl/sra with constant shift amount.
9898 if (isSplatVector(Amt.getNode())) {
9899 SDValue SclrAmt = Amt->getOperand(0);
9900 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9901 uint64_t ShiftAmt = C->getZExtValue();
9902
9903 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9904 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9905 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9906 R, DAG.getConstant(ShiftAmt, MVT::i32));
9907
9908 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9909 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9910 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9911 R, DAG.getConstant(ShiftAmt, MVT::i32));
9912
9913 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9914 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9915 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9916 R, DAG.getConstant(ShiftAmt, MVT::i32));
9917
9918 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9919 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9920 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9921 R, DAG.getConstant(ShiftAmt, MVT::i32));
9922
9923 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9924 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9925 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9926 R, DAG.getConstant(ShiftAmt, MVT::i32));
9927
9928 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9929 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9930 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9931 R, DAG.getConstant(ShiftAmt, MVT::i32));
9932
9933 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9934 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9935 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9936 R, DAG.getConstant(ShiftAmt, MVT::i32));
9937
9938 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9939 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9940 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9941 R, DAG.getConstant(ShiftAmt, MVT::i32));
9942 }
9943 }
9944
9945 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009946 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009947 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9948 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9949 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9950
9951 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009952
Nate Begeman51409212010-07-28 00:21:48 +00009953 std::vector<Constant*> CV(4, CI);
9954 Constant *C = ConstantVector::get(CV);
9955 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9956 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009957 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009958 false, false, 16);
9959
9960 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009961 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009962 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9963 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9964 }
Nadav Rotem43012222011-05-11 08:12:09 +00009965 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009966 // a = a << 5;
9967 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9968 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9969 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9970
9971 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9972 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9973
9974 std::vector<Constant*> CVM1(16, CM1);
9975 std::vector<Constant*> CVM2(16, CM2);
9976 Constant *C = ConstantVector::get(CVM1);
9977 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9978 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009979 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009980 false, false, 16);
9981
9982 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9983 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9984 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9985 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9986 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +00009987 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +00009988 // a += a
9989 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009990
Nate Begeman51409212010-07-28 00:21:48 +00009991 C = ConstantVector::get(CVM2);
9992 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9993 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009994 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009995 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009996
Nate Begeman51409212010-07-28 00:21:48 +00009997 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9998 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9999 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10000 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10001 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010002 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010003 // a += a
10004 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010005
Nate Begeman51409212010-07-28 00:21:48 +000010006 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010007 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10008 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010009 return R;
10010 }
10011 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010012}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010013
Dan Gohmand858e902010-04-17 15:26:15 +000010014SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010015 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10016 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010017 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10018 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010019 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010020 SDValue LHS = N->getOperand(0);
10021 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010022 unsigned BaseOp = 0;
10023 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010024 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010025 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010026 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010027 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010028 // A subtract of one will be selected as a INC. Note that INC doesn't
10029 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10031 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010032 BaseOp = X86ISD::INC;
10033 Cond = X86::COND_O;
10034 break;
10035 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010036 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010037 Cond = X86::COND_O;
10038 break;
10039 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010040 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010041 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010042 break;
10043 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010044 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10045 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010046 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10047 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010048 BaseOp = X86ISD::DEC;
10049 Cond = X86::COND_O;
10050 break;
10051 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010052 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010053 Cond = X86::COND_O;
10054 break;
10055 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010056 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010057 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010058 break;
10059 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010060 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010061 Cond = X86::COND_O;
10062 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010063 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10064 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10065 MVT::i32);
10066 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010067
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010068 SDValue SetCC =
10069 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10070 DAG.getConstant(X86::COND_O, MVT::i32),
10071 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010072
Dan Gohman6e5fda22011-07-22 18:45:15 +000010073 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010074 }
Bill Wendling74c37652008-12-09 22:08:41 +000010075 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010076
Bill Wendling61edeb52008-12-02 01:06:39 +000010077 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010078 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010079 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010080
Bill Wendling61edeb52008-12-02 01:06:39 +000010081 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010082 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10083 DAG.getConstant(Cond, MVT::i32),
10084 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010085
Dan Gohman6e5fda22011-07-22 18:45:15 +000010086 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010087}
10088
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010089SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10090 DebugLoc dl = Op.getDebugLoc();
10091 SDNode* Node = Op.getNode();
10092 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10093 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010094 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010095 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10096 ExtraVT.getScalarType().getSizeInBits();
10097 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10098
10099 unsigned SHLIntrinsicsID = 0;
10100 unsigned SRAIntrinsicsID = 0;
10101 switch (VT.getSimpleVT().SimpleTy) {
10102 default:
10103 return SDValue();
10104 case MVT::v2i64: {
10105 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
10106 SRAIntrinsicsID = 0;
10107 break;
10108 }
10109 case MVT::v4i32: {
10110 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10111 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10112 break;
10113 }
10114 case MVT::v8i16: {
10115 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10116 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10117 break;
10118 }
10119 }
10120
10121 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10122 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10123 Node->getOperand(0), ShAmt);
10124
10125 // In case of 1 bit sext, no need to shr
10126 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10127
10128 if (SRAIntrinsicsID) {
10129 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10130 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10131 Tmp1, ShAmt);
10132 }
10133 return Tmp1;
10134 }
10135
10136 return SDValue();
10137}
10138
10139
Eric Christopher9a9d2752010-07-22 02:48:34 +000010140SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10141 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010142
Eric Christopher77ed1352011-07-08 00:04:56 +000010143 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10144 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010145 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010146 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010147 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010148 SDValue Ops[] = {
10149 DAG.getRegister(X86::ESP, MVT::i32), // Base
10150 DAG.getTargetConstant(1, MVT::i8), // Scale
10151 DAG.getRegister(0, MVT::i32), // Index
10152 DAG.getTargetConstant(0, MVT::i32), // Disp
10153 DAG.getRegister(0, MVT::i32), // Segment.
10154 Zero,
10155 Chain
10156 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010157 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010158 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10159 array_lengthof(Ops));
10160 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010161 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010162
Eric Christopher9a9d2752010-07-22 02:48:34 +000010163 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010164 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010165 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010166
Chris Lattner132929a2010-08-14 17:26:09 +000010167 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10168 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10169 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10170 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010171
Chris Lattner132929a2010-08-14 17:26:09 +000010172 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10173 if (!Op1 && !Op2 && !Op3 && Op4)
10174 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010175
Chris Lattner132929a2010-08-14 17:26:09 +000010176 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10177 if (Op1 && !Op2 && !Op3 && !Op4)
10178 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010179
10180 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010181 // (MFENCE)>;
10182 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010183}
10184
Eli Friedman14648462011-07-27 22:21:52 +000010185SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10186 SelectionDAG &DAG) const {
10187 DebugLoc dl = Op.getDebugLoc();
10188 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10189 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10190 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10191 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10192
10193 // The only fence that needs an instruction is a sequentially-consistent
10194 // cross-thread fence.
10195 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10196 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10197 // no-sse2). There isn't any reason to disable it if the target processor
10198 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010199 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010200 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10201
10202 SDValue Chain = Op.getOperand(0);
10203 SDValue Zero = DAG.getConstant(0, MVT::i32);
10204 SDValue Ops[] = {
10205 DAG.getRegister(X86::ESP, MVT::i32), // Base
10206 DAG.getTargetConstant(1, MVT::i8), // Scale
10207 DAG.getRegister(0, MVT::i32), // Index
10208 DAG.getTargetConstant(0, MVT::i32), // Disp
10209 DAG.getRegister(0, MVT::i32), // Segment.
10210 Zero,
10211 Chain
10212 };
10213 SDNode *Res =
10214 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10215 array_lengthof(Ops));
10216 return SDValue(Res, 0);
10217 }
10218
10219 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10220 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10221}
10222
10223
Dan Gohmand858e902010-04-17 15:26:15 +000010224SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010225 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010226 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010227 unsigned Reg = 0;
10228 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010229 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010230 default:
10231 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010232 case MVT::i8: Reg = X86::AL; size = 1; break;
10233 case MVT::i16: Reg = X86::AX; size = 2; break;
10234 case MVT::i32: Reg = X86::EAX; size = 4; break;
10235 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010236 assert(Subtarget->is64Bit() && "Node not type legal!");
10237 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010238 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010239 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010240 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010241 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010242 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010243 Op.getOperand(1),
10244 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010245 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010246 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010247 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010248 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10249 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10250 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010251 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010252 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010253 return cpOut;
10254}
10255
Duncan Sands1607f052008-12-01 11:39:25 +000010256SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010257 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010258 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010259 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010260 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010261 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010262 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010263 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10264 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010265 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010266 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10267 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010268 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010269 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010270 rdx.getValue(1)
10271 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010272 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010273}
10274
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010275SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010276 SelectionDAG &DAG) const {
10277 EVT SrcVT = Op.getOperand(0).getValueType();
10278 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010279 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010280 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010281 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010282 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010283 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010284 // i64 <=> MMX conversions are Legal.
10285 if (SrcVT==MVT::i64 && DstVT.isVector())
10286 return Op;
10287 if (DstVT==MVT::i64 && SrcVT.isVector())
10288 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010289 // MMX <=> MMX conversions are Legal.
10290 if (SrcVT.isVector() && DstVT.isVector())
10291 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010292 // All other conversions need to be expanded.
10293 return SDValue();
10294}
Chris Lattner5b856542010-12-20 00:59:46 +000010295
Dan Gohmand858e902010-04-17 15:26:15 +000010296SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010297 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010298 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010299 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010300 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010301 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010302 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010303 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010304 Node->getOperand(0),
10305 Node->getOperand(1), negOp,
10306 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010307 cast<AtomicSDNode>(Node)->getAlignment(),
10308 cast<AtomicSDNode>(Node)->getOrdering(),
10309 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010310}
10311
Eli Friedman327236c2011-08-24 20:50:09 +000010312static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10313 SDNode *Node = Op.getNode();
10314 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010315 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010316
10317 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010318 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10319 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10320 // (The only way to get a 16-byte store is cmpxchg16b)
10321 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10322 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10323 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010324 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10325 cast<AtomicSDNode>(Node)->getMemoryVT(),
10326 Node->getOperand(0),
10327 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010328 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010329 cast<AtomicSDNode>(Node)->getOrdering(),
10330 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010331 return Swap.getValue(1);
10332 }
10333 // Other atomic stores have a simple pattern.
10334 return Op;
10335}
10336
Chris Lattner5b856542010-12-20 00:59:46 +000010337static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10338 EVT VT = Op.getNode()->getValueType(0);
10339
10340 // Let legalize expand this if it isn't a legal type yet.
10341 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10342 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010343
Chris Lattner5b856542010-12-20 00:59:46 +000010344 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010345
Chris Lattner5b856542010-12-20 00:59:46 +000010346 unsigned Opc;
10347 bool ExtraOp = false;
10348 switch (Op.getOpcode()) {
10349 default: assert(0 && "Invalid code");
10350 case ISD::ADDC: Opc = X86ISD::ADD; break;
10351 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10352 case ISD::SUBC: Opc = X86ISD::SUB; break;
10353 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10354 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010355
Chris Lattner5b856542010-12-20 00:59:46 +000010356 if (!ExtraOp)
10357 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10358 Op.getOperand(1));
10359 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10360 Op.getOperand(1), Op.getOperand(2));
10361}
10362
Evan Cheng0db9fe62006-04-25 20:13:52 +000010363/// LowerOperation - Provide custom lowering hooks for some operations.
10364///
Dan Gohmand858e902010-04-17 15:26:15 +000010365SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010366 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010367 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010368 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010369 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010370 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010371 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10372 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010373 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010374 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010375 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010376 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10377 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10378 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010379 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010380 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010381 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10382 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10383 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010384 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010385 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010386 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010387 case ISD::SHL_PARTS:
10388 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010389 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010390 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010391 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010392 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010393 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010394 case ISD::FABS: return LowerFABS(Op, DAG);
10395 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010396 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010397 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010398 case ISD::SETCC: return LowerSETCC(Op, DAG);
10399 case ISD::SELECT: return LowerSELECT(Op, DAG);
10400 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010401 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010402 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010403 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010404 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010405 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010406 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10407 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010408 case ISD::FRAME_TO_ARGS_OFFSET:
10409 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010410 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010411 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010412 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10413 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010414 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010415 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10416 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010417 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010418 case ISD::SRA:
10419 case ISD::SRL:
10420 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010421 case ISD::SADDO:
10422 case ISD::UADDO:
10423 case ISD::SSUBO:
10424 case ISD::USUBO:
10425 case ISD::SMULO:
10426 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010427 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010428 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010429 case ISD::ADDC:
10430 case ISD::ADDE:
10431 case ISD::SUBC:
10432 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010433 case ISD::ADD: return LowerADD(Op, DAG);
10434 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010435 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010436}
10437
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010438static void ReplaceATOMIC_LOAD(SDNode *Node,
10439 SmallVectorImpl<SDValue> &Results,
10440 SelectionDAG &DAG) {
10441 DebugLoc dl = Node->getDebugLoc();
10442 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10443
10444 // Convert wide load -> cmpxchg8b/cmpxchg16b
10445 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10446 // (The only way to get a 16-byte load is cmpxchg16b)
10447 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010448 SDValue Zero = DAG.getConstant(0, VT);
10449 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010450 Node->getOperand(0),
10451 Node->getOperand(1), Zero, Zero,
10452 cast<AtomicSDNode>(Node)->getMemOperand(),
10453 cast<AtomicSDNode>(Node)->getOrdering(),
10454 cast<AtomicSDNode>(Node)->getSynchScope());
10455 Results.push_back(Swap.getValue(0));
10456 Results.push_back(Swap.getValue(1));
10457}
10458
Duncan Sands1607f052008-12-01 11:39:25 +000010459void X86TargetLowering::
10460ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010461 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010462 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010463 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010464 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010465
10466 SDValue Chain = Node->getOperand(0);
10467 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010468 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010469 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010470 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010471 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010472 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010473 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010474 SDValue Result =
10475 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10476 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010477 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010478 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010479 Results.push_back(Result.getValue(2));
10480}
10481
Duncan Sands126d9072008-07-04 11:47:58 +000010482/// ReplaceNodeResults - Replace a node with an illegal result type
10483/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010484void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10485 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010486 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010487 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010488 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010489 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010490 assert(false && "Do not know how to custom type legalize this operation!");
10491 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010492 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010493 case ISD::ADDC:
10494 case ISD::ADDE:
10495 case ISD::SUBC:
10496 case ISD::SUBE:
10497 // We don't want to expand or promote these.
10498 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010499 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010500 std::pair<SDValue,SDValue> Vals =
10501 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010502 SDValue FIST = Vals.first, StackSlot = Vals.second;
10503 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010504 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010505 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010506 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10507 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010508 }
10509 return;
10510 }
10511 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010512 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010513 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010514 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010515 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010516 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010517 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010518 eax.getValue(2));
10519 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10520 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010521 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010522 Results.push_back(edx.getValue(1));
10523 return;
10524 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010525 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010526 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010527 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010528 bool Regs64bit = T == MVT::i128;
10529 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010530 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010531 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10532 DAG.getConstant(0, HalfT));
10533 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10534 DAG.getConstant(1, HalfT));
10535 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10536 Regs64bit ? X86::RAX : X86::EAX,
10537 cpInL, SDValue());
10538 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10539 Regs64bit ? X86::RDX : X86::EDX,
10540 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010541 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010542 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10543 DAG.getConstant(0, HalfT));
10544 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10545 DAG.getConstant(1, HalfT));
10546 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10547 Regs64bit ? X86::RBX : X86::EBX,
10548 swapInL, cpInH.getValue(1));
10549 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10550 Regs64bit ? X86::RCX : X86::ECX,
10551 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010552 SDValue Ops[] = { swapInH.getValue(0),
10553 N->getOperand(1),
10554 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010555 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010556 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010557 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10558 X86ISD::LCMPXCHG8_DAG;
10559 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010560 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010561 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10562 Regs64bit ? X86::RAX : X86::EAX,
10563 HalfT, Result.getValue(1));
10564 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10565 Regs64bit ? X86::RDX : X86::EDX,
10566 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010567 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010568 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010569 Results.push_back(cpOutH.getValue(1));
10570 return;
10571 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010572 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010573 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10574 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010575 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010576 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10577 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010578 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010579 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10580 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010581 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010582 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10583 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010584 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010585 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10586 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010587 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010588 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10589 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010590 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010591 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10592 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010593 case ISD::ATOMIC_LOAD:
10594 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010595 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010596}
10597
Evan Cheng72261582005-12-20 06:22:03 +000010598const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10599 switch (Opcode) {
10600 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010601 case X86ISD::BSF: return "X86ISD::BSF";
10602 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010603 case X86ISD::SHLD: return "X86ISD::SHLD";
10604 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010605 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010606 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010607 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010608 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010609 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010610 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010611 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10612 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10613 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010614 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010615 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010616 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010617 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010618 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010619 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010620 case X86ISD::COMI: return "X86ISD::COMI";
10621 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010622 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010623 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010624 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10625 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010626 case X86ISD::CMOV: return "X86ISD::CMOV";
10627 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010628 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010629 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10630 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010631 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010632 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010633 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010634 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010635 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010636 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10637 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010638 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010639 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010640 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010641 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10642 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10643 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000010644 case X86ISD::FMAX: return "X86ISD::FMAX";
10645 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010646 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10647 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010648 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010649 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010650 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010651 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010652 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010653 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10654 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010655 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10656 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10657 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10658 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10659 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10660 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010661 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10662 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010663 case X86ISD::VSHL: return "X86ISD::VSHL";
10664 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010665 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10666 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10667 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10668 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10669 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10670 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10671 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10672 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10673 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10674 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010675 case X86ISD::ADD: return "X86ISD::ADD";
10676 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010677 case X86ISD::ADC: return "X86ISD::ADC";
10678 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010679 case X86ISD::SMUL: return "X86ISD::SMUL";
10680 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010681 case X86ISD::INC: return "X86ISD::INC";
10682 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010683 case X86ISD::OR: return "X86ISD::OR";
10684 case X86ISD::XOR: return "X86ISD::XOR";
10685 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010686 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010687 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010688 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010689 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10690 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10691 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10692 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10693 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10694 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10695 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10696 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10697 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010698 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010699 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010700 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010701 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10702 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010703 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10704 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10705 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10706 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10707 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10708 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10709 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10710 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10711 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010712 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010713 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10714 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10715 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10716 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10717 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10718 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10719 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10720 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10721 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10722 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010723 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010724 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10725 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10726 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10727 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010728 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010729 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010730 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010731 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010732 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010733 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010734 }
10735}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010736
Chris Lattnerc9addb72007-03-30 23:15:24 +000010737// isLegalAddressingMode - Return true if the addressing mode represented
10738// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010739bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010740 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010741 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010742 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010743 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010744
Chris Lattnerc9addb72007-03-30 23:15:24 +000010745 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010746 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010747 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010748
Chris Lattnerc9addb72007-03-30 23:15:24 +000010749 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010750 unsigned GVFlags =
10751 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010752
Chris Lattnerdfed4132009-07-10 07:38:24 +000010753 // If a reference to this global requires an extra load, we can't fold it.
10754 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010755 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010756
Chris Lattnerdfed4132009-07-10 07:38:24 +000010757 // If BaseGV requires a register for the PIC base, we cannot also have a
10758 // BaseReg specified.
10759 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010760 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010761
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010762 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010763 if ((M != CodeModel::Small || R != Reloc::Static) &&
10764 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010765 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010766 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010767
Chris Lattnerc9addb72007-03-30 23:15:24 +000010768 switch (AM.Scale) {
10769 case 0:
10770 case 1:
10771 case 2:
10772 case 4:
10773 case 8:
10774 // These scales always work.
10775 break;
10776 case 3:
10777 case 5:
10778 case 9:
10779 // These scales are formed with basereg+scalereg. Only accept if there is
10780 // no basereg yet.
10781 if (AM.HasBaseReg)
10782 return false;
10783 break;
10784 default: // Other stuff never works.
10785 return false;
10786 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010787
Chris Lattnerc9addb72007-03-30 23:15:24 +000010788 return true;
10789}
10790
10791
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010792bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010793 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010794 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010795 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10796 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010797 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010798 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010799 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010800}
10801
Owen Andersone50ed302009-08-10 22:56:29 +000010802bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010803 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010804 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010805 unsigned NumBits1 = VT1.getSizeInBits();
10806 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010807 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010808 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010809 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010810}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010811
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010812bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010813 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010814 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010815}
10816
Owen Andersone50ed302009-08-10 22:56:29 +000010817bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010818 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010819 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010820}
10821
Owen Andersone50ed302009-08-10 22:56:29 +000010822bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010823 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010824 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010825}
10826
Evan Cheng60c07e12006-07-05 22:17:51 +000010827/// isShuffleMaskLegal - Targets can use this to indicate that they only
10828/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10829/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10830/// are assumed to be legal.
10831bool
Eric Christopherfd179292009-08-27 18:07:15 +000010832X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010833 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010834 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010835 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010836 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000010837
Nate Begemana09008b2009-10-19 02:17:23 +000010838 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010839 return (VT.getVectorNumElements() == 2 ||
10840 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10841 isMOVLMask(M, VT) ||
10842 isSHUFPMask(M, VT) ||
10843 isPSHUFDMask(M, VT) ||
10844 isPSHUFHWMask(M, VT) ||
10845 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010846 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010847 isUNPCKLMask(M, VT) ||
10848 isUNPCKHMask(M, VT) ||
10849 isUNPCKL_v_undef_Mask(M, VT) ||
10850 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010851}
10852
Dan Gohman7d8143f2008-04-09 20:09:42 +000010853bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010854X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010855 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010856 unsigned NumElts = VT.getVectorNumElements();
10857 // FIXME: This collection of masks seems suspect.
10858 if (NumElts == 2)
10859 return true;
10860 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10861 return (isMOVLMask(Mask, VT) ||
10862 isCommutedMOVLMask(Mask, VT, true) ||
10863 isSHUFPMask(Mask, VT) ||
10864 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010865 }
10866 return false;
10867}
10868
10869//===----------------------------------------------------------------------===//
10870// X86 Scheduler Hooks
10871//===----------------------------------------------------------------------===//
10872
Mon P Wang63307c32008-05-05 19:05:59 +000010873// private utility function
10874MachineBasicBlock *
10875X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10876 MachineBasicBlock *MBB,
10877 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010878 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010879 unsigned LoadOpc,
10880 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010881 unsigned notOpc,
10882 unsigned EAXreg,
10883 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010884 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010885 // For the atomic bitwise operator, we generate
10886 // thisMBB:
10887 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010888 // ld t1 = [bitinstr.addr]
10889 // op t2 = t1, [bitinstr.val]
10890 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010891 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10892 // bz newMBB
10893 // fallthrough -->nextMBB
10894 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10895 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010896 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010897 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010898
Mon P Wang63307c32008-05-05 19:05:59 +000010899 /// First build the CFG
10900 MachineFunction *F = MBB->getParent();
10901 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010902 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10903 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10904 F->insert(MBBIter, newMBB);
10905 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010906
Dan Gohman14152b42010-07-06 20:24:04 +000010907 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10908 nextMBB->splice(nextMBB->begin(), thisMBB,
10909 llvm::next(MachineBasicBlock::iterator(bInstr)),
10910 thisMBB->end());
10911 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010912
Mon P Wang63307c32008-05-05 19:05:59 +000010913 // Update thisMBB to fall through to newMBB
10914 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010915
Mon P Wang63307c32008-05-05 19:05:59 +000010916 // newMBB jumps to itself and fall through to nextMBB
10917 newMBB->addSuccessor(nextMBB);
10918 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010919
Mon P Wang63307c32008-05-05 19:05:59 +000010920 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010921 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010922 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010923 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010924 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010925 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010926 int numArgs = bInstr->getNumOperands() - 1;
10927 for (int i=0; i < numArgs; ++i)
10928 argOpers[i] = &bInstr->getOperand(i+1);
10929
10930 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010931 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010932 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010933
Dale Johannesen140be2d2008-08-19 18:47:28 +000010934 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010935 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010936 for (int i=0; i <= lastAddrIndx; ++i)
10937 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010938
Dale Johannesen140be2d2008-08-19 18:47:28 +000010939 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010940 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010941 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010942 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010943 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010944 tt = t1;
10945
Dale Johannesen140be2d2008-08-19 18:47:28 +000010946 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010947 assert((argOpers[valArgIndx]->isReg() ||
10948 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010949 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010950 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010951 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010952 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010953 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010954 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010955 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010956
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010957 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010958 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010959
Dale Johannesene4d209d2009-02-03 20:21:25 +000010960 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010961 for (int i=0; i <= lastAddrIndx; ++i)
10962 (*MIB).addOperand(*argOpers[i]);
10963 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010964 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010965 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10966 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010967
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010968 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010969 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010970
Mon P Wang63307c32008-05-05 19:05:59 +000010971 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010972 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010973
Dan Gohman14152b42010-07-06 20:24:04 +000010974 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010975 return nextMBB;
10976}
10977
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010978// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010979MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010980X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10981 MachineBasicBlock *MBB,
10982 unsigned regOpcL,
10983 unsigned regOpcH,
10984 unsigned immOpcL,
10985 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010986 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010987 // For the atomic bitwise operator, we generate
10988 // thisMBB (instructions are in pairs, except cmpxchg8b)
10989 // ld t1,t2 = [bitinstr.addr]
10990 // newMBB:
10991 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
10992 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000010993 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010994 // mov ECX, EBX <- t5, t6
10995 // mov EAX, EDX <- t1, t2
10996 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
10997 // mov t3, t4 <- EAX, EDX
10998 // bz newMBB
10999 // result in out1, out2
11000 // fallthrough -->nextMBB
11001
11002 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11003 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011004 const unsigned NotOpc = X86::NOT32r;
11005 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11006 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11007 MachineFunction::iterator MBBIter = MBB;
11008 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011009
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011010 /// First build the CFG
11011 MachineFunction *F = MBB->getParent();
11012 MachineBasicBlock *thisMBB = MBB;
11013 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11014 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11015 F->insert(MBBIter, newMBB);
11016 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011017
Dan Gohman14152b42010-07-06 20:24:04 +000011018 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11019 nextMBB->splice(nextMBB->begin(), thisMBB,
11020 llvm::next(MachineBasicBlock::iterator(bInstr)),
11021 thisMBB->end());
11022 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011023
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011024 // Update thisMBB to fall through to newMBB
11025 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011026
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011027 // newMBB jumps to itself and fall through to nextMBB
11028 newMBB->addSuccessor(nextMBB);
11029 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011030
Dale Johannesene4d209d2009-02-03 20:21:25 +000011031 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011032 // Insert instructions into newMBB based on incoming instruction
11033 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011034 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011035 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011036 MachineOperand& dest1Oper = bInstr->getOperand(0);
11037 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011038 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11039 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011040 argOpers[i] = &bInstr->getOperand(i+2);
11041
Dan Gohman71ea4e52010-05-14 21:01:44 +000011042 // We use some of the operands multiple times, so conservatively just
11043 // clear any kill flags that might be present.
11044 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11045 argOpers[i]->setIsKill(false);
11046 }
11047
Evan Chengad5b52f2010-01-08 19:14:57 +000011048 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011049 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011050
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011051 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011052 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011053 for (int i=0; i <= lastAddrIndx; ++i)
11054 (*MIB).addOperand(*argOpers[i]);
11055 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011056 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011057 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011058 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011059 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011060 MachineOperand newOp3 = *(argOpers[3]);
11061 if (newOp3.isImm())
11062 newOp3.setImm(newOp3.getImm()+4);
11063 else
11064 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011065 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011066 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011067
11068 // t3/4 are defined later, at the bottom of the loop
11069 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11070 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011071 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011072 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011073 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011074 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11075
Evan Cheng306b4ca2010-01-08 23:41:50 +000011076 // The subsequent operations should be using the destination registers of
11077 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011078 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011079 t1 = F->getRegInfo().createVirtualRegister(RC);
11080 t2 = F->getRegInfo().createVirtualRegister(RC);
11081 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11082 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011083 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011084 t1 = dest1Oper.getReg();
11085 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011086 }
11087
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011088 int valArgIndx = lastAddrIndx + 1;
11089 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011090 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011091 "invalid operand");
11092 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11093 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011094 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011095 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011096 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011097 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011098 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011099 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011100 (*MIB).addOperand(*argOpers[valArgIndx]);
11101 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011102 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011103 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011104 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011105 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011106 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011107 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011108 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011109 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011110 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011111 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011112
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011113 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011114 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011115 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011116 MIB.addReg(t2);
11117
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011118 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011119 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011120 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011121 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011122
Dale Johannesene4d209d2009-02-03 20:21:25 +000011123 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011124 for (int i=0; i <= lastAddrIndx; ++i)
11125 (*MIB).addOperand(*argOpers[i]);
11126
11127 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011128 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11129 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011130
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011131 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011132 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011133 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011134 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011135
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011136 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011137 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011138
Dan Gohman14152b42010-07-06 20:24:04 +000011139 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011140 return nextMBB;
11141}
11142
11143// private utility function
11144MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011145X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11146 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011147 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011148 // For the atomic min/max operator, we generate
11149 // thisMBB:
11150 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011151 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011152 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011153 // cmp t1, t2
11154 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011155 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011156 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11157 // bz newMBB
11158 // fallthrough -->nextMBB
11159 //
11160 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11161 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011162 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011163 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011164
Mon P Wang63307c32008-05-05 19:05:59 +000011165 /// First build the CFG
11166 MachineFunction *F = MBB->getParent();
11167 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011168 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11169 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11170 F->insert(MBBIter, newMBB);
11171 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011172
Dan Gohman14152b42010-07-06 20:24:04 +000011173 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11174 nextMBB->splice(nextMBB->begin(), thisMBB,
11175 llvm::next(MachineBasicBlock::iterator(mInstr)),
11176 thisMBB->end());
11177 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011178
Mon P Wang63307c32008-05-05 19:05:59 +000011179 // Update thisMBB to fall through to newMBB
11180 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011181
Mon P Wang63307c32008-05-05 19:05:59 +000011182 // newMBB jumps to newMBB and fall through to nextMBB
11183 newMBB->addSuccessor(nextMBB);
11184 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011185
Dale Johannesene4d209d2009-02-03 20:21:25 +000011186 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011187 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011188 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011189 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011190 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011191 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011192 int numArgs = mInstr->getNumOperands() - 1;
11193 for (int i=0; i < numArgs; ++i)
11194 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011195
Mon P Wang63307c32008-05-05 19:05:59 +000011196 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011197 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011198 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011199
Mon P Wangab3e7472008-05-05 22:56:23 +000011200 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011201 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011202 for (int i=0; i <= lastAddrIndx; ++i)
11203 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011204
Mon P Wang63307c32008-05-05 19:05:59 +000011205 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011206 assert((argOpers[valArgIndx]->isReg() ||
11207 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011208 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011209
11210 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011211 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011212 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011213 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011214 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011215 (*MIB).addOperand(*argOpers[valArgIndx]);
11216
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011217 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011218 MIB.addReg(t1);
11219
Dale Johannesene4d209d2009-02-03 20:21:25 +000011220 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011221 MIB.addReg(t1);
11222 MIB.addReg(t2);
11223
11224 // Generate movc
11225 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011226 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011227 MIB.addReg(t2);
11228 MIB.addReg(t1);
11229
11230 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011231 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011232 for (int i=0; i <= lastAddrIndx; ++i)
11233 (*MIB).addOperand(*argOpers[i]);
11234 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011235 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011236 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11237 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011238
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011239 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011240 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011241
Mon P Wang63307c32008-05-05 19:05:59 +000011242 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011243 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011244
Dan Gohman14152b42010-07-06 20:24:04 +000011245 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011246 return nextMBB;
11247}
11248
Eric Christopherf83a5de2009-08-27 18:08:16 +000011249// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011250// or XMM0_V32I8 in AVX all of this code can be replaced with that
11251// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011252MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011253X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011254 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011255 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11256 "Target must have SSE4.2 or AVX features enabled");
11257
Eric Christopherb120ab42009-08-18 22:50:32 +000011258 DebugLoc dl = MI->getDebugLoc();
11259 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011260 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011261 if (!Subtarget->hasAVX()) {
11262 if (memArg)
11263 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11264 else
11265 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11266 } else {
11267 if (memArg)
11268 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11269 else
11270 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11271 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011272
Eric Christopher41c902f2010-11-30 08:20:21 +000011273 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011274 for (unsigned i = 0; i < numArgs; ++i) {
11275 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011276 if (!(Op.isReg() && Op.isImplicit()))
11277 MIB.addOperand(Op);
11278 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011279 BuildMI(*BB, MI, dl,
11280 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11281 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011282 .addReg(X86::XMM0);
11283
Dan Gohman14152b42010-07-06 20:24:04 +000011284 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011285 return BB;
11286}
11287
11288MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011289X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011290 DebugLoc dl = MI->getDebugLoc();
11291 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011292
Eric Christopher228232b2010-11-30 07:20:12 +000011293 // Address into RAX/EAX, other two args into ECX, EDX.
11294 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11295 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11296 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11297 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011298 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011299
Eric Christopher228232b2010-11-30 07:20:12 +000011300 unsigned ValOps = X86::AddrNumOperands;
11301 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11302 .addReg(MI->getOperand(ValOps).getReg());
11303 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11304 .addReg(MI->getOperand(ValOps+1).getReg());
11305
11306 // The instruction doesn't actually take any operands though.
11307 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011308
Eric Christopher228232b2010-11-30 07:20:12 +000011309 MI->eraseFromParent(); // The pseudo is gone now.
11310 return BB;
11311}
11312
11313MachineBasicBlock *
11314X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011315 DebugLoc dl = MI->getDebugLoc();
11316 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011317
Eric Christopher228232b2010-11-30 07:20:12 +000011318 // First arg in ECX, the second in EAX.
11319 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11320 .addReg(MI->getOperand(0).getReg());
11321 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11322 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011323
Eric Christopher228232b2010-11-30 07:20:12 +000011324 // The instruction doesn't actually take any operands though.
11325 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011326
Eric Christopher228232b2010-11-30 07:20:12 +000011327 MI->eraseFromParent(); // The pseudo is gone now.
11328 return BB;
11329}
11330
11331MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011332X86TargetLowering::EmitVAARG64WithCustomInserter(
11333 MachineInstr *MI,
11334 MachineBasicBlock *MBB) const {
11335 // Emit va_arg instruction on X86-64.
11336
11337 // Operands to this pseudo-instruction:
11338 // 0 ) Output : destination address (reg)
11339 // 1-5) Input : va_list address (addr, i64mem)
11340 // 6 ) ArgSize : Size (in bytes) of vararg type
11341 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11342 // 8 ) Align : Alignment of type
11343 // 9 ) EFLAGS (implicit-def)
11344
11345 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11346 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11347
11348 unsigned DestReg = MI->getOperand(0).getReg();
11349 MachineOperand &Base = MI->getOperand(1);
11350 MachineOperand &Scale = MI->getOperand(2);
11351 MachineOperand &Index = MI->getOperand(3);
11352 MachineOperand &Disp = MI->getOperand(4);
11353 MachineOperand &Segment = MI->getOperand(5);
11354 unsigned ArgSize = MI->getOperand(6).getImm();
11355 unsigned ArgMode = MI->getOperand(7).getImm();
11356 unsigned Align = MI->getOperand(8).getImm();
11357
11358 // Memory Reference
11359 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11360 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11361 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11362
11363 // Machine Information
11364 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11365 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11366 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11367 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11368 DebugLoc DL = MI->getDebugLoc();
11369
11370 // struct va_list {
11371 // i32 gp_offset
11372 // i32 fp_offset
11373 // i64 overflow_area (address)
11374 // i64 reg_save_area (address)
11375 // }
11376 // sizeof(va_list) = 24
11377 // alignment(va_list) = 8
11378
11379 unsigned TotalNumIntRegs = 6;
11380 unsigned TotalNumXMMRegs = 8;
11381 bool UseGPOffset = (ArgMode == 1);
11382 bool UseFPOffset = (ArgMode == 2);
11383 unsigned MaxOffset = TotalNumIntRegs * 8 +
11384 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11385
11386 /* Align ArgSize to a multiple of 8 */
11387 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11388 bool NeedsAlign = (Align > 8);
11389
11390 MachineBasicBlock *thisMBB = MBB;
11391 MachineBasicBlock *overflowMBB;
11392 MachineBasicBlock *offsetMBB;
11393 MachineBasicBlock *endMBB;
11394
11395 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11396 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11397 unsigned OffsetReg = 0;
11398
11399 if (!UseGPOffset && !UseFPOffset) {
11400 // If we only pull from the overflow region, we don't create a branch.
11401 // We don't need to alter control flow.
11402 OffsetDestReg = 0; // unused
11403 OverflowDestReg = DestReg;
11404
11405 offsetMBB = NULL;
11406 overflowMBB = thisMBB;
11407 endMBB = thisMBB;
11408 } else {
11409 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11410 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11411 // If not, pull from overflow_area. (branch to overflowMBB)
11412 //
11413 // thisMBB
11414 // | .
11415 // | .
11416 // offsetMBB overflowMBB
11417 // | .
11418 // | .
11419 // endMBB
11420
11421 // Registers for the PHI in endMBB
11422 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11423 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11424
11425 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11426 MachineFunction *MF = MBB->getParent();
11427 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11428 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11429 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11430
11431 MachineFunction::iterator MBBIter = MBB;
11432 ++MBBIter;
11433
11434 // Insert the new basic blocks
11435 MF->insert(MBBIter, offsetMBB);
11436 MF->insert(MBBIter, overflowMBB);
11437 MF->insert(MBBIter, endMBB);
11438
11439 // Transfer the remainder of MBB and its successor edges to endMBB.
11440 endMBB->splice(endMBB->begin(), thisMBB,
11441 llvm::next(MachineBasicBlock::iterator(MI)),
11442 thisMBB->end());
11443 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11444
11445 // Make offsetMBB and overflowMBB successors of thisMBB
11446 thisMBB->addSuccessor(offsetMBB);
11447 thisMBB->addSuccessor(overflowMBB);
11448
11449 // endMBB is a successor of both offsetMBB and overflowMBB
11450 offsetMBB->addSuccessor(endMBB);
11451 overflowMBB->addSuccessor(endMBB);
11452
11453 // Load the offset value into a register
11454 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11455 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11456 .addOperand(Base)
11457 .addOperand(Scale)
11458 .addOperand(Index)
11459 .addDisp(Disp, UseFPOffset ? 4 : 0)
11460 .addOperand(Segment)
11461 .setMemRefs(MMOBegin, MMOEnd);
11462
11463 // Check if there is enough room left to pull this argument.
11464 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11465 .addReg(OffsetReg)
11466 .addImm(MaxOffset + 8 - ArgSizeA8);
11467
11468 // Branch to "overflowMBB" if offset >= max
11469 // Fall through to "offsetMBB" otherwise
11470 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11471 .addMBB(overflowMBB);
11472 }
11473
11474 // In offsetMBB, emit code to use the reg_save_area.
11475 if (offsetMBB) {
11476 assert(OffsetReg != 0);
11477
11478 // Read the reg_save_area address.
11479 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11480 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11481 .addOperand(Base)
11482 .addOperand(Scale)
11483 .addOperand(Index)
11484 .addDisp(Disp, 16)
11485 .addOperand(Segment)
11486 .setMemRefs(MMOBegin, MMOEnd);
11487
11488 // Zero-extend the offset
11489 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11490 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11491 .addImm(0)
11492 .addReg(OffsetReg)
11493 .addImm(X86::sub_32bit);
11494
11495 // Add the offset to the reg_save_area to get the final address.
11496 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11497 .addReg(OffsetReg64)
11498 .addReg(RegSaveReg);
11499
11500 // Compute the offset for the next argument
11501 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11502 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11503 .addReg(OffsetReg)
11504 .addImm(UseFPOffset ? 16 : 8);
11505
11506 // Store it back into the va_list.
11507 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11508 .addOperand(Base)
11509 .addOperand(Scale)
11510 .addOperand(Index)
11511 .addDisp(Disp, UseFPOffset ? 4 : 0)
11512 .addOperand(Segment)
11513 .addReg(NextOffsetReg)
11514 .setMemRefs(MMOBegin, MMOEnd);
11515
11516 // Jump to endMBB
11517 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11518 .addMBB(endMBB);
11519 }
11520
11521 //
11522 // Emit code to use overflow area
11523 //
11524
11525 // Load the overflow_area address into a register.
11526 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11527 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11528 .addOperand(Base)
11529 .addOperand(Scale)
11530 .addOperand(Index)
11531 .addDisp(Disp, 8)
11532 .addOperand(Segment)
11533 .setMemRefs(MMOBegin, MMOEnd);
11534
11535 // If we need to align it, do so. Otherwise, just copy the address
11536 // to OverflowDestReg.
11537 if (NeedsAlign) {
11538 // Align the overflow address
11539 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11540 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11541
11542 // aligned_addr = (addr + (align-1)) & ~(align-1)
11543 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11544 .addReg(OverflowAddrReg)
11545 .addImm(Align-1);
11546
11547 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11548 .addReg(TmpReg)
11549 .addImm(~(uint64_t)(Align-1));
11550 } else {
11551 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11552 .addReg(OverflowAddrReg);
11553 }
11554
11555 // Compute the next overflow address after this argument.
11556 // (the overflow address should be kept 8-byte aligned)
11557 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11558 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11559 .addReg(OverflowDestReg)
11560 .addImm(ArgSizeA8);
11561
11562 // Store the new overflow address.
11563 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11564 .addOperand(Base)
11565 .addOperand(Scale)
11566 .addOperand(Index)
11567 .addDisp(Disp, 8)
11568 .addOperand(Segment)
11569 .addReg(NextAddrReg)
11570 .setMemRefs(MMOBegin, MMOEnd);
11571
11572 // If we branched, emit the PHI to the front of endMBB.
11573 if (offsetMBB) {
11574 BuildMI(*endMBB, endMBB->begin(), DL,
11575 TII->get(X86::PHI), DestReg)
11576 .addReg(OffsetDestReg).addMBB(offsetMBB)
11577 .addReg(OverflowDestReg).addMBB(overflowMBB);
11578 }
11579
11580 // Erase the pseudo instruction
11581 MI->eraseFromParent();
11582
11583 return endMBB;
11584}
11585
11586MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011587X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11588 MachineInstr *MI,
11589 MachineBasicBlock *MBB) const {
11590 // Emit code to save XMM registers to the stack. The ABI says that the
11591 // number of registers to save is given in %al, so it's theoretically
11592 // possible to do an indirect jump trick to avoid saving all of them,
11593 // however this code takes a simpler approach and just executes all
11594 // of the stores if %al is non-zero. It's less code, and it's probably
11595 // easier on the hardware branch predictor, and stores aren't all that
11596 // expensive anyway.
11597
11598 // Create the new basic blocks. One block contains all the XMM stores,
11599 // and one block is the final destination regardless of whether any
11600 // stores were performed.
11601 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11602 MachineFunction *F = MBB->getParent();
11603 MachineFunction::iterator MBBIter = MBB;
11604 ++MBBIter;
11605 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11606 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11607 F->insert(MBBIter, XMMSaveMBB);
11608 F->insert(MBBIter, EndMBB);
11609
Dan Gohman14152b42010-07-06 20:24:04 +000011610 // Transfer the remainder of MBB and its successor edges to EndMBB.
11611 EndMBB->splice(EndMBB->begin(), MBB,
11612 llvm::next(MachineBasicBlock::iterator(MI)),
11613 MBB->end());
11614 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11615
Dan Gohmand6708ea2009-08-15 01:38:56 +000011616 // The original block will now fall through to the XMM save block.
11617 MBB->addSuccessor(XMMSaveMBB);
11618 // The XMMSaveMBB will fall through to the end block.
11619 XMMSaveMBB->addSuccessor(EndMBB);
11620
11621 // Now add the instructions.
11622 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11623 DebugLoc DL = MI->getDebugLoc();
11624
11625 unsigned CountReg = MI->getOperand(0).getReg();
11626 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11627 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11628
11629 if (!Subtarget->isTargetWin64()) {
11630 // If %al is 0, branch around the XMM save block.
11631 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011632 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011633 MBB->addSuccessor(EndMBB);
11634 }
11635
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011636 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011637 // In the XMM save block, save all the XMM argument registers.
11638 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11639 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011640 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011641 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011642 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011643 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011644 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011645 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011646 .addFrameIndex(RegSaveFrameIndex)
11647 .addImm(/*Scale=*/1)
11648 .addReg(/*IndexReg=*/0)
11649 .addImm(/*Disp=*/Offset)
11650 .addReg(/*Segment=*/0)
11651 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011652 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011653 }
11654
Dan Gohman14152b42010-07-06 20:24:04 +000011655 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011656
11657 return EndMBB;
11658}
Mon P Wang63307c32008-05-05 19:05:59 +000011659
Evan Cheng60c07e12006-07-05 22:17:51 +000011660MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011661X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011662 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011663 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11664 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011665
Chris Lattner52600972009-09-02 05:57:00 +000011666 // To "insert" a SELECT_CC instruction, we actually have to insert the
11667 // diamond control-flow pattern. The incoming instruction knows the
11668 // destination vreg to set, the condition code register to branch on, the
11669 // true/false values to select between, and a branch opcode to use.
11670 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11671 MachineFunction::iterator It = BB;
11672 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011673
Chris Lattner52600972009-09-02 05:57:00 +000011674 // thisMBB:
11675 // ...
11676 // TrueVal = ...
11677 // cmpTY ccX, r1, r2
11678 // bCC copy1MBB
11679 // fallthrough --> copy0MBB
11680 MachineBasicBlock *thisMBB = BB;
11681 MachineFunction *F = BB->getParent();
11682 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11683 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011684 F->insert(It, copy0MBB);
11685 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011686
Bill Wendling730c07e2010-06-25 20:48:10 +000011687 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11688 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011689 if (!MI->killsRegister(X86::EFLAGS)) {
11690 copy0MBB->addLiveIn(X86::EFLAGS);
11691 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011692 }
11693
Dan Gohman14152b42010-07-06 20:24:04 +000011694 // Transfer the remainder of BB and its successor edges to sinkMBB.
11695 sinkMBB->splice(sinkMBB->begin(), BB,
11696 llvm::next(MachineBasicBlock::iterator(MI)),
11697 BB->end());
11698 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11699
11700 // Add the true and fallthrough blocks as its successors.
11701 BB->addSuccessor(copy0MBB);
11702 BB->addSuccessor(sinkMBB);
11703
11704 // Create the conditional branch instruction.
11705 unsigned Opc =
11706 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11707 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11708
Chris Lattner52600972009-09-02 05:57:00 +000011709 // copy0MBB:
11710 // %FalseValue = ...
11711 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011712 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011713
Chris Lattner52600972009-09-02 05:57:00 +000011714 // sinkMBB:
11715 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11716 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011717 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11718 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011719 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11720 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11721
Dan Gohman14152b42010-07-06 20:24:04 +000011722 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011723 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011724}
11725
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011726MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011727X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11728 bool Is64Bit) const {
11729 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11730 DebugLoc DL = MI->getDebugLoc();
11731 MachineFunction *MF = BB->getParent();
11732 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11733
11734 assert(EnableSegmentedStacks);
11735
11736 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11737 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11738
11739 // BB:
11740 // ... [Till the alloca]
11741 // If stacklet is not large enough, jump to mallocMBB
11742 //
11743 // bumpMBB:
11744 // Allocate by subtracting from RSP
11745 // Jump to continueMBB
11746 //
11747 // mallocMBB:
11748 // Allocate by call to runtime
11749 //
11750 // continueMBB:
11751 // ...
11752 // [rest of original BB]
11753 //
11754
11755 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11756 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11757 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11758
11759 MachineRegisterInfo &MRI = MF->getRegInfo();
11760 const TargetRegisterClass *AddrRegClass =
11761 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11762
11763 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11764 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11765 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11766 sizeVReg = MI->getOperand(1).getReg(),
11767 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11768
11769 MachineFunction::iterator MBBIter = BB;
11770 ++MBBIter;
11771
11772 MF->insert(MBBIter, bumpMBB);
11773 MF->insert(MBBIter, mallocMBB);
11774 MF->insert(MBBIter, continueMBB);
11775
11776 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11777 (MachineBasicBlock::iterator(MI)), BB->end());
11778 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11779
11780 // Add code to the main basic block to check if the stack limit has been hit,
11781 // and if so, jump to mallocMBB otherwise to bumpMBB.
11782 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11783 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11784 .addReg(tmpSPVReg).addReg(sizeVReg);
11785 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11786 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11787 .addReg(tmpSPVReg);
11788 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11789
11790 // bumpMBB simply decreases the stack pointer, since we know the current
11791 // stacklet has enough space.
11792 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11793 .addReg(tmpSPVReg);
11794 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11795 .addReg(tmpSPVReg);
11796 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11797
11798 // Calls into a routine in libgcc to allocate more space from the heap.
11799 if (Is64Bit) {
11800 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11801 .addReg(sizeVReg);
11802 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11803 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11804 } else {
11805 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11806 .addImm(12);
11807 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11808 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11809 .addExternalSymbol("__morestack_allocate_stack_space");
11810 }
11811
11812 if (!Is64Bit)
11813 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11814 .addImm(16);
11815
11816 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11817 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11818 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11819
11820 // Set up the CFG correctly.
11821 BB->addSuccessor(bumpMBB);
11822 BB->addSuccessor(mallocMBB);
11823 mallocMBB->addSuccessor(continueMBB);
11824 bumpMBB->addSuccessor(continueMBB);
11825
11826 // Take care of the PHI nodes.
11827 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11828 MI->getOperand(0).getReg())
11829 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11830 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11831
11832 // Delete the original pseudo instruction.
11833 MI->eraseFromParent();
11834
11835 // And we're done.
11836 return continueMBB;
11837}
11838
11839MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011840X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011841 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011842 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11843 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011844
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011845 assert(!Subtarget->isTargetEnvMacho());
11846
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011847 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11848 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011849
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011850 if (Subtarget->isTargetWin64()) {
11851 if (Subtarget->isTargetCygMing()) {
11852 // ___chkstk(Mingw64):
11853 // Clobbers R10, R11, RAX and EFLAGS.
11854 // Updates RSP.
11855 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11856 .addExternalSymbol("___chkstk")
11857 .addReg(X86::RAX, RegState::Implicit)
11858 .addReg(X86::RSP, RegState::Implicit)
11859 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11860 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11861 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11862 } else {
11863 // __chkstk(MSVCRT): does not update stack pointer.
11864 // Clobbers R10, R11 and EFLAGS.
11865 // FIXME: RAX(allocated size) might be reused and not killed.
11866 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11867 .addExternalSymbol("__chkstk")
11868 .addReg(X86::RAX, RegState::Implicit)
11869 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11870 // RAX has the offset to subtracted from RSP.
11871 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11872 .addReg(X86::RSP)
11873 .addReg(X86::RAX);
11874 }
11875 } else {
11876 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011877 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11878
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011879 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11880 .addExternalSymbol(StackProbeSymbol)
11881 .addReg(X86::EAX, RegState::Implicit)
11882 .addReg(X86::ESP, RegState::Implicit)
11883 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11884 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11885 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11886 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011887
Dan Gohman14152b42010-07-06 20:24:04 +000011888 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011889 return BB;
11890}
Chris Lattner52600972009-09-02 05:57:00 +000011891
11892MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011893X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11894 MachineBasicBlock *BB) const {
11895 // This is pretty easy. We're taking the value that we received from
11896 // our load from the relocation, sticking it in either RDI (x86-64)
11897 // or EAX and doing an indirect call. The return value will then
11898 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011899 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011900 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011901 DebugLoc DL = MI->getDebugLoc();
11902 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011903
11904 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011905 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011906
Eric Christopher30ef0e52010-06-03 04:07:48 +000011907 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011908 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11909 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011910 .addReg(X86::RIP)
11911 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011912 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011913 MI->getOperand(3).getTargetFlags())
11914 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011915 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011916 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011917 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011918 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11919 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011920 .addReg(0)
11921 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011922 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011923 MI->getOperand(3).getTargetFlags())
11924 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011925 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011926 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011927 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011928 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11929 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011930 .addReg(TII->getGlobalBaseReg(F))
11931 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011932 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011933 MI->getOperand(3).getTargetFlags())
11934 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011935 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011936 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011937 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011938
Dan Gohman14152b42010-07-06 20:24:04 +000011939 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011940 return BB;
11941}
11942
11943MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011944X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011945 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011946 switch (MI->getOpcode()) {
11947 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011948 case X86::TAILJMPd64:
11949 case X86::TAILJMPr64:
11950 case X86::TAILJMPm64:
11951 assert(!"TAILJMP64 would not be touched here.");
11952 case X86::TCRETURNdi64:
11953 case X86::TCRETURNri64:
11954 case X86::TCRETURNmi64:
11955 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11956 // On AMD64, additional defs should be added before register allocation.
11957 if (!Subtarget->isTargetWin64()) {
11958 MI->addRegisterDefined(X86::RSI);
11959 MI->addRegisterDefined(X86::RDI);
11960 MI->addRegisterDefined(X86::XMM6);
11961 MI->addRegisterDefined(X86::XMM7);
11962 MI->addRegisterDefined(X86::XMM8);
11963 MI->addRegisterDefined(X86::XMM9);
11964 MI->addRegisterDefined(X86::XMM10);
11965 MI->addRegisterDefined(X86::XMM11);
11966 MI->addRegisterDefined(X86::XMM12);
11967 MI->addRegisterDefined(X86::XMM13);
11968 MI->addRegisterDefined(X86::XMM14);
11969 MI->addRegisterDefined(X86::XMM15);
11970 }
11971 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011972 case X86::WIN_ALLOCA:
11973 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011974 case X86::SEG_ALLOCA_32:
11975 return EmitLoweredSegAlloca(MI, BB, false);
11976 case X86::SEG_ALLOCA_64:
11977 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011978 case X86::TLSCall_32:
11979 case X86::TLSCall_64:
11980 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011981 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011982 case X86::CMOV_FR32:
11983 case X86::CMOV_FR64:
11984 case X86::CMOV_V4F32:
11985 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011986 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011987 case X86::CMOV_V8F32:
11988 case X86::CMOV_V4F64:
11989 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000011990 case X86::CMOV_GR16:
11991 case X86::CMOV_GR32:
11992 case X86::CMOV_RFP32:
11993 case X86::CMOV_RFP64:
11994 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011995 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011996
Dale Johannesen849f2142007-07-03 00:53:03 +000011997 case X86::FP32_TO_INT16_IN_MEM:
11998 case X86::FP32_TO_INT32_IN_MEM:
11999 case X86::FP32_TO_INT64_IN_MEM:
12000 case X86::FP64_TO_INT16_IN_MEM:
12001 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012002 case X86::FP64_TO_INT64_IN_MEM:
12003 case X86::FP80_TO_INT16_IN_MEM:
12004 case X86::FP80_TO_INT32_IN_MEM:
12005 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012006 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12007 DebugLoc DL = MI->getDebugLoc();
12008
Evan Cheng60c07e12006-07-05 22:17:51 +000012009 // Change the floating point control register to use "round towards zero"
12010 // mode when truncating to an integer value.
12011 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012012 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012013 addFrameReference(BuildMI(*BB, MI, DL,
12014 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012015
12016 // Load the old value of the high byte of the control word...
12017 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012018 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012019 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012020 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012021
12022 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012023 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012024 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012025
12026 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012027 addFrameReference(BuildMI(*BB, MI, DL,
12028 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012029
12030 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012031 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012032 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012033
12034 // Get the X86 opcode to use.
12035 unsigned Opc;
12036 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012037 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012038 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12039 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12040 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12041 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12042 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12043 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012044 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12045 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12046 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012047 }
12048
12049 X86AddressMode AM;
12050 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012051 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012052 AM.BaseType = X86AddressMode::RegBase;
12053 AM.Base.Reg = Op.getReg();
12054 } else {
12055 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012056 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012057 }
12058 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012059 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012060 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012061 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012062 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012063 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012064 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012065 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012066 AM.GV = Op.getGlobal();
12067 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012068 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012069 }
Dan Gohman14152b42010-07-06 20:24:04 +000012070 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012071 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012072
12073 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012074 addFrameReference(BuildMI(*BB, MI, DL,
12075 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012076
Dan Gohman14152b42010-07-06 20:24:04 +000012077 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012078 return BB;
12079 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012080 // String/text processing lowering.
12081 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012082 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012083 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12084 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012085 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012086 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12087 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012088 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012089 return EmitPCMP(MI, BB, 5, false /* in mem */);
12090 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012091 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012092 return EmitPCMP(MI, BB, 5, true /* in mem */);
12093
Eric Christopher228232b2010-11-30 07:20:12 +000012094 // Thread synchronization.
12095 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012096 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012097 case X86::MWAIT:
12098 return EmitMwait(MI, BB);
12099
Eric Christopherb120ab42009-08-18 22:50:32 +000012100 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012101 case X86::ATOMAND32:
12102 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012103 X86::AND32ri, 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);
Mon P Wang63307c32008-05-05 19:05:59 +000012107 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012108 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12109 X86::OR32ri, 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);
Mon P Wang63307c32008-05-05 19:05:59 +000012113 case X86::ATOMXOR32:
12114 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012115 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012116 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012117 X86::NOT32r, X86::EAX,
12118 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012119 case X86::ATOMNAND32:
12120 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012121 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012122 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012123 X86::NOT32r, X86::EAX,
12124 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012125 case X86::ATOMMIN32:
12126 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12127 case X86::ATOMMAX32:
12128 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12129 case X86::ATOMUMIN32:
12130 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12131 case X86::ATOMUMAX32:
12132 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012133
12134 case X86::ATOMAND16:
12135 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12136 X86::AND16ri, 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::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012141 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012142 X86::OR16ri, 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);
12146 case X86::ATOMXOR16:
12147 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12148 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012149 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012150 X86::NOT16r, X86::AX,
12151 X86::GR16RegisterClass);
12152 case X86::ATOMNAND16:
12153 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12154 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012155 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012156 X86::NOT16r, X86::AX,
12157 X86::GR16RegisterClass, true);
12158 case X86::ATOMMIN16:
12159 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12160 case X86::ATOMMAX16:
12161 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12162 case X86::ATOMUMIN16:
12163 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12164 case X86::ATOMUMAX16:
12165 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12166
12167 case X86::ATOMAND8:
12168 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12169 X86::AND8ri, 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::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012174 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012175 X86::OR8ri, 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);
12179 case X86::ATOMXOR8:
12180 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12181 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012182 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012183 X86::NOT8r, X86::AL,
12184 X86::GR8RegisterClass);
12185 case X86::ATOMNAND8:
12186 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12187 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012188 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012189 X86::NOT8r, X86::AL,
12190 X86::GR8RegisterClass, true);
12191 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012192 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012193 case X86::ATOMAND64:
12194 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012195 X86::AND64ri32, 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::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012200 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12201 X86::OR64ri32, 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);
12205 case X86::ATOMXOR64:
12206 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012207 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012208 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012209 X86::NOT64r, X86::RAX,
12210 X86::GR64RegisterClass);
12211 case X86::ATOMNAND64:
12212 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12213 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012214 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012215 X86::NOT64r, X86::RAX,
12216 X86::GR64RegisterClass, true);
12217 case X86::ATOMMIN64:
12218 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12219 case X86::ATOMMAX64:
12220 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12221 case X86::ATOMUMIN64:
12222 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12223 case X86::ATOMUMAX64:
12224 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012225
12226 // This group does 64-bit operations on a 32-bit host.
12227 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012228 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012229 X86::AND32rr, X86::AND32rr,
12230 X86::AND32ri, X86::AND32ri,
12231 false);
12232 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012233 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012234 X86::OR32rr, X86::OR32rr,
12235 X86::OR32ri, X86::OR32ri,
12236 false);
12237 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012238 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012239 X86::XOR32rr, X86::XOR32rr,
12240 X86::XOR32ri, X86::XOR32ri,
12241 false);
12242 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012243 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012244 X86::AND32rr, X86::AND32rr,
12245 X86::AND32ri, X86::AND32ri,
12246 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012247 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012248 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012249 X86::ADD32rr, X86::ADC32rr,
12250 X86::ADD32ri, X86::ADC32ri,
12251 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012252 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012253 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012254 X86::SUB32rr, X86::SBB32rr,
12255 X86::SUB32ri, X86::SBB32ri,
12256 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012257 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012258 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012259 X86::MOV32rr, X86::MOV32rr,
12260 X86::MOV32ri, X86::MOV32ri,
12261 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012262 case X86::VASTART_SAVE_XMM_REGS:
12263 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012264
12265 case X86::VAARG_64:
12266 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012267 }
12268}
12269
12270//===----------------------------------------------------------------------===//
12271// X86 Optimization Hooks
12272//===----------------------------------------------------------------------===//
12273
Dan Gohman475871a2008-07-27 21:46:04 +000012274void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012275 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012276 APInt &KnownZero,
12277 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012278 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012279 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012280 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012281 assert((Opc >= ISD::BUILTIN_OP_END ||
12282 Opc == ISD::INTRINSIC_WO_CHAIN ||
12283 Opc == ISD::INTRINSIC_W_CHAIN ||
12284 Opc == ISD::INTRINSIC_VOID) &&
12285 "Should use MaskedValueIsZero if you don't know whether Op"
12286 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012287
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012288 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012289 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012290 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012291 case X86ISD::ADD:
12292 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012293 case X86ISD::ADC:
12294 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012295 case X86ISD::SMUL:
12296 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012297 case X86ISD::INC:
12298 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012299 case X86ISD::OR:
12300 case X86ISD::XOR:
12301 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012302 // These nodes' second result is a boolean.
12303 if (Op.getResNo() == 0)
12304 break;
12305 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012306 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012307 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12308 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012309 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012310 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012311}
Chris Lattner259e97c2006-01-31 19:43:35 +000012312
Owen Andersonbc146b02010-09-21 20:42:50 +000012313unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12314 unsigned Depth) const {
12315 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12316 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12317 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012318
Owen Andersonbc146b02010-09-21 20:42:50 +000012319 // Fallback case.
12320 return 1;
12321}
12322
Evan Cheng206ee9d2006-07-07 08:33:52 +000012323/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012324/// node is a GlobalAddress + offset.
12325bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012326 const GlobalValue* &GA,
12327 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012328 if (N->getOpcode() == X86ISD::Wrapper) {
12329 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012330 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012331 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012332 return true;
12333 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012334 }
Evan Chengad4196b2008-05-12 19:56:52 +000012335 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012336}
12337
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012338/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12339/// same as extracting the high 128-bit part of 256-bit vector and then
12340/// inserting the result into the low part of a new 256-bit vector
12341static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12342 EVT VT = SVOp->getValueType(0);
12343 int NumElems = VT.getVectorNumElements();
12344
12345 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12346 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12347 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12348 SVOp->getMaskElt(j) >= 0)
12349 return false;
12350
12351 return true;
12352}
12353
12354/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12355/// same as extracting the low 128-bit part of 256-bit vector and then
12356/// inserting the result into the high part of a new 256-bit vector
12357static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12358 EVT VT = SVOp->getValueType(0);
12359 int NumElems = VT.getVectorNumElements();
12360
12361 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12362 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12363 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12364 SVOp->getMaskElt(j) >= 0)
12365 return false;
12366
12367 return true;
12368}
12369
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012370/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12371static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12372 TargetLowering::DAGCombinerInfo &DCI) {
12373 DebugLoc dl = N->getDebugLoc();
12374 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12375 SDValue V1 = SVOp->getOperand(0);
12376 SDValue V2 = SVOp->getOperand(1);
12377 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012378 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012379
12380 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12381 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12382 //
12383 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012384 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012385 // V UNDEF BUILD_VECTOR UNDEF
12386 // \ / \ /
12387 // CONCAT_VECTOR CONCAT_VECTOR
12388 // \ /
12389 // \ /
12390 // RESULT: V + zero extended
12391 //
12392 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12393 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12394 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12395 return SDValue();
12396
12397 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12398 return SDValue();
12399
12400 // To match the shuffle mask, the first half of the mask should
12401 // be exactly the first vector, and all the rest a splat with the
12402 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012403 for (int i = 0; i < NumElems/2; ++i)
12404 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12405 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12406 return SDValue();
12407
12408 // Emit a zeroed vector and insert the desired subvector on its
12409 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012410 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012411 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12412 DAG.getConstant(0, MVT::i32), DAG, dl);
12413 return DCI.CombineTo(N, InsV);
12414 }
12415
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012416 //===--------------------------------------------------------------------===//
12417 // Combine some shuffles into subvector extracts and inserts:
12418 //
12419
12420 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12421 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12422 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12423 DAG, dl);
12424 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12425 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12426 return DCI.CombineTo(N, InsV);
12427 }
12428
12429 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12430 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12431 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12432 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12433 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12434 return DCI.CombineTo(N, InsV);
12435 }
12436
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012437 return SDValue();
12438}
12439
12440/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012441static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012442 TargetLowering::DAGCombinerInfo &DCI,
12443 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012444 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012445 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012446
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012447 // Don't create instructions with illegal types after legalize types has run.
12448 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12449 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12450 return SDValue();
12451
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012452 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12453 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12454 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012455 return PerformShuffleCombine256(N, DAG, DCI);
12456
12457 // Only handle 128 wide vector from here on.
12458 if (VT.getSizeInBits() != 128)
12459 return SDValue();
12460
12461 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12462 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12463 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012464 SmallVector<SDValue, 16> Elts;
12465 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012466 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012467
Nate Begemanfdea31a2010-03-24 20:49:50 +000012468 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012469}
Evan Chengd880b972008-05-09 21:53:03 +000012470
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012471/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12472/// generation and convert it from being a bunch of shuffles and extracts
12473/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012474static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12475 const TargetLowering &TLI) {
12476 SDValue InputVector = N->getOperand(0);
12477
12478 // Only operate on vectors of 4 elements, where the alternative shuffling
12479 // gets to be more expensive.
12480 if (InputVector.getValueType() != MVT::v4i32)
12481 return SDValue();
12482
12483 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12484 // single use which is a sign-extend or zero-extend, and all elements are
12485 // used.
12486 SmallVector<SDNode *, 4> Uses;
12487 unsigned ExtractedElements = 0;
12488 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12489 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12490 if (UI.getUse().getResNo() != InputVector.getResNo())
12491 return SDValue();
12492
12493 SDNode *Extract = *UI;
12494 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12495 return SDValue();
12496
12497 if (Extract->getValueType(0) != MVT::i32)
12498 return SDValue();
12499 if (!Extract->hasOneUse())
12500 return SDValue();
12501 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12502 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12503 return SDValue();
12504 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12505 return SDValue();
12506
12507 // Record which element was extracted.
12508 ExtractedElements |=
12509 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12510
12511 Uses.push_back(Extract);
12512 }
12513
12514 // If not all the elements were used, this may not be worthwhile.
12515 if (ExtractedElements != 15)
12516 return SDValue();
12517
12518 // Ok, we've now decided to do the transformation.
12519 DebugLoc dl = InputVector.getDebugLoc();
12520
12521 // Store the value to a temporary stack slot.
12522 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012523 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12524 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012525
12526 // Replace each use (extract) with a load of the appropriate element.
12527 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12528 UE = Uses.end(); UI != UE; ++UI) {
12529 SDNode *Extract = *UI;
12530
Nadav Rotem86694292011-05-17 08:31:57 +000012531 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012532 SDValue Idx = Extract->getOperand(1);
12533 unsigned EltSize =
12534 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12535 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12536 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12537
Nadav Rotem86694292011-05-17 08:31:57 +000012538 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012539 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012540
12541 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012542 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012543 ScalarAddr, MachinePointerInfo(),
12544 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012545
12546 // Replace the exact with the load.
12547 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12548 }
12549
12550 // The replacement was made in place; don't return anything.
12551 return SDValue();
12552}
12553
Chris Lattner83e6c992006-10-04 06:57:07 +000012554/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012555static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012556 const X86Subtarget *Subtarget) {
12557 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012558 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012559 // Get the LHS/RHS of the select.
12560 SDValue LHS = N->getOperand(1);
12561 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000012562
Dan Gohman670e5392009-09-21 18:03:22 +000012563 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012564 // instructions match the semantics of the common C idiom x<y?x:y but not
12565 // x<=y?x:y, because of how they handle negative zero (which can be
12566 // ignored in unsafe-math mode).
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012567 if (Subtarget->hasXMMInt() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000012568 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000012569 Cond.getOpcode() == ISD::SETCC) {
12570 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012571
Chris Lattner47b4ce82009-03-11 05:48:52 +000012572 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012573 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012574 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12575 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012576 switch (CC) {
12577 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012578 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012579 // Converting this to a min would handle NaNs incorrectly, and swapping
12580 // the operands would cause it to handle comparisons between positive
12581 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012582 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012583 if (!UnsafeFPMath &&
12584 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12585 break;
12586 std::swap(LHS, RHS);
12587 }
Dan Gohman670e5392009-09-21 18:03:22 +000012588 Opcode = X86ISD::FMIN;
12589 break;
12590 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012591 // Converting this to a min would handle comparisons between positive
12592 // and negative zero incorrectly.
12593 if (!UnsafeFPMath &&
12594 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12595 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012596 Opcode = X86ISD::FMIN;
12597 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012598 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012599 // Converting this to a min would handle both negative zeros and NaNs
12600 // incorrectly, but we can swap the operands to fix both.
12601 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012602 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012603 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012604 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012605 Opcode = X86ISD::FMIN;
12606 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012607
Dan Gohman670e5392009-09-21 18:03:22 +000012608 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012609 // Converting this to a max would handle comparisons between positive
12610 // and negative zero incorrectly.
12611 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012612 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012613 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012614 Opcode = X86ISD::FMAX;
12615 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012616 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012617 // Converting this to a max would handle NaNs incorrectly, and swapping
12618 // the operands would cause it to handle comparisons between positive
12619 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012620 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012621 if (!UnsafeFPMath &&
12622 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12623 break;
12624 std::swap(LHS, RHS);
12625 }
Dan Gohman670e5392009-09-21 18:03:22 +000012626 Opcode = X86ISD::FMAX;
12627 break;
12628 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012629 // Converting this to a max would handle both negative zeros and NaNs
12630 // incorrectly, but we can swap the operands to fix both.
12631 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012632 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012633 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012634 case ISD::SETGE:
12635 Opcode = X86ISD::FMAX;
12636 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012637 }
Dan Gohman670e5392009-09-21 18:03:22 +000012638 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012639 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12640 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012641 switch (CC) {
12642 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012643 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012644 // Converting this to a min would handle comparisons between positive
12645 // and negative zero incorrectly, and swapping the operands would
12646 // cause it to handle NaNs incorrectly.
12647 if (!UnsafeFPMath &&
12648 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012649 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012650 break;
12651 std::swap(LHS, RHS);
12652 }
Dan Gohman670e5392009-09-21 18:03:22 +000012653 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012654 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012655 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012656 // Converting this to a min would handle NaNs incorrectly.
12657 if (!UnsafeFPMath &&
12658 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12659 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012660 Opcode = X86ISD::FMIN;
12661 break;
12662 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012663 // Converting this to a min would handle both negative zeros and NaNs
12664 // incorrectly, but we can swap the operands to fix both.
12665 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012666 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012667 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012668 case ISD::SETGE:
12669 Opcode = X86ISD::FMIN;
12670 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012671
Dan Gohman670e5392009-09-21 18:03:22 +000012672 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012673 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012674 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012675 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012676 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012677 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012678 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012679 // Converting this to a max would handle comparisons between positive
12680 // and negative zero incorrectly, and swapping the operands would
12681 // cause it to handle NaNs incorrectly.
12682 if (!UnsafeFPMath &&
12683 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012684 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012685 break;
12686 std::swap(LHS, RHS);
12687 }
Dan Gohman670e5392009-09-21 18:03:22 +000012688 Opcode = X86ISD::FMAX;
12689 break;
12690 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012691 // Converting this to a max would handle both negative zeros and NaNs
12692 // incorrectly, but we can swap the operands to fix both.
12693 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012694 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012695 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012696 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012697 Opcode = X86ISD::FMAX;
12698 break;
12699 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012700 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012701
Chris Lattner47b4ce82009-03-11 05:48:52 +000012702 if (Opcode)
12703 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012704 }
Eric Christopherfd179292009-08-27 18:07:15 +000012705
Chris Lattnerd1980a52009-03-12 06:52:53 +000012706 // If this is a select between two integer constants, try to do some
12707 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012708 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12709 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012710 // Don't do this for crazy integer types.
12711 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12712 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012713 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012714 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012715
Chris Lattnercee56e72009-03-13 05:53:31 +000012716 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012717 // Efficiently invertible.
12718 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12719 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12720 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12721 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012722 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012723 }
Eric Christopherfd179292009-08-27 18:07:15 +000012724
Chris Lattnerd1980a52009-03-12 06:52:53 +000012725 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012726 if (FalseC->getAPIntValue() == 0 &&
12727 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012728 if (NeedsCondInvert) // Invert the condition if needed.
12729 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12730 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012731
Chris Lattnerd1980a52009-03-12 06:52:53 +000012732 // Zero extend the condition if needed.
12733 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012734
Chris Lattnercee56e72009-03-13 05:53:31 +000012735 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012736 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012737 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012738 }
Eric Christopherfd179292009-08-27 18:07:15 +000012739
Chris Lattner97a29a52009-03-13 05:22:11 +000012740 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012741 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012742 if (NeedsCondInvert) // Invert the condition if needed.
12743 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12744 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012745
Chris Lattner97a29a52009-03-13 05:22:11 +000012746 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012747 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12748 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012749 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012750 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012751 }
Eric Christopherfd179292009-08-27 18:07:15 +000012752
Chris Lattnercee56e72009-03-13 05:53:31 +000012753 // Optimize cases that will turn into an LEA instruction. This requires
12754 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012755 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012756 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012757 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012758
Chris Lattnercee56e72009-03-13 05:53:31 +000012759 bool isFastMultiplier = false;
12760 if (Diff < 10) {
12761 switch ((unsigned char)Diff) {
12762 default: break;
12763 case 1: // result = add base, cond
12764 case 2: // result = lea base( , cond*2)
12765 case 3: // result = lea base(cond, cond*2)
12766 case 4: // result = lea base( , cond*4)
12767 case 5: // result = lea base(cond, cond*4)
12768 case 8: // result = lea base( , cond*8)
12769 case 9: // result = lea base(cond, cond*8)
12770 isFastMultiplier = true;
12771 break;
12772 }
12773 }
Eric Christopherfd179292009-08-27 18:07:15 +000012774
Chris Lattnercee56e72009-03-13 05:53:31 +000012775 if (isFastMultiplier) {
12776 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12777 if (NeedsCondInvert) // Invert the condition if needed.
12778 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12779 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012780
Chris Lattnercee56e72009-03-13 05:53:31 +000012781 // Zero extend the condition if needed.
12782 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12783 Cond);
12784 // Scale the condition by the difference.
12785 if (Diff != 1)
12786 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12787 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012788
Chris Lattnercee56e72009-03-13 05:53:31 +000012789 // Add the base if non-zero.
12790 if (FalseC->getAPIntValue() != 0)
12791 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12792 SDValue(FalseC, 0));
12793 return Cond;
12794 }
Eric Christopherfd179292009-08-27 18:07:15 +000012795 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012796 }
12797 }
Eric Christopherfd179292009-08-27 18:07:15 +000012798
Dan Gohman475871a2008-07-27 21:46:04 +000012799 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012800}
12801
Chris Lattnerd1980a52009-03-12 06:52:53 +000012802/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12803static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12804 TargetLowering::DAGCombinerInfo &DCI) {
12805 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012806
Chris Lattnerd1980a52009-03-12 06:52:53 +000012807 // If the flag operand isn't dead, don't touch this CMOV.
12808 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12809 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012810
Evan Chengb5a55d92011-05-24 01:48:22 +000012811 SDValue FalseOp = N->getOperand(0);
12812 SDValue TrueOp = N->getOperand(1);
12813 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12814 SDValue Cond = N->getOperand(3);
12815 if (CC == X86::COND_E || CC == X86::COND_NE) {
12816 switch (Cond.getOpcode()) {
12817 default: break;
12818 case X86ISD::BSR:
12819 case X86ISD::BSF:
12820 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12821 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12822 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12823 }
12824 }
12825
Chris Lattnerd1980a52009-03-12 06:52:53 +000012826 // If this is a select between two integer constants, try to do some
12827 // optimizations. Note that the operands are ordered the opposite of SELECT
12828 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012829 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12830 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012831 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12832 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012833 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12834 CC = X86::GetOppositeBranchCondition(CC);
12835 std::swap(TrueC, FalseC);
12836 }
Eric Christopherfd179292009-08-27 18:07:15 +000012837
Chris Lattnerd1980a52009-03-12 06:52:53 +000012838 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012839 // This is efficient for any integer data type (including i8/i16) and
12840 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012841 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012842 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12843 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012844
Chris Lattnerd1980a52009-03-12 06:52:53 +000012845 // Zero extend the condition if needed.
12846 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012847
Chris Lattnerd1980a52009-03-12 06:52:53 +000012848 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12849 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012850 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012851 if (N->getNumValues() == 2) // Dead flag value?
12852 return DCI.CombineTo(N, Cond, SDValue());
12853 return Cond;
12854 }
Eric Christopherfd179292009-08-27 18:07:15 +000012855
Chris Lattnercee56e72009-03-13 05:53:31 +000012856 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12857 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012858 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012859 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12860 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012861
Chris Lattner97a29a52009-03-13 05:22:11 +000012862 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012863 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12864 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012865 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12866 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012867
Chris Lattner97a29a52009-03-13 05:22:11 +000012868 if (N->getNumValues() == 2) // Dead flag value?
12869 return DCI.CombineTo(N, Cond, SDValue());
12870 return Cond;
12871 }
Eric Christopherfd179292009-08-27 18:07:15 +000012872
Chris Lattnercee56e72009-03-13 05:53:31 +000012873 // Optimize cases that will turn into an LEA instruction. This requires
12874 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012875 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012876 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012877 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012878
Chris Lattnercee56e72009-03-13 05:53:31 +000012879 bool isFastMultiplier = false;
12880 if (Diff < 10) {
12881 switch ((unsigned char)Diff) {
12882 default: break;
12883 case 1: // result = add base, cond
12884 case 2: // result = lea base( , cond*2)
12885 case 3: // result = lea base(cond, cond*2)
12886 case 4: // result = lea base( , cond*4)
12887 case 5: // result = lea base(cond, cond*4)
12888 case 8: // result = lea base( , cond*8)
12889 case 9: // result = lea base(cond, cond*8)
12890 isFastMultiplier = true;
12891 break;
12892 }
12893 }
Eric Christopherfd179292009-08-27 18:07:15 +000012894
Chris Lattnercee56e72009-03-13 05:53:31 +000012895 if (isFastMultiplier) {
12896 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012897 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12898 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012899 // Zero extend the condition if needed.
12900 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12901 Cond);
12902 // Scale the condition by the difference.
12903 if (Diff != 1)
12904 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12905 DAG.getConstant(Diff, Cond.getValueType()));
12906
12907 // Add the base if non-zero.
12908 if (FalseC->getAPIntValue() != 0)
12909 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12910 SDValue(FalseC, 0));
12911 if (N->getNumValues() == 2) // Dead flag value?
12912 return DCI.CombineTo(N, Cond, SDValue());
12913 return Cond;
12914 }
Eric Christopherfd179292009-08-27 18:07:15 +000012915 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012916 }
12917 }
12918 return SDValue();
12919}
12920
12921
Evan Cheng0b0cd912009-03-28 05:57:29 +000012922/// PerformMulCombine - Optimize a single multiply with constant into two
12923/// in order to implement it with two cheaper instructions, e.g.
12924/// LEA + SHL, LEA + LEA.
12925static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12926 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012927 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12928 return SDValue();
12929
Owen Andersone50ed302009-08-10 22:56:29 +000012930 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012931 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012932 return SDValue();
12933
12934 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12935 if (!C)
12936 return SDValue();
12937 uint64_t MulAmt = C->getZExtValue();
12938 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12939 return SDValue();
12940
12941 uint64_t MulAmt1 = 0;
12942 uint64_t MulAmt2 = 0;
12943 if ((MulAmt % 9) == 0) {
12944 MulAmt1 = 9;
12945 MulAmt2 = MulAmt / 9;
12946 } else if ((MulAmt % 5) == 0) {
12947 MulAmt1 = 5;
12948 MulAmt2 = MulAmt / 5;
12949 } else if ((MulAmt % 3) == 0) {
12950 MulAmt1 = 3;
12951 MulAmt2 = MulAmt / 3;
12952 }
12953 if (MulAmt2 &&
12954 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12955 DebugLoc DL = N->getDebugLoc();
12956
12957 if (isPowerOf2_64(MulAmt2) &&
12958 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12959 // If second multiplifer is pow2, issue it first. We want the multiply by
12960 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12961 // is an add.
12962 std::swap(MulAmt1, MulAmt2);
12963
12964 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012965 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012966 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012967 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012968 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012969 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012970 DAG.getConstant(MulAmt1, VT));
12971
Eric Christopherfd179292009-08-27 18:07:15 +000012972 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012973 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000012974 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000012975 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012976 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000012977 DAG.getConstant(MulAmt2, VT));
12978
12979 // Do not add new nodes to DAG combiner worklist.
12980 DCI.CombineTo(N, NewMul, false);
12981 }
12982 return SDValue();
12983}
12984
Evan Chengad9c0a32009-12-15 00:53:42 +000012985static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
12986 SDValue N0 = N->getOperand(0);
12987 SDValue N1 = N->getOperand(1);
12988 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
12989 EVT VT = N0.getValueType();
12990
12991 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
12992 // since the result of setcc_c is all zero's or all ones.
12993 if (N1C && N0.getOpcode() == ISD::AND &&
12994 N0.getOperand(1).getOpcode() == ISD::Constant) {
12995 SDValue N00 = N0.getOperand(0);
12996 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
12997 ((N00.getOpcode() == ISD::ANY_EXTEND ||
12998 N00.getOpcode() == ISD::ZERO_EXTEND) &&
12999 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13000 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13001 APInt ShAmt = N1C->getAPIntValue();
13002 Mask = Mask.shl(ShAmt);
13003 if (Mask != 0)
13004 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13005 N00, DAG.getConstant(Mask, VT));
13006 }
13007 }
13008
13009 return SDValue();
13010}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013011
Nate Begeman740ab032009-01-26 00:52:55 +000013012/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13013/// when possible.
13014static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13015 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013016 EVT VT = N->getValueType(0);
13017 if (!VT.isVector() && VT.isInteger() &&
13018 N->getOpcode() == ISD::SHL)
13019 return PerformSHLCombine(N, DAG);
13020
Nate Begeman740ab032009-01-26 00:52:55 +000013021 // On X86 with SSE2 support, we can transform this to a vector shift if
13022 // all elements are shifted by the same amount. We can't do this in legalize
13023 // because the a constant vector is typically transformed to a constant pool
13024 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013025 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013026 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013027
Owen Anderson825b72b2009-08-11 20:47:22 +000013028 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013029 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013030
Mon P Wang3becd092009-01-28 08:12:05 +000013031 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013032 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013033 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013034 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013035 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13036 unsigned NumElts = VT.getVectorNumElements();
13037 unsigned i = 0;
13038 for (; i != NumElts; ++i) {
13039 SDValue Arg = ShAmtOp.getOperand(i);
13040 if (Arg.getOpcode() == ISD::UNDEF) continue;
13041 BaseShAmt = Arg;
13042 break;
13043 }
13044 for (; i != NumElts; ++i) {
13045 SDValue Arg = ShAmtOp.getOperand(i);
13046 if (Arg.getOpcode() == ISD::UNDEF) continue;
13047 if (Arg != BaseShAmt) {
13048 return SDValue();
13049 }
13050 }
13051 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013052 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013053 SDValue InVec = ShAmtOp.getOperand(0);
13054 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13055 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13056 unsigned i = 0;
13057 for (; i != NumElts; ++i) {
13058 SDValue Arg = InVec.getOperand(i);
13059 if (Arg.getOpcode() == ISD::UNDEF) continue;
13060 BaseShAmt = Arg;
13061 break;
13062 }
13063 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13064 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013065 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013066 if (C->getZExtValue() == SplatIdx)
13067 BaseShAmt = InVec.getOperand(1);
13068 }
13069 }
13070 if (BaseShAmt.getNode() == 0)
13071 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13072 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013073 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013074 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013075
Mon P Wangefa42202009-09-03 19:56:25 +000013076 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013077 if (EltVT.bitsGT(MVT::i32))
13078 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13079 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013080 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013081
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013082 // The shift amount is identical so we can do a vector shift.
13083 SDValue ValOp = N->getOperand(0);
13084 switch (N->getOpcode()) {
13085 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013086 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013087 break;
13088 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013089 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013090 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013091 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013092 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013093 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013094 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013095 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013096 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013097 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013098 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013099 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013100 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013101 break;
13102 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013103 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013104 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013105 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013106 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013107 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013108 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013109 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013110 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013111 break;
13112 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013113 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013114 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013115 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013116 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013117 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013118 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013119 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013120 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013121 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013122 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013123 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013124 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013125 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013126 }
13127 return SDValue();
13128}
13129
Nate Begemanb65c1752010-12-17 22:55:37 +000013130
Stuart Hastings865f0932011-06-03 23:53:54 +000013131// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13132// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13133// and friends. Likewise for OR -> CMPNEQSS.
13134static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13135 TargetLowering::DAGCombinerInfo &DCI,
13136 const X86Subtarget *Subtarget) {
13137 unsigned opcode;
13138
13139 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13140 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013141 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013142 SDValue N0 = N->getOperand(0);
13143 SDValue N1 = N->getOperand(1);
13144 SDValue CMP0 = N0->getOperand(1);
13145 SDValue CMP1 = N1->getOperand(1);
13146 DebugLoc DL = N->getDebugLoc();
13147
13148 // The SETCCs should both refer to the same CMP.
13149 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13150 return SDValue();
13151
13152 SDValue CMP00 = CMP0->getOperand(0);
13153 SDValue CMP01 = CMP0->getOperand(1);
13154 EVT VT = CMP00.getValueType();
13155
13156 if (VT == MVT::f32 || VT == MVT::f64) {
13157 bool ExpectingFlags = false;
13158 // Check for any users that want flags:
13159 for (SDNode::use_iterator UI = N->use_begin(),
13160 UE = N->use_end();
13161 !ExpectingFlags && UI != UE; ++UI)
13162 switch (UI->getOpcode()) {
13163 default:
13164 case ISD::BR_CC:
13165 case ISD::BRCOND:
13166 case ISD::SELECT:
13167 ExpectingFlags = true;
13168 break;
13169 case ISD::CopyToReg:
13170 case ISD::SIGN_EXTEND:
13171 case ISD::ZERO_EXTEND:
13172 case ISD::ANY_EXTEND:
13173 break;
13174 }
13175
13176 if (!ExpectingFlags) {
13177 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13178 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13179
13180 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13181 X86::CondCode tmp = cc0;
13182 cc0 = cc1;
13183 cc1 = tmp;
13184 }
13185
13186 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13187 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13188 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13189 X86ISD::NodeType NTOperator = is64BitFP ?
13190 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13191 // FIXME: need symbolic constants for these magic numbers.
13192 // See X86ATTInstPrinter.cpp:printSSECC().
13193 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13194 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13195 DAG.getConstant(x86cc, MVT::i8));
13196 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13197 OnesOrZeroesF);
13198 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13199 DAG.getConstant(1, MVT::i32));
13200 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13201 return OneBitOfTruth;
13202 }
13203 }
13204 }
13205 }
13206 return SDValue();
13207}
13208
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013209/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13210/// so it can be folded inside ANDNP.
13211static bool CanFoldXORWithAllOnes(const SDNode *N) {
13212 EVT VT = N->getValueType(0);
13213
13214 // Match direct AllOnes for 128 and 256-bit vectors
13215 if (ISD::isBuildVectorAllOnes(N))
13216 return true;
13217
13218 // Look through a bit convert.
13219 if (N->getOpcode() == ISD::BITCAST)
13220 N = N->getOperand(0).getNode();
13221
13222 // Sometimes the operand may come from a insert_subvector building a 256-bit
13223 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013224 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013225 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13226 SDValue V1 = N->getOperand(0);
13227 SDValue V2 = N->getOperand(1);
13228
13229 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13230 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13231 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13232 ISD::isBuildVectorAllOnes(V2.getNode()))
13233 return true;
13234 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013235
13236 return false;
13237}
13238
Nate Begemanb65c1752010-12-17 22:55:37 +000013239static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13240 TargetLowering::DAGCombinerInfo &DCI,
13241 const X86Subtarget *Subtarget) {
13242 if (DCI.isBeforeLegalizeOps())
13243 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013244
Stuart Hastings865f0932011-06-03 23:53:54 +000013245 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13246 if (R.getNode())
13247 return R;
13248
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013249 // Want to form ANDNP nodes:
13250 // 1) In the hopes of then easily combining them with OR and AND nodes
13251 // to form PBLEND/PSIGN.
13252 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013253 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013254 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013255 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013256
Nate Begemanb65c1752010-12-17 22:55:37 +000013257 SDValue N0 = N->getOperand(0);
13258 SDValue N1 = N->getOperand(1);
13259 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013260
Nate Begemanb65c1752010-12-17 22:55:37 +000013261 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013262 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013263 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13264 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013265 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013266
13267 // Check RHS for vnot
13268 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013269 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13270 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013271 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013272
Nate Begemanb65c1752010-12-17 22:55:37 +000013273 return SDValue();
13274}
13275
Evan Cheng760d1942010-01-04 21:22:48 +000013276static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013277 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013278 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013279 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013280 return SDValue();
13281
Stuart Hastings865f0932011-06-03 23:53:54 +000013282 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13283 if (R.getNode())
13284 return R;
13285
Evan Cheng760d1942010-01-04 21:22:48 +000013286 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013287 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013288 return SDValue();
13289
Evan Cheng760d1942010-01-04 21:22:48 +000013290 SDValue N0 = N->getOperand(0);
13291 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013292
Nate Begemanb65c1752010-12-17 22:55:37 +000013293 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013294 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013295 if (VT == MVT::v2i64) {
13296 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013297 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013298 std::swap(N0, N1);
13299 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013300 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013301 SDValue Mask = N1.getOperand(0);
13302 SDValue X = N1.getOperand(1);
13303 SDValue Y;
13304 if (N0.getOperand(0) == Mask)
13305 Y = N0.getOperand(1);
13306 if (N0.getOperand(1) == Mask)
13307 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013308
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013309 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013310 if (!Y.getNode())
13311 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013312
Nate Begemanb65c1752010-12-17 22:55:37 +000013313 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13314 if (Mask.getOpcode() != ISD::BITCAST ||
13315 X.getOpcode() != ISD::BITCAST ||
13316 Y.getOpcode() != ISD::BITCAST)
13317 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013318
Nate Begemanb65c1752010-12-17 22:55:37 +000013319 // Look through mask bitcast.
13320 Mask = Mask.getOperand(0);
13321 EVT MaskVT = Mask.getValueType();
13322
13323 // Validate that the Mask operand is a vector sra node. The sra node
13324 // will be an intrinsic.
13325 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13326 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013327
Nate Begemanb65c1752010-12-17 22:55:37 +000013328 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13329 // there is no psrai.b
13330 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13331 case Intrinsic::x86_sse2_psrai_w:
13332 case Intrinsic::x86_sse2_psrai_d:
13333 break;
13334 default: return SDValue();
13335 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013336
Nate Begemanb65c1752010-12-17 22:55:37 +000013337 // Check that the SRA is all signbits.
13338 SDValue SraC = Mask.getOperand(2);
13339 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13340 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13341 if ((SraAmt + 1) != EltBits)
13342 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013343
Nate Begemanb65c1752010-12-17 22:55:37 +000013344 DebugLoc DL = N->getDebugLoc();
13345
13346 // Now we know we at least have a plendvb with the mask val. See if
13347 // we can form a psignb/w/d.
13348 // psign = x.type == y.type == mask.type && y = sub(0, x);
13349 X = X.getOperand(0);
13350 Y = Y.getOperand(0);
13351 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13352 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13353 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13354 unsigned Opc = 0;
13355 switch (EltBits) {
13356 case 8: Opc = X86ISD::PSIGNB; break;
13357 case 16: Opc = X86ISD::PSIGNW; break;
13358 case 32: Opc = X86ISD::PSIGND; break;
13359 default: break;
13360 }
13361 if (Opc) {
13362 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13363 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13364 }
13365 }
13366 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013367 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013368 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013369
Nate Begemanb65c1752010-12-17 22:55:37 +000013370 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13371 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13372 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013373 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013374 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13375 }
13376 }
13377 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013378
Nate Begemanb65c1752010-12-17 22:55:37 +000013379 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013380 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13381 std::swap(N0, N1);
13382 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13383 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013384 if (!N0.hasOneUse() || !N1.hasOneUse())
13385 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013386
13387 SDValue ShAmt0 = N0.getOperand(1);
13388 if (ShAmt0.getValueType() != MVT::i8)
13389 return SDValue();
13390 SDValue ShAmt1 = N1.getOperand(1);
13391 if (ShAmt1.getValueType() != MVT::i8)
13392 return SDValue();
13393 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13394 ShAmt0 = ShAmt0.getOperand(0);
13395 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13396 ShAmt1 = ShAmt1.getOperand(0);
13397
13398 DebugLoc DL = N->getDebugLoc();
13399 unsigned Opc = X86ISD::SHLD;
13400 SDValue Op0 = N0.getOperand(0);
13401 SDValue Op1 = N1.getOperand(0);
13402 if (ShAmt0.getOpcode() == ISD::SUB) {
13403 Opc = X86ISD::SHRD;
13404 std::swap(Op0, Op1);
13405 std::swap(ShAmt0, ShAmt1);
13406 }
13407
Evan Cheng8b1190a2010-04-28 01:18:01 +000013408 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013409 if (ShAmt1.getOpcode() == ISD::SUB) {
13410 SDValue Sum = ShAmt1.getOperand(0);
13411 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013412 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13413 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13414 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13415 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013416 return DAG.getNode(Opc, DL, VT,
13417 Op0, Op1,
13418 DAG.getNode(ISD::TRUNCATE, DL,
13419 MVT::i8, ShAmt0));
13420 }
13421 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13422 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13423 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013424 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013425 return DAG.getNode(Opc, DL, VT,
13426 N0.getOperand(0), N1.getOperand(0),
13427 DAG.getNode(ISD::TRUNCATE, DL,
13428 MVT::i8, ShAmt0));
13429 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013430
Evan Cheng760d1942010-01-04 21:22:48 +000013431 return SDValue();
13432}
13433
Chris Lattner149a4e52008-02-22 02:09:43 +000013434/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013435static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013436 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013437 StoreSDNode *St = cast<StoreSDNode>(N);
13438 EVT VT = St->getValue().getValueType();
13439 EVT StVT = St->getMemoryVT();
13440 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013441 SDValue StoredVal = St->getOperand(1);
13442 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13443
13444 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013445 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13446 // 128-bit ones. If in the future the cost becomes only one memory access the
13447 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013448 if (VT.getSizeInBits() == 256 &&
13449 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13450 StoredVal.getNumOperands() == 2) {
13451
13452 SDValue Value0 = StoredVal.getOperand(0);
13453 SDValue Value1 = StoredVal.getOperand(1);
13454
13455 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13456 SDValue Ptr0 = St->getBasePtr();
13457 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13458
13459 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13460 St->getPointerInfo(), St->isVolatile(),
13461 St->isNonTemporal(), St->getAlignment());
13462 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13463 St->getPointerInfo(), St->isVolatile(),
13464 St->isNonTemporal(), St->getAlignment());
13465 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13466 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013467
13468 // Optimize trunc store (of multiple scalars) to shuffle and store.
13469 // First, pack all of the elements in one place. Next, store to memory
13470 // in fewer chunks.
13471 if (St->isTruncatingStore() && VT.isVector()) {
13472 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13473 unsigned NumElems = VT.getVectorNumElements();
13474 assert(StVT != VT && "Cannot truncate to the same type");
13475 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13476 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13477
13478 // From, To sizes and ElemCount must be pow of two
13479 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
13480 // We are going to use the original vector elt for storing.
13481 // accumulated smaller vector elements must be a multiple of bigger size.
13482 if (0 != (NumElems * ToSz) % FromSz) return SDValue();
13483 unsigned SizeRatio = FromSz / ToSz;
13484
13485 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13486
13487 // Create a type on which we perform the shuffle
13488 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13489 StVT.getScalarType(), NumElems*SizeRatio);
13490
13491 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13492
13493 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13494 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13495 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13496
13497 // Can't shuffle using an illegal type
13498 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13499
13500 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13501 DAG.getUNDEF(WideVec.getValueType()),
13502 ShuffleVec.data());
13503 // At this point all of the data is stored at the bottom of the
13504 // register. We now need to save it to mem.
13505
13506 // Find the largest store unit
13507 MVT StoreType = MVT::i8;
13508 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13509 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13510 MVT Tp = (MVT::SimpleValueType)tp;
13511 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13512 StoreType = Tp;
13513 }
13514
13515 // Bitcast the original vector into a vector of store-size units
13516 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13517 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13518 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13519 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13520 SmallVector<SDValue, 8> Chains;
13521 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13522 TLI.getPointerTy());
13523 SDValue Ptr = St->getBasePtr();
13524
13525 // Perform one or more big stores into memory.
13526 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13527 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13528 StoreType, ShuffWide,
13529 DAG.getIntPtrConstant(i));
13530 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13531 St->getPointerInfo(), St->isVolatile(),
13532 St->isNonTemporal(), St->getAlignment());
13533 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13534 Chains.push_back(Ch);
13535 }
13536
13537 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13538 Chains.size());
13539 }
13540
13541
Chris Lattner149a4e52008-02-22 02:09:43 +000013542 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13543 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013544 // A preferable solution to the general problem is to figure out the right
13545 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013546
13547 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013548 if (VT.getSizeInBits() != 64)
13549 return SDValue();
13550
Devang Patel578efa92009-06-05 21:57:13 +000013551 const Function *F = DAG.getMachineFunction().getFunction();
13552 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013553 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013554 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000013555 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013556 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013557 isa<LoadSDNode>(St->getValue()) &&
13558 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13559 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013560 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013561 LoadSDNode *Ld = 0;
13562 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013563 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013564 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013565 // Must be a store of a load. We currently handle two cases: the load
13566 // is a direct child, and it's under an intervening TokenFactor. It is
13567 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013568 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013569 Ld = cast<LoadSDNode>(St->getChain());
13570 else if (St->getValue().hasOneUse() &&
13571 ChainVal->getOpcode() == ISD::TokenFactor) {
13572 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013573 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013574 TokenFactorIndex = i;
13575 Ld = cast<LoadSDNode>(St->getValue());
13576 } else
13577 Ops.push_back(ChainVal->getOperand(i));
13578 }
13579 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013580
Evan Cheng536e6672009-03-12 05:59:15 +000013581 if (!Ld || !ISD::isNormalLoad(Ld))
13582 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013583
Evan Cheng536e6672009-03-12 05:59:15 +000013584 // If this is not the MMX case, i.e. we are just turning i64 load/store
13585 // into f64 load/store, avoid the transformation if there are multiple
13586 // uses of the loaded value.
13587 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13588 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013589
Evan Cheng536e6672009-03-12 05:59:15 +000013590 DebugLoc LdDL = Ld->getDebugLoc();
13591 DebugLoc StDL = N->getDebugLoc();
13592 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13593 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13594 // pair instead.
13595 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013596 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013597 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13598 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013599 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013600 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013601 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013602 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013603 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013604 Ops.size());
13605 }
Evan Cheng536e6672009-03-12 05:59:15 +000013606 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013607 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013608 St->isVolatile(), St->isNonTemporal(),
13609 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013610 }
Evan Cheng536e6672009-03-12 05:59:15 +000013611
13612 // Otherwise, lower to two pairs of 32-bit loads / stores.
13613 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013614 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13615 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013616
Owen Anderson825b72b2009-08-11 20:47:22 +000013617 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013618 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013619 Ld->isVolatile(), Ld->isNonTemporal(),
13620 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013621 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013622 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013623 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013624 MinAlign(Ld->getAlignment(), 4));
13625
13626 SDValue NewChain = LoLd.getValue(1);
13627 if (TokenFactorIndex != -1) {
13628 Ops.push_back(LoLd);
13629 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013630 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013631 Ops.size());
13632 }
13633
13634 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013635 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13636 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013637
13638 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013639 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013640 St->isVolatile(), St->isNonTemporal(),
13641 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013642 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013643 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013644 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013645 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013646 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013647 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013648 }
Dan Gohman475871a2008-07-27 21:46:04 +000013649 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013650}
13651
Chris Lattner6cf73262008-01-25 06:14:17 +000013652/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13653/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013654static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013655 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13656 // F[X]OR(0.0, x) -> x
13657 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013658 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13659 if (C->getValueAPF().isPosZero())
13660 return N->getOperand(1);
13661 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13662 if (C->getValueAPF().isPosZero())
13663 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013664 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013665}
13666
13667/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013668static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013669 // FAND(0.0, x) -> 0.0
13670 // FAND(x, 0.0) -> 0.0
13671 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13672 if (C->getValueAPF().isPosZero())
13673 return N->getOperand(0);
13674 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13675 if (C->getValueAPF().isPosZero())
13676 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013677 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013678}
13679
Dan Gohmane5af2d32009-01-29 01:59:02 +000013680static SDValue PerformBTCombine(SDNode *N,
13681 SelectionDAG &DAG,
13682 TargetLowering::DAGCombinerInfo &DCI) {
13683 // BT ignores high bits in the bit index operand.
13684 SDValue Op1 = N->getOperand(1);
13685 if (Op1.hasOneUse()) {
13686 unsigned BitWidth = Op1.getValueSizeInBits();
13687 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13688 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013689 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13690 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013691 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013692 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13693 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13694 DCI.CommitTargetLoweringOpt(TLO);
13695 }
13696 return SDValue();
13697}
Chris Lattner83e6c992006-10-04 06:57:07 +000013698
Eli Friedman7a5e5552009-06-07 06:52:44 +000013699static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13700 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013701 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013702 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013703 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013704 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013705 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013706 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013707 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013708 }
13709 return SDValue();
13710}
13711
Evan Cheng2e489c42009-12-16 00:53:11 +000013712static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13713 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13714 // (and (i32 x86isd::setcc_carry), 1)
13715 // This eliminates the zext. This transformation is necessary because
13716 // ISD::SETCC is always legalized to i8.
13717 DebugLoc dl = N->getDebugLoc();
13718 SDValue N0 = N->getOperand(0);
13719 EVT VT = N->getValueType(0);
13720 if (N0.getOpcode() == ISD::AND &&
13721 N0.hasOneUse() &&
13722 N0.getOperand(0).hasOneUse()) {
13723 SDValue N00 = N0.getOperand(0);
13724 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13725 return SDValue();
13726 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13727 if (!C || C->getZExtValue() != 1)
13728 return SDValue();
13729 return DAG.getNode(ISD::AND, dl, VT,
13730 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13731 N00.getOperand(0), N00.getOperand(1)),
13732 DAG.getConstant(1, VT));
13733 }
13734
13735 return SDValue();
13736}
13737
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013738// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13739static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13740 unsigned X86CC = N->getConstantOperandVal(0);
13741 SDValue EFLAG = N->getOperand(1);
13742 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013743
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013744 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
13745 // a zext and produces an all-ones bit which is more useful than 0/1 in some
13746 // cases.
13747 if (X86CC == X86::COND_B)
13748 return DAG.getNode(ISD::AND, DL, MVT::i8,
13749 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
13750 DAG.getConstant(X86CC, MVT::i8), EFLAG),
13751 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013752
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013753 return SDValue();
13754}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013755
Benjamin Kramer1396c402011-06-18 11:09:41 +000013756static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
13757 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013758 SDValue Op0 = N->getOperand(0);
13759 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
13760 // a 32-bit target where SSE doesn't support i64->FP operations.
13761 if (Op0.getOpcode() == ISD::LOAD) {
13762 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
13763 EVT VT = Ld->getValueType(0);
13764 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
13765 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
13766 !XTLI->getSubtarget()->is64Bit() &&
13767 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000013768 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
13769 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013770 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
13771 return FILDChain;
13772 }
13773 }
13774 return SDValue();
13775}
13776
Chris Lattner23a01992010-12-20 01:37:09 +000013777// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
13778static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
13779 X86TargetLowering::DAGCombinerInfo &DCI) {
13780 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
13781 // the result is either zero or one (depending on the input carry bit).
13782 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
13783 if (X86::isZeroNode(N->getOperand(0)) &&
13784 X86::isZeroNode(N->getOperand(1)) &&
13785 // We don't have a good way to replace an EFLAGS use, so only do this when
13786 // dead right now.
13787 SDValue(N, 1).use_empty()) {
13788 DebugLoc DL = N->getDebugLoc();
13789 EVT VT = N->getValueType(0);
13790 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
13791 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
13792 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
13793 DAG.getConstant(X86::COND_B,MVT::i8),
13794 N->getOperand(2)),
13795 DAG.getConstant(1, VT));
13796 return DCI.CombineTo(N, Res1, CarryOut);
13797 }
13798
13799 return SDValue();
13800}
13801
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013802// fold (add Y, (sete X, 0)) -> adc 0, Y
13803// (add Y, (setne X, 0)) -> sbb -1, Y
13804// (sub (sete X, 0), Y) -> sbb 0, Y
13805// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013806static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013807 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013808
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013809 // Look through ZExts.
13810 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
13811 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
13812 return SDValue();
13813
13814 SDValue SetCC = Ext.getOperand(0);
13815 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
13816 return SDValue();
13817
13818 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
13819 if (CC != X86::COND_E && CC != X86::COND_NE)
13820 return SDValue();
13821
13822 SDValue Cmp = SetCC.getOperand(1);
13823 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000013824 !X86::isZeroNode(Cmp.getOperand(1)) ||
13825 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013826 return SDValue();
13827
13828 SDValue CmpOp0 = Cmp.getOperand(0);
13829 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
13830 DAG.getConstant(1, CmpOp0.getValueType()));
13831
13832 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
13833 if (CC == X86::COND_NE)
13834 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
13835 DL, OtherVal.getValueType(), OtherVal,
13836 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
13837 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
13838 DL, OtherVal.getValueType(), OtherVal,
13839 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
13840}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013841
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013842static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
13843 SDValue Op0 = N->getOperand(0);
13844 SDValue Op1 = N->getOperand(1);
13845
13846 // X86 can't encode an immediate LHS of a sub. See if we can push the
13847 // negation into a preceding instruction.
13848 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013849 // If the RHS of the sub is a XOR with one use and a constant, invert the
13850 // immediate. Then add one to the LHS of the sub so we can turn
13851 // X-Y -> X+~Y+1, saving one register.
13852 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
13853 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000013854 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013855 EVT VT = Op0.getValueType();
13856 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
13857 Op1.getOperand(0),
13858 DAG.getConstant(~XorC, VT));
13859 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000013860 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013861 }
13862 }
13863
13864 return OptimizeConditionalInDecrement(N, DAG);
13865}
13866
Dan Gohman475871a2008-07-27 21:46:04 +000013867SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000013868 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013869 SelectionDAG &DAG = DCI.DAG;
13870 switch (N->getOpcode()) {
13871 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013872 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013873 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000013874 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013875 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013876 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
13877 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000013878 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000013879 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000013880 case ISD::SHL:
13881 case ISD::SRA:
13882 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000013883 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000013884 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000013885 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013886 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000013887 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000013888 case X86ISD::FOR: return PerformFORCombine(N, DAG);
13889 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000013890 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013891 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000013892 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013893 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013894 case X86ISD::SHUFPS: // Handle all target specific shuffles
13895 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000013896 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013897 case X86ISD::PUNPCKHBW:
13898 case X86ISD::PUNPCKHWD:
13899 case X86ISD::PUNPCKHDQ:
13900 case X86ISD::PUNPCKHQDQ:
13901 case X86ISD::UNPCKHPS:
13902 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000013903 case X86ISD::VUNPCKHPSY:
13904 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013905 case X86ISD::PUNPCKLBW:
13906 case X86ISD::PUNPCKLWD:
13907 case X86ISD::PUNPCKLDQ:
13908 case X86ISD::PUNPCKLQDQ:
13909 case X86ISD::UNPCKLPS:
13910 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000013911 case X86ISD::VUNPCKLPSY:
13912 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013913 case X86ISD::MOVHLPS:
13914 case X86ISD::MOVLHPS:
13915 case X86ISD::PSHUFD:
13916 case X86ISD::PSHUFHW:
13917 case X86ISD::PSHUFLW:
13918 case X86ISD::MOVSS:
13919 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000013920 case X86ISD::VPERMILPS:
13921 case X86ISD::VPERMILPSY:
13922 case X86ISD::VPERMILPD:
13923 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000013924 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013925 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013926 }
13927
Dan Gohman475871a2008-07-27 21:46:04 +000013928 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013929}
13930
Evan Chenge5b51ac2010-04-17 06:13:15 +000013931/// isTypeDesirableForOp - Return true if the target has native support for
13932/// the specified value type and it is 'desirable' to use the type for the
13933/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
13934/// instruction encodings are longer and some i16 instructions are slow.
13935bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
13936 if (!isTypeLegal(VT))
13937 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013938 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000013939 return true;
13940
13941 switch (Opc) {
13942 default:
13943 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000013944 case ISD::LOAD:
13945 case ISD::SIGN_EXTEND:
13946 case ISD::ZERO_EXTEND:
13947 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013948 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013949 case ISD::SRL:
13950 case ISD::SUB:
13951 case ISD::ADD:
13952 case ISD::MUL:
13953 case ISD::AND:
13954 case ISD::OR:
13955 case ISD::XOR:
13956 return false;
13957 }
13958}
13959
13960/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000013961/// beneficial for dag combiner to promote the specified node. If true, it
13962/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000013963bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013964 EVT VT = Op.getValueType();
13965 if (VT != MVT::i16)
13966 return false;
13967
Evan Cheng4c26e932010-04-19 19:29:22 +000013968 bool Promote = false;
13969 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013970 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000013971 default: break;
13972 case ISD::LOAD: {
13973 LoadSDNode *LD = cast<LoadSDNode>(Op);
13974 // If the non-extending load has a single use and it's not live out, then it
13975 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013976 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
13977 Op.hasOneUse()*/) {
13978 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
13979 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
13980 // The only case where we'd want to promote LOAD (rather then it being
13981 // promoted as an operand is when it's only use is liveout.
13982 if (UI->getOpcode() != ISD::CopyToReg)
13983 return false;
13984 }
13985 }
Evan Cheng4c26e932010-04-19 19:29:22 +000013986 Promote = true;
13987 break;
13988 }
13989 case ISD::SIGN_EXTEND:
13990 case ISD::ZERO_EXTEND:
13991 case ISD::ANY_EXTEND:
13992 Promote = true;
13993 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013994 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013995 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000013996 SDValue N0 = Op.getOperand(0);
13997 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000013998 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000013999 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014000 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014001 break;
14002 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014003 case ISD::ADD:
14004 case ISD::MUL:
14005 case ISD::AND:
14006 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014007 case ISD::XOR:
14008 Commute = true;
14009 // fallthrough
14010 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014011 SDValue N0 = Op.getOperand(0);
14012 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014013 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014014 return false;
14015 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014016 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014017 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014018 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014019 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014020 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014021 }
14022 }
14023
14024 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014025 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014026}
14027
Evan Cheng60c07e12006-07-05 22:17:51 +000014028//===----------------------------------------------------------------------===//
14029// X86 Inline Assembly Support
14030//===----------------------------------------------------------------------===//
14031
Chris Lattnerb8105652009-07-20 17:51:36 +000014032bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14033 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014034
14035 std::string AsmStr = IA->getAsmString();
14036
14037 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014038 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014039 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014040
14041 switch (AsmPieces.size()) {
14042 default: return false;
14043 case 1:
14044 AsmStr = AsmPieces[0];
14045 AsmPieces.clear();
14046 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14047
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014048 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014049 // we will turn this bswap into something that will be lowered to logical ops
14050 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14051 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014052 // bswap $0
14053 if (AsmPieces.size() == 2 &&
14054 (AsmPieces[0] == "bswap" ||
14055 AsmPieces[0] == "bswapq" ||
14056 AsmPieces[0] == "bswapl") &&
14057 (AsmPieces[1] == "$0" ||
14058 AsmPieces[1] == "${0:q}")) {
14059 // No need to check constraints, nothing other than the equivalent of
14060 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014061 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014062 if (!Ty || Ty->getBitWidth() % 16 != 0)
14063 return false;
14064 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014065 }
14066 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014067 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014068 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014069 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014070 AsmPieces[1] == "$$8," &&
14071 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014072 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14073 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014074 const std::string &ConstraintsStr = IA->getConstraintString();
14075 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014076 std::sort(AsmPieces.begin(), AsmPieces.end());
14077 if (AsmPieces.size() == 4 &&
14078 AsmPieces[0] == "~{cc}" &&
14079 AsmPieces[1] == "~{dirflag}" &&
14080 AsmPieces[2] == "~{flags}" &&
14081 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014082 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014083 if (!Ty || Ty->getBitWidth() % 16 != 0)
14084 return false;
14085 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014086 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014087 }
14088 break;
14089 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014090 if (CI->getType()->isIntegerTy(32) &&
14091 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14092 SmallVector<StringRef, 4> Words;
14093 SplitString(AsmPieces[0], Words, " \t,");
14094 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14095 Words[2] == "${0:w}") {
14096 Words.clear();
14097 SplitString(AsmPieces[1], Words, " \t,");
14098 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14099 Words[2] == "$0") {
14100 Words.clear();
14101 SplitString(AsmPieces[2], Words, " \t,");
14102 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14103 Words[2] == "${0:w}") {
14104 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014105 const std::string &ConstraintsStr = IA->getConstraintString();
14106 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014107 std::sort(AsmPieces.begin(), AsmPieces.end());
14108 if (AsmPieces.size() == 4 &&
14109 AsmPieces[0] == "~{cc}" &&
14110 AsmPieces[1] == "~{dirflag}" &&
14111 AsmPieces[2] == "~{flags}" &&
14112 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014113 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014114 if (!Ty || Ty->getBitWidth() % 16 != 0)
14115 return false;
14116 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014117 }
14118 }
14119 }
14120 }
14121 }
Evan Cheng55d42002011-01-08 01:24:27 +000014122
14123 if (CI->getType()->isIntegerTy(64)) {
14124 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14125 if (Constraints.size() >= 2 &&
14126 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14127 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14128 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14129 SmallVector<StringRef, 4> Words;
14130 SplitString(AsmPieces[0], Words, " \t");
14131 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014132 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014133 SplitString(AsmPieces[1], Words, " \t");
14134 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14135 Words.clear();
14136 SplitString(AsmPieces[2], Words, " \t,");
14137 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14138 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014139 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014140 if (!Ty || Ty->getBitWidth() % 16 != 0)
14141 return false;
14142 return IntrinsicLowering::LowerToByteSwap(CI);
14143 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014144 }
14145 }
14146 }
14147 }
14148 break;
14149 }
14150 return false;
14151}
14152
14153
14154
Chris Lattnerf4dff842006-07-11 02:54:03 +000014155/// getConstraintType - Given a constraint letter, return the type of
14156/// constraint it is for this target.
14157X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014158X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14159 if (Constraint.size() == 1) {
14160 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014161 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014162 case 'q':
14163 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014164 case 'f':
14165 case 't':
14166 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014167 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014168 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014169 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014170 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014171 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014172 case 'a':
14173 case 'b':
14174 case 'c':
14175 case 'd':
14176 case 'S':
14177 case 'D':
14178 case 'A':
14179 return C_Register;
14180 case 'I':
14181 case 'J':
14182 case 'K':
14183 case 'L':
14184 case 'M':
14185 case 'N':
14186 case 'G':
14187 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014188 case 'e':
14189 case 'Z':
14190 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014191 default:
14192 break;
14193 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014194 }
Chris Lattner4234f572007-03-25 02:14:49 +000014195 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014196}
14197
John Thompson44ab89e2010-10-29 17:29:13 +000014198/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014199/// This object must already have been set up with the operand type
14200/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014201TargetLowering::ConstraintWeight
14202 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014203 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014204 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014205 Value *CallOperandVal = info.CallOperandVal;
14206 // If we don't have a value, we can't do a match,
14207 // but allow it at the lowest weight.
14208 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014209 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014210 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014211 // Look at the constraint type.
14212 switch (*constraint) {
14213 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014214 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14215 case 'R':
14216 case 'q':
14217 case 'Q':
14218 case 'a':
14219 case 'b':
14220 case 'c':
14221 case 'd':
14222 case 'S':
14223 case 'D':
14224 case 'A':
14225 if (CallOperandVal->getType()->isIntegerTy())
14226 weight = CW_SpecificReg;
14227 break;
14228 case 'f':
14229 case 't':
14230 case 'u':
14231 if (type->isFloatingPointTy())
14232 weight = CW_SpecificReg;
14233 break;
14234 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014235 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014236 weight = CW_SpecificReg;
14237 break;
14238 case 'x':
14239 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014240 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014241 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014242 break;
14243 case 'I':
14244 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14245 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014246 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014247 }
14248 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014249 case 'J':
14250 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14251 if (C->getZExtValue() <= 63)
14252 weight = CW_Constant;
14253 }
14254 break;
14255 case 'K':
14256 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14257 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14258 weight = CW_Constant;
14259 }
14260 break;
14261 case 'L':
14262 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14263 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14264 weight = CW_Constant;
14265 }
14266 break;
14267 case 'M':
14268 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14269 if (C->getZExtValue() <= 3)
14270 weight = CW_Constant;
14271 }
14272 break;
14273 case 'N':
14274 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14275 if (C->getZExtValue() <= 0xff)
14276 weight = CW_Constant;
14277 }
14278 break;
14279 case 'G':
14280 case 'C':
14281 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14282 weight = CW_Constant;
14283 }
14284 break;
14285 case 'e':
14286 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14287 if ((C->getSExtValue() >= -0x80000000LL) &&
14288 (C->getSExtValue() <= 0x7fffffffLL))
14289 weight = CW_Constant;
14290 }
14291 break;
14292 case 'Z':
14293 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14294 if (C->getZExtValue() <= 0xffffffff)
14295 weight = CW_Constant;
14296 }
14297 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014298 }
14299 return weight;
14300}
14301
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014302/// LowerXConstraint - try to replace an X constraint, which matches anything,
14303/// with another that has more specific requirements based on the type of the
14304/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014305const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014306LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014307 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14308 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014309 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014310 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014311 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014312 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014313 return "x";
14314 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014315
Chris Lattner5e764232008-04-26 23:02:14 +000014316 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014317}
14318
Chris Lattner48884cd2007-08-25 00:47:38 +000014319/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14320/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014321void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014322 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014323 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014324 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014325 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014326
Eric Christopher100c8332011-06-02 23:16:42 +000014327 // Only support length 1 constraints for now.
14328 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014329
Eric Christopher100c8332011-06-02 23:16:42 +000014330 char ConstraintLetter = Constraint[0];
14331 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014332 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014333 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014335 if (C->getZExtValue() <= 31) {
14336 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014337 break;
14338 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014339 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014340 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014341 case 'J':
14342 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014343 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014344 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14345 break;
14346 }
14347 }
14348 return;
14349 case 'K':
14350 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014351 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014352 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14353 break;
14354 }
14355 }
14356 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014357 case 'N':
14358 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014359 if (C->getZExtValue() <= 255) {
14360 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014361 break;
14362 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014363 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014364 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014365 case 'e': {
14366 // 32-bit signed value
14367 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014368 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14369 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014370 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014371 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014372 break;
14373 }
14374 // FIXME gcc accepts some relocatable values here too, but only in certain
14375 // memory models; it's complicated.
14376 }
14377 return;
14378 }
14379 case 'Z': {
14380 // 32-bit unsigned value
14381 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014382 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14383 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014384 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14385 break;
14386 }
14387 }
14388 // FIXME gcc accepts some relocatable values here too, but only in certain
14389 // memory models; it's complicated.
14390 return;
14391 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014392 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014393 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014394 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014395 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014396 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014397 break;
14398 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014399
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014400 // In any sort of PIC mode addresses need to be computed at runtime by
14401 // adding in a register or some sort of table lookup. These can't
14402 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014403 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014404 return;
14405
Chris Lattnerdc43a882007-05-03 16:52:29 +000014406 // If we are in non-pic codegen mode, we allow the address of a global (with
14407 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014408 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014409 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014410
Chris Lattner49921962009-05-08 18:23:14 +000014411 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14412 while (1) {
14413 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14414 Offset += GA->getOffset();
14415 break;
14416 } else if (Op.getOpcode() == ISD::ADD) {
14417 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14418 Offset += C->getZExtValue();
14419 Op = Op.getOperand(0);
14420 continue;
14421 }
14422 } else if (Op.getOpcode() == ISD::SUB) {
14423 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14424 Offset += -C->getZExtValue();
14425 Op = Op.getOperand(0);
14426 continue;
14427 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014428 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014429
Chris Lattner49921962009-05-08 18:23:14 +000014430 // Otherwise, this isn't something we can handle, reject it.
14431 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014432 }
Eric Christopherfd179292009-08-27 18:07:15 +000014433
Dan Gohman46510a72010-04-15 01:51:59 +000014434 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014435 // If we require an extra load to get this address, as in PIC mode, we
14436 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014437 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14438 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014439 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014440
Devang Patel0d881da2010-07-06 22:08:15 +000014441 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14442 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014443 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014444 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014445 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014446
Gabor Greifba36cb52008-08-28 21:40:38 +000014447 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014448 Ops.push_back(Result);
14449 return;
14450 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014451 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014452}
14453
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014454std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014455X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014456 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014457 // First, see if this is a constraint that directly corresponds to an LLVM
14458 // register class.
14459 if (Constraint.size() == 1) {
14460 // GCC Constraint Letters
14461 switch (Constraint[0]) {
14462 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014463 // TODO: Slight differences here in allocation order and leaving
14464 // RIP in the class. Do they matter any more here than they do
14465 // in the normal allocation?
14466 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14467 if (Subtarget->is64Bit()) {
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::GR32RegisterClass);
14470 else if (VT == MVT::i16)
14471 return std::make_pair(0U, X86::GR16RegisterClass);
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::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014474 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014475 return std::make_pair(0U, X86::GR64RegisterClass);
14476 break;
14477 }
14478 // 32-bit fallthrough
14479 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014480 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014481 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14482 else if (VT == MVT::i16)
14483 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014484 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014485 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14486 else if (VT == MVT::i64)
14487 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14488 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014489 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014490 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014491 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014492 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014493 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014494 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014495 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014496 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014497 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014498 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014499 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014500 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14501 if (VT == MVT::i16)
14502 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14503 if (VT == MVT::i32 || !Subtarget->is64Bit())
14504 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14505 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014506 case 'f': // FP Stack registers.
14507 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14508 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014509 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014510 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014511 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014512 return std::make_pair(0U, X86::RFP64RegisterClass);
14513 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014514 case 'y': // MMX_REGS if MMX allowed.
14515 if (!Subtarget->hasMMX()) break;
14516 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014517 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014518 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014519 // FALL THROUGH.
14520 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014521 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014522
Owen Anderson825b72b2009-08-11 20:47:22 +000014523 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014524 default: break;
14525 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014526 case MVT::f32:
14527 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014528 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014529 case MVT::f64:
14530 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014531 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014532 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014533 case MVT::v16i8:
14534 case MVT::v8i16:
14535 case MVT::v4i32:
14536 case MVT::v2i64:
14537 case MVT::v4f32:
14538 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014539 return std::make_pair(0U, X86::VR128RegisterClass);
14540 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014541 break;
14542 }
14543 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014544
Chris Lattnerf76d1802006-07-31 23:26:50 +000014545 // Use the default implementation in TargetLowering to convert the register
14546 // constraint into a member of a register class.
14547 std::pair<unsigned, const TargetRegisterClass*> Res;
14548 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014549
14550 // Not found as a standard register?
14551 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014552 // Map st(0) -> st(7) -> ST0
14553 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14554 tolower(Constraint[1]) == 's' &&
14555 tolower(Constraint[2]) == 't' &&
14556 Constraint[3] == '(' &&
14557 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14558 Constraint[5] == ')' &&
14559 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014560
Chris Lattner56d77c72009-09-13 22:41:48 +000014561 Res.first = X86::ST0+Constraint[4]-'0';
14562 Res.second = X86::RFP80RegisterClass;
14563 return Res;
14564 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014565
Chris Lattner56d77c72009-09-13 22:41:48 +000014566 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014567 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014568 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014569 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014570 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014571 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014572
14573 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014574 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014575 Res.first = X86::EFLAGS;
14576 Res.second = X86::CCRRegisterClass;
14577 return Res;
14578 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014579
Dale Johannesen330169f2008-11-13 21:52:36 +000014580 // 'A' means EAX + EDX.
14581 if (Constraint == "A") {
14582 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014583 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014584 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014585 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014586 return Res;
14587 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014588
Chris Lattnerf76d1802006-07-31 23:26:50 +000014589 // Otherwise, check to see if this is a register class of the wrong value
14590 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14591 // turn into {ax},{dx}.
14592 if (Res.second->hasType(VT))
14593 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014594
Chris Lattnerf76d1802006-07-31 23:26:50 +000014595 // All of the single-register GCC register classes map their values onto
14596 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14597 // really want an 8-bit or 32-bit register, map to the appropriate register
14598 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014599 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014600 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014601 unsigned DestReg = 0;
14602 switch (Res.first) {
14603 default: break;
14604 case X86::AX: DestReg = X86::AL; break;
14605 case X86::DX: DestReg = X86::DL; break;
14606 case X86::CX: DestReg = X86::CL; break;
14607 case X86::BX: DestReg = X86::BL; break;
14608 }
14609 if (DestReg) {
14610 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014611 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014612 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014613 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014614 unsigned DestReg = 0;
14615 switch (Res.first) {
14616 default: break;
14617 case X86::AX: DestReg = X86::EAX; break;
14618 case X86::DX: DestReg = X86::EDX; break;
14619 case X86::CX: DestReg = X86::ECX; break;
14620 case X86::BX: DestReg = X86::EBX; break;
14621 case X86::SI: DestReg = X86::ESI; break;
14622 case X86::DI: DestReg = X86::EDI; break;
14623 case X86::BP: DestReg = X86::EBP; break;
14624 case X86::SP: DestReg = X86::ESP; break;
14625 }
14626 if (DestReg) {
14627 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014628 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014629 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014630 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014631 unsigned DestReg = 0;
14632 switch (Res.first) {
14633 default: break;
14634 case X86::AX: DestReg = X86::RAX; break;
14635 case X86::DX: DestReg = X86::RDX; break;
14636 case X86::CX: DestReg = X86::RCX; break;
14637 case X86::BX: DestReg = X86::RBX; break;
14638 case X86::SI: DestReg = X86::RSI; break;
14639 case X86::DI: DestReg = X86::RDI; break;
14640 case X86::BP: DestReg = X86::RBP; break;
14641 case X86::SP: DestReg = X86::RSP; break;
14642 }
14643 if (DestReg) {
14644 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014645 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014646 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014647 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014648 } else if (Res.second == X86::FR32RegisterClass ||
14649 Res.second == X86::FR64RegisterClass ||
14650 Res.second == X86::VR128RegisterClass) {
14651 // Handle references to XMM physical registers that got mapped into the
14652 // wrong class. This can happen with constraints like {xmm0} where the
14653 // target independent register mapper will just pick the first match it can
14654 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014655 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014656 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014657 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014658 Res.second = X86::FR64RegisterClass;
14659 else if (X86::VR128RegisterClass->hasType(VT))
14660 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014661 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014662
Chris Lattnerf76d1802006-07-31 23:26:50 +000014663 return Res;
14664}