blob: 0d454149301cbe03bfe08f1e745d1ff819cfe851 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopesaed890b2011-08-01 21:54:05 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt() || Subtarget->hasAVX();
173 X86ScalarSSEf32 = Subtarget->hasXMM() || Subtarget->hasAVX();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000184
Eric Christopherde5e1012011-03-11 01:05:58 +0000185 // For 64-bit since we have so many registers use the ILP scheduler, for
186 // 32-bit code use the register pressure specific scheduling.
187 if (Subtarget->is64Bit())
188 setSchedulingPreference(Sched::ILP);
189 else
190 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000191 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000192
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000193 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000194 // Setup Windows compiler runtime calls.
195 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000196 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000197 setLibcallName(RTLIB::SREM_I64, "_allrem");
198 setLibcallName(RTLIB::UREM_I64, "_aullrem");
199 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000200 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000201 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000202 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000203 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000204 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
205 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
206 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000207 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
208 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000209 }
210
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000211 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000212 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 setUseUnderscoreSetJmp(false);
214 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000215 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000216 // MS runtime is weird: it exports _setjmp, but longjmp!
217 setUseUnderscoreSetJmp(true);
218 setUseUnderscoreLongJmp(false);
219 } else {
220 setUseUnderscoreSetJmp(true);
221 setUseUnderscoreLongJmp(true);
222 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000223
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000224 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000226 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000228 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000230
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000232
Scott Michelfdc40a02009-02-17 22:15:04 +0000233 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000235 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
239 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000240
241 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
243 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
244 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000248
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000249 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
250 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
252 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
253 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000254
Evan Cheng25ab6902006-09-08 06:48:29 +0000255 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
257 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000258 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000259 // We have an algorithm for SSE2->double, and we turn this into a
260 // 64-bit FILD followed by conditional FADD for other targets.
261 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000262 // We have an algorithm for SSE2, and we turn this into a 64-bit
263 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000264 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000265 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000266
267 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
268 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
270 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000271
Devang Patel6a784892009-06-05 18:48:29 +0000272 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000273 // SSE has no i16 to fp conversion, only i32
274 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000276 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
280 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000281 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000282 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
284 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000285 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000286
Dale Johannesen73328d12007-09-19 23:55:34 +0000287 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
288 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
290 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000291
Evan Cheng02568ff2006-01-30 22:13:22 +0000292 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
293 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
295 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000296
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000297 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000299 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000301 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
303 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000304 }
305
306 // Handle FP_TO_UINT by promoting the destination to a larger signed
307 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
309 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
310 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000311
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
314 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000315 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000316 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 // Expand FP_TO_UINT into a select.
318 // FIXME: We would like to use a Custom expander here eventually to do
319 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000322 // With SSE3 we can use fisttpll to convert to a signed i64; without
323 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000325 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000326
Chris Lattner399610a2006-12-05 18:22:22 +0000327 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000328 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000329 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
330 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000331 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000333 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000335 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000336 }
Chris Lattner21f66852005-12-23 05:15:23 +0000337
Dan Gohmanb00ee212008-02-18 19:34:53 +0000338 // Scalar integer divide and remainder are lowered to use operations that
339 // produce two results, to match the available instructions. This exposes
340 // the two-result form to trivial CSE, which is able to combine x/y and x%y
341 // into a single instruction.
342 //
343 // Scalar integer multiply-high is also lowered to use two-result
344 // operations, to match the available instructions. However, plain multiply
345 // (low) operations are left as Legal, as there are single-result
346 // instructions for this in x86. Using the two-result multiply instructions
347 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000348 for (unsigned i = 0, e = 4; i != e; ++i) {
349 MVT VT = IntVTs[i];
350 setOperationAction(ISD::MULHS, VT, Expand);
351 setOperationAction(ISD::MULHU, VT, Expand);
352 setOperationAction(ISD::SDIV, VT, Expand);
353 setOperationAction(ISD::UDIV, VT, Expand);
354 setOperationAction(ISD::SREM, VT, Expand);
355 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000356
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000357 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000358 setOperationAction(ISD::ADDC, VT, Custom);
359 setOperationAction(ISD::ADDE, VT, Custom);
360 setOperationAction(ISD::SUBC, VT, Custom);
361 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000362 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000363
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
365 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
366 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
367 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000368 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
370 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
373 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
374 setOperationAction(ISD::FREM , MVT::f32 , Expand);
375 setOperationAction(ISD::FREM , MVT::f64 , Expand);
376 setOperationAction(ISD::FREM , MVT::f80 , Expand);
377 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000378
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
380 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000381 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
382 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
384 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000385 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
387 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000388 }
389
Benjamin Kramer1292c222010-12-04 20:32:23 +0000390 if (Subtarget->hasPOPCNT()) {
391 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
392 } else {
393 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
394 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
395 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
396 if (Subtarget->is64Bit())
397 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
398 }
399
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
401 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000402
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000403 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000404 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000405 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000406 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000407 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
409 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
410 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
411 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
412 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000413 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
415 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
416 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
417 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000418 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000420 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000423
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000424 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
426 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
427 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
428 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000429 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
431 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000432 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000433 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
435 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
436 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
437 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000438 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000439 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000440 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
442 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
443 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000444 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
446 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
447 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000448 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000449
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000450 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000452
Eric Christopher9a9d2752010-07-22 02:48:34 +0000453 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000454 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000455
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000456 // On X86 and X86-64, atomic operations are lowered to locked instructions.
457 // Locked instructions, in turn, have implicit fence semantics (all memory
458 // operations are flushed before issuing the locked instruction, and they
459 // are not buffered), so we can fold away the common pattern of
460 // fence-atomic-fence.
461 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000462
Mon P Wang63307c32008-05-05 19:05:59 +0000463 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000464 for (unsigned i = 0, e = 4; i != e; ++i) {
465 MVT VT = IntVTs[i];
466 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
467 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000468 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000469 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000470
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000471 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000472 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
474 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
475 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
476 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
479 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000480 }
481
Eli Friedman43f51ae2011-08-26 21:21:21 +0000482 if (Subtarget->hasCmpxchg16b()) {
483 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
484 }
485
Evan Cheng3c992d22006-03-07 02:02:57 +0000486 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000487 if (!Subtarget->isTargetDarwin() &&
488 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000489 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000491 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000492
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
494 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
495 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
496 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000497 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000498 setExceptionPointerRegister(X86::RAX);
499 setExceptionSelectorRegister(X86::RDX);
500 } else {
501 setExceptionPointerRegister(X86::EAX);
502 setExceptionSelectorRegister(X86::EDX);
503 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
505 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000506
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000508
Owen Anderson825b72b2009-08-11 20:47:22 +0000509 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000510
Nate Begemanacc398c2006-01-25 18:21:52 +0000511 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::VASTART , MVT::Other, Custom);
513 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000514 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::VAARG , MVT::Other, Custom);
516 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000517 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::VAARG , MVT::Other, Expand);
519 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000520 }
Evan Chengae642192007-03-02 23:16:35 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
523 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000524
525 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
526 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
527 MVT::i64 : MVT::i32, Custom);
528 else if (EnableSegmentedStacks)
529 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
530 MVT::i64 : MVT::i32, Custom);
531 else
532 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
533 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000534
Evan Chengc7ce29b2009-02-13 22:36:38 +0000535 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000536 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000537 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
539 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000540
Evan Cheng223547a2006-01-31 22:28:30 +0000541 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::FABS , MVT::f64, Custom);
543 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000544
545 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::FNEG , MVT::f64, Custom);
547 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000548
Evan Cheng68c47cb2007-01-05 07:55:56 +0000549 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
551 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000552
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000553 // Lower this to FGETSIGNx86 plus an AND.
554 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
555 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
556
Evan Chengd25e9e82006-02-02 00:28:23 +0000557 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::FSIN , MVT::f64, Expand);
559 setOperationAction(ISD::FCOS , MVT::f64, Expand);
560 setOperationAction(ISD::FSIN , MVT::f32, Expand);
561 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000562
Chris Lattnera54aa942006-01-29 06:26:08 +0000563 // Expand FP immediates into loads from the stack, except for the special
564 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000565 addLegalFPImmediate(APFloat(+0.0)); // xorpd
566 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000567 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000568 // Use SSE for f32, x87 for f64.
569 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
571 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000572
573 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000575
576 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000580
581 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
583 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FSIN , MVT::f32, Expand);
587 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
Nate Begemane1795842008-02-14 08:57:00 +0000589 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000590 addLegalFPImmediate(APFloat(+0.0f)); // xorps
591 addLegalFPImmediate(APFloat(+0.0)); // FLD0
592 addLegalFPImmediate(APFloat(+1.0)); // FLD1
593 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
594 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
595
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
598 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000600 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000601 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000602 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
604 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000605
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
607 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
608 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
609 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000610
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000611 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
613 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000615 addLegalFPImmediate(APFloat(+0.0)); // FLD0
616 addLegalFPImmediate(APFloat(+1.0)); // FLD1
617 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
618 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
620 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
621 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
622 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000623 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000624
Cameron Zwarich33390842011-07-08 21:39:21 +0000625 // We don't support FMA.
626 setOperationAction(ISD::FMA, MVT::f64, Expand);
627 setOperationAction(ISD::FMA, MVT::f32, Expand);
628
Dale Johannesen59a58732007-08-05 18:49:15 +0000629 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000630 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
632 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000634 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000635 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000636 addLegalFPImmediate(TmpFlt); // FLD0
637 TmpFlt.changeSign();
638 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000639
640 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000641 APFloat TmpFlt2(+1.0);
642 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
643 &ignored);
644 addLegalFPImmediate(TmpFlt2); // FLD1
645 TmpFlt2.changeSign();
646 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
647 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000648
Evan Chengc7ce29b2009-02-13 22:36:38 +0000649 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
651 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000653
654 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000655 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000656
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000657 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
659 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
660 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FLOG, MVT::f80, Expand);
663 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
664 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
665 setOperationAction(ISD::FEXP, MVT::f80, Expand);
666 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000667
Mon P Wangf007a8b2008-11-06 05:31:54 +0000668 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000669 // (for widening) or expand (for scalarization). Then we will selectively
670 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
672 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
673 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
674 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
675 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
676 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
677 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000689 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
690 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000722 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000723 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
727 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
728 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
729 setTruncStoreAction((MVT::SimpleValueType)VT,
730 (MVT::SimpleValueType)InnerVT, Expand);
731 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
732 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
733 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000734 }
735
Evan Chengc7ce29b2009-02-13 22:36:38 +0000736 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
737 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000738 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000739 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000740 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000741 }
742
Dale Johannesen0488fb62010-09-30 23:57:10 +0000743 // MMX-sized vectors (other than x86mmx) are expected to be expanded
744 // into smaller operations.
745 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
746 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
747 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
748 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
749 setOperationAction(ISD::AND, MVT::v8i8, Expand);
750 setOperationAction(ISD::AND, MVT::v4i16, Expand);
751 setOperationAction(ISD::AND, MVT::v2i32, Expand);
752 setOperationAction(ISD::AND, MVT::v1i64, Expand);
753 setOperationAction(ISD::OR, MVT::v8i8, Expand);
754 setOperationAction(ISD::OR, MVT::v4i16, Expand);
755 setOperationAction(ISD::OR, MVT::v2i32, Expand);
756 setOperationAction(ISD::OR, MVT::v1i64, Expand);
757 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
758 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
759 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
760 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
761 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
762 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
763 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
764 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
765 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
766 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
767 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
768 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
769 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000770 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
771 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
772 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
773 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000774
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000775 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000777
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
779 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
780 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
781 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
782 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
783 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
784 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
785 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
786 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
787 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
788 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
789 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000790 }
791
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000792 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000794
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000795 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
796 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
798 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
799 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
800 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000801
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
803 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
804 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
805 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
806 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
807 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
808 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
809 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
810 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
811 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
812 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
813 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
814 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
815 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
816 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
817 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000818
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
820 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
821 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
822 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000823
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
825 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
826 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
827 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
828 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000829
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000830 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
831 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
832 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
833 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
834 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
835
Evan Cheng2c3ae372006-04-12 21:21:57 +0000836 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
838 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000839 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000840 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000841 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000842 // Do not attempt to custom lower non-128-bit vectors
843 if (!VT.is128BitVector())
844 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::BUILD_VECTOR,
846 VT.getSimpleVT().SimpleTy, Custom);
847 setOperationAction(ISD::VECTOR_SHUFFLE,
848 VT.getSimpleVT().SimpleTy, Custom);
849 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
850 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000851 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000852
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
854 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
855 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
856 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
857 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
858 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000859
Nate Begemancdd1eec2008-02-12 22:51:28 +0000860 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
862 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000863 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000864
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000865 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
867 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000868 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000869
870 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000871 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000872 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000873
Owen Andersond6662ad2009-08-10 20:46:15 +0000874 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000876 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000877 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000878 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000880 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000881 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000882 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000884 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000885
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000887
Evan Cheng2c3ae372006-04-12 21:21:57 +0000888 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
890 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
891 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
892 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000893
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
895 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000896 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000897
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000898 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000899 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
900 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
901 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
902 setOperationAction(ISD::FRINT, MVT::f32, Legal);
903 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
904 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
905 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
906 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
907 setOperationAction(ISD::FRINT, MVT::f64, Legal);
908 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
909
Nate Begeman14d12ca2008-02-11 04:19:36 +0000910 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000912
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000913 // Can turn SHL into an integer multiply.
914 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000915 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000916
Nate Begeman14d12ca2008-02-11 04:19:36 +0000917 // i8 and i16 vectors are custom , because the source register and source
918 // source memory operand types are not the same width. f32 vectors are
919 // custom since the immediate controlling the insert encodes additional
920 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
922 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
923 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
924 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000925
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
927 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
928 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
929 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000930
931 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
933 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000934 }
935 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000936
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000937 if (Subtarget->hasSSE2() || Subtarget->hasAVX()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000938 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
939 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
940 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000941 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000942
943 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
944 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
945 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
946
947 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
948 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
949 }
950
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000951 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000953
David Greene9b9838d2009-06-29 16:47:10 +0000954 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000955 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
956 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
957 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
958 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
959 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
960 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000961
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
964 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000965
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
967 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
968 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
969 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
970 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
971 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000972
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
974 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
975 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
976 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
977 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
978 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000979
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000980 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
981 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000982 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000983
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000984 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
985 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
986 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
987 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
988 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
989 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
990
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000991 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
992 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
993 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
994 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
995
996 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
997 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
998 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
999 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1000
1001 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1002 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1003
Craig Toppera5347802011-08-23 04:36:33 +00001004 setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
1005 setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001006 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
1007 setOperationAction(ISD::VSETCC, MVT::v4i64, Custom);
1008
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001009 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1010 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1011 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1012
Craig Topper13894fa2011-08-24 06:14:18 +00001013 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1014 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1015 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1016 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1017
1018 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1019 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1020 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1021 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1022
1023 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1024 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1025 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1026 // Don't lower v32i8 because there is no 128-bit byte mul
1027
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001028 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001029 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001030 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1031 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1032 EVT VT = SVT;
1033
1034 // Extract subvector is special because the value type
1035 // (result) is 128-bit but the source is 256-bit wide.
1036 if (VT.is128BitVector())
1037 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1038
1039 // Do not attempt to custom lower other non-256-bit vectors
1040 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001041 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001042
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001043 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1044 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1045 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1046 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001047 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001048 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001049 }
1050
David Greene54d8eba2011-01-27 22:38:56 +00001051 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001052 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1053 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1054 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001055
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001056 // Do not attempt to promote non-256-bit vectors
1057 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001058 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001059
1060 setOperationAction(ISD::AND, SVT, Promote);
1061 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1062 setOperationAction(ISD::OR, SVT, Promote);
1063 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1064 setOperationAction(ISD::XOR, SVT, Promote);
1065 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1066 setOperationAction(ISD::LOAD, SVT, Promote);
1067 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1068 setOperationAction(ISD::SELECT, SVT, Promote);
1069 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001070 }
David Greene9b9838d2009-06-29 16:47:10 +00001071 }
1072
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001073 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1074 // of this type with custom code.
1075 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1076 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1077 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1078 }
1079
Evan Cheng6be2c582006-04-05 23:38:46 +00001080 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001081 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001082
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001083
Eli Friedman962f5492010-06-02 19:35:46 +00001084 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1085 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001086 //
Eli Friedman962f5492010-06-02 19:35:46 +00001087 // FIXME: We really should do custom legalization for addition and
1088 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1089 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001090 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1091 // Add/Sub/Mul with overflow operations are custom lowered.
1092 MVT VT = IntVTs[i];
1093 setOperationAction(ISD::SADDO, VT, Custom);
1094 setOperationAction(ISD::UADDO, VT, Custom);
1095 setOperationAction(ISD::SSUBO, VT, Custom);
1096 setOperationAction(ISD::USUBO, VT, Custom);
1097 setOperationAction(ISD::SMULO, VT, Custom);
1098 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001099 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001100
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001101 // There are no 8-bit 3-address imul/mul instructions
1102 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1103 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001104
Evan Chengd54f2d52009-03-31 19:38:51 +00001105 if (!Subtarget->is64Bit()) {
1106 // These libcalls are not available in 32-bit.
1107 setLibcallName(RTLIB::SHL_I128, 0);
1108 setLibcallName(RTLIB::SRL_I128, 0);
1109 setLibcallName(RTLIB::SRA_I128, 0);
1110 }
1111
Evan Cheng206ee9d2006-07-07 08:33:52 +00001112 // We have target-specific dag combine patterns for the following nodes:
1113 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001114 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001115 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001116 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001117 setTargetDAGCombine(ISD::SHL);
1118 setTargetDAGCombine(ISD::SRA);
1119 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001120 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001121 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001122 setTargetDAGCombine(ISD::ADD);
1123 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001124 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001125 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001126 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001127 if (Subtarget->is64Bit())
1128 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001129
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001130 computeRegisterProperties();
1131
Evan Cheng05219282011-01-06 06:52:41 +00001132 // On Darwin, -Os means optimize for size without hurting performance,
1133 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001134 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001135 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001136 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001137 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1138 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1139 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001140 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001141 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001142
1143 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001144}
1145
Scott Michel5b8f82e2008-03-10 15:42:14 +00001146
Owen Anderson825b72b2009-08-11 20:47:22 +00001147MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1148 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001149}
1150
1151
Evan Cheng29286502008-01-23 23:17:41 +00001152/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1153/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001154static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001155 if (MaxAlign == 16)
1156 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001157 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001158 if (VTy->getBitWidth() == 128)
1159 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001160 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001161 unsigned EltAlign = 0;
1162 getMaxByValAlign(ATy->getElementType(), EltAlign);
1163 if (EltAlign > MaxAlign)
1164 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001165 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001166 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1167 unsigned EltAlign = 0;
1168 getMaxByValAlign(STy->getElementType(i), EltAlign);
1169 if (EltAlign > MaxAlign)
1170 MaxAlign = EltAlign;
1171 if (MaxAlign == 16)
1172 break;
1173 }
1174 }
1175 return;
1176}
1177
1178/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1179/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001180/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1181/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001182unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001183 if (Subtarget->is64Bit()) {
1184 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001185 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001186 if (TyAlign > 8)
1187 return TyAlign;
1188 return 8;
1189 }
1190
Evan Cheng29286502008-01-23 23:17:41 +00001191 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001192 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001193 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001194 return Align;
1195}
Chris Lattner2b02a442007-02-25 08:29:00 +00001196
Evan Chengf0df0312008-05-15 08:39:06 +00001197/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001198/// and store operations as a result of memset, memcpy, and memmove
1199/// lowering. If DstAlign is zero that means it's safe to destination
1200/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1201/// means there isn't a need to check it against alignment requirement,
1202/// probably because the source does not need to be loaded. If
1203/// 'NonScalarIntSafe' is true, that means it's safe to return a
1204/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1205/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1206/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001207/// It returns EVT::Other if the type should be determined using generic
1208/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001209EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001210X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1211 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001212 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001213 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001214 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001215 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1216 // linux. This is because the stack realignment code can't handle certain
1217 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001218 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001219 if (NonScalarIntSafe &&
1220 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001221 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001222 (Subtarget->isUnalignedMemAccessFast() ||
1223 ((DstAlign == 0 || DstAlign >= 16) &&
1224 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001225 Subtarget->getStackAlignment() >= 16) {
1226 if (Subtarget->hasSSE2())
1227 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001228 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001229 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001230 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001231 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001232 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001233 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001234 // Do not use f64 to lower memcpy if source is string constant. It's
1235 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001236 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001237 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001238 }
Evan Chengf0df0312008-05-15 08:39:06 +00001239 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 return MVT::i64;
1241 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001242}
1243
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001244/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1245/// current function. The returned value is a member of the
1246/// MachineJumpTableInfo::JTEntryKind enum.
1247unsigned X86TargetLowering::getJumpTableEncoding() const {
1248 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1249 // symbol.
1250 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1251 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001252 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001253
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001254 // Otherwise, use the normal jump table encoding heuristics.
1255 return TargetLowering::getJumpTableEncoding();
1256}
1257
Chris Lattnerc64daab2010-01-26 05:02:42 +00001258const MCExpr *
1259X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1260 const MachineBasicBlock *MBB,
1261 unsigned uid,MCContext &Ctx) const{
1262 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1263 Subtarget->isPICStyleGOT());
1264 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1265 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001266 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1267 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001268}
1269
Evan Chengcc415862007-11-09 01:32:10 +00001270/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1271/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001272SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001273 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001274 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001275 // This doesn't have DebugLoc associated with it, but is not really the
1276 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001277 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001278 return Table;
1279}
1280
Chris Lattner589c6f62010-01-26 06:28:43 +00001281/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1282/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1283/// MCExpr.
1284const MCExpr *X86TargetLowering::
1285getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1286 MCContext &Ctx) const {
1287 // X86-64 uses RIP relative addressing based on the jump table label.
1288 if (Subtarget->isPICStyleRIPRel())
1289 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1290
1291 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001292 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001293}
1294
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001295// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001296std::pair<const TargetRegisterClass*, uint8_t>
1297X86TargetLowering::findRepresentativeClass(EVT VT) const{
1298 const TargetRegisterClass *RRC = 0;
1299 uint8_t Cost = 1;
1300 switch (VT.getSimpleVT().SimpleTy) {
1301 default:
1302 return TargetLowering::findRepresentativeClass(VT);
1303 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1304 RRC = (Subtarget->is64Bit()
1305 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1306 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001307 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001308 RRC = X86::VR64RegisterClass;
1309 break;
1310 case MVT::f32: case MVT::f64:
1311 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1312 case MVT::v4f32: case MVT::v2f64:
1313 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1314 case MVT::v4f64:
1315 RRC = X86::VR128RegisterClass;
1316 break;
1317 }
1318 return std::make_pair(RRC, Cost);
1319}
1320
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001321bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1322 unsigned &Offset) const {
1323 if (!Subtarget->isTargetLinux())
1324 return false;
1325
1326 if (Subtarget->is64Bit()) {
1327 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1328 Offset = 0x28;
1329 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1330 AddressSpace = 256;
1331 else
1332 AddressSpace = 257;
1333 } else {
1334 // %gs:0x14 on i386
1335 Offset = 0x14;
1336 AddressSpace = 256;
1337 }
1338 return true;
1339}
1340
1341
Chris Lattner2b02a442007-02-25 08:29:00 +00001342//===----------------------------------------------------------------------===//
1343// Return Value Calling Convention Implementation
1344//===----------------------------------------------------------------------===//
1345
Chris Lattner59ed56b2007-02-28 04:55:35 +00001346#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001347
Michael J. Spencerec38de22010-10-10 22:04:20 +00001348bool
Eric Christopher471e4222011-06-08 23:55:35 +00001349X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1350 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001351 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001352 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001353 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001354 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001355 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001356 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001357}
1358
Dan Gohman98ca4f22009-08-05 01:29:28 +00001359SDValue
1360X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001361 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001362 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001363 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001364 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001365 MachineFunction &MF = DAG.getMachineFunction();
1366 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001367
Chris Lattner9774c912007-02-27 05:28:59 +00001368 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001369 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001370 RVLocs, *DAG.getContext());
1371 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001372
Evan Chengdcea1632010-02-04 02:40:39 +00001373 // Add the regs to the liveout set for the function.
1374 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1375 for (unsigned i = 0; i != RVLocs.size(); ++i)
1376 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1377 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001378
Dan Gohman475871a2008-07-27 21:46:04 +00001379 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001380
Dan Gohman475871a2008-07-27 21:46:04 +00001381 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001382 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1383 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001384 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1385 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001386
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001387 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001388 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1389 CCValAssign &VA = RVLocs[i];
1390 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001391 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001392 EVT ValVT = ValToCopy.getValueType();
1393
Dale Johannesenc4510512010-09-24 19:05:48 +00001394 // If this is x86-64, and we disabled SSE, we can't return FP values,
1395 // or SSE or MMX vectors.
1396 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1397 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001398 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001399 report_fatal_error("SSE register return with SSE disabled");
1400 }
1401 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1402 // llvm-gcc has never done it right and no one has noticed, so this
1403 // should be OK for now.
1404 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001405 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001406 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001407
Chris Lattner447ff682008-03-11 03:23:40 +00001408 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1409 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001410 if (VA.getLocReg() == X86::ST0 ||
1411 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001412 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1413 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001414 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001415 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001416 RetOps.push_back(ValToCopy);
1417 // Don't emit a copytoreg.
1418 continue;
1419 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001420
Evan Cheng242b38b2009-02-23 09:03:22 +00001421 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1422 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001423 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001424 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001425 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001426 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001427 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1428 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001429 // If we don't have SSE2 available, convert to v4f32 so the generated
1430 // register is legal.
1431 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001432 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001433 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001434 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001435 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001436
Dale Johannesendd64c412009-02-04 00:33:20 +00001437 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001438 Flag = Chain.getValue(1);
1439 }
Dan Gohman61a92132008-04-21 23:59:07 +00001440
1441 // The x86-64 ABI for returning structs by value requires that we copy
1442 // the sret argument into %rax for the return. We saved the argument into
1443 // a virtual register in the entry block, so now we copy the value out
1444 // and into %rax.
1445 if (Subtarget->is64Bit() &&
1446 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1447 MachineFunction &MF = DAG.getMachineFunction();
1448 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1449 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001450 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001451 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001452 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001453
Dale Johannesendd64c412009-02-04 00:33:20 +00001454 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001455 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001456
1457 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001458 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001460
Chris Lattner447ff682008-03-11 03:23:40 +00001461 RetOps[0] = Chain; // Update chain.
1462
1463 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001464 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001465 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001466
1467 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001468 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001469}
1470
Evan Cheng3d2125c2010-11-30 23:55:39 +00001471bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1472 if (N->getNumValues() != 1)
1473 return false;
1474 if (!N->hasNUsesOfValue(1, 0))
1475 return false;
1476
1477 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001478 if (Copy->getOpcode() != ISD::CopyToReg &&
1479 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001480 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001481
1482 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001483 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001484 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001485 if (UI->getOpcode() != X86ISD::RET_FLAG)
1486 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001487 HasRet = true;
1488 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001489
Evan Cheng1bf891a2010-12-01 22:59:46 +00001490 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001491}
1492
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001493EVT
1494X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001495 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001496 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001497 // TODO: Is this also valid on 32-bit?
1498 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001499 ReturnMVT = MVT::i8;
1500 else
1501 ReturnMVT = MVT::i32;
1502
1503 EVT MinVT = getRegisterType(Context, ReturnMVT);
1504 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001505}
1506
Dan Gohman98ca4f22009-08-05 01:29:28 +00001507/// LowerCallResult - Lower the result values of a call into the
1508/// appropriate copies out of appropriate physical registers.
1509///
1510SDValue
1511X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001512 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001513 const SmallVectorImpl<ISD::InputArg> &Ins,
1514 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001515 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001516
Chris Lattnere32bbf62007-02-28 07:09:55 +00001517 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001518 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001519 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001520 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1521 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001522 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001523
Chris Lattner3085e152007-02-25 08:59:22 +00001524 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001525 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001526 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001527 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001528
Torok Edwin3f142c32009-02-01 18:15:56 +00001529 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001531 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001532 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001533 }
1534
Evan Cheng79fb3b42009-02-20 20:43:02 +00001535 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001536
1537 // If this is a call to a function that returns an fp value on the floating
1538 // point stack, we must guarantee the the value is popped from the stack, so
1539 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001540 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001541 // instead.
1542 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1543 // If we prefer to use the value in xmm registers, copy it out as f80 and
1544 // use a truncate to move it from fp stack reg to xmm reg.
1545 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001546 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001547 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1548 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001549 Val = Chain.getValue(0);
1550
1551 // Round the f80 to the right size, which also moves it to the appropriate
1552 // xmm register.
1553 if (CopyVT != VA.getValVT())
1554 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1555 // This truncation won't change the value.
1556 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001557 } else {
1558 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1559 CopyVT, InFlag).getValue(1);
1560 Val = Chain.getValue(0);
1561 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001562 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001563 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001564 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001565
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001567}
1568
1569
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001570//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001571// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001572//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001573// StdCall calling convention seems to be standard for many Windows' API
1574// routines and around. It differs from C calling convention just a little:
1575// callee should clean up the stack, not caller. Symbols should be also
1576// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001577// For info on fast calling convention see Fast Calling Convention (tail call)
1578// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001579
Dan Gohman98ca4f22009-08-05 01:29:28 +00001580/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001581/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001582static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1583 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001584 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001585
Dan Gohman98ca4f22009-08-05 01:29:28 +00001586 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001587}
1588
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001589/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001590/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591static bool
1592ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1593 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001594 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001595
Dan Gohman98ca4f22009-08-05 01:29:28 +00001596 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001597}
1598
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001599/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1600/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001601/// the specific parameter attribute. The copy will be passed as a byval
1602/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001603static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001604CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001605 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1606 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001607 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001608
Dale Johannesendd64c412009-02-04 00:33:20 +00001609 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001610 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001611 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001612}
1613
Chris Lattner29689432010-03-11 00:22:57 +00001614/// IsTailCallConvention - Return true if the calling convention is one that
1615/// supports tail call optimization.
1616static bool IsTailCallConvention(CallingConv::ID CC) {
1617 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1618}
1619
Evan Cheng485fafc2011-03-21 01:19:09 +00001620bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1621 if (!CI->isTailCall())
1622 return false;
1623
1624 CallSite CS(CI);
1625 CallingConv::ID CalleeCC = CS.getCallingConv();
1626 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1627 return false;
1628
1629 return true;
1630}
1631
Evan Cheng0c439eb2010-01-27 00:07:07 +00001632/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1633/// a tailcall target by changing its ABI.
1634static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001635 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001636}
1637
Dan Gohman98ca4f22009-08-05 01:29:28 +00001638SDValue
1639X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001640 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 const SmallVectorImpl<ISD::InputArg> &Ins,
1642 DebugLoc dl, SelectionDAG &DAG,
1643 const CCValAssign &VA,
1644 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001645 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001646 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001647 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001648 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001649 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001650 EVT ValVT;
1651
1652 // If value is passed by pointer we have address passed instead of the value
1653 // itself.
1654 if (VA.getLocInfo() == CCValAssign::Indirect)
1655 ValVT = VA.getLocVT();
1656 else
1657 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001658
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001659 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001660 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001661 // In case of tail call optimization mark all arguments mutable. Since they
1662 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001663 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001664 unsigned Bytes = Flags.getByValSize();
1665 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1666 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001667 return DAG.getFrameIndex(FI, getPointerTy());
1668 } else {
1669 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001670 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001671 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1672 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001673 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001674 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001675 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001676}
1677
Dan Gohman475871a2008-07-27 21:46:04 +00001678SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001680 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001681 bool isVarArg,
1682 const SmallVectorImpl<ISD::InputArg> &Ins,
1683 DebugLoc dl,
1684 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001685 SmallVectorImpl<SDValue> &InVals)
1686 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001687 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001688 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001689
Gordon Henriksen86737662008-01-05 16:56:59 +00001690 const Function* Fn = MF.getFunction();
1691 if (Fn->hasExternalLinkage() &&
1692 Subtarget->isTargetCygMing() &&
1693 Fn->getName() == "main")
1694 FuncInfo->setForceFramePointer(true);
1695
Evan Cheng1bc78042006-04-26 01:20:17 +00001696 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001697 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001698 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001699
Chris Lattner29689432010-03-11 00:22:57 +00001700 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1701 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001702
Chris Lattner638402b2007-02-28 07:00:42 +00001703 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001704 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001705 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001706 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001707
1708 // Allocate shadow area for Win64
1709 if (IsWin64) {
1710 CCInfo.AllocateStack(32, 8);
1711 }
1712
Duncan Sands45907662010-10-31 13:21:44 +00001713 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001714
Chris Lattnerf39f7712007-02-28 05:46:49 +00001715 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001716 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001717 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1718 CCValAssign &VA = ArgLocs[i];
1719 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1720 // places.
1721 assert(VA.getValNo() != LastVal &&
1722 "Don't support value assigned to multiple locs yet");
1723 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001724
Chris Lattnerf39f7712007-02-28 05:46:49 +00001725 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001726 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001727 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001729 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001730 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001731 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001732 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001733 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001734 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001735 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001736 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1737 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001738 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001739 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001740 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001741 RC = X86::VR64RegisterClass;
1742 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001743 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001744
Devang Patel68e6bee2011-02-21 23:21:26 +00001745 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001746 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001747
Chris Lattnerf39f7712007-02-28 05:46:49 +00001748 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1749 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1750 // right size.
1751 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001752 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001753 DAG.getValueType(VA.getValVT()));
1754 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001755 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001756 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001757 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001758 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001759
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001760 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001761 // Handle MMX values passed in XMM regs.
1762 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001763 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1764 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001765 } else
1766 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001767 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001768 } else {
1769 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001770 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001771 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001772
1773 // If value is passed via pointer - do a load.
1774 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001775 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1776 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001777
Dan Gohman98ca4f22009-08-05 01:29:28 +00001778 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001779 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001780
Dan Gohman61a92132008-04-21 23:59:07 +00001781 // The x86-64 ABI for returning structs by value requires that we copy
1782 // the sret argument into %rax for the return. Save the argument into
1783 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001784 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001785 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1786 unsigned Reg = FuncInfo->getSRetReturnReg();
1787 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001788 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001789 FuncInfo->setSRetReturnReg(Reg);
1790 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001791 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001792 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001793 }
1794
Chris Lattnerf39f7712007-02-28 05:46:49 +00001795 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001796 // Align stack specially for tail calls.
1797 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001798 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001799
Evan Cheng1bc78042006-04-26 01:20:17 +00001800 // If the function takes variable number of arguments, make a frame index for
1801 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001802 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001803 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1804 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001805 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001806 }
1807 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001808 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1809
1810 // FIXME: We should really autogenerate these arrays
1811 static const unsigned GPR64ArgRegsWin64[] = {
1812 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001813 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001814 static const unsigned GPR64ArgRegs64Bit[] = {
1815 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1816 };
1817 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001818 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1819 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1820 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001821 const unsigned *GPR64ArgRegs;
1822 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001823
1824 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001825 // The XMM registers which might contain var arg parameters are shadowed
1826 // in their paired GPR. So we only need to save the GPR to their home
1827 // slots.
1828 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001829 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001830 } else {
1831 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1832 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001833
1834 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001835 }
1836 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1837 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001838
Devang Patel578efa92009-06-05 21:57:13 +00001839 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001840 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001841 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001842 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001843 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001844 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001845 // Kernel mode asks for SSE to be disabled, so don't push them
1846 // on the stack.
1847 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001848
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001849 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001850 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001851 // Get to the caller-allocated home save location. Add 8 to account
1852 // for the return address.
1853 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001854 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001855 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001856 // Fixup to set vararg frame on shadow area (4 x i64).
1857 if (NumIntRegs < 4)
1858 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001859 } else {
1860 // For X86-64, if there are vararg parameters that are passed via
1861 // registers, then we must store them to their spots on the stack so they
1862 // may be loaded by deferencing the result of va_next.
1863 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1864 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1865 FuncInfo->setRegSaveFrameIndex(
1866 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001867 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001868 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001869
Gordon Henriksen86737662008-01-05 16:56:59 +00001870 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001871 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001872 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1873 getPointerTy());
1874 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001875 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001876 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1877 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001878 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001879 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001881 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001882 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001883 MachinePointerInfo::getFixedStack(
1884 FuncInfo->getRegSaveFrameIndex(), Offset),
1885 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001886 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001887 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001889
Dan Gohmanface41a2009-08-16 21:24:25 +00001890 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1891 // Now store the XMM (fp + vector) parameter registers.
1892 SmallVector<SDValue, 11> SaveXMMOps;
1893 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001894
Devang Patel68e6bee2011-02-21 23:21:26 +00001895 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001896 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1897 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001898
Dan Gohman1e93df62010-04-17 14:41:14 +00001899 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1900 FuncInfo->getRegSaveFrameIndex()));
1901 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1902 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001903
Dan Gohmanface41a2009-08-16 21:24:25 +00001904 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001905 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001906 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001907 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1908 SaveXMMOps.push_back(Val);
1909 }
1910 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1911 MVT::Other,
1912 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001913 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001914
1915 if (!MemOps.empty())
1916 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1917 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001918 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001919 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001920
Gordon Henriksen86737662008-01-05 16:56:59 +00001921 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001922 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001923 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001924 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001925 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001926 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001927 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001928 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001929 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001930
Gordon Henriksen86737662008-01-05 16:56:59 +00001931 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001932 // RegSaveFrameIndex is X86-64 only.
1933 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001934 if (CallConv == CallingConv::X86_FastCall ||
1935 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001936 // fastcc functions can't have varargs.
1937 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001938 }
Evan Cheng25caf632006-05-23 21:06:34 +00001939
Rafael Espindola76927d752011-08-30 19:39:58 +00001940 FuncInfo->setArgumentStackSize(StackSize);
1941
Dan Gohman98ca4f22009-08-05 01:29:28 +00001942 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001943}
1944
Dan Gohman475871a2008-07-27 21:46:04 +00001945SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001946X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1947 SDValue StackPtr, SDValue Arg,
1948 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001949 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001950 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001951 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001952 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001953 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001954 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001955 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001956
1957 return DAG.getStore(Chain, dl, Arg, PtrOff,
1958 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001959 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001960}
1961
Bill Wendling64e87322009-01-16 19:25:27 +00001962/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001963/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001964SDValue
1965X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001966 SDValue &OutRetAddr, SDValue Chain,
1967 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001968 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001969 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001970 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001971 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001972
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001973 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001974 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1975 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001976 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001977}
1978
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001979/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001980/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001981static SDValue
1982EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001983 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001984 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001985 // Store the return address to the appropriate stack slot.
1986 if (!FPDiff) return Chain;
1987 // Calculate the new stack slot for the return address.
1988 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001989 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001990 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001993 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001994 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001995 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001996 return Chain;
1997}
1998
Dan Gohman98ca4f22009-08-05 01:29:28 +00001999SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002000X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002001 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002002 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002003 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002004 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002005 const SmallVectorImpl<ISD::InputArg> &Ins,
2006 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002007 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002008 MachineFunction &MF = DAG.getMachineFunction();
2009 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002010 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002011 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002012 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002013
Evan Cheng5f941932010-02-05 02:21:12 +00002014 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002015 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002016 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2017 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002018 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002019
2020 // Sibcalls are automatically detected tailcalls which do not require
2021 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002022 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002023 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002024
2025 if (isTailCall)
2026 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002027 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002028
Chris Lattner29689432010-03-11 00:22:57 +00002029 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2030 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002031
Chris Lattner638402b2007-02-28 07:00:42 +00002032 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002033 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002034 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002035 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002036
2037 // Allocate shadow area for Win64
2038 if (IsWin64) {
2039 CCInfo.AllocateStack(32, 8);
2040 }
2041
Duncan Sands45907662010-10-31 13:21:44 +00002042 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002043
Chris Lattner423c5f42007-02-28 05:31:48 +00002044 // Get a count of how many bytes are to be pushed on the stack.
2045 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002046 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002047 // This is a sibcall. The memory operands are available in caller's
2048 // own caller's stack.
2049 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002050 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002051 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002052
Gordon Henriksen86737662008-01-05 16:56:59 +00002053 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002054 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002055 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002056 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002057 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2058 FPDiff = NumBytesCallerPushed - NumBytes;
2059
2060 // Set the delta of movement of the returnaddr stackslot.
2061 // But only set if delta is greater than previous delta.
2062 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2063 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2064 }
2065
Evan Chengf22f9b32010-02-06 03:28:46 +00002066 if (!IsSibcall)
2067 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002068
Dan Gohman475871a2008-07-27 21:46:04 +00002069 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002070 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002071 if (isTailCall && FPDiff)
2072 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2073 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002074
Dan Gohman475871a2008-07-27 21:46:04 +00002075 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2076 SmallVector<SDValue, 8> MemOpChains;
2077 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002078
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002079 // Walk the register/memloc assignments, inserting copies/loads. In the case
2080 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002081 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2082 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002083 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002084 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002085 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002086 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002087
Chris Lattner423c5f42007-02-28 05:31:48 +00002088 // Promote the value if needed.
2089 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002090 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002091 case CCValAssign::Full: break;
2092 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002093 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002094 break;
2095 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002096 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002097 break;
2098 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002099 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2100 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002101 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002102 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2103 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002104 } else
2105 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2106 break;
2107 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002108 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002109 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002110 case CCValAssign::Indirect: {
2111 // Store the argument.
2112 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002113 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002114 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002115 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002116 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002117 Arg = SpillSlot;
2118 break;
2119 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002121
Chris Lattner423c5f42007-02-28 05:31:48 +00002122 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002123 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2124 if (isVarArg && IsWin64) {
2125 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2126 // shadow reg if callee is a varargs function.
2127 unsigned ShadowReg = 0;
2128 switch (VA.getLocReg()) {
2129 case X86::XMM0: ShadowReg = X86::RCX; break;
2130 case X86::XMM1: ShadowReg = X86::RDX; break;
2131 case X86::XMM2: ShadowReg = X86::R8; break;
2132 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002133 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002134 if (ShadowReg)
2135 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002136 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002137 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002138 assert(VA.isMemLoc());
2139 if (StackPtr.getNode() == 0)
2140 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2141 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2142 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002143 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002145
Evan Cheng32fe1032006-05-25 00:59:30 +00002146 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002147 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002148 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002149
Evan Cheng347d5f72006-04-28 21:29:37 +00002150 // Build a sequence of copy-to-reg nodes chained together with token chain
2151 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002152 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002153 // Tail call byval lowering might overwrite argument registers so in case of
2154 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002155 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002156 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002157 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002158 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002159 InFlag = Chain.getValue(1);
2160 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002161
Chris Lattner88e1fd52009-07-09 04:24:46 +00002162 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002163 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2164 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002165 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002166 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2167 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002168 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002169 InFlag);
2170 InFlag = Chain.getValue(1);
2171 } else {
2172 // If we are tail calling and generating PIC/GOT style code load the
2173 // address of the callee into ECX. The value in ecx is used as target of
2174 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2175 // for tail calls on PIC/GOT architectures. Normally we would just put the
2176 // address of GOT into ebx and then call target@PLT. But for tail calls
2177 // ebx would be restored (since ebx is callee saved) before jumping to the
2178 // target@PLT.
2179
2180 // Note: The actual moving to ECX is done further down.
2181 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2182 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2183 !G->getGlobal()->hasProtectedVisibility())
2184 Callee = LowerGlobalAddress(Callee, DAG);
2185 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002186 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002187 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002188 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002189
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002190 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002191 // From AMD64 ABI document:
2192 // For calls that may call functions that use varargs or stdargs
2193 // (prototype-less calls or calls to functions containing ellipsis (...) in
2194 // the declaration) %al is used as hidden argument to specify the number
2195 // of SSE registers used. The contents of %al do not need to match exactly
2196 // the number of registers, but must be an ubound on the number of SSE
2197 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002198
Gordon Henriksen86737662008-01-05 16:56:59 +00002199 // Count the number of XMM registers allocated.
2200 static const unsigned XMMArgRegs[] = {
2201 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2202 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2203 };
2204 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002205 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002206 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002207
Dale Johannesendd64c412009-02-04 00:33:20 +00002208 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002209 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002210 InFlag = Chain.getValue(1);
2211 }
2212
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002213
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002214 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002215 if (isTailCall) {
2216 // Force all the incoming stack arguments to be loaded from the stack
2217 // before any new outgoing arguments are stored to the stack, because the
2218 // outgoing stack slots may alias the incoming argument stack slots, and
2219 // the alias isn't otherwise explicit. This is slightly more conservative
2220 // than necessary, because it means that each store effectively depends
2221 // on every argument instead of just those arguments it would clobber.
2222 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2223
Dan Gohman475871a2008-07-27 21:46:04 +00002224 SmallVector<SDValue, 8> MemOpChains2;
2225 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002226 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002227 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002228 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002229 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002230 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2231 CCValAssign &VA = ArgLocs[i];
2232 if (VA.isRegLoc())
2233 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002234 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002235 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002236 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002237 // Create frame index.
2238 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002239 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002240 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002241 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002242
Duncan Sands276dcbd2008-03-21 09:14:45 +00002243 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002244 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002245 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002246 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002247 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002248 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002249 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002250
Dan Gohman98ca4f22009-08-05 01:29:28 +00002251 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2252 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002253 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002254 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002255 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002256 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002257 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002258 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002259 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002260 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002261 }
2262 }
2263
2264 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002265 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002266 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002267
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002268 // Copy arguments to their registers.
2269 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002270 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002271 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002272 InFlag = Chain.getValue(1);
2273 }
Dan Gohman475871a2008-07-27 21:46:04 +00002274 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002275
Gordon Henriksen86737662008-01-05 16:56:59 +00002276 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002277 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002278 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002279 }
2280
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002281 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2282 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2283 // In the 64-bit large code model, we have to make all calls
2284 // through a register, since the call instruction's 32-bit
2285 // pc-relative offset may not be large enough to hold the whole
2286 // address.
2287 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002288 // If the callee is a GlobalAddress node (quite common, every direct call
2289 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2290 // it.
2291
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002292 // We should use extra load for direct calls to dllimported functions in
2293 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002294 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002295 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002296 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002297 bool ExtraLoad = false;
2298 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002299
Chris Lattner48a7d022009-07-09 05:02:21 +00002300 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2301 // external symbols most go through the PLT in PIC mode. If the symbol
2302 // has hidden or protected visibility, or if it is static or local, then
2303 // we don't need to use the PLT - we can directly call it.
2304 if (Subtarget->isTargetELF() &&
2305 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002306 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002307 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002308 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002309 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002310 (!Subtarget->getTargetTriple().isMacOSX() ||
2311 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002312 // PC-relative references to external symbols should go through $stub,
2313 // unless we're building with the leopard linker or later, which
2314 // automatically synthesizes these stubs.
2315 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002316 } else if (Subtarget->isPICStyleRIPRel() &&
2317 isa<Function>(GV) &&
2318 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2319 // If the function is marked as non-lazy, generate an indirect call
2320 // which loads from the GOT directly. This avoids runtime overhead
2321 // at the cost of eager binding (and one extra byte of encoding).
2322 OpFlags = X86II::MO_GOTPCREL;
2323 WrapperKind = X86ISD::WrapperRIP;
2324 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002325 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002326
Devang Patel0d881da2010-07-06 22:08:15 +00002327 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002328 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002329
2330 // Add a wrapper if needed.
2331 if (WrapperKind != ISD::DELETED_NODE)
2332 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2333 // Add extra indirection if needed.
2334 if (ExtraLoad)
2335 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2336 MachinePointerInfo::getGOT(),
2337 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002338 }
Bill Wendling056292f2008-09-16 21:48:12 +00002339 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002340 unsigned char OpFlags = 0;
2341
Evan Cheng1bf891a2010-12-01 22:59:46 +00002342 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2343 // external symbols should go through the PLT.
2344 if (Subtarget->isTargetELF() &&
2345 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2346 OpFlags = X86II::MO_PLT;
2347 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002348 (!Subtarget->getTargetTriple().isMacOSX() ||
2349 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002350 // PC-relative references to external symbols should go through $stub,
2351 // unless we're building with the leopard linker or later, which
2352 // automatically synthesizes these stubs.
2353 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002354 }
Eric Christopherfd179292009-08-27 18:07:15 +00002355
Chris Lattner48a7d022009-07-09 05:02:21 +00002356 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2357 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002358 }
2359
Chris Lattnerd96d0722007-02-25 06:40:16 +00002360 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002361 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002362 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002363
Evan Chengf22f9b32010-02-06 03:28:46 +00002364 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002365 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2366 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002367 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002368 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002369
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002370 Ops.push_back(Chain);
2371 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002372
Dan Gohman98ca4f22009-08-05 01:29:28 +00002373 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002374 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002375
Gordon Henriksen86737662008-01-05 16:56:59 +00002376 // Add argument registers to the end of the list so that they are known live
2377 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002378 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2379 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2380 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002381
Evan Cheng586ccac2008-03-18 23:36:35 +00002382 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002383 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002384 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2385
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002386 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002387 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002388 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002389
Gabor Greifba36cb52008-08-28 21:40:38 +00002390 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002391 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002392
Dan Gohman98ca4f22009-08-05 01:29:28 +00002393 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002394 // We used to do:
2395 //// If this is the first return lowered for this function, add the regs
2396 //// to the liveout set for the function.
2397 // This isn't right, although it's probably harmless on x86; liveouts
2398 // should be computed from returns not tail calls. Consider a void
2399 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002400 return DAG.getNode(X86ISD::TC_RETURN, dl,
2401 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002402 }
2403
Dale Johannesenace16102009-02-03 19:33:06 +00002404 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002405 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002406
Chris Lattner2d297092006-05-23 18:50:38 +00002407 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002408 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002409 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002410 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002411 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002412 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002413 // pops the hidden struct pointer, so we have to push it back.
2414 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002415 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002416 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002417 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002418
Gordon Henriksenae636f82008-01-03 16:47:34 +00002419 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002420 if (!IsSibcall) {
2421 Chain = DAG.getCALLSEQ_END(Chain,
2422 DAG.getIntPtrConstant(NumBytes, true),
2423 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2424 true),
2425 InFlag);
2426 InFlag = Chain.getValue(1);
2427 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002428
Chris Lattner3085e152007-02-25 08:59:22 +00002429 // Handle result values, copying them out of physregs into vregs that we
2430 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002431 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2432 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002433}
2434
Evan Cheng25ab6902006-09-08 06:48:29 +00002435
2436//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002437// Fast Calling Convention (tail call) implementation
2438//===----------------------------------------------------------------------===//
2439
2440// Like std call, callee cleans arguments, convention except that ECX is
2441// reserved for storing the tail called function address. Only 2 registers are
2442// free for argument passing (inreg). Tail call optimization is performed
2443// provided:
2444// * tailcallopt is enabled
2445// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002446// On X86_64 architecture with GOT-style position independent code only local
2447// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002448// To keep the stack aligned according to platform abi the function
2449// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2450// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002451// If a tail called function callee has more arguments than the caller the
2452// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002453// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002454// original REtADDR, but before the saved framepointer or the spilled registers
2455// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2456// stack layout:
2457// arg1
2458// arg2
2459// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002460// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002461// move area ]
2462// (possible EBP)
2463// ESI
2464// EDI
2465// local1 ..
2466
2467/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2468/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002469unsigned
2470X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2471 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002472 MachineFunction &MF = DAG.getMachineFunction();
2473 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002474 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002475 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002476 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002477 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002478 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002479 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2480 // Number smaller than 12 so just add the difference.
2481 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2482 } else {
2483 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002484 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002485 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002486 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002487 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002488}
2489
Evan Cheng5f941932010-02-05 02:21:12 +00002490/// MatchingStackOffset - Return true if the given stack call argument is
2491/// already available in the same position (relatively) of the caller's
2492/// incoming argument stack.
2493static
2494bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2495 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2496 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002497 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2498 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002499 if (Arg.getOpcode() == ISD::CopyFromReg) {
2500 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002501 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002502 return false;
2503 MachineInstr *Def = MRI->getVRegDef(VR);
2504 if (!Def)
2505 return false;
2506 if (!Flags.isByVal()) {
2507 if (!TII->isLoadFromStackSlot(Def, FI))
2508 return false;
2509 } else {
2510 unsigned Opcode = Def->getOpcode();
2511 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2512 Def->getOperand(1).isFI()) {
2513 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002514 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002515 } else
2516 return false;
2517 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002518 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2519 if (Flags.isByVal())
2520 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002521 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002522 // define @foo(%struct.X* %A) {
2523 // tail call @bar(%struct.X* byval %A)
2524 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002525 return false;
2526 SDValue Ptr = Ld->getBasePtr();
2527 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2528 if (!FINode)
2529 return false;
2530 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002531 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002532 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002533 FI = FINode->getIndex();
2534 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002535 } else
2536 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002537
Evan Cheng4cae1332010-03-05 08:38:04 +00002538 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002539 if (!MFI->isFixedObjectIndex(FI))
2540 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002541 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002542}
2543
Dan Gohman98ca4f22009-08-05 01:29:28 +00002544/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2545/// for tail call optimization. Targets which want to do tail call
2546/// optimization should implement this function.
2547bool
2548X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002549 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002550 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002551 bool isCalleeStructRet,
2552 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002553 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002554 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002555 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002556 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002557 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002558 CalleeCC != CallingConv::C)
2559 return false;
2560
Evan Cheng7096ae42010-01-29 06:45:59 +00002561 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002562 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002563 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002564 CallingConv::ID CallerCC = CallerF->getCallingConv();
2565 bool CCMatch = CallerCC == CalleeCC;
2566
Dan Gohman1797ed52010-02-08 20:27:50 +00002567 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002568 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002569 return true;
2570 return false;
2571 }
2572
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002573 // Look for obvious safe cases to perform tail call optimization that do not
2574 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002575
Evan Cheng2c12cb42010-03-26 16:26:03 +00002576 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2577 // emit a special epilogue.
2578 if (RegInfo->needsStackRealignment(MF))
2579 return false;
2580
Evan Chenga375d472010-03-15 18:54:48 +00002581 // Also avoid sibcall optimization if either caller or callee uses struct
2582 // return semantics.
2583 if (isCalleeStructRet || isCallerStructRet)
2584 return false;
2585
Chad Rosier2416da32011-06-24 21:15:36 +00002586 // An stdcall caller is expected to clean up its arguments; the callee
2587 // isn't going to do that.
2588 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2589 return false;
2590
Chad Rosier871f6642011-05-18 19:59:50 +00002591 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002592 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002593 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002594
2595 // Optimizing for varargs on Win64 is unlikely to be safe without
2596 // additional testing.
2597 if (Subtarget->isTargetWin64())
2598 return false;
2599
Chad Rosier871f6642011-05-18 19:59:50 +00002600 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002601 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2602 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002603
Chad Rosier871f6642011-05-18 19:59:50 +00002604 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2605 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2606 if (!ArgLocs[i].isRegLoc())
2607 return false;
2608 }
2609
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002610 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2611 // Therefore if it's not used by the call it is not safe to optimize this into
2612 // a sibcall.
2613 bool Unused = false;
2614 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2615 if (!Ins[i].Used) {
2616 Unused = true;
2617 break;
2618 }
2619 }
2620 if (Unused) {
2621 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002622 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2623 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002624 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002625 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002626 CCValAssign &VA = RVLocs[i];
2627 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2628 return false;
2629 }
2630 }
2631
Evan Cheng13617962010-04-30 01:12:32 +00002632 // If the calling conventions do not match, then we'd better make sure the
2633 // results are returned in the same way as what the caller expects.
2634 if (!CCMatch) {
2635 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002636 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2637 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002638 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2639
2640 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002641 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2642 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002643 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2644
2645 if (RVLocs1.size() != RVLocs2.size())
2646 return false;
2647 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2648 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2649 return false;
2650 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2651 return false;
2652 if (RVLocs1[i].isRegLoc()) {
2653 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2654 return false;
2655 } else {
2656 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2657 return false;
2658 }
2659 }
2660 }
2661
Evan Chenga6bff982010-01-30 01:22:00 +00002662 // If the callee takes no arguments then go on to check the results of the
2663 // call.
2664 if (!Outs.empty()) {
2665 // Check if stack adjustment is needed. For now, do not do this if any
2666 // argument is passed on the stack.
2667 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002668 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2669 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002670
2671 // Allocate shadow area for Win64
2672 if (Subtarget->isTargetWin64()) {
2673 CCInfo.AllocateStack(32, 8);
2674 }
2675
Duncan Sands45907662010-10-31 13:21:44 +00002676 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002677 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002678 MachineFunction &MF = DAG.getMachineFunction();
2679 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2680 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002681
2682 // Check if the arguments are already laid out in the right way as
2683 // the caller's fixed stack objects.
2684 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002685 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2686 const X86InstrInfo *TII =
2687 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002688 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2689 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002690 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002691 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002692 if (VA.getLocInfo() == CCValAssign::Indirect)
2693 return false;
2694 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002695 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2696 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002697 return false;
2698 }
2699 }
2700 }
Evan Cheng9c044672010-05-29 01:35:22 +00002701
2702 // If the tailcall address may be in a register, then make sure it's
2703 // possible to register allocate for it. In 32-bit, the call address can
2704 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002705 // callee-saved registers are restored. These happen to be the same
2706 // registers used to pass 'inreg' arguments so watch out for those.
2707 if (!Subtarget->is64Bit() &&
2708 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002709 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002710 unsigned NumInRegs = 0;
2711 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2712 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002713 if (!VA.isRegLoc())
2714 continue;
2715 unsigned Reg = VA.getLocReg();
2716 switch (Reg) {
2717 default: break;
2718 case X86::EAX: case X86::EDX: case X86::ECX:
2719 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002720 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002721 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002722 }
2723 }
2724 }
Evan Chenga6bff982010-01-30 01:22:00 +00002725 }
Evan Chengb1712452010-01-27 06:25:16 +00002726
Evan Cheng86809cc2010-02-03 03:28:02 +00002727 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002728}
2729
Dan Gohman3df24e62008-09-03 23:12:08 +00002730FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002731X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2732 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002733}
2734
2735
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002736//===----------------------------------------------------------------------===//
2737// Other Lowering Hooks
2738//===----------------------------------------------------------------------===//
2739
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002740static bool MayFoldLoad(SDValue Op) {
2741 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2742}
2743
2744static bool MayFoldIntoStore(SDValue Op) {
2745 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2746}
2747
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002748static bool isTargetShuffle(unsigned Opcode) {
2749 switch(Opcode) {
2750 default: return false;
2751 case X86ISD::PSHUFD:
2752 case X86ISD::PSHUFHW:
2753 case X86ISD::PSHUFLW:
2754 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002755 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002756 case X86ISD::SHUFPS:
2757 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002758 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002759 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002760 case X86ISD::MOVLPS:
2761 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002762 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002763 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002764 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002765 case X86ISD::MOVSS:
2766 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002767 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002768 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002769 case X86ISD::VUNPCKLPSY:
2770 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002771 case X86ISD::PUNPCKLWD:
2772 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002773 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002774 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002775 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002776 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002777 case X86ISD::VUNPCKHPSY:
2778 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002779 case X86ISD::PUNPCKHWD:
2780 case X86ISD::PUNPCKHBW:
2781 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002782 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002783 case X86ISD::VPERMILPS:
2784 case X86ISD::VPERMILPSY:
2785 case X86ISD::VPERMILPD:
2786 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002787 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002788 return true;
2789 }
2790 return false;
2791}
2792
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002793static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002794 SDValue V1, SelectionDAG &DAG) {
2795 switch(Opc) {
2796 default: llvm_unreachable("Unknown x86 shuffle node");
2797 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002798 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002799 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002800 return DAG.getNode(Opc, dl, VT, V1);
2801 }
2802
2803 return SDValue();
2804}
2805
2806static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002807 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002808 switch(Opc) {
2809 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002810 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002811 case X86ISD::PSHUFHW:
2812 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002813 case X86ISD::VPERMILPS:
2814 case X86ISD::VPERMILPSY:
2815 case X86ISD::VPERMILPD:
2816 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002817 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2818 }
2819
2820 return SDValue();
2821}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002822
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002823static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2824 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2825 switch(Opc) {
2826 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002827 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002828 case X86ISD::SHUFPD:
2829 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002830 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002831 return DAG.getNode(Opc, dl, VT, V1, V2,
2832 DAG.getConstant(TargetMask, MVT::i8));
2833 }
2834 return SDValue();
2835}
2836
2837static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2838 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2839 switch(Opc) {
2840 default: llvm_unreachable("Unknown x86 shuffle node");
2841 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002842 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002843 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002844 case X86ISD::MOVLPS:
2845 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002846 case X86ISD::MOVSS:
2847 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002848 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002849 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002850 case X86ISD::VUNPCKLPSY:
2851 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002852 case X86ISD::PUNPCKLWD:
2853 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002854 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002855 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002856 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002857 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002858 case X86ISD::VUNPCKHPSY:
2859 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002860 case X86ISD::PUNPCKHWD:
2861 case X86ISD::PUNPCKHBW:
2862 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002863 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002864 return DAG.getNode(Opc, dl, VT, V1, V2);
2865 }
2866 return SDValue();
2867}
2868
Dan Gohmand858e902010-04-17 15:26:15 +00002869SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002870 MachineFunction &MF = DAG.getMachineFunction();
2871 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2872 int ReturnAddrIndex = FuncInfo->getRAIndex();
2873
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002874 if (ReturnAddrIndex == 0) {
2875 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002876 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002877 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002878 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002879 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002880 }
2881
Evan Cheng25ab6902006-09-08 06:48:29 +00002882 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002883}
2884
2885
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002886bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2887 bool hasSymbolicDisplacement) {
2888 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002889 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002890 return false;
2891
2892 // If we don't have a symbolic displacement - we don't have any extra
2893 // restrictions.
2894 if (!hasSymbolicDisplacement)
2895 return true;
2896
2897 // FIXME: Some tweaks might be needed for medium code model.
2898 if (M != CodeModel::Small && M != CodeModel::Kernel)
2899 return false;
2900
2901 // For small code model we assume that latest object is 16MB before end of 31
2902 // bits boundary. We may also accept pretty large negative constants knowing
2903 // that all objects are in the positive half of address space.
2904 if (M == CodeModel::Small && Offset < 16*1024*1024)
2905 return true;
2906
2907 // For kernel code model we know that all object resist in the negative half
2908 // of 32bits address space. We may not accept negative offsets, since they may
2909 // be just off and we may accept pretty large positive ones.
2910 if (M == CodeModel::Kernel && Offset > 0)
2911 return true;
2912
2913 return false;
2914}
2915
Evan Chengef41ff62011-06-23 17:54:54 +00002916/// isCalleePop - Determines whether the callee is required to pop its
2917/// own arguments. Callee pop is necessary to support tail calls.
2918bool X86::isCalleePop(CallingConv::ID CallingConv,
2919 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2920 if (IsVarArg)
2921 return false;
2922
2923 switch (CallingConv) {
2924 default:
2925 return false;
2926 case CallingConv::X86_StdCall:
2927 return !is64Bit;
2928 case CallingConv::X86_FastCall:
2929 return !is64Bit;
2930 case CallingConv::X86_ThisCall:
2931 return !is64Bit;
2932 case CallingConv::Fast:
2933 return TailCallOpt;
2934 case CallingConv::GHC:
2935 return TailCallOpt;
2936 }
2937}
2938
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002939/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2940/// specific condition code, returning the condition code and the LHS/RHS of the
2941/// comparison to make.
2942static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2943 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002944 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002945 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2946 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2947 // X > -1 -> X == 0, jump !sign.
2948 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002949 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002950 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2951 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002952 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002953 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002954 // X < 1 -> X <= 0
2955 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002956 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002957 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002958 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002959
Evan Chengd9558e02006-01-06 00:43:03 +00002960 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002961 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002962 case ISD::SETEQ: return X86::COND_E;
2963 case ISD::SETGT: return X86::COND_G;
2964 case ISD::SETGE: return X86::COND_GE;
2965 case ISD::SETLT: return X86::COND_L;
2966 case ISD::SETLE: return X86::COND_LE;
2967 case ISD::SETNE: return X86::COND_NE;
2968 case ISD::SETULT: return X86::COND_B;
2969 case ISD::SETUGT: return X86::COND_A;
2970 case ISD::SETULE: return X86::COND_BE;
2971 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002972 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002973 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002974
Chris Lattner4c78e022008-12-23 23:42:27 +00002975 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002976
Chris Lattner4c78e022008-12-23 23:42:27 +00002977 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002978 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2979 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002980 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2981 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002982 }
2983
Chris Lattner4c78e022008-12-23 23:42:27 +00002984 switch (SetCCOpcode) {
2985 default: break;
2986 case ISD::SETOLT:
2987 case ISD::SETOLE:
2988 case ISD::SETUGT:
2989 case ISD::SETUGE:
2990 std::swap(LHS, RHS);
2991 break;
2992 }
2993
2994 // On a floating point condition, the flags are set as follows:
2995 // ZF PF CF op
2996 // 0 | 0 | 0 | X > Y
2997 // 0 | 0 | 1 | X < Y
2998 // 1 | 0 | 0 | X == Y
2999 // 1 | 1 | 1 | unordered
3000 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003001 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003002 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003003 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003004 case ISD::SETOLT: // flipped
3005 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003006 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003007 case ISD::SETOLE: // flipped
3008 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003009 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003010 case ISD::SETUGT: // flipped
3011 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003012 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003013 case ISD::SETUGE: // flipped
3014 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003015 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003016 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003017 case ISD::SETNE: return X86::COND_NE;
3018 case ISD::SETUO: return X86::COND_P;
3019 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003020 case ISD::SETOEQ:
3021 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003022 }
Evan Chengd9558e02006-01-06 00:43:03 +00003023}
3024
Evan Cheng4a460802006-01-11 00:33:36 +00003025/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3026/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003027/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003028static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003029 switch (X86CC) {
3030 default:
3031 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003032 case X86::COND_B:
3033 case X86::COND_BE:
3034 case X86::COND_E:
3035 case X86::COND_P:
3036 case X86::COND_A:
3037 case X86::COND_AE:
3038 case X86::COND_NE:
3039 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003040 return true;
3041 }
3042}
3043
Evan Chengeb2f9692009-10-27 19:56:55 +00003044/// isFPImmLegal - Returns true if the target can instruction select the
3045/// specified FP immediate natively. If false, the legalizer will
3046/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003047bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003048 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3049 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3050 return true;
3051 }
3052 return false;
3053}
3054
Nate Begeman9008ca62009-04-27 18:41:29 +00003055/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3056/// the specified range (L, H].
3057static bool isUndefOrInRange(int Val, int Low, int Hi) {
3058 return (Val < 0) || (Val >= Low && Val < Hi);
3059}
3060
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003061/// isUndefOrInRange - Return true if every element in Mask, begining
3062/// from position Pos and ending in Pos+Size, falls within the specified
3063/// range (L, L+Pos]. or is undef.
3064static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3065 int Pos, int Size, int Low, int Hi) {
3066 for (int i = Pos, e = Pos+Size; i != e; ++i)
3067 if (!isUndefOrInRange(Mask[i], Low, Hi))
3068 return false;
3069 return true;
3070}
3071
Nate Begeman9008ca62009-04-27 18:41:29 +00003072/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3073/// specified value.
3074static bool isUndefOrEqual(int Val, int CmpVal) {
3075 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003076 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003077 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003078}
3079
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003080/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3081/// from position Pos and ending in Pos+Size, falls within the specified
3082/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003083static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3084 int Pos, int Size, int Low) {
3085 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3086 if (!isUndefOrEqual(Mask[i], Low))
3087 return false;
3088 return true;
3089}
3090
Nate Begeman9008ca62009-04-27 18:41:29 +00003091/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3092/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3093/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003094static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003095 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003096 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003097 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003098 return (Mask[0] < 2 && Mask[1] < 2);
3099 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003100}
3101
Nate Begeman9008ca62009-04-27 18:41:29 +00003102bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003103 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003104 N->getMask(M);
3105 return ::isPSHUFDMask(M, N->getValueType(0));
3106}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003107
Nate Begeman9008ca62009-04-27 18:41:29 +00003108/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3109/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003110static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003111 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003112 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003113
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 // Lower quadword copied in order or undef.
3115 for (int i = 0; i != 4; ++i)
3116 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003117 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003118
Evan Cheng506d3df2006-03-29 23:07:14 +00003119 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003120 for (int i = 4; i != 8; ++i)
3121 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003122 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003123
Evan Cheng506d3df2006-03-29 23:07:14 +00003124 return true;
3125}
3126
Nate Begeman9008ca62009-04-27 18:41:29 +00003127bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003128 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003129 N->getMask(M);
3130 return ::isPSHUFHWMask(M, N->getValueType(0));
3131}
Evan Cheng506d3df2006-03-29 23:07:14 +00003132
Nate Begeman9008ca62009-04-27 18:41:29 +00003133/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3134/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003135static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003136 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003137 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003138
Rafael Espindola15684b22009-04-24 12:40:33 +00003139 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003140 for (int i = 4; i != 8; ++i)
3141 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003142 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003143
Rafael Espindola15684b22009-04-24 12:40:33 +00003144 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003145 for (int i = 0; i != 4; ++i)
3146 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003147 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003148
Rafael Espindola15684b22009-04-24 12:40:33 +00003149 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003150}
3151
Nate Begeman9008ca62009-04-27 18:41:29 +00003152bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003153 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 N->getMask(M);
3155 return ::isPSHUFLWMask(M, N->getValueType(0));
3156}
3157
Nate Begemana09008b2009-10-19 02:17:23 +00003158/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3159/// is suitable for input to PALIGNR.
3160static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3161 bool hasSSSE3) {
3162 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003163 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3164 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003165
Nate Begemana09008b2009-10-19 02:17:23 +00003166 // Do not handle v2i64 / v2f64 shuffles with palignr.
3167 if (e < 4 || !hasSSSE3)
3168 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003169
Nate Begemana09008b2009-10-19 02:17:23 +00003170 for (i = 0; i != e; ++i)
3171 if (Mask[i] >= 0)
3172 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003173
Nate Begemana09008b2009-10-19 02:17:23 +00003174 // All undef, not a palignr.
3175 if (i == e)
3176 return false;
3177
Eli Friedman63f8dde2011-07-25 21:36:45 +00003178 // Make sure we're shifting in the right direction.
3179 if (Mask[i] <= i)
3180 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003181
3182 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003183
Nate Begemana09008b2009-10-19 02:17:23 +00003184 // Check the rest of the elements to see if they are consecutive.
3185 for (++i; i != e; ++i) {
3186 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003187 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003188 return false;
3189 }
3190 return true;
3191}
3192
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003193/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3194/// specifies a shuffle of elements that is suitable for input to 256-bit
3195/// VSHUFPSY.
3196static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3197 const X86Subtarget *Subtarget) {
3198 int NumElems = VT.getVectorNumElements();
3199
3200 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3201 return false;
3202
3203 if (NumElems != 8)
3204 return false;
3205
3206 // VSHUFPSY divides the resulting vector into 4 chunks.
3207 // The sources are also splitted into 4 chunks, and each destination
3208 // chunk must come from a different source chunk.
3209 //
3210 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3211 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3212 //
3213 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3214 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3215 //
3216 int QuarterSize = NumElems/4;
3217 int HalfSize = QuarterSize*2;
3218 for (int i = 0; i < QuarterSize; ++i)
3219 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3220 return false;
3221 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3222 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3223 return false;
3224
3225 // The mask of the second half must be the same as the first but with
3226 // the appropriate offsets. This works in the same way as VPERMILPS
3227 // works with masks.
3228 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3229 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3230 return false;
3231 int FstHalfIdx = i-HalfSize;
3232 if (Mask[FstHalfIdx] < 0)
3233 continue;
3234 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3235 return false;
3236 }
3237 for (int i = QuarterSize*3; i < NumElems; ++i) {
3238 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3239 return false;
3240 int FstHalfIdx = i-HalfSize;
3241 if (Mask[FstHalfIdx] < 0)
3242 continue;
3243 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3244 return false;
3245
3246 }
3247
3248 return true;
3249}
3250
3251/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3252/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3253static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3254 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3255 EVT VT = SVOp->getValueType(0);
3256 int NumElems = VT.getVectorNumElements();
3257
3258 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3259 "Only supports v8i32 and v8f32 types");
3260
3261 int HalfSize = NumElems/2;
3262 unsigned Mask = 0;
3263 for (int i = 0; i != NumElems ; ++i) {
3264 if (SVOp->getMaskElt(i) < 0)
3265 continue;
3266 // The mask of the first half must be equal to the second one.
3267 unsigned Shamt = (i%HalfSize)*2;
3268 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3269 Mask |= Elt << Shamt;
3270 }
3271
3272 return Mask;
3273}
3274
3275/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3276/// specifies a shuffle of elements that is suitable for input to 256-bit
3277/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3278/// version and the mask of the second half isn't binded with the first
3279/// one.
3280static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3281 const X86Subtarget *Subtarget) {
3282 int NumElems = VT.getVectorNumElements();
3283
3284 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3285 return false;
3286
3287 if (NumElems != 4)
3288 return false;
3289
3290 // VSHUFPSY divides the resulting vector into 4 chunks.
3291 // The sources are also splitted into 4 chunks, and each destination
3292 // chunk must come from a different source chunk.
3293 //
3294 // SRC1 => X3 X2 X1 X0
3295 // SRC2 => Y3 Y2 Y1 Y0
3296 //
3297 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3298 //
3299 int QuarterSize = NumElems/4;
3300 int HalfSize = QuarterSize*2;
3301 for (int i = 0; i < QuarterSize; ++i)
3302 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3303 return false;
3304 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3305 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3306 return false;
3307 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3308 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3309 return false;
3310 for (int i = QuarterSize*3; i < NumElems; ++i)
3311 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3312 return false;
3313
3314 return true;
3315}
3316
3317/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3318/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3319static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3320 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3321 EVT VT = SVOp->getValueType(0);
3322 int NumElems = VT.getVectorNumElements();
3323
3324 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3325 "Only supports v4i64 and v4f64 types");
3326
3327 int HalfSize = NumElems/2;
3328 unsigned Mask = 0;
3329 for (int i = 0; i != NumElems ; ++i) {
3330 if (SVOp->getMaskElt(i) < 0)
3331 continue;
3332 int Elt = SVOp->getMaskElt(i) % HalfSize;
3333 Mask |= Elt << i;
3334 }
3335
3336 return Mask;
3337}
3338
Evan Cheng14aed5e2006-03-24 01:18:28 +00003339/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003340/// specifies a shuffle of elements that is suitable for input to 128-bit
3341/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003342static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003343 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003344
3345 if (VT.getSizeInBits() != 128)
3346 return false;
3347
Nate Begeman9008ca62009-04-27 18:41:29 +00003348 if (NumElems != 2 && NumElems != 4)
3349 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003350
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 int Half = NumElems / 2;
3352 for (int i = 0; i < Half; ++i)
3353 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003354 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003355 for (int i = Half; i < NumElems; ++i)
3356 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003357 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003358
Evan Cheng14aed5e2006-03-24 01:18:28 +00003359 return true;
3360}
3361
Nate Begeman9008ca62009-04-27 18:41:29 +00003362bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3363 SmallVector<int, 8> M;
3364 N->getMask(M);
3365 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003366}
3367
Evan Cheng213d2cf2007-05-17 18:45:50 +00003368/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003369/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3370/// half elements to come from vector 1 (which would equal the dest.) and
3371/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003372static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003373 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003374
3375 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003376 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003377
Nate Begeman9008ca62009-04-27 18:41:29 +00003378 int Half = NumElems / 2;
3379 for (int i = 0; i < Half; ++i)
3380 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003381 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 for (int i = Half; i < NumElems; ++i)
3383 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003384 return false;
3385 return true;
3386}
3387
Nate Begeman9008ca62009-04-27 18:41:29 +00003388static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3389 SmallVector<int, 8> M;
3390 N->getMask(M);
3391 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003392}
3393
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003394/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3395/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003396bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003397 EVT VT = N->getValueType(0);
3398 unsigned NumElems = VT.getVectorNumElements();
3399
3400 if (VT.getSizeInBits() != 128)
3401 return false;
3402
3403 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003404 return false;
3405
Evan Cheng2064a2b2006-03-28 06:50:32 +00003406 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3408 isUndefOrEqual(N->getMaskElt(1), 7) &&
3409 isUndefOrEqual(N->getMaskElt(2), 2) &&
3410 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003411}
3412
Nate Begeman0b10b912009-11-07 23:17:15 +00003413/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3414/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3415/// <2, 3, 2, 3>
3416bool X86::isMOVHLPS_v_undef_Mask(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;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003422
Nate Begeman0b10b912009-11-07 23:17:15 +00003423 if (NumElems != 4)
3424 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003425
Nate Begeman0b10b912009-11-07 23:17:15 +00003426 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003427 isUndefOrEqual(N->getMaskElt(1), 3) &&
3428 isUndefOrEqual(N->getMaskElt(2), 2) &&
3429 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003430}
3431
Evan Cheng5ced1d82006-04-06 23:23:56 +00003432/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3433/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003434bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3435 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003436
Evan Cheng5ced1d82006-04-06 23:23:56 +00003437 if (NumElems != 2 && NumElems != 4)
3438 return false;
3439
Evan Chengc5cdff22006-04-07 21:53:05 +00003440 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003442 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003443
Evan Chengc5cdff22006-04-07 21:53:05 +00003444 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003446 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447
3448 return true;
3449}
3450
Nate Begeman0b10b912009-11-07 23:17:15 +00003451/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3452/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3453bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003455
David Greenea20244d2011-03-02 17:23:43 +00003456 if ((NumElems != 2 && NumElems != 4)
3457 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458 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))
Evan Chengc5cdff22006-04-07 21:53:05 +00003462 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003463
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 for (unsigned i = 0; i < NumElems/2; ++i)
3465 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003466 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003467
3468 return true;
3469}
3470
Evan Cheng0038e592006-03-28 00:39:58 +00003471/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3472/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003473static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003474 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003476
3477 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3478 "Unsupported vector type for unpckh");
3479
3480 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003481 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003482
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003483 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3484 // independently on 128-bit lanes.
3485 unsigned NumLanes = VT.getSizeInBits()/128;
3486 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003487
3488 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003489 unsigned End = NumLaneElts;
3490 for (unsigned s = 0; s < NumLanes; ++s) {
3491 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003492 i != End;
3493 i += 2, ++j) {
3494 int BitI = Mask[i];
3495 int BitI1 = Mask[i+1];
3496 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003497 return false;
David Greenea20244d2011-03-02 17:23:43 +00003498 if (V2IsSplat) {
3499 if (!isUndefOrEqual(BitI1, NumElts))
3500 return false;
3501 } else {
3502 if (!isUndefOrEqual(BitI1, j + NumElts))
3503 return false;
3504 }
Evan Cheng39623da2006-04-20 08:58:49 +00003505 }
David Greenea20244d2011-03-02 17:23:43 +00003506 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003507 Start += NumLaneElts;
3508 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003509 }
David Greenea20244d2011-03-02 17:23:43 +00003510
Evan Cheng0038e592006-03-28 00:39:58 +00003511 return true;
3512}
3513
Nate Begeman9008ca62009-04-27 18:41:29 +00003514bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3515 SmallVector<int, 8> M;
3516 N->getMask(M);
3517 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003518}
3519
Evan Cheng4fcb9222006-03-28 02:43:26 +00003520/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3521/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003522static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003523 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003524 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003525
3526 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3527 "Unsupported vector type for unpckh");
3528
3529 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003530 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003531
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003532 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3533 // independently on 128-bit lanes.
3534 unsigned NumLanes = VT.getSizeInBits()/128;
3535 unsigned NumLaneElts = NumElts/NumLanes;
3536
3537 unsigned Start = 0;
3538 unsigned End = NumLaneElts;
3539 for (unsigned l = 0; l != NumLanes; ++l) {
3540 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3541 i != End; i += 2, ++j) {
3542 int BitI = Mask[i];
3543 int BitI1 = Mask[i+1];
3544 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003545 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003546 if (V2IsSplat) {
3547 if (isUndefOrEqual(BitI1, NumElts))
3548 return false;
3549 } else {
3550 if (!isUndefOrEqual(BitI1, j+NumElts))
3551 return false;
3552 }
Evan Cheng39623da2006-04-20 08:58:49 +00003553 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003554 // Process the next 128 bits.
3555 Start += NumLaneElts;
3556 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003557 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003558 return true;
3559}
3560
Nate Begeman9008ca62009-04-27 18:41:29 +00003561bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3562 SmallVector<int, 8> M;
3563 N->getMask(M);
3564 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003565}
3566
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003567/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3568/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3569/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003570static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003571 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003572 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003573 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003574
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003575 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3576 // FIXME: Need a better way to get rid of this, there's no latency difference
3577 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3578 // the former later. We should also remove the "_undef" special mask.
3579 if (NumElems == 4 && VT.getSizeInBits() == 256)
3580 return false;
3581
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003582 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3583 // independently on 128-bit lanes.
3584 unsigned NumLanes = VT.getSizeInBits() / 128;
3585 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003586
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003587 for (unsigned s = 0; s < NumLanes; ++s) {
3588 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3589 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003590 i += 2, ++j) {
3591 int BitI = Mask[i];
3592 int BitI1 = Mask[i+1];
3593
3594 if (!isUndefOrEqual(BitI, j))
3595 return false;
3596 if (!isUndefOrEqual(BitI1, j))
3597 return false;
3598 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003599 }
David Greenea20244d2011-03-02 17:23:43 +00003600
Rafael Espindola15684b22009-04-24 12:40:33 +00003601 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003602}
3603
Nate Begeman9008ca62009-04-27 18:41:29 +00003604bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3605 SmallVector<int, 8> M;
3606 N->getMask(M);
3607 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3608}
3609
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003610/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3611/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3612/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003613static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003615 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3616 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003617
Nate Begeman9008ca62009-04-27 18:41:29 +00003618 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3619 int BitI = Mask[i];
3620 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003621 if (!isUndefOrEqual(BitI, j))
3622 return false;
3623 if (!isUndefOrEqual(BitI1, j))
3624 return false;
3625 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003626 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003627}
3628
Nate Begeman9008ca62009-04-27 18:41:29 +00003629bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3630 SmallVector<int, 8> M;
3631 N->getMask(M);
3632 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3633}
3634
Evan Cheng017dcc62006-04-21 01:05:10 +00003635/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3636/// specifies a shuffle of elements that is suitable for input to MOVSS,
3637/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003638static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003639 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003640 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003641
3642 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003643
Nate Begeman9008ca62009-04-27 18:41:29 +00003644 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003645 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003646
Nate Begeman9008ca62009-04-27 18:41:29 +00003647 for (int i = 1; i < NumElts; ++i)
3648 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003649 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003650
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003651 return true;
3652}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003653
Nate Begeman9008ca62009-04-27 18:41:29 +00003654bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3655 SmallVector<int, 8> M;
3656 N->getMask(M);
3657 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003658}
3659
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003660/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3661/// as permutations between 128-bit chunks or halves. As an example: this
3662/// shuffle bellow:
3663/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3664/// The first half comes from the second half of V1 and the second half from the
3665/// the second half of V2.
3666static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3667 const X86Subtarget *Subtarget) {
3668 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3669 return false;
3670
3671 // The shuffle result is divided into half A and half B. In total the two
3672 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3673 // B must come from C, D, E or F.
3674 int HalfSize = VT.getVectorNumElements()/2;
3675 bool MatchA = false, MatchB = false;
3676
3677 // Check if A comes from one of C, D, E, F.
3678 for (int Half = 0; Half < 4; ++Half) {
3679 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3680 MatchA = true;
3681 break;
3682 }
3683 }
3684
3685 // Check if B comes from one of C, D, E, F.
3686 for (int Half = 0; Half < 4; ++Half) {
3687 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3688 MatchB = true;
3689 break;
3690 }
3691 }
3692
3693 return MatchA && MatchB;
3694}
3695
3696/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3697/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3698static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3699 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3700 EVT VT = SVOp->getValueType(0);
3701
3702 int HalfSize = VT.getVectorNumElements()/2;
3703
3704 int FstHalf = 0, SndHalf = 0;
3705 for (int i = 0; i < HalfSize; ++i) {
3706 if (SVOp->getMaskElt(i) > 0) {
3707 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3708 break;
3709 }
3710 }
3711 for (int i = HalfSize; i < HalfSize*2; ++i) {
3712 if (SVOp->getMaskElt(i) > 0) {
3713 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3714 break;
3715 }
3716 }
3717
3718 return (FstHalf | (SndHalf << 4));
3719}
3720
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003721/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3722/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3723/// Note that VPERMIL mask matching is different depending whether theunderlying
3724/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3725/// to the same elements of the low, but to the higher half of the source.
3726/// In VPERMILPD the two lanes could be shuffled independently of each other
3727/// with the same restriction that lanes can't be crossed.
3728static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3729 const X86Subtarget *Subtarget) {
3730 int NumElts = VT.getVectorNumElements();
3731 int NumLanes = VT.getSizeInBits()/128;
3732
3733 if (!Subtarget->hasAVX())
3734 return false;
3735
3736 // Match any permutation of 128-bit vector with 64-bit types
3737 if (NumLanes == 1 && NumElts != 2)
3738 return false;
3739
3740 // Only match 256-bit with 32 types
3741 if (VT.getSizeInBits() == 256 && NumElts != 4)
3742 return false;
3743
3744 // The mask on the high lane is independent of the low. Both can match
3745 // any element in inside its own lane, but can't cross.
3746 int LaneSize = NumElts/NumLanes;
3747 for (int l = 0; l < NumLanes; ++l)
3748 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3749 int LaneStart = l*LaneSize;
3750 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3751 return false;
3752 }
3753
3754 return true;
3755}
3756
3757/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3758/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3759/// Note that VPERMIL mask matching is different depending whether theunderlying
3760/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3761/// to the same elements of the low, but to the higher half of the source.
3762/// In VPERMILPD the two lanes could be shuffled independently of each other
3763/// with the same restriction that lanes can't be crossed.
3764static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3765 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003766 unsigned NumElts = VT.getVectorNumElements();
3767 unsigned NumLanes = VT.getSizeInBits()/128;
3768
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003769 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003770 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003771
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003772 // Match any permutation of 128-bit vector with 32-bit types
3773 if (NumLanes == 1 && NumElts != 4)
3774 return false;
3775
3776 // Only match 256-bit with 32 types
3777 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003778 return false;
3779
3780 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003781 // they can differ if any of the corresponding index in a lane is undef
3782 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003783 int LaneSize = NumElts/NumLanes;
3784 for (int i = 0; i < LaneSize; ++i) {
3785 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003786 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3787 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3788
3789 if (!HighValid || !LowValid)
3790 return false;
3791 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003792 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003793 if (Mask[HighElt]-Mask[i] != LaneSize)
3794 return false;
3795 }
3796
3797 return true;
3798}
3799
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003800/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3801/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3802static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003803 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3804 EVT VT = SVOp->getValueType(0);
3805
3806 int NumElts = VT.getVectorNumElements();
3807 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003808 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003809
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003810 // Although the mask is equal for both lanes do it twice to get the cases
3811 // where a mask will match because the same mask element is undef on the
3812 // first half but valid on the second. This would get pathological cases
3813 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003814 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003815 for (int l = 0; l < NumLanes; ++l) {
3816 for (int i = 0; i < LaneSize; ++i) {
3817 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3818 if (MaskElt < 0)
3819 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003820 if (MaskElt >= LaneSize)
3821 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003822 Mask |= MaskElt << (i*2);
3823 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003824 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003825
3826 return Mask;
3827}
3828
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003829/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3830/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3831static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3832 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3833 EVT VT = SVOp->getValueType(0);
3834
3835 int NumElts = VT.getVectorNumElements();
3836 int NumLanes = VT.getSizeInBits()/128;
3837
3838 unsigned Mask = 0;
3839 int LaneSize = NumElts/NumLanes;
3840 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003841 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3842 int MaskElt = SVOp->getMaskElt(i);
3843 if (MaskElt < 0)
3844 continue;
3845 Mask |= (MaskElt-l*LaneSize) << i;
3846 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003847
3848 return Mask;
3849}
3850
Evan Cheng017dcc62006-04-21 01:05:10 +00003851/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3852/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003853/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003854static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003855 bool V2IsSplat = false, bool V2IsUndef = false) {
3856 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003857 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003858 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003859
Nate Begeman9008ca62009-04-27 18:41:29 +00003860 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003861 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003862
Nate Begeman9008ca62009-04-27 18:41:29 +00003863 for (int i = 1; i < NumOps; ++i)
3864 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3865 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3866 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003867 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003868
Evan Cheng39623da2006-04-20 08:58:49 +00003869 return true;
3870}
3871
Nate Begeman9008ca62009-04-27 18:41:29 +00003872static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003873 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 SmallVector<int, 8> M;
3875 N->getMask(M);
3876 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003877}
3878
Evan Chengd9539472006-04-14 21:59:03 +00003879/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3880/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003881/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3882bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3883 const X86Subtarget *Subtarget) {
3884 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003885 return false;
3886
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003887 // The second vector must be undef
3888 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3889 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003890
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003891 EVT VT = N->getValueType(0);
3892 unsigned NumElems = VT.getVectorNumElements();
3893
3894 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3895 (VT.getSizeInBits() == 256 && NumElems != 8))
3896 return false;
3897
3898 // "i+1" is the value the indexed mask element must have
3899 for (unsigned i = 0; i < NumElems; i += 2)
3900 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3901 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003902 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003903
3904 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003905}
3906
3907/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3908/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003909/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3910bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3911 const X86Subtarget *Subtarget) {
3912 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003913 return false;
3914
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003915 // The second vector must be undef
3916 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3917 return false;
3918
3919 EVT VT = N->getValueType(0);
3920 unsigned NumElems = VT.getVectorNumElements();
3921
3922 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3923 (VT.getSizeInBits() == 256 && NumElems != 8))
3924 return false;
3925
3926 // "i" is the value the indexed mask element must have
3927 for (unsigned i = 0; i < NumElems; i += 2)
3928 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3929 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003931
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003932 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003933}
3934
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003935/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3936/// specifies a shuffle of elements that is suitable for input to 256-bit
3937/// version of MOVDDUP.
3938static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3939 const X86Subtarget *Subtarget) {
3940 EVT VT = N->getValueType(0);
3941 int NumElts = VT.getVectorNumElements();
3942 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3943
3944 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3945 !V2IsUndef || NumElts != 4)
3946 return false;
3947
3948 for (int i = 0; i != NumElts/2; ++i)
3949 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3950 return false;
3951 for (int i = NumElts/2; i != NumElts; ++i)
3952 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3953 return false;
3954 return true;
3955}
3956
Evan Cheng0b457f02008-09-25 20:50:48 +00003957/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003958/// specifies a shuffle of elements that is suitable for input to 128-bit
3959/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003960bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003961 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003962
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003963 if (VT.getSizeInBits() != 128)
3964 return false;
3965
3966 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003967 for (int i = 0; i < e; ++i)
3968 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003969 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003970 for (int i = 0; i < e; ++i)
3971 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003972 return false;
3973 return true;
3974}
3975
David Greenec38a03e2011-02-03 15:50:00 +00003976/// isVEXTRACTF128Index - Return true if the specified
3977/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3978/// suitable for input to VEXTRACTF128.
3979bool X86::isVEXTRACTF128Index(SDNode *N) {
3980 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3981 return false;
3982
3983 // The index should be aligned on a 128-bit boundary.
3984 uint64_t Index =
3985 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3986
3987 unsigned VL = N->getValueType(0).getVectorNumElements();
3988 unsigned VBits = N->getValueType(0).getSizeInBits();
3989 unsigned ElSize = VBits / VL;
3990 bool Result = (Index * ElSize) % 128 == 0;
3991
3992 return Result;
3993}
3994
David Greeneccacdc12011-02-04 16:08:29 +00003995/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3996/// operand specifies a subvector insert that is suitable for input to
3997/// VINSERTF128.
3998bool X86::isVINSERTF128Index(SDNode *N) {
3999 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4000 return false;
4001
4002 // The index should be aligned on a 128-bit boundary.
4003 uint64_t Index =
4004 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4005
4006 unsigned VL = N->getValueType(0).getVectorNumElements();
4007 unsigned VBits = N->getValueType(0).getSizeInBits();
4008 unsigned ElSize = VBits / VL;
4009 bool Result = (Index * ElSize) % 128 == 0;
4010
4011 return Result;
4012}
4013
Evan Cheng63d33002006-03-22 08:01:21 +00004014/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004015/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004016unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004017 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4018 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4019
Evan Chengb9df0ca2006-03-22 02:53:00 +00004020 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4021 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004022 for (int i = 0; i < NumOperands; ++i) {
4023 int Val = SVOp->getMaskElt(NumOperands-i-1);
4024 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004025 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004026 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004027 if (i != NumOperands - 1)
4028 Mask <<= Shift;
4029 }
Evan Cheng63d33002006-03-22 08:01:21 +00004030 return Mask;
4031}
4032
Evan Cheng506d3df2006-03-29 23:07:14 +00004033/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004034/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004035unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004036 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004037 unsigned Mask = 0;
4038 // 8 nodes, but we only care about the last 4.
4039 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004040 int Val = SVOp->getMaskElt(i);
4041 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004042 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004043 if (i != 4)
4044 Mask <<= 2;
4045 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004046 return Mask;
4047}
4048
4049/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004050/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004051unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004052 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004053 unsigned Mask = 0;
4054 // 8 nodes, but we only care about the first 4.
4055 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 int Val = SVOp->getMaskElt(i);
4057 if (Val >= 0)
4058 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004059 if (i != 0)
4060 Mask <<= 2;
4061 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004062 return Mask;
4063}
4064
Nate Begemana09008b2009-10-19 02:17:23 +00004065/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4066/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4067unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4068 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4069 EVT VVT = N->getValueType(0);
4070 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4071 int Val = 0;
4072
4073 unsigned i, e;
4074 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4075 Val = SVOp->getMaskElt(i);
4076 if (Val >= 0)
4077 break;
4078 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004079 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004080 return (Val - i) * EltSize;
4081}
4082
David Greenec38a03e2011-02-03 15:50:00 +00004083/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4084/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4085/// instructions.
4086unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4087 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4088 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4089
4090 uint64_t Index =
4091 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4092
4093 EVT VecVT = N->getOperand(0).getValueType();
4094 EVT ElVT = VecVT.getVectorElementType();
4095
4096 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004097 return Index / NumElemsPerChunk;
4098}
4099
David Greeneccacdc12011-02-04 16:08:29 +00004100/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4101/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4102/// instructions.
4103unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4104 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4105 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4106
4107 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004108 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004109
4110 EVT VecVT = N->getValueType(0);
4111 EVT ElVT = VecVT.getVectorElementType();
4112
4113 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004114 return Index / NumElemsPerChunk;
4115}
4116
Evan Cheng37b73872009-07-30 08:33:02 +00004117/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4118/// constant +0.0.
4119bool X86::isZeroNode(SDValue Elt) {
4120 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004121 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004122 (isa<ConstantFPSDNode>(Elt) &&
4123 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4124}
4125
Nate Begeman9008ca62009-04-27 18:41:29 +00004126/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4127/// their permute mask.
4128static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4129 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004130 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004131 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004132 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004133
Nate Begeman5a5ca152009-04-29 05:20:52 +00004134 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004135 int idx = SVOp->getMaskElt(i);
4136 if (idx < 0)
4137 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004138 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004139 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004140 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004141 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004142 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004143 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4144 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004145}
4146
Evan Cheng779ccea2007-12-07 21:30:01 +00004147/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4148/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004149static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004150 unsigned NumElems = VT.getVectorNumElements();
4151 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004152 int idx = Mask[i];
4153 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004154 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004155 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004156 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004157 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004158 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004159 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004160}
4161
Evan Cheng533a0aa2006-04-19 20:35:22 +00004162/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4163/// match movhlps. The lower half elements should come from upper half of
4164/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004165/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004166static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004167 EVT VT = Op->getValueType(0);
4168 if (VT.getSizeInBits() != 128)
4169 return false;
4170 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004171 return false;
4172 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004173 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004174 return false;
4175 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004176 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004177 return false;
4178 return true;
4179}
4180
Evan Cheng5ced1d82006-04-06 23:23:56 +00004181/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004182/// is promoted to a vector. It also returns the LoadSDNode by reference if
4183/// required.
4184static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004185 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4186 return false;
4187 N = N->getOperand(0).getNode();
4188 if (!ISD::isNON_EXTLoad(N))
4189 return false;
4190 if (LD)
4191 *LD = cast<LoadSDNode>(N);
4192 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004193}
4194
Evan Cheng533a0aa2006-04-19 20:35:22 +00004195/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4196/// match movlp{s|d}. The lower half elements should come from lower half of
4197/// V1 (and in order), and the upper half elements should come from the upper
4198/// half of V2 (and in order). And since V1 will become the source of the
4199/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004200static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4201 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004202 EVT VT = Op->getValueType(0);
4203 if (VT.getSizeInBits() != 128)
4204 return false;
4205
Evan Cheng466685d2006-10-09 20:57:25 +00004206 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004207 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004208 // Is V2 is a vector load, don't do this transformation. We will try to use
4209 // load folding shufps op.
4210 if (ISD::isNON_EXTLoad(V2))
4211 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004212
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004213 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004214
Evan Cheng533a0aa2006-04-19 20:35:22 +00004215 if (NumElems != 2 && NumElems != 4)
4216 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004217 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004218 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004219 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004220 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004221 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004222 return false;
4223 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004224}
4225
Evan Cheng39623da2006-04-20 08:58:49 +00004226/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4227/// all the same.
4228static bool isSplatVector(SDNode *N) {
4229 if (N->getOpcode() != ISD::BUILD_VECTOR)
4230 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004231
Dan Gohman475871a2008-07-27 21:46:04 +00004232 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004233 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4234 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004235 return false;
4236 return true;
4237}
4238
Evan Cheng213d2cf2007-05-17 18:45:50 +00004239/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004240/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004241/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004242static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004243 SDValue V1 = N->getOperand(0);
4244 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004245 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4246 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004247 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004248 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004249 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004250 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4251 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004252 if (Opc != ISD::BUILD_VECTOR ||
4253 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004254 return false;
4255 } else if (Idx >= 0) {
4256 unsigned Opc = V1.getOpcode();
4257 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4258 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004259 if (Opc != ISD::BUILD_VECTOR ||
4260 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004261 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004262 }
4263 }
4264 return true;
4265}
4266
4267/// getZeroVector - Returns a vector of specified type with all zero elements.
4268///
Owen Andersone50ed302009-08-10 22:56:29 +00004269static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004270 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004271 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004272
Dale Johannesen0488fb62010-09-30 23:57:10 +00004273 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004274 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004275 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004276 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004277 if (HasSSE2) { // SSE2
4278 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4279 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4280 } else { // SSE1
4281 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4282 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4283 }
4284 } else if (VT.getSizeInBits() == 256) { // AVX
4285 // 256-bit logic and arithmetic instructions in AVX are
4286 // all floating-point, no support for integer ops. Default
4287 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004288 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004289 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4290 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004291 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004292 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004293}
4294
Chris Lattner8a594482007-11-25 00:24:49 +00004295/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004296/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4297/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4298/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004299static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004300 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004301 assert((VT.is128BitVector() || VT.is256BitVector())
4302 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004303
Owen Anderson825b72b2009-08-11 20:47:22 +00004304 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004305 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4306 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004307
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004308 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004309 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4310 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4311 Vec = Insert128BitVector(InsV, Vec,
4312 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4313 }
4314
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004315 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004316}
4317
Evan Cheng39623da2006-04-20 08:58:49 +00004318/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4319/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004320static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004321 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004322 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004323
Evan Cheng39623da2006-04-20 08:58:49 +00004324 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004325 SmallVector<int, 8> MaskVec;
4326 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004327
Nate Begeman5a5ca152009-04-29 05:20:52 +00004328 for (unsigned i = 0; i != NumElems; ++i) {
4329 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 MaskVec[i] = NumElems;
4331 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004332 }
Evan Cheng39623da2006-04-20 08:58:49 +00004333 }
Evan Cheng39623da2006-04-20 08:58:49 +00004334 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004335 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4336 SVOp->getOperand(1), &MaskVec[0]);
4337 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004338}
4339
Evan Cheng017dcc62006-04-21 01:05:10 +00004340/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4341/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004342static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 SDValue V2) {
4344 unsigned NumElems = VT.getVectorNumElements();
4345 SmallVector<int, 8> Mask;
4346 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004347 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 Mask.push_back(i);
4349 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004350}
4351
Nate Begeman9008ca62009-04-27 18:41:29 +00004352/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004353static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004354 SDValue V2) {
4355 unsigned NumElems = VT.getVectorNumElements();
4356 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004357 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004358 Mask.push_back(i);
4359 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004360 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004362}
4363
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004364/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004365static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 SDValue V2) {
4367 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004368 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004370 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004371 Mask.push_back(i + Half);
4372 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004373 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004375}
4376
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004377// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004378// a generic shuffle instruction because the target has no such instructions.
4379// Generate shuffles which repeat i16 and i8 several times until they can be
4380// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004381static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004382 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004383 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004384 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004385
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 while (NumElems > 4) {
4387 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004388 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004390 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004391 EltNo -= NumElems/2;
4392 }
4393 NumElems >>= 1;
4394 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004395 return V;
4396}
Eric Christopherfd179292009-08-27 18:07:15 +00004397
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004398/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4399static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4400 EVT VT = V.getValueType();
4401 DebugLoc dl = V.getDebugLoc();
4402 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4403 && "Vector size not supported");
4404
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004405 if (VT.getSizeInBits() == 128) {
4406 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004407 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004408 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4409 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004410 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004411 // To use VPERMILPS to splat scalars, the second half of indicies must
4412 // refer to the higher part, which is a duplication of the lower one,
4413 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004414 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4415 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004416
4417 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4418 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4419 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004420 }
4421
4422 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4423}
4424
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004425/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004426static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4427 EVT SrcVT = SV->getValueType(0);
4428 SDValue V1 = SV->getOperand(0);
4429 DebugLoc dl = SV->getDebugLoc();
4430
4431 int EltNo = SV->getSplatIndex();
4432 int NumElems = SrcVT.getVectorNumElements();
4433 unsigned Size = SrcVT.getSizeInBits();
4434
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004435 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4436 "Unknown how to promote splat for type");
4437
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004438 // Extract the 128-bit part containing the splat element and update
4439 // the splat element index when it refers to the higher register.
4440 if (Size == 256) {
4441 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4442 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4443 if (Idx > 0)
4444 EltNo -= NumElems/2;
4445 }
4446
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004447 // All i16 and i8 vector types can't be used directly by a generic shuffle
4448 // instruction because the target has no such instruction. Generate shuffles
4449 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004450 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004451 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004452 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004453 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004454
4455 // Recreate the 256-bit vector and place the same 128-bit vector
4456 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004457 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004458 if (Size == 256) {
4459 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4460 DAG.getConstant(0, MVT::i32), DAG, dl);
4461 V1 = Insert128BitVector(InsV, V1,
4462 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4463 }
4464
4465 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004466}
4467
Evan Chengba05f722006-04-21 23:03:30 +00004468/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004469/// vector of zero or undef vector. This produces a shuffle where the low
4470/// element of V2 is swizzled into the zero/undef vector, landing at element
4471/// 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 +00004472static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00004473 bool isZero, bool HasSSE2,
4474 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004475 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004476 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4478 unsigned NumElems = VT.getVectorNumElements();
4479 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004480 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004481 // If this is the insertion idx, put the low elt of V2 here.
4482 MaskVec.push_back(i == Idx ? NumElems : i);
4483 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004484}
4485
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004486/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4487/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004488static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4489 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004490 if (Depth == 6)
4491 return SDValue(); // Limit search depth.
4492
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004493 SDValue V = SDValue(N, 0);
4494 EVT VT = V.getValueType();
4495 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004496
4497 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4498 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4499 Index = SV->getMaskElt(Index);
4500
4501 if (Index < 0)
4502 return DAG.getUNDEF(VT.getVectorElementType());
4503
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004504 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004505 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004506 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004507 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004508
4509 // Recurse into target specific vector shuffles to find scalars.
4510 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004511 int NumElems = VT.getVectorNumElements();
4512 SmallVector<unsigned, 16> ShuffleMask;
4513 SDValue ImmN;
4514
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004515 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004516 case X86ISD::SHUFPS:
4517 case X86ISD::SHUFPD:
4518 ImmN = N->getOperand(N->getNumOperands()-1);
4519 DecodeSHUFPSMask(NumElems,
4520 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4521 ShuffleMask);
4522 break;
4523 case X86ISD::PUNPCKHBW:
4524 case X86ISD::PUNPCKHWD:
4525 case X86ISD::PUNPCKHDQ:
4526 case X86ISD::PUNPCKHQDQ:
4527 DecodePUNPCKHMask(NumElems, ShuffleMask);
4528 break;
4529 case X86ISD::UNPCKHPS:
4530 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004531 case X86ISD::VUNPCKHPSY:
4532 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004533 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4534 break;
4535 case X86ISD::PUNPCKLBW:
4536 case X86ISD::PUNPCKLWD:
4537 case X86ISD::PUNPCKLDQ:
4538 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004539 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004540 break;
4541 case X86ISD::UNPCKLPS:
4542 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004543 case X86ISD::VUNPCKLPSY:
4544 case X86ISD::VUNPCKLPDY:
4545 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004546 break;
4547 case X86ISD::MOVHLPS:
4548 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4549 break;
4550 case X86ISD::MOVLHPS:
4551 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4552 break;
4553 case X86ISD::PSHUFD:
4554 ImmN = N->getOperand(N->getNumOperands()-1);
4555 DecodePSHUFMask(NumElems,
4556 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4557 ShuffleMask);
4558 break;
4559 case X86ISD::PSHUFHW:
4560 ImmN = N->getOperand(N->getNumOperands()-1);
4561 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4562 ShuffleMask);
4563 break;
4564 case X86ISD::PSHUFLW:
4565 ImmN = N->getOperand(N->getNumOperands()-1);
4566 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4567 ShuffleMask);
4568 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004569 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004570 case X86ISD::MOVSD: {
4571 // The index 0 always comes from the first element of the second source,
4572 // this is why MOVSS and MOVSD are used in the first place. The other
4573 // elements come from the other positions of the first source vector.
4574 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004575 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4576 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004577 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004578 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004579 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004580 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004581 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004582 break;
4583 case X86ISD::VPERMILPSY:
4584 ImmN = N->getOperand(N->getNumOperands()-1);
4585 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4586 ShuffleMask);
4587 break;
4588 case X86ISD::VPERMILPD:
4589 ImmN = N->getOperand(N->getNumOperands()-1);
4590 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4591 ShuffleMask);
4592 break;
4593 case X86ISD::VPERMILPDY:
4594 ImmN = N->getOperand(N->getNumOperands()-1);
4595 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4596 ShuffleMask);
4597 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004598 case X86ISD::VPERM2F128:
4599 ImmN = N->getOperand(N->getNumOperands()-1);
4600 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4601 ShuffleMask);
4602 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004603 default:
4604 assert("not implemented for target shuffle node");
4605 return SDValue();
4606 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004607
4608 Index = ShuffleMask[Index];
4609 if (Index < 0)
4610 return DAG.getUNDEF(VT.getVectorElementType());
4611
4612 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4613 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4614 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004615 }
4616
4617 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004618 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004619 V = V.getOperand(0);
4620 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004621 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004622
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004623 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004624 return SDValue();
4625 }
4626
4627 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4628 return (Index == 0) ? V.getOperand(0)
4629 : DAG.getUNDEF(VT.getVectorElementType());
4630
4631 if (V.getOpcode() == ISD::BUILD_VECTOR)
4632 return V.getOperand(Index);
4633
4634 return SDValue();
4635}
4636
4637/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4638/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004639/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004640static
4641unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4642 bool ZerosFromLeft, SelectionDAG &DAG) {
4643 int i = 0;
4644
4645 while (i < NumElems) {
4646 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004647 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648 if (!(Elt.getNode() &&
4649 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4650 break;
4651 ++i;
4652 }
4653
4654 return i;
4655}
4656
4657/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4658/// MaskE correspond consecutively to elements from one of the vector operands,
4659/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4660static
4661bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4662 int OpIdx, int NumElems, unsigned &OpNum) {
4663 bool SeenV1 = false;
4664 bool SeenV2 = false;
4665
4666 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4667 int Idx = SVOp->getMaskElt(i);
4668 // Ignore undef indicies
4669 if (Idx < 0)
4670 continue;
4671
4672 if (Idx < NumElems)
4673 SeenV1 = true;
4674 else
4675 SeenV2 = true;
4676
4677 // Only accept consecutive elements from the same vector
4678 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4679 return false;
4680 }
4681
4682 OpNum = SeenV1 ? 0 : 1;
4683 return true;
4684}
4685
4686/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4687/// logical left shift of a vector.
4688static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4689 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4690 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4691 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4692 false /* check zeros from right */, DAG);
4693 unsigned OpSrc;
4694
4695 if (!NumZeros)
4696 return false;
4697
4698 // Considering the elements in the mask that are not consecutive zeros,
4699 // check if they consecutively come from only one of the source vectors.
4700 //
4701 // V1 = {X, A, B, C} 0
4702 // \ \ \ /
4703 // vector_shuffle V1, V2 <1, 2, 3, X>
4704 //
4705 if (!isShuffleMaskConsecutive(SVOp,
4706 0, // Mask Start Index
4707 NumElems-NumZeros-1, // Mask End Index
4708 NumZeros, // Where to start looking in the src vector
4709 NumElems, // Number of elements in vector
4710 OpSrc)) // Which source operand ?
4711 return false;
4712
4713 isLeft = false;
4714 ShAmt = NumZeros;
4715 ShVal = SVOp->getOperand(OpSrc);
4716 return true;
4717}
4718
4719/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4720/// logical left shift of a vector.
4721static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4722 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4723 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4724 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4725 true /* check zeros from left */, DAG);
4726 unsigned OpSrc;
4727
4728 if (!NumZeros)
4729 return false;
4730
4731 // Considering the elements in the mask that are not consecutive zeros,
4732 // check if they consecutively come from only one of the source vectors.
4733 //
4734 // 0 { A, B, X, X } = V2
4735 // / \ / /
4736 // vector_shuffle V1, V2 <X, X, 4, 5>
4737 //
4738 if (!isShuffleMaskConsecutive(SVOp,
4739 NumZeros, // Mask Start Index
4740 NumElems-1, // Mask End Index
4741 0, // Where to start looking in the src vector
4742 NumElems, // Number of elements in vector
4743 OpSrc)) // Which source operand ?
4744 return false;
4745
4746 isLeft = true;
4747 ShAmt = NumZeros;
4748 ShVal = SVOp->getOperand(OpSrc);
4749 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004750}
4751
4752/// isVectorShift - Returns true if the shuffle can be implemented as a
4753/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004754static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004755 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004756 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4757 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4758 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004759
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004760 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004761}
4762
Evan Chengc78d3b42006-04-24 18:01:45 +00004763/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4764///
Dan Gohman475871a2008-07-27 21:46:04 +00004765static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004766 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004767 SelectionDAG &DAG,
4768 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004769 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004770 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004771
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004772 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004773 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004774 bool First = true;
4775 for (unsigned i = 0; i < 16; ++i) {
4776 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4777 if (ThisIsNonZero && First) {
4778 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004779 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004780 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004781 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004782 First = false;
4783 }
4784
4785 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004786 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004787 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4788 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004789 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004791 }
4792 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004793 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4794 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4795 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004796 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004797 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004798 } else
4799 ThisElt = LastElt;
4800
Gabor Greifba36cb52008-08-28 21:40:38 +00004801 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004802 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004803 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004804 }
4805 }
4806
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004807 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004808}
4809
Bill Wendlinga348c562007-03-22 18:42:45 +00004810/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004811///
Dan Gohman475871a2008-07-27 21:46:04 +00004812static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004813 unsigned NumNonZero, unsigned NumZero,
4814 SelectionDAG &DAG,
4815 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004816 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004817 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004818
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004819 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004820 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004821 bool First = true;
4822 for (unsigned i = 0; i < 8; ++i) {
4823 bool isNonZero = (NonZeros & (1 << i)) != 0;
4824 if (isNonZero) {
4825 if (First) {
4826 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004828 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004830 First = false;
4831 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004832 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004833 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004834 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004835 }
4836 }
4837
4838 return V;
4839}
4840
Evan Chengf26ffe92008-05-29 08:22:04 +00004841/// getVShift - Return a vector logical shift node.
4842///
Owen Andersone50ed302009-08-10 22:56:29 +00004843static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004844 unsigned NumBits, SelectionDAG &DAG,
4845 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004846 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004847 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004848 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4849 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004850 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004851 DAG.getConstant(NumBits,
4852 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004853}
4854
Dan Gohman475871a2008-07-27 21:46:04 +00004855SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004856X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004857 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004858
Evan Chengc3630942009-12-09 21:00:30 +00004859 // Check if the scalar load can be widened into a vector load. And if
4860 // the address is "base + cst" see if the cst can be "absorbed" into
4861 // the shuffle mask.
4862 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4863 SDValue Ptr = LD->getBasePtr();
4864 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4865 return SDValue();
4866 EVT PVT = LD->getValueType(0);
4867 if (PVT != MVT::i32 && PVT != MVT::f32)
4868 return SDValue();
4869
4870 int FI = -1;
4871 int64_t Offset = 0;
4872 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4873 FI = FINode->getIndex();
4874 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004875 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004876 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4877 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4878 Offset = Ptr.getConstantOperandVal(1);
4879 Ptr = Ptr.getOperand(0);
4880 } else {
4881 return SDValue();
4882 }
4883
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004884 // FIXME: 256-bit vector instructions don't require a strict alignment,
4885 // improve this code to support it better.
4886 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004887 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004888 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004889 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004890 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004891 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004892 // Can't change the alignment. FIXME: It's possible to compute
4893 // the exact stack offset and reference FI + adjust offset instead.
4894 // If someone *really* cares about this. That's the way to implement it.
4895 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004896 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004897 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004898 }
4899 }
4900
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004901 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004902 // Ptr + (Offset & ~15).
4903 if (Offset < 0)
4904 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004905 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004906 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004907 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004908 if (StartOffset)
4909 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4910 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4911
4912 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004913 int NumElems = VT.getVectorNumElements();
4914
4915 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4916 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4917 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004918 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004919 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004920
4921 // Canonicalize it to a v4i32 or v8i32 shuffle.
4922 SmallVector<int, 8> Mask;
4923 for (int i = 0; i < NumElems; ++i)
4924 Mask.push_back(EltNo);
4925
4926 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4927 return DAG.getNode(ISD::BITCAST, dl, NVT,
4928 DAG.getVectorShuffle(CanonVT, dl, V1,
4929 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004930 }
4931
4932 return SDValue();
4933}
4934
Michael J. Spencerec38de22010-10-10 22:04:20 +00004935/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4936/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004937/// load which has the same value as a build_vector whose operands are 'elts'.
4938///
4939/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004940///
Nate Begeman1449f292010-03-24 22:19:06 +00004941/// FIXME: we'd also like to handle the case where the last elements are zero
4942/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4943/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004944static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004945 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004946 EVT EltVT = VT.getVectorElementType();
4947 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004948
Nate Begemanfdea31a2010-03-24 20:49:50 +00004949 LoadSDNode *LDBase = NULL;
4950 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004951
Nate Begeman1449f292010-03-24 22:19:06 +00004952 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004953 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004954 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004955 for (unsigned i = 0; i < NumElems; ++i) {
4956 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004957
Nate Begemanfdea31a2010-03-24 20:49:50 +00004958 if (!Elt.getNode() ||
4959 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4960 return SDValue();
4961 if (!LDBase) {
4962 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4963 return SDValue();
4964 LDBase = cast<LoadSDNode>(Elt.getNode());
4965 LastLoadedElt = i;
4966 continue;
4967 }
4968 if (Elt.getOpcode() == ISD::UNDEF)
4969 continue;
4970
4971 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4972 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4973 return SDValue();
4974 LastLoadedElt = i;
4975 }
Nate Begeman1449f292010-03-24 22:19:06 +00004976
4977 // If we have found an entire vector of loads and undefs, then return a large
4978 // load of the entire vector width starting at the base pointer. If we found
4979 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004980 if (LastLoadedElt == NumElems - 1) {
4981 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004982 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004983 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004984 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004985 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004986 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004987 LDBase->isVolatile(), LDBase->isNonTemporal(),
4988 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004989 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4990 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004991 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4992 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004993 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4994 Ops, 2, MVT::i32,
4995 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004996 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004997 }
4998 return SDValue();
4999}
5000
Evan Chengc3630942009-12-09 21:00:30 +00005001SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005002X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005003 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005004
David Greenef125a292011-02-08 19:04:41 +00005005 EVT VT = Op.getValueType();
5006 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005007 unsigned NumElems = Op.getNumOperands();
5008
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005009 // Vectors containing all zeros can be matched by pxor and xorps later
5010 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5011 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5012 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005013 if (Op.getValueType() == MVT::v4i32 ||
5014 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005015 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005016
Dale Johannesenace16102009-02-03 19:33:06 +00005017 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005018 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005019
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005020 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5021 // vectors or broken into v4i32 operations on 256-bit vectors.
5022 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5023 if (Op.getValueType() == MVT::v4i32)
5024 return Op;
5025
5026 return getOnesVector(Op.getValueType(), DAG, dl);
5027 }
5028
Owen Andersone50ed302009-08-10 22:56:29 +00005029 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005030
Evan Cheng0db9fe62006-04-25 20:13:52 +00005031 unsigned NumZero = 0;
5032 unsigned NumNonZero = 0;
5033 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005034 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005035 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005036 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005037 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005038 if (Elt.getOpcode() == ISD::UNDEF)
5039 continue;
5040 Values.insert(Elt);
5041 if (Elt.getOpcode() != ISD::Constant &&
5042 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005043 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005044 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005045 NumZero++;
5046 else {
5047 NonZeros |= (1 << i);
5048 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005049 }
5050 }
5051
Chris Lattner97a2a562010-08-26 05:24:29 +00005052 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5053 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005054 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005055
Chris Lattner67f453a2008-03-09 05:42:06 +00005056 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005057 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005058 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005059 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005060
Chris Lattner62098042008-03-09 01:05:04 +00005061 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5062 // the value are obviously zero, truncate the value to i32 and do the
5063 // insertion that way. Only do this if the value is non-constant or if the
5064 // value is a constant being inserted into element 0. It is cheaper to do
5065 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005066 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005067 (!IsAllConstants || Idx == 0)) {
5068 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005069 // Handle SSE only.
5070 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5071 EVT VecVT = MVT::v4i32;
5072 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005073
Chris Lattner62098042008-03-09 01:05:04 +00005074 // Truncate the value (which may itself be a constant) to i32, and
5075 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005076 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005077 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005078 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5079 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005080
Chris Lattner62098042008-03-09 01:05:04 +00005081 // Now we have our 32-bit value zero extended in the low element of
5082 // a vector. If Idx != 0, swizzle it into place.
5083 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005084 SmallVector<int, 4> Mask;
5085 Mask.push_back(Idx);
5086 for (unsigned i = 1; i != VecElts; ++i)
5087 Mask.push_back(i);
5088 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005089 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005090 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005091 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005092 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005093 }
5094 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005095
Chris Lattner19f79692008-03-08 22:59:52 +00005096 // If we have a constant or non-constant insertion into the low element of
5097 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5098 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005099 // depending on what the source datatype is.
5100 if (Idx == 0) {
5101 if (NumZero == 0) {
5102 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005103 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5104 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005105 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5106 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
5107 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
5108 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005109 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5110 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005111 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5112 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005113 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5114 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5115 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005116 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005117 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005118 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005119
5120 // Is it a vector logical left shift?
5121 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005122 X86::isZeroNode(Op.getOperand(0)) &&
5123 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005124 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005125 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005126 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005127 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005128 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005129 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005130
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005131 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005132 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005133
Chris Lattner19f79692008-03-08 22:59:52 +00005134 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5135 // is a non-constant being inserted into an element other than the low one,
5136 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5137 // movd/movss) to move this into the low element, then shuffle it into
5138 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005139 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005140 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005141
Evan Cheng0db9fe62006-04-25 20:13:52 +00005142 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005143 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
5144 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005145 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005146 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005147 MaskVec.push_back(i == Idx ? 0 : 1);
5148 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005149 }
5150 }
5151
Chris Lattner67f453a2008-03-09 05:42:06 +00005152 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005153 if (Values.size() == 1) {
5154 if (EVTBits == 32) {
5155 // Instead of a shuffle like this:
5156 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5157 // Check if it's possible to issue this instead.
5158 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5159 unsigned Idx = CountTrailingZeros_32(NonZeros);
5160 SDValue Item = Op.getOperand(Idx);
5161 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5162 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5163 }
Dan Gohman475871a2008-07-27 21:46:04 +00005164 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005165 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005166
Dan Gohmana3941172007-07-24 22:55:08 +00005167 // A vector full of immediates; various special cases are already
5168 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005169 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005170 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005171
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005172 // For AVX-length vectors, build the individual 128-bit pieces and use
5173 // shuffles to put them in place.
5174 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5175 SmallVector<SDValue, 32> V;
5176 for (unsigned i = 0; i < NumElems; ++i)
5177 V.push_back(Op.getOperand(i));
5178
5179 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5180
5181 // Build both the lower and upper subvector.
5182 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5183 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5184 NumElems/2);
5185
5186 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005187 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5188 DAG.getConstant(0, MVT::i32), DAG, dl);
5189 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005190 DAG, dl);
5191 }
5192
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005193 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005194 if (EVTBits == 64) {
5195 if (NumNonZero == 1) {
5196 // One half is zero or undef.
5197 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005198 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005199 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005200 return getShuffleVectorZeroOrUndef(V2, Idx, true,
5201 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005202 }
Dan Gohman475871a2008-07-27 21:46:04 +00005203 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005204 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005205
5206 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005207 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005208 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005209 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005210 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005211 }
5212
Bill Wendling826f36f2007-03-28 00:57:11 +00005213 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005214 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005215 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005216 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005217 }
5218
5219 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005220 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005221 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005222 if (NumElems == 4 && NumZero > 0) {
5223 for (unsigned i = 0; i < 4; ++i) {
5224 bool isZero = !(NonZeros & (1 << i));
5225 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00005226 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005227 else
Dale Johannesenace16102009-02-03 19:33:06 +00005228 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005229 }
5230
5231 for (unsigned i = 0; i < 2; ++i) {
5232 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5233 default: break;
5234 case 0:
5235 V[i] = V[i*2]; // Must be a zero vector.
5236 break;
5237 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005238 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005239 break;
5240 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005241 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005242 break;
5243 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005244 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005245 break;
5246 }
5247 }
5248
Nate Begeman9008ca62009-04-27 18:41:29 +00005249 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005250 bool Reverse = (NonZeros & 0x3) == 2;
5251 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005252 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005253 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5254 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005255 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5256 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005257 }
5258
Nate Begemanfdea31a2010-03-24 20:49:50 +00005259 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5260 // Check for a build vector of consecutive loads.
5261 for (unsigned i = 0; i < NumElems; ++i)
5262 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005263
Nate Begemanfdea31a2010-03-24 20:49:50 +00005264 // Check for elements which are consecutive loads.
5265 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5266 if (LD.getNode())
5267 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005268
5269 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005270 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005271 SDValue Result;
5272 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5273 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5274 else
5275 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005276
Chris Lattner24faf612010-08-28 17:59:08 +00005277 for (unsigned i = 1; i < NumElems; ++i) {
5278 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5279 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005280 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005281 }
5282 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005283 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005284
Chris Lattner6e80e442010-08-28 17:15:43 +00005285 // Otherwise, expand into a number of unpckl*, start by extending each of
5286 // our (non-undef) elements to the full vector width with the element in the
5287 // bottom slot of the vector (which generates no code for SSE).
5288 for (unsigned i = 0; i < NumElems; ++i) {
5289 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5290 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5291 else
5292 V[i] = DAG.getUNDEF(VT);
5293 }
5294
5295 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005296 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5297 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5298 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005299 unsigned EltStride = NumElems >> 1;
5300 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005301 for (unsigned i = 0; i < EltStride; ++i) {
5302 // If V[i+EltStride] is undef and this is the first round of mixing,
5303 // then it is safe to just drop this shuffle: V[i] is already in the
5304 // right place, the one element (since it's the first round) being
5305 // inserted as undef can be dropped. This isn't safe for successive
5306 // rounds because they will permute elements within both vectors.
5307 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5308 EltStride == NumElems/2)
5309 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005310
Chris Lattner6e80e442010-08-28 17:15:43 +00005311 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005312 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005313 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005314 }
5315 return V[0];
5316 }
Dan Gohman475871a2008-07-27 21:46:04 +00005317 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005318}
5319
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005320// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5321// them in a MMX register. This is better than doing a stack convert.
5322static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005323 DebugLoc dl = Op.getDebugLoc();
5324 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005325
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005326 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5327 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5328 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005329 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005330 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5331 InVec = Op.getOperand(1);
5332 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5333 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005334 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005335 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5336 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5337 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005338 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005339 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5340 Mask[0] = 0; Mask[1] = 2;
5341 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5342 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005343 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005344}
5345
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005346// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5347// to create 256-bit vectors from two other 128-bit ones.
5348static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5349 DebugLoc dl = Op.getDebugLoc();
5350 EVT ResVT = Op.getValueType();
5351
5352 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5353
5354 SDValue V1 = Op.getOperand(0);
5355 SDValue V2 = Op.getOperand(1);
5356 unsigned NumElems = ResVT.getVectorNumElements();
5357
5358 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5359 DAG.getConstant(0, MVT::i32), DAG, dl);
5360 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5361 DAG, dl);
5362}
5363
5364SDValue
5365X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005366 EVT ResVT = Op.getValueType();
5367
5368 assert(Op.getNumOperands() == 2);
5369 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5370 "Unsupported CONCAT_VECTORS for value type");
5371
5372 // We support concatenate two MMX registers and place them in a MMX register.
5373 // This is better than doing a stack convert.
5374 if (ResVT.is128BitVector())
5375 return LowerMMXCONCAT_VECTORS(Op, DAG);
5376
5377 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5378 // from two other 128-bit ones.
5379 return LowerAVXCONCAT_VECTORS(Op, DAG);
5380}
5381
Nate Begemanb9a47b82009-02-23 08:49:38 +00005382// v8i16 shuffles - Prefer shuffles in the following order:
5383// 1. [all] pshuflw, pshufhw, optional move
5384// 2. [ssse3] 1 x pshufb
5385// 3. [ssse3] 2 x pshufb + 1 x por
5386// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005387SDValue
5388X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5389 SelectionDAG &DAG) const {
5390 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005391 SDValue V1 = SVOp->getOperand(0);
5392 SDValue V2 = SVOp->getOperand(1);
5393 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005394 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005395
Nate Begemanb9a47b82009-02-23 08:49:38 +00005396 // Determine if more than 1 of the words in each of the low and high quadwords
5397 // of the result come from the same quadword of one of the two inputs. Undef
5398 // mask values count as coming from any quadword, for better codegen.
5399 SmallVector<unsigned, 4> LoQuad(4);
5400 SmallVector<unsigned, 4> HiQuad(4);
5401 BitVector InputQuads(4);
5402 for (unsigned i = 0; i < 8; ++i) {
5403 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005404 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005405 MaskVals.push_back(EltIdx);
5406 if (EltIdx < 0) {
5407 ++Quad[0];
5408 ++Quad[1];
5409 ++Quad[2];
5410 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005411 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005412 }
5413 ++Quad[EltIdx / 4];
5414 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005415 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005416
Nate Begemanb9a47b82009-02-23 08:49:38 +00005417 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005418 unsigned MaxQuad = 1;
5419 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005420 if (LoQuad[i] > MaxQuad) {
5421 BestLoQuad = i;
5422 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005423 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005424 }
5425
Nate Begemanb9a47b82009-02-23 08:49:38 +00005426 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005427 MaxQuad = 1;
5428 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005429 if (HiQuad[i] > MaxQuad) {
5430 BestHiQuad = i;
5431 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005432 }
5433 }
5434
Nate Begemanb9a47b82009-02-23 08:49:38 +00005435 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005436 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005437 // single pshufb instruction is necessary. If There are more than 2 input
5438 // quads, disable the next transformation since it does not help SSSE3.
5439 bool V1Used = InputQuads[0] || InputQuads[1];
5440 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005441 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005442 if (InputQuads.count() == 2 && V1Used && V2Used) {
5443 BestLoQuad = InputQuads.find_first();
5444 BestHiQuad = InputQuads.find_next(BestLoQuad);
5445 }
5446 if (InputQuads.count() > 2) {
5447 BestLoQuad = -1;
5448 BestHiQuad = -1;
5449 }
5450 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005451
Nate Begemanb9a47b82009-02-23 08:49:38 +00005452 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5453 // the shuffle mask. If a quad is scored as -1, that means that it contains
5454 // words from all 4 input quadwords.
5455 SDValue NewV;
5456 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005457 SmallVector<int, 8> MaskV;
5458 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5459 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005460 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005461 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5462 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5463 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005464
Nate Begemanb9a47b82009-02-23 08:49:38 +00005465 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5466 // source words for the shuffle, to aid later transformations.
5467 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005468 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005469 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005470 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005471 if (idx != (int)i)
5472 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005473 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005474 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005475 AllWordsInNewV = false;
5476 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005477 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005478
Nate Begemanb9a47b82009-02-23 08:49:38 +00005479 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5480 if (AllWordsInNewV) {
5481 for (int i = 0; i != 8; ++i) {
5482 int idx = MaskVals[i];
5483 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005484 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005485 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005486 if ((idx != i) && idx < 4)
5487 pshufhw = false;
5488 if ((idx != i) && idx > 3)
5489 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005490 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005491 V1 = NewV;
5492 V2Used = false;
5493 BestLoQuad = 0;
5494 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005495 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005496
Nate Begemanb9a47b82009-02-23 08:49:38 +00005497 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5498 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005499 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005500 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5501 unsigned TargetMask = 0;
5502 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005503 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005504 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5505 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5506 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005507 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005508 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005509 }
Eric Christopherfd179292009-08-27 18:07:15 +00005510
Nate Begemanb9a47b82009-02-23 08:49:38 +00005511 // If we have SSSE3, and all words of the result are from 1 input vector,
5512 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5513 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005514 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005515 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005516
Nate Begemanb9a47b82009-02-23 08:49:38 +00005517 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005518 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005519 // mask, and elements that come from V1 in the V2 mask, so that the two
5520 // results can be OR'd together.
5521 bool TwoInputs = V1Used && V2Used;
5522 for (unsigned i = 0; i != 8; ++i) {
5523 int EltIdx = MaskVals[i] * 2;
5524 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005525 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5526 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005527 continue;
5528 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005529 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5530 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005531 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005532 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005533 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005534 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005535 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005536 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005537 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005538
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 // Calculate the shuffle mask for the second input, shuffle it, and
5540 // OR it with the first shuffled input.
5541 pshufbMask.clear();
5542 for (unsigned i = 0; i != 8; ++i) {
5543 int EltIdx = MaskVals[i] * 2;
5544 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005545 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5546 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005547 continue;
5548 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5550 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005551 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005552 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005553 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005554 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005555 MVT::v16i8, &pshufbMask[0], 16));
5556 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005557 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005558 }
5559
5560 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5561 // and update MaskVals with new element order.
5562 BitVector InOrder(8);
5563 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005564 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005565 for (int i = 0; i != 4; ++i) {
5566 int idx = MaskVals[i];
5567 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005568 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005569 InOrder.set(i);
5570 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005571 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005572 InOrder.set(i);
5573 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005574 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005575 }
5576 }
5577 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005578 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005579 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005580 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005581
5582 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5583 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5584 NewV.getOperand(0),
5585 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5586 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005587 }
Eric Christopherfd179292009-08-27 18:07:15 +00005588
Nate Begemanb9a47b82009-02-23 08:49:38 +00005589 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5590 // and update MaskVals with the new element order.
5591 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005593 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005594 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005595 for (unsigned i = 4; i != 8; ++i) {
5596 int idx = MaskVals[i];
5597 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005598 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005599 InOrder.set(i);
5600 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005601 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005602 InOrder.set(i);
5603 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005604 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005605 }
5606 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005607 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005608 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005609
5610 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5611 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5612 NewV.getOperand(0),
5613 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5614 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005615 }
Eric Christopherfd179292009-08-27 18:07:15 +00005616
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 // In case BestHi & BestLo were both -1, which means each quadword has a word
5618 // from each of the four input quadwords, calculate the InOrder bitvector now
5619 // before falling through to the insert/extract cleanup.
5620 if (BestLoQuad == -1 && BestHiQuad == -1) {
5621 NewV = V1;
5622 for (int i = 0; i != 8; ++i)
5623 if (MaskVals[i] < 0 || MaskVals[i] == i)
5624 InOrder.set(i);
5625 }
Eric Christopherfd179292009-08-27 18:07:15 +00005626
Nate Begemanb9a47b82009-02-23 08:49:38 +00005627 // The other elements are put in the right place using pextrw and pinsrw.
5628 for (unsigned i = 0; i != 8; ++i) {
5629 if (InOrder[i])
5630 continue;
5631 int EltIdx = MaskVals[i];
5632 if (EltIdx < 0)
5633 continue;
5634 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005635 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005636 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005637 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005639 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005640 DAG.getIntPtrConstant(i));
5641 }
5642 return NewV;
5643}
5644
5645// v16i8 shuffles - Prefer shuffles in the following order:
5646// 1. [ssse3] 1 x pshufb
5647// 2. [ssse3] 2 x pshufb + 1 x por
5648// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5649static
Nate Begeman9008ca62009-04-27 18:41:29 +00005650SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005651 SelectionDAG &DAG,
5652 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005653 SDValue V1 = SVOp->getOperand(0);
5654 SDValue V2 = SVOp->getOperand(1);
5655 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005657 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005658
Nate Begemanb9a47b82009-02-23 08:49:38 +00005659 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005660 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005661 // present, fall back to case 3.
5662 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5663 bool V1Only = true;
5664 bool V2Only = true;
5665 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005666 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005667 if (EltIdx < 0)
5668 continue;
5669 if (EltIdx < 16)
5670 V2Only = false;
5671 else
5672 V1Only = false;
5673 }
Eric Christopherfd179292009-08-27 18:07:15 +00005674
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5676 if (TLI.getSubtarget()->hasSSSE3()) {
5677 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005678
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005680 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005681 //
5682 // Otherwise, we have elements from both input vectors, and must zero out
5683 // elements that come from V2 in the first mask, and V1 in the second mask
5684 // so that we can OR them together.
5685 bool TwoInputs = !(V1Only || V2Only);
5686 for (unsigned i = 0; i != 16; ++i) {
5687 int EltIdx = MaskVals[i];
5688 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005689 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 continue;
5691 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005692 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005693 }
5694 // If all the elements are from V2, assign it to V1 and return after
5695 // building the first pshufb.
5696 if (V2Only)
5697 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005698 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005699 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005700 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 if (!TwoInputs)
5702 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005703
Nate Begemanb9a47b82009-02-23 08:49:38 +00005704 // Calculate the shuffle mask for the second input, shuffle it, and
5705 // OR it with the first shuffled input.
5706 pshufbMask.clear();
5707 for (unsigned i = 0; i != 16; ++i) {
5708 int EltIdx = MaskVals[i];
5709 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711 continue;
5712 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005713 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005715 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005716 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005717 MVT::v16i8, &pshufbMask[0], 16));
5718 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005719 }
Eric Christopherfd179292009-08-27 18:07:15 +00005720
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 // No SSSE3 - Calculate in place words and then fix all out of place words
5722 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5723 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005724 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5725 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 SDValue NewV = V2Only ? V2 : V1;
5727 for (int i = 0; i != 8; ++i) {
5728 int Elt0 = MaskVals[i*2];
5729 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005730
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 // This word of the result is all undef, skip it.
5732 if (Elt0 < 0 && Elt1 < 0)
5733 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005734
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 // This word of the result is already in the correct place, skip it.
5736 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5737 continue;
5738 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5739 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005740
Nate Begemanb9a47b82009-02-23 08:49:38 +00005741 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5742 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5743 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005744
5745 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5746 // using a single extract together, load it and store it.
5747 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005748 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005749 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005750 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005751 DAG.getIntPtrConstant(i));
5752 continue;
5753 }
5754
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005756 // source byte is not also odd, shift the extracted word left 8 bits
5757 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005759 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005760 DAG.getIntPtrConstant(Elt1 / 2));
5761 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005762 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005763 DAG.getConstant(8,
5764 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005765 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005766 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5767 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 }
5769 // If Elt0 is defined, extract it from the appropriate source. If the
5770 // source byte is not also even, shift the extracted word right 8 bits. If
5771 // Elt1 was also defined, OR the extracted values together before
5772 // inserting them in the result.
5773 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5776 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005777 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005778 DAG.getConstant(8,
5779 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005780 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005781 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5782 DAG.getConstant(0x00FF, MVT::i16));
5783 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 : InsElt0;
5785 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005786 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 DAG.getIntPtrConstant(i));
5788 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005789 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005790}
5791
Evan Cheng7a831ce2007-12-15 03:00:47 +00005792/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005793/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005794/// done when every pair / quad of shuffle mask elements point to elements in
5795/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005796/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005797static
Nate Begeman9008ca62009-04-27 18:41:29 +00005798SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005799 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005800 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005801 SDValue V1 = SVOp->getOperand(0);
5802 SDValue V2 = SVOp->getOperand(1);
5803 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005804 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005805 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005807 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005808 case MVT::v4f32: NewVT = MVT::v2f64; break;
5809 case MVT::v4i32: NewVT = MVT::v2i64; break;
5810 case MVT::v8i16: NewVT = MVT::v4i32; break;
5811 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005812 }
5813
Nate Begeman9008ca62009-04-27 18:41:29 +00005814 int Scale = NumElems / NewWidth;
5815 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005816 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005817 int StartIdx = -1;
5818 for (int j = 0; j < Scale; ++j) {
5819 int EltIdx = SVOp->getMaskElt(i+j);
5820 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005821 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005822 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005823 StartIdx = EltIdx - (EltIdx % Scale);
5824 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005825 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005826 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005827 if (StartIdx == -1)
5828 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005829 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005830 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005831 }
5832
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005833 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5834 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005835 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005836}
5837
Evan Chengd880b972008-05-09 21:53:03 +00005838/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005839///
Owen Andersone50ed302009-08-10 22:56:29 +00005840static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 SDValue SrcOp, SelectionDAG &DAG,
5842 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005843 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005844 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005845 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005846 LD = dyn_cast<LoadSDNode>(SrcOp);
5847 if (!LD) {
5848 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5849 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005850 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005851 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005852 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005853 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005854 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005855 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005856 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005857 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005858 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5859 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5860 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005861 SrcOp.getOperand(0)
5862 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005863 }
5864 }
5865 }
5866
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005867 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005868 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005869 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005870 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005871}
5872
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005873/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5874/// shuffle node referes to only one lane in the sources.
5875static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5876 EVT VT = SVOp->getValueType(0);
5877 int NumElems = VT.getVectorNumElements();
5878 int HalfSize = NumElems/2;
5879 SmallVector<int, 16> M;
5880 SVOp->getMask(M);
5881 bool MatchA = false, MatchB = false;
5882
5883 for (int l = 0; l < NumElems*2; l += HalfSize) {
5884 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5885 MatchA = true;
5886 break;
5887 }
5888 }
5889
5890 for (int l = 0; l < NumElems*2; l += HalfSize) {
5891 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5892 MatchB = true;
5893 break;
5894 }
5895 }
5896
5897 return MatchA && MatchB;
5898}
5899
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005900/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5901/// which could not be matched by any known target speficic shuffle
5902static SDValue
5903LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005904 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5905 // If each half of a vector shuffle node referes to only one lane in the
5906 // source vectors, extract each used 128-bit lane and shuffle them using
5907 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5908 // the work to the legalizer.
5909 DebugLoc dl = SVOp->getDebugLoc();
5910 EVT VT = SVOp->getValueType(0);
5911 int NumElems = VT.getVectorNumElements();
5912 int HalfSize = NumElems/2;
5913
5914 // Extract the reference for each half
5915 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5916 int FstVecOpNum = 0, SndVecOpNum = 0;
5917 for (int i = 0; i < HalfSize; ++i) {
5918 int Elt = SVOp->getMaskElt(i);
5919 if (SVOp->getMaskElt(i) < 0)
5920 continue;
5921 FstVecOpNum = Elt/NumElems;
5922 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5923 break;
5924 }
5925 for (int i = HalfSize; i < NumElems; ++i) {
5926 int Elt = SVOp->getMaskElt(i);
5927 if (SVOp->getMaskElt(i) < 0)
5928 continue;
5929 SndVecOpNum = Elt/NumElems;
5930 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5931 break;
5932 }
5933
5934 // Extract the subvectors
5935 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5936 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5937 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5938 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5939
5940 // Generate 128-bit shuffles
5941 SmallVector<int, 16> MaskV1, MaskV2;
5942 for (int i = 0; i < HalfSize; ++i) {
5943 int Elt = SVOp->getMaskElt(i);
5944 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5945 }
5946 for (int i = HalfSize; i < NumElems; ++i) {
5947 int Elt = SVOp->getMaskElt(i);
5948 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5949 }
5950
5951 EVT NVT = V1.getValueType();
5952 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5953 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5954
5955 // Concatenate the result back
5956 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5957 DAG.getConstant(0, MVT::i32), DAG, dl);
5958 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5959 DAG, dl);
5960 }
5961
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005962 return SDValue();
5963}
5964
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005965/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5966/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005967static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005968LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005969 SDValue V1 = SVOp->getOperand(0);
5970 SDValue V2 = SVOp->getOperand(1);
5971 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005972 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005973
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005974 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5975
Evan Chengace3c172008-07-22 21:13:36 +00005976 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005977 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005978 SmallVector<int, 8> Mask1(4U, -1);
5979 SmallVector<int, 8> PermMask;
5980 SVOp->getMask(PermMask);
5981
Evan Chengace3c172008-07-22 21:13:36 +00005982 unsigned NumHi = 0;
5983 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005984 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005985 int Idx = PermMask[i];
5986 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005987 Locs[i] = std::make_pair(-1, -1);
5988 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005989 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5990 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005991 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005992 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005993 NumLo++;
5994 } else {
5995 Locs[i] = std::make_pair(1, NumHi);
5996 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005997 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005998 NumHi++;
5999 }
6000 }
6001 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006002
Evan Chengace3c172008-07-22 21:13:36 +00006003 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006004 // If no more than two elements come from either vector. This can be
6005 // implemented with two shuffles. First shuffle gather the elements.
6006 // The second shuffle, which takes the first shuffle as both of its
6007 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006008 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006009
Nate Begeman9008ca62009-04-27 18:41:29 +00006010 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006011
Evan Chengace3c172008-07-22 21:13:36 +00006012 for (unsigned i = 0; i != 4; ++i) {
6013 if (Locs[i].first == -1)
6014 continue;
6015 else {
6016 unsigned Idx = (i < 2) ? 0 : 4;
6017 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006018 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006019 }
6020 }
6021
Nate Begeman9008ca62009-04-27 18:41:29 +00006022 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006023 } else if (NumLo == 3 || NumHi == 3) {
6024 // Otherwise, we must have three elements from one vector, call it X, and
6025 // one element from the other, call it Y. First, use a shufps to build an
6026 // intermediate vector with the one element from Y and the element from X
6027 // that will be in the same half in the final destination (the indexes don't
6028 // matter). Then, use a shufps to build the final vector, taking the half
6029 // containing the element from Y from the intermediate, and the other half
6030 // from X.
6031 if (NumHi == 3) {
6032 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006033 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006034 std::swap(V1, V2);
6035 }
6036
6037 // Find the element from V2.
6038 unsigned HiIndex;
6039 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006040 int Val = PermMask[HiIndex];
6041 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006042 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006043 if (Val >= 4)
6044 break;
6045 }
6046
Nate Begeman9008ca62009-04-27 18:41:29 +00006047 Mask1[0] = PermMask[HiIndex];
6048 Mask1[1] = -1;
6049 Mask1[2] = PermMask[HiIndex^1];
6050 Mask1[3] = -1;
6051 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006052
6053 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006054 Mask1[0] = PermMask[0];
6055 Mask1[1] = PermMask[1];
6056 Mask1[2] = HiIndex & 1 ? 6 : 4;
6057 Mask1[3] = HiIndex & 1 ? 4 : 6;
6058 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006059 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006060 Mask1[0] = HiIndex & 1 ? 2 : 0;
6061 Mask1[1] = HiIndex & 1 ? 0 : 2;
6062 Mask1[2] = PermMask[2];
6063 Mask1[3] = PermMask[3];
6064 if (Mask1[2] >= 0)
6065 Mask1[2] += 4;
6066 if (Mask1[3] >= 0)
6067 Mask1[3] += 4;
6068 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006069 }
Evan Chengace3c172008-07-22 21:13:36 +00006070 }
6071
6072 // Break it into (shuffle shuffle_hi, shuffle_lo).
6073 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006074 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006075 SmallVector<int,8> LoMask(4U, -1);
6076 SmallVector<int,8> HiMask(4U, -1);
6077
6078 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006079 unsigned MaskIdx = 0;
6080 unsigned LoIdx = 0;
6081 unsigned HiIdx = 2;
6082 for (unsigned i = 0; i != 4; ++i) {
6083 if (i == 2) {
6084 MaskPtr = &HiMask;
6085 MaskIdx = 1;
6086 LoIdx = 0;
6087 HiIdx = 2;
6088 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006089 int Idx = PermMask[i];
6090 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006091 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006092 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006093 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006094 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006095 LoIdx++;
6096 } else {
6097 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006098 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006099 HiIdx++;
6100 }
6101 }
6102
Nate Begeman9008ca62009-04-27 18:41:29 +00006103 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6104 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6105 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006106 for (unsigned i = 0; i != 4; ++i) {
6107 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006108 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006109 } else {
6110 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006111 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006112 }
6113 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006114 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006115}
6116
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006117static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006118 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006119 V = V.getOperand(0);
6120 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6121 V = V.getOperand(0);
6122 if (MayFoldLoad(V))
6123 return true;
6124 return false;
6125}
6126
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006127// FIXME: the version above should always be used. Since there's
6128// a bug where several vector shuffles can't be folded because the
6129// DAG is not updated during lowering and a node claims to have two
6130// uses while it only has one, use this version, and let isel match
6131// another instruction if the load really happens to have more than
6132// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006133// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006134static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006135 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006136 V = V.getOperand(0);
6137 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6138 V = V.getOperand(0);
6139 if (ISD::isNormalLoad(V.getNode()))
6140 return true;
6141 return false;
6142}
6143
6144/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6145/// a vector extract, and if both can be later optimized into a single load.
6146/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6147/// here because otherwise a target specific shuffle node is going to be
6148/// emitted for this shuffle, and the optimization not done.
6149/// FIXME: This is probably not the best approach, but fix the problem
6150/// until the right path is decided.
6151static
6152bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6153 const TargetLowering &TLI) {
6154 EVT VT = V.getValueType();
6155 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6156
6157 // Be sure that the vector shuffle is present in a pattern like this:
6158 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6159 if (!V.hasOneUse())
6160 return false;
6161
6162 SDNode *N = *V.getNode()->use_begin();
6163 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6164 return false;
6165
6166 SDValue EltNo = N->getOperand(1);
6167 if (!isa<ConstantSDNode>(EltNo))
6168 return false;
6169
6170 // If the bit convert changed the number of elements, it is unsafe
6171 // to examine the mask.
6172 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006173 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006174 EVT SrcVT = V.getOperand(0).getValueType();
6175 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6176 return false;
6177 V = V.getOperand(0);
6178 HasShuffleIntoBitcast = true;
6179 }
6180
6181 // Select the input vector, guarding against out of range extract vector.
6182 unsigned NumElems = VT.getVectorNumElements();
6183 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6184 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6185 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6186
6187 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006188 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006189 V = V.getOperand(0);
6190
6191 if (ISD::isNormalLoad(V.getNode())) {
6192 // Is the original load suitable?
6193 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6194
6195 // FIXME: avoid the multi-use bug that is preventing lots of
6196 // of foldings to be detected, this is still wrong of course, but
6197 // give the temporary desired behavior, and if it happens that
6198 // the load has real more uses, during isel it will not fold, and
6199 // will generate poor code.
6200 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6201 return false;
6202
6203 if (!HasShuffleIntoBitcast)
6204 return true;
6205
6206 // If there's a bitcast before the shuffle, check if the load type and
6207 // alignment is valid.
6208 unsigned Align = LN0->getAlignment();
6209 unsigned NewAlign =
6210 TLI.getTargetData()->getABITypeAlignment(
6211 VT.getTypeForEVT(*DAG.getContext()));
6212
6213 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6214 return false;
6215 }
6216
6217 return true;
6218}
6219
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006220static
Evan Cheng835580f2010-10-07 20:50:20 +00006221SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6222 EVT VT = Op.getValueType();
6223
6224 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006225 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6226 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006227 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6228 V1, DAG));
6229}
6230
6231static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006232SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
6233 bool HasSSE2) {
6234 SDValue V1 = Op.getOperand(0);
6235 SDValue V2 = Op.getOperand(1);
6236 EVT VT = Op.getValueType();
6237
6238 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6239
6240 if (HasSSE2 && VT == MVT::v2f64)
6241 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6242
Evan Cheng0899f5c2011-08-31 02:05:24 +00006243 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6244 return DAG.getNode(ISD::BITCAST, dl, VT,
6245 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6246 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6247 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006248}
6249
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006250static
6251SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6252 SDValue V1 = Op.getOperand(0);
6253 SDValue V2 = Op.getOperand(1);
6254 EVT VT = Op.getValueType();
6255
6256 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6257 "unsupported shuffle type");
6258
6259 if (V2.getOpcode() == ISD::UNDEF)
6260 V2 = V1;
6261
6262 // v4i32 or v4f32
6263 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6264}
6265
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006266static inline unsigned getSHUFPOpcode(EVT VT) {
6267 switch(VT.getSimpleVT().SimpleTy) {
6268 case MVT::v8i32: // Use fp unit for int unpack.
6269 case MVT::v8f32:
6270 case MVT::v4i32: // Use fp unit for int unpack.
6271 case MVT::v4f32: return X86ISD::SHUFPS;
6272 case MVT::v4i64: // Use fp unit for int unpack.
6273 case MVT::v4f64:
6274 case MVT::v2i64: // Use fp unit for int unpack.
6275 case MVT::v2f64: return X86ISD::SHUFPD;
6276 default:
6277 llvm_unreachable("Unknown type for shufp*");
6278 }
6279 return 0;
6280}
6281
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006282static
6283SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
6284 SDValue V1 = Op.getOperand(0);
6285 SDValue V2 = Op.getOperand(1);
6286 EVT VT = Op.getValueType();
6287 unsigned NumElems = VT.getVectorNumElements();
6288
6289 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6290 // operand of these instructions is only memory, so check if there's a
6291 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6292 // same masks.
6293 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006294
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006295 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006296 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006297 CanFoldLoad = true;
6298
6299 // When V1 is a load, it can be folded later into a store in isel, example:
6300 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6301 // turns into:
6302 // (MOVLPSmr addr:$src1, VR128:$src2)
6303 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006304 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006305 CanFoldLoad = true;
6306
Eric Christopher893a8822011-02-20 05:04:42 +00006307 // Both of them can't be memory operations though.
6308 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6309 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006310
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006311 if (CanFoldLoad) {
6312 if (HasSSE2 && NumElems == 2)
6313 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6314
6315 if (NumElems == 4)
6316 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6317 }
6318
6319 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6320 // movl and movlp will both match v2i64, but v2i64 is never matched by
6321 // movl earlier because we make it strict to avoid messing with the movlp load
6322 // folding logic (see the code above getMOVLP call). Match it here then,
6323 // this is horrible, but will stay like this until we move all shuffle
6324 // matching to x86 specific nodes. Note that for the 1st condition all
6325 // types are matched with movsd.
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006326 if (HasSSE2) {
6327 if (NumElems == 2)
6328 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006329 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006330 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006331
6332 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6333
6334 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006335 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006336 X86::getShuffleSHUFImmediate(SVOp), DAG);
6337}
6338
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006339static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006340 switch(VT.getSimpleVT().SimpleTy) {
6341 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6342 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006343 case MVT::v4f32: return X86ISD::UNPCKLPS;
6344 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006345 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006346 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006347 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006348 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006349 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6350 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6351 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006352 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006353 }
6354 return 0;
6355}
6356
6357static inline unsigned getUNPCKHOpcode(EVT VT) {
6358 switch(VT.getSimpleVT().SimpleTy) {
6359 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6360 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6361 case MVT::v4f32: return X86ISD::UNPCKHPS;
6362 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006363 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006364 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006365 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006366 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006367 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6368 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6369 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006370 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006371 }
6372 return 0;
6373}
6374
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006375static inline unsigned getVPERMILOpcode(EVT VT) {
6376 switch(VT.getSimpleVT().SimpleTy) {
6377 case MVT::v4i32:
6378 case MVT::v4f32: return X86ISD::VPERMILPS;
6379 case MVT::v2i64:
6380 case MVT::v2f64: return X86ISD::VPERMILPD;
6381 case MVT::v8i32:
6382 case MVT::v8f32: return X86ISD::VPERMILPSY;
6383 case MVT::v4i64:
6384 case MVT::v4f64: return X86ISD::VPERMILPDY;
6385 default:
6386 llvm_unreachable("Unknown type for vpermil");
6387 }
6388 return 0;
6389}
6390
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006391/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6392/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6393/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6394static bool isVectorBroadcast(SDValue &Op) {
6395 EVT VT = Op.getValueType();
6396 bool Is256 = VT.getSizeInBits() == 256;
6397
6398 assert((VT.getSizeInBits() == 128 || Is256) &&
6399 "Unsupported type for vbroadcast node");
6400
6401 SDValue V = Op;
6402 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6403 V = V.getOperand(0);
6404
6405 if (Is256 && !(V.hasOneUse() &&
6406 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6407 V.getOperand(0).getOpcode() == ISD::UNDEF))
6408 return false;
6409
6410 if (Is256)
6411 V = V.getOperand(1);
6412 if (V.hasOneUse() && V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6413 return false;
6414
6415 // Check the source scalar_to_vector type. 256-bit broadcasts are
6416 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6417 // for 32-bit scalars.
6418 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6419 if (ScalarSize != 32 && ScalarSize != 64)
6420 return false;
6421 if (!Is256 && ScalarSize == 64)
6422 return false;
6423
6424 V = V.getOperand(0);
6425 if (!MayFoldLoad(V))
6426 return false;
6427
6428 // Return the load node
6429 Op = V;
6430 return true;
6431}
6432
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006433static
6434SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006435 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006436 const X86Subtarget *Subtarget) {
6437 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6438 EVT VT = Op.getValueType();
6439 DebugLoc dl = Op.getDebugLoc();
6440 SDValue V1 = Op.getOperand(0);
6441 SDValue V2 = Op.getOperand(1);
6442
6443 if (isZeroShuffle(SVOp))
6444 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
6445
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006446 // Handle splat operations
6447 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006448 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006449 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006450 // Special case, this is the only place now where it's allowed to return
6451 // a vector_shuffle operation without using a target specific node, because
6452 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6453 // this be moved to DAGCombine instead?
6454 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006455 return Op;
6456
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006457 // Use vbroadcast whenever the splat comes from a foldable load
6458 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6459 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6460
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006461 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006462 if ((Size == 128 && NumElem <= 4) ||
6463 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006464 return SDValue();
6465
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006466 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006467 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006468 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006469
6470 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6471 // do it!
6472 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6473 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6474 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006475 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006476 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
6477 // FIXME: Figure out a cleaner way to do this.
6478 // Try to make use of movq to zero out the top part.
6479 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6480 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6481 if (NewOp.getNode()) {
6482 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6483 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6484 DAG, Subtarget, dl);
6485 }
6486 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6487 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6488 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6489 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6490 DAG, Subtarget, dl);
6491 }
6492 }
6493 return SDValue();
6494}
6495
Dan Gohman475871a2008-07-27 21:46:04 +00006496SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006497X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006498 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006499 SDValue V1 = Op.getOperand(0);
6500 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006501 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006502 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006503 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006504 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006505 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6506 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006507 bool V1IsSplat = false;
6508 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006509 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006510 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006511 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006512 MachineFunction &MF = DAG.getMachineFunction();
6513 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006514
Dale Johannesen0488fb62010-09-30 23:57:10 +00006515 // Shuffle operations on MMX not supported.
6516 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006517 return Op;
6518
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006519 // Vector shuffle lowering takes 3 steps:
6520 //
6521 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6522 // narrowing and commutation of operands should be handled.
6523 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6524 // shuffle nodes.
6525 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6526 // so the shuffle can be broken into other shuffles and the legalizer can
6527 // try the lowering again.
6528 //
6529 // The general ideia is that no vector_shuffle operation should be left to
6530 // be matched during isel, all of them must be converted to a target specific
6531 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006532
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006533 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6534 // narrowing and commutation of operands should be handled. The actual code
6535 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006536 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006537 if (NewOp.getNode())
6538 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006539
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006540 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6541 // unpckh_undef). Only use pshufd if speed is more important than size.
6542 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006543 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006544 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006545 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006546
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006547 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00006548 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006549 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006550
Dale Johannesen0488fb62010-09-30 23:57:10 +00006551 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006552 return getMOVHighToLow(Op, dl, DAG);
6553
6554 // Use to match splats
6555 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
6556 (VT == MVT::v2f64 || VT == MVT::v2i64))
6557 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6558
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006559 if (X86::isPSHUFDMask(SVOp)) {
6560 // The actual implementation will match the mask in the if above and then
6561 // during isel it can match several different instructions, not only pshufd
6562 // as its name says, sad but true, emulate the behavior for now...
6563 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6564 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6565
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006566 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6567
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006568 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006569 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6570
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006571 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6572 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006573 }
Eric Christopherfd179292009-08-27 18:07:15 +00006574
Evan Chengf26ffe92008-05-29 08:22:04 +00006575 // Check if this can be converted into a logical shift.
6576 bool isLeft = false;
6577 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006578 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00006579 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00006580 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006581 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006582 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006583 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006584 EVT EltVT = VT.getVectorElementType();
6585 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006586 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006587 }
Eric Christopherfd179292009-08-27 18:07:15 +00006588
Nate Begeman9008ca62009-04-27 18:41:29 +00006589 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006590 if (V1IsUndef)
6591 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006592 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006593 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006594 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006595 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006596 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6597
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006598 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006599 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6600 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006601 }
Eric Christopherfd179292009-08-27 18:07:15 +00006602
Nate Begeman9008ca62009-04-27 18:41:29 +00006603 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006604 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
6605 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006606
Dale Johannesen0488fb62010-09-30 23:57:10 +00006607 if (X86::isMOVHLPSMask(SVOp))
6608 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006609
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006610 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006611 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006612
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006613 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006614 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006615
Dale Johannesen0488fb62010-09-30 23:57:10 +00006616 if (X86::isMOVLPMask(SVOp))
6617 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006618
Nate Begeman9008ca62009-04-27 18:41:29 +00006619 if (ShouldXformToMOVHLPS(SVOp) ||
6620 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6621 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006622
Evan Chengf26ffe92008-05-29 08:22:04 +00006623 if (isShift) {
6624 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006625 EVT EltVT = VT.getVectorElementType();
6626 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006627 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006628 }
Eric Christopherfd179292009-08-27 18:07:15 +00006629
Evan Cheng9eca5e82006-10-25 21:49:50 +00006630 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006631 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6632 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006633 V1IsSplat = isSplatVector(V1.getNode());
6634 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006635
Chris Lattner8a594482007-11-25 00:24:49 +00006636 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006637 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006638 Op = CommuteVectorShuffle(SVOp, DAG);
6639 SVOp = cast<ShuffleVectorSDNode>(Op);
6640 V1 = SVOp->getOperand(0);
6641 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006642 std::swap(V1IsSplat, V2IsSplat);
6643 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006644 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006645 }
6646
Nate Begeman9008ca62009-04-27 18:41:29 +00006647 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6648 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006649 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006650 return V1;
6651 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6652 // the instruction selector will not match, so get a canonical MOVL with
6653 // swapped operands to undo the commute.
6654 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006655 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006656
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006657 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006658 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006659
6660 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006661 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006662
Evan Cheng9bbbb982006-10-25 20:48:19 +00006663 if (V2IsSplat) {
6664 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006665 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006666 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006667 SDValue NewMask = NormalizeMask(SVOp, DAG);
6668 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6669 if (NSVOp != SVOp) {
6670 if (X86::isUNPCKLMask(NSVOp, true)) {
6671 return NewMask;
6672 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6673 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006674 }
6675 }
6676 }
6677
Evan Cheng9eca5e82006-10-25 21:49:50 +00006678 if (Commuted) {
6679 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006680 // FIXME: this seems wrong.
6681 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6682 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006683
6684 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006685 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006686
6687 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006688 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006689 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006690
Nate Begeman9008ca62009-04-27 18:41:29 +00006691 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006692 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006693 return CommuteVectorShuffle(SVOp, DAG);
6694
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006695 // The checks below are all present in isShuffleMaskLegal, but they are
6696 // inlined here right now to enable us to directly emit target specific
6697 // nodes, and remove one by one until they don't return Op anymore.
6698 SmallVector<int, 16> M;
6699 SVOp->getMask(M);
6700
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006701 if (isPALIGNRMask(M, VT, HasSSSE3))
6702 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6703 X86::getShufflePALIGNRImmediate(SVOp),
6704 DAG);
6705
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006706 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6707 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006708 if (VT == MVT::v2f64)
6709 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006710 if (VT == MVT::v2i64)
6711 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6712 }
6713
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006714 if (isPSHUFHWMask(M, VT))
6715 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6716 X86::getShufflePSHUFHWImmediate(SVOp),
6717 DAG);
6718
6719 if (isPSHUFLWMask(M, VT))
6720 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6721 X86::getShufflePSHUFLWImmediate(SVOp),
6722 DAG);
6723
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006724 if (isSHUFPMask(M, VT))
6725 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6726 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006727
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006728 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006729 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006730 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006731 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006732
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006733 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006734 // Generate target specific nodes for 128 or 256-bit shuffles only
6735 // supported in the AVX instruction set.
6736 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006737
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006738 // Handle VMOVDDUPY permutations
6739 if (isMOVDDUPYMask(SVOp, Subtarget))
6740 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6741
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006742 // Handle VPERMILPS* permutations
6743 if (isVPERMILPSMask(M, VT, Subtarget))
6744 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6745 getShuffleVPERMILPSImmediate(SVOp), DAG);
6746
6747 // Handle VPERMILPD* permutations
6748 if (isVPERMILPDMask(M, VT, Subtarget))
6749 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6750 getShuffleVPERMILPDImmediate(SVOp), DAG);
6751
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006752 // Handle VPERM2F128 permutations
6753 if (isVPERM2F128Mask(M, VT, Subtarget))
6754 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6755 getShuffleVPERM2F128Immediate(SVOp), DAG);
6756
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006757 // Handle VSHUFPSY permutations
6758 if (isVSHUFPSYMask(M, VT, Subtarget))
6759 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6760 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6761
6762 // Handle VSHUFPDY permutations
6763 if (isVSHUFPDYMask(M, VT, Subtarget))
6764 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6765 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6766
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006767 //===--------------------------------------------------------------------===//
6768 // Since no target specific shuffle was selected for this generic one,
6769 // lower it into other known shuffles. FIXME: this isn't true yet, but
6770 // this is the plan.
6771 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006772
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006773 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6774 if (VT == MVT::v8i16) {
6775 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6776 if (NewOp.getNode())
6777 return NewOp;
6778 }
6779
6780 if (VT == MVT::v16i8) {
6781 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6782 if (NewOp.getNode())
6783 return NewOp;
6784 }
6785
6786 // Handle all 128-bit wide vectors with 4 elements, and match them with
6787 // several different shuffle types.
6788 if (NumElems == 4 && VT.getSizeInBits() == 128)
6789 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6790
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006791 // Handle general 256-bit shuffles
6792 if (VT.is256BitVector())
6793 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6794
Dan Gohman475871a2008-07-27 21:46:04 +00006795 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006796}
6797
Dan Gohman475871a2008-07-27 21:46:04 +00006798SDValue
6799X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006800 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006801 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006802 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006803
6804 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6805 return SDValue();
6806
Duncan Sands83ec4b62008-06-06 12:08:01 +00006807 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006808 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006809 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006810 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006811 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006812 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006813 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006814 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6815 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6816 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006817 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6818 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006819 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006820 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006821 Op.getOperand(0)),
6822 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006823 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006824 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006825 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006826 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006827 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006828 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006829 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6830 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006831 // result has a single use which is a store or a bitcast to i32. And in
6832 // the case of a store, it's not worth it if the index is a constant 0,
6833 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006834 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006835 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006836 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006837 if ((User->getOpcode() != ISD::STORE ||
6838 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6839 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006840 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006841 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006842 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006843 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006844 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006845 Op.getOperand(0)),
6846 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006847 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006848 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006849 // ExtractPS works with constant index.
6850 if (isa<ConstantSDNode>(Op.getOperand(1)))
6851 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006852 }
Dan Gohman475871a2008-07-27 21:46:04 +00006853 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006854}
6855
6856
Dan Gohman475871a2008-07-27 21:46:04 +00006857SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006858X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6859 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006860 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006861 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006862
David Greene74a579d2011-02-10 16:57:36 +00006863 SDValue Vec = Op.getOperand(0);
6864 EVT VecVT = Vec.getValueType();
6865
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006866 // If this is a 256-bit vector result, first extract the 128-bit vector and
6867 // then extract the element from the 128-bit vector.
6868 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006869 DebugLoc dl = Op.getNode()->getDebugLoc();
6870 unsigned NumElems = VecVT.getVectorNumElements();
6871 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006872 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6873
6874 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006875 bool Upper = IdxVal >= NumElems/2;
6876 Vec = Extract128BitVector(Vec,
6877 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006878
David Greene74a579d2011-02-10 16:57:36 +00006879 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006880 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006881 }
6882
6883 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6884
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006885 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006886 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006887 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006888 return Res;
6889 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006890
Owen Andersone50ed302009-08-10 22:56:29 +00006891 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006892 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006893 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006894 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006895 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006896 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006897 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006898 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6899 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006900 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006902 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006903 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006904 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006905 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006906 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006907 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006908 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006909 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006910 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006911 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006912 if (Idx == 0)
6913 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006914
Evan Cheng0db9fe62006-04-25 20:13:52 +00006915 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006916 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006917 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006918 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006919 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006920 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006921 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006922 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006923 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6924 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6925 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006926 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006927 if (Idx == 0)
6928 return Op;
6929
6930 // UNPCKHPD the element to the lowest double word, then movsd.
6931 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6932 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006933 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006934 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006935 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006936 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006937 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006938 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006939 }
6940
Dan Gohman475871a2008-07-27 21:46:04 +00006941 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006942}
6943
Dan Gohman475871a2008-07-27 21:46:04 +00006944SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006945X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6946 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006947 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006948 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006949 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006950
Dan Gohman475871a2008-07-27 21:46:04 +00006951 SDValue N0 = Op.getOperand(0);
6952 SDValue N1 = Op.getOperand(1);
6953 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006954
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006955 if (VT.getSizeInBits() == 256)
6956 return SDValue();
6957
Dan Gohman8a55ce42009-09-23 21:02:20 +00006958 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006959 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006960 unsigned Opc;
6961 if (VT == MVT::v8i16)
6962 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006963 else if (VT == MVT::v16i8)
6964 Opc = X86ISD::PINSRB;
6965 else
6966 Opc = X86ISD::PINSRB;
6967
Nate Begeman14d12ca2008-02-11 04:19:36 +00006968 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6969 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006970 if (N1.getValueType() != MVT::i32)
6971 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6972 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006973 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006974 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006975 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006976 // Bits [7:6] of the constant are the source select. This will always be
6977 // zero here. The DAG Combiner may combine an extract_elt index into these
6978 // bits. For example (insert (extract, 3), 2) could be matched by putting
6979 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006980 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006981 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006982 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006983 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006984 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006985 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006986 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006987 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006988 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006989 // PINSR* works with constant index.
6990 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006991 }
Dan Gohman475871a2008-07-27 21:46:04 +00006992 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006993}
6994
Dan Gohman475871a2008-07-27 21:46:04 +00006995SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006996X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006997 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006998 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006999
David Greene6b381262011-02-09 15:32:06 +00007000 DebugLoc dl = Op.getDebugLoc();
7001 SDValue N0 = Op.getOperand(0);
7002 SDValue N1 = Op.getOperand(1);
7003 SDValue N2 = Op.getOperand(2);
7004
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007005 // If this is a 256-bit vector result, first extract the 128-bit vector,
7006 // insert the element into the extracted half and then place it back.
7007 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007008 if (!isa<ConstantSDNode>(N2))
7009 return SDValue();
7010
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007011 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007012 unsigned NumElems = VT.getVectorNumElements();
7013 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007014 bool Upper = IdxVal >= NumElems/2;
7015 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7016 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007017
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007018 // Insert the element into the desired half.
7019 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7020 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007021
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007022 // Insert the changed part back to the 256-bit vector
7023 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007024 }
7025
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007026 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007027 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7028
Dan Gohman8a55ce42009-09-23 21:02:20 +00007029 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007030 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007031
Dan Gohman8a55ce42009-09-23 21:02:20 +00007032 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007033 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7034 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007035 if (N1.getValueType() != MVT::i32)
7036 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7037 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007038 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007039 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007040 }
Dan Gohman475871a2008-07-27 21:46:04 +00007041 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007042}
7043
Dan Gohman475871a2008-07-27 21:46:04 +00007044SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007045X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007046 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007047 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007048 EVT OpVT = Op.getValueType();
7049
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007050 // If this is a 256-bit vector result, first insert into a 128-bit
7051 // vector and then insert into the 256-bit vector.
7052 if (OpVT.getSizeInBits() > 128) {
7053 // Insert into a 128-bit vector.
7054 EVT VT128 = EVT::getVectorVT(*Context,
7055 OpVT.getVectorElementType(),
7056 OpVT.getVectorNumElements() / 2);
7057
7058 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7059
7060 // Insert the 128-bit vector.
7061 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7062 DAG.getConstant(0, MVT::i32),
7063 DAG, dl);
7064 }
7065
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007066 if (Op.getValueType() == MVT::v1i64 &&
7067 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007069
Owen Anderson825b72b2009-08-11 20:47:22 +00007070 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007071 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7072 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007073 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007074 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007075}
7076
David Greene91585092011-01-26 15:38:49 +00007077// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7078// a simple subregister reference or explicit instructions to grab
7079// upper bits of a vector.
7080SDValue
7081X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7082 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007083 DebugLoc dl = Op.getNode()->getDebugLoc();
7084 SDValue Vec = Op.getNode()->getOperand(0);
7085 SDValue Idx = Op.getNode()->getOperand(1);
7086
7087 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7088 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7089 return Extract128BitVector(Vec, Idx, DAG, dl);
7090 }
David Greene91585092011-01-26 15:38:49 +00007091 }
7092 return SDValue();
7093}
7094
David Greenecfe33c42011-01-26 19:13:22 +00007095// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7096// simple superregister reference or explicit instructions to insert
7097// the upper bits of a vector.
7098SDValue
7099X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7100 if (Subtarget->hasAVX()) {
7101 DebugLoc dl = Op.getNode()->getDebugLoc();
7102 SDValue Vec = Op.getNode()->getOperand(0);
7103 SDValue SubVec = Op.getNode()->getOperand(1);
7104 SDValue Idx = Op.getNode()->getOperand(2);
7105
7106 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7107 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007108 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007109 }
7110 }
7111 return SDValue();
7112}
7113
Bill Wendling056292f2008-09-16 21:48:12 +00007114// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7115// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7116// one of the above mentioned nodes. It has to be wrapped because otherwise
7117// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7118// be used to form addressing mode. These wrapped nodes will be selected
7119// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007120SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007121X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007122 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007123
Chris Lattner41621a22009-06-26 19:22:52 +00007124 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7125 // global base reg.
7126 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007127 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007128 CodeModel::Model M = getTargetMachine().getCodeModel();
7129
Chris Lattner4f066492009-07-11 20:29:19 +00007130 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007131 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007132 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007133 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007134 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007135 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007136 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007137
Evan Cheng1606e8e2009-03-13 07:51:59 +00007138 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007139 CP->getAlignment(),
7140 CP->getOffset(), OpFlag);
7141 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007142 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007143 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007144 if (OpFlag) {
7145 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007146 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007147 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007148 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149 }
7150
7151 return Result;
7152}
7153
Dan Gohmand858e902010-04-17 15:26:15 +00007154SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007155 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007156
Chris Lattner18c59872009-06-27 04:16:01 +00007157 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7158 // global base reg.
7159 unsigned char OpFlag = 0;
7160 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007161 CodeModel::Model M = getTargetMachine().getCodeModel();
7162
Chris Lattner4f066492009-07-11 20:29:19 +00007163 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007164 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007165 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007166 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007167 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007168 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007169 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007170
Chris Lattner18c59872009-06-27 04:16:01 +00007171 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7172 OpFlag);
7173 DebugLoc DL = JT->getDebugLoc();
7174 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007175
Chris Lattner18c59872009-06-27 04:16:01 +00007176 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007177 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007178 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7179 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007180 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007181 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007182
Chris Lattner18c59872009-06-27 04:16:01 +00007183 return Result;
7184}
7185
7186SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007187X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007188 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007189
Chris Lattner18c59872009-06-27 04:16:01 +00007190 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7191 // global base reg.
7192 unsigned char OpFlag = 0;
7193 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007194 CodeModel::Model M = getTargetMachine().getCodeModel();
7195
Chris Lattner4f066492009-07-11 20:29:19 +00007196 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007197 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7198 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7199 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007200 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007201 } else if (Subtarget->isPICStyleGOT()) {
7202 OpFlag = X86II::MO_GOT;
7203 } else if (Subtarget->isPICStyleStubPIC()) {
7204 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7205 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7206 OpFlag = X86II::MO_DARWIN_NONLAZY;
7207 }
Eric Christopherfd179292009-08-27 18:07:15 +00007208
Chris Lattner18c59872009-06-27 04:16:01 +00007209 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007210
Chris Lattner18c59872009-06-27 04:16:01 +00007211 DebugLoc DL = Op.getDebugLoc();
7212 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007213
7214
Chris Lattner18c59872009-06-27 04:16:01 +00007215 // With PIC, the address is actually $g + Offset.
7216 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007217 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007218 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7219 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007220 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007221 Result);
7222 }
Eric Christopherfd179292009-08-27 18:07:15 +00007223
Eli Friedman586272d2011-08-11 01:48:05 +00007224 // For symbols that require a load from a stub to get the address, emit the
7225 // load.
7226 if (isGlobalStubReference(OpFlag))
7227 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7228 MachinePointerInfo::getGOT(), false, false, 0);
7229
Chris Lattner18c59872009-06-27 04:16:01 +00007230 return Result;
7231}
7232
Dan Gohman475871a2008-07-27 21:46:04 +00007233SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007234X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007235 // Create the TargetBlockAddressAddress node.
7236 unsigned char OpFlags =
7237 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007238 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007239 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007240 DebugLoc dl = Op.getDebugLoc();
7241 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7242 /*isTarget=*/true, OpFlags);
7243
Dan Gohmanf705adb2009-10-30 01:28:02 +00007244 if (Subtarget->isPICStyleRIPRel() &&
7245 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007246 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7247 else
7248 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007249
Dan Gohman29cbade2009-11-20 23:18:13 +00007250 // With PIC, the address is actually $g + Offset.
7251 if (isGlobalRelativeToPICBase(OpFlags)) {
7252 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7253 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7254 Result);
7255 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007256
7257 return Result;
7258}
7259
7260SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007261X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007262 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007263 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007264 // Create the TargetGlobalAddress node, folding in the constant
7265 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007266 unsigned char OpFlags =
7267 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007268 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007269 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007270 if (OpFlags == X86II::MO_NO_FLAG &&
7271 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007272 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007273 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007274 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007275 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007276 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007277 }
Eric Christopherfd179292009-08-27 18:07:15 +00007278
Chris Lattner4f066492009-07-11 20:29:19 +00007279 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007280 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007281 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7282 else
7283 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007284
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007285 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007286 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007287 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7288 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007289 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007291
Chris Lattner36c25012009-07-10 07:34:39 +00007292 // For globals that require a load from a stub to get the address, emit the
7293 // load.
7294 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007295 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007296 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007297
Dan Gohman6520e202008-10-18 02:06:02 +00007298 // If there was a non-zero offset that we didn't fold, create an explicit
7299 // addition for it.
7300 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007301 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007302 DAG.getConstant(Offset, getPointerTy()));
7303
Evan Cheng0db9fe62006-04-25 20:13:52 +00007304 return Result;
7305}
7306
Evan Chengda43bcf2008-09-24 00:05:32 +00007307SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007308X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007309 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007310 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007311 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007312}
7313
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007314static SDValue
7315GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007316 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007317 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007318 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007319 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007320 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007321 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007322 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007323 GA->getOffset(),
7324 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007325 if (InFlag) {
7326 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007327 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007328 } else {
7329 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007330 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007331 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007332
7333 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007334 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007335
Rafael Espindola15f1b662009-04-24 12:59:40 +00007336 SDValue Flag = Chain.getValue(1);
7337 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007338}
7339
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007340// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007341static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007342LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007343 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007344 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007345 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7346 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007347 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007348 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007349 InFlag = Chain.getValue(1);
7350
Chris Lattnerb903bed2009-06-26 21:20:29 +00007351 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007352}
7353
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007354// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007355static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007356LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007357 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007358 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7359 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007360}
7361
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007362// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7363// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007364static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007365 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007366 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007367 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007368
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007369 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7370 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7371 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007372
Michael J. Spencerec38de22010-10-10 22:04:20 +00007373 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007374 DAG.getIntPtrConstant(0),
7375 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007376
Chris Lattnerb903bed2009-06-26 21:20:29 +00007377 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007378 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7379 // initialexec.
7380 unsigned WrapperKind = X86ISD::Wrapper;
7381 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007382 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007383 } else if (is64Bit) {
7384 assert(model == TLSModel::InitialExec);
7385 OperandFlags = X86II::MO_GOTTPOFF;
7386 WrapperKind = X86ISD::WrapperRIP;
7387 } else {
7388 assert(model == TLSModel::InitialExec);
7389 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007390 }
Eric Christopherfd179292009-08-27 18:07:15 +00007391
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007392 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7393 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007394 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007395 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007396 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007397 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007398
Rafael Espindola9a580232009-02-27 13:37:18 +00007399 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007400 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007401 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007402
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007403 // The address of the thread local variable is the add of the thread
7404 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007405 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007406}
7407
Dan Gohman475871a2008-07-27 21:46:04 +00007408SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007409X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007410
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007411 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007412 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007413
Eric Christopher30ef0e52010-06-03 04:07:48 +00007414 if (Subtarget->isTargetELF()) {
7415 // TODO: implement the "local dynamic" model
7416 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007417
Eric Christopher30ef0e52010-06-03 04:07:48 +00007418 // If GV is an alias then use the aliasee for determining
7419 // thread-localness.
7420 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7421 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007422
7423 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007424 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007425
Eric Christopher30ef0e52010-06-03 04:07:48 +00007426 switch (model) {
7427 case TLSModel::GeneralDynamic:
7428 case TLSModel::LocalDynamic: // not implemented
7429 if (Subtarget->is64Bit())
7430 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7431 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007432
Eric Christopher30ef0e52010-06-03 04:07:48 +00007433 case TLSModel::InitialExec:
7434 case TLSModel::LocalExec:
7435 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7436 Subtarget->is64Bit());
7437 }
7438 } else if (Subtarget->isTargetDarwin()) {
7439 // Darwin only has one model of TLS. Lower to that.
7440 unsigned char OpFlag = 0;
7441 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7442 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007443
Eric Christopher30ef0e52010-06-03 04:07:48 +00007444 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7445 // global base reg.
7446 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7447 !Subtarget->is64Bit();
7448 if (PIC32)
7449 OpFlag = X86II::MO_TLVP_PIC_BASE;
7450 else
7451 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007452 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007453 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007454 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007455 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007456 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007457
Eric Christopher30ef0e52010-06-03 04:07:48 +00007458 // With PIC32, the address is actually $g + Offset.
7459 if (PIC32)
7460 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7461 DAG.getNode(X86ISD::GlobalBaseReg,
7462 DebugLoc(), getPointerTy()),
7463 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007464
Eric Christopher30ef0e52010-06-03 04:07:48 +00007465 // Lowering the machine isd will make sure everything is in the right
7466 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007467 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007468 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007469 SDValue Args[] = { Chain, Offset };
7470 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007471
Eric Christopher30ef0e52010-06-03 04:07:48 +00007472 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7473 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7474 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007475
Eric Christopher30ef0e52010-06-03 04:07:48 +00007476 // And our return value (tls address) is in the standard call return value
7477 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007478 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7479 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007480 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007481
Eric Christopher30ef0e52010-06-03 04:07:48 +00007482 assert(false &&
7483 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007484
Torok Edwinc23197a2009-07-14 16:55:14 +00007485 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007486 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007487}
7488
Evan Cheng0db9fe62006-04-25 20:13:52 +00007489
Nadav Rotem43012222011-05-11 08:12:09 +00007490/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007491/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007492SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007493 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007494 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007495 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007496 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007497 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007498 SDValue ShOpLo = Op.getOperand(0);
7499 SDValue ShOpHi = Op.getOperand(1);
7500 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007501 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007502 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007503 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007504
Dan Gohman475871a2008-07-27 21:46:04 +00007505 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007506 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007507 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7508 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007509 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007510 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7511 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007512 }
Evan Chenge3413162006-01-09 18:33:28 +00007513
Owen Anderson825b72b2009-08-11 20:47:22 +00007514 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7515 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007516 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007517 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007518
Dan Gohman475871a2008-07-27 21:46:04 +00007519 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007520 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007521 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7522 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007523
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007524 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007525 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7526 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007527 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007528 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7529 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007530 }
7531
Dan Gohman475871a2008-07-27 21:46:04 +00007532 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007533 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007534}
Evan Chenga3195e82006-01-12 22:54:21 +00007535
Dan Gohmand858e902010-04-17 15:26:15 +00007536SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7537 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007538 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007539
Dale Johannesen0488fb62010-09-30 23:57:10 +00007540 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007541 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007542
Owen Anderson825b72b2009-08-11 20:47:22 +00007543 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007544 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007545
Eli Friedman36df4992009-05-27 00:47:34 +00007546 // These are really Legal; return the operand so the caller accepts it as
7547 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007548 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007549 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007550 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007551 Subtarget->is64Bit()) {
7552 return Op;
7553 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007554
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007555 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007556 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007557 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007558 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007559 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007560 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007561 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007562 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007563 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007564 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7565}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007566
Owen Andersone50ed302009-08-10 22:56:29 +00007567SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007568 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007569 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007570 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007571 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007572 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007573 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007574 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007575 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007576 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007577 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007578
Chris Lattner492a43e2010-09-22 01:28:21 +00007579 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007580
Stuart Hastings84be9582011-06-02 15:57:11 +00007581 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7582 MachineMemOperand *MMO;
7583 if (FI) {
7584 int SSFI = FI->getIndex();
7585 MMO =
7586 DAG.getMachineFunction()
7587 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7588 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7589 } else {
7590 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7591 StackSlot = StackSlot.getOperand(1);
7592 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007593 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007594 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7595 X86ISD::FILD, DL,
7596 Tys, Ops, array_lengthof(Ops),
7597 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007598
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007599 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007600 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007601 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007602
7603 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7604 // shouldn't be necessary except that RFP cannot be live across
7605 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007606 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007607 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7608 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007609 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007610 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007611 SDValue Ops[] = {
7612 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7613 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007614 MachineMemOperand *MMO =
7615 DAG.getMachineFunction()
7616 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007617 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007618
Chris Lattner492a43e2010-09-22 01:28:21 +00007619 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7620 Ops, array_lengthof(Ops),
7621 Op.getValueType(), MMO);
7622 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007623 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007624 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007625 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007626
Evan Cheng0db9fe62006-04-25 20:13:52 +00007627 return Result;
7628}
7629
Bill Wendling8b8a6362009-01-17 03:56:04 +00007630// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007631SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7632 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007633 // This algorithm is not obvious. Here it is in C code, more or less:
7634 /*
7635 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7636 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7637 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007638
Bill Wendling8b8a6362009-01-17 03:56:04 +00007639 // Copy ints to xmm registers.
7640 __m128i xh = _mm_cvtsi32_si128( hi );
7641 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007642
Bill Wendling8b8a6362009-01-17 03:56:04 +00007643 // Combine into low half of a single xmm register.
7644 __m128i x = _mm_unpacklo_epi32( xh, xl );
7645 __m128d d;
7646 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007647
Bill Wendling8b8a6362009-01-17 03:56:04 +00007648 // Merge in appropriate exponents to give the integer bits the right
7649 // magnitude.
7650 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007651
Bill Wendling8b8a6362009-01-17 03:56:04 +00007652 // Subtract away the biases to deal with the IEEE-754 double precision
7653 // implicit 1.
7654 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007655
Bill Wendling8b8a6362009-01-17 03:56:04 +00007656 // All conversions up to here are exact. The correctly rounded result is
7657 // calculated using the current rounding mode using the following
7658 // horizontal add.
7659 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7660 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7661 // store doesn't really need to be here (except
7662 // maybe to zero the other double)
7663 return sd;
7664 }
7665 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007666
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007667 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007668 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007669
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007670 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007671 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007672 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7673 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7674 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7675 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007676 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007677 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007678
Bill Wendling8b8a6362009-01-17 03:56:04 +00007679 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007680 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007681 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007682 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007683 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007684 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007685 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007686
Owen Anderson825b72b2009-08-11 20:47:22 +00007687 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7688 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007689 Op.getOperand(0),
7690 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007691 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7692 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007693 Op.getOperand(0),
7694 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007695 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7696 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007697 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007698 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007699 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007700 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007701 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007702 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007703 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007704 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007705
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007706 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007707 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007708 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7709 DAG.getUNDEF(MVT::v2f64), ShufMask);
7710 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7711 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007712 DAG.getIntPtrConstant(0));
7713}
7714
Bill Wendling8b8a6362009-01-17 03:56:04 +00007715// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007716SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7717 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007718 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007719 // FP constant to bias correct the final result.
7720 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007721 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007722
7723 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007724 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007725 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007726
Eli Friedmanf3704762011-08-29 21:15:46 +00007727 // Zero out the upper parts of the register.
7728 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasSSE2(), DAG);
7729
Owen Anderson825b72b2009-08-11 20:47:22 +00007730 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007731 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007732 DAG.getIntPtrConstant(0));
7733
7734 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007735 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007736 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007737 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007738 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007739 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007740 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007741 MVT::v2f64, Bias)));
7742 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007743 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007744 DAG.getIntPtrConstant(0));
7745
7746 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007747 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007748
7749 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007750 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007751
Owen Anderson825b72b2009-08-11 20:47:22 +00007752 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007753 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007754 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007755 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007756 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007757 }
7758
7759 // Handle final rounding.
7760 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007761}
7762
Dan Gohmand858e902010-04-17 15:26:15 +00007763SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7764 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007765 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007766 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007767
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007768 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007769 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7770 // the optimization here.
7771 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007772 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007773
Owen Andersone50ed302009-08-10 22:56:29 +00007774 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007775 EVT DstVT = Op.getValueType();
7776 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007777 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007778 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007779 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007780
7781 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007782 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007783 if (SrcVT == MVT::i32) {
7784 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7785 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7786 getPointerTy(), StackSlot, WordOff);
7787 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007788 StackSlot, MachinePointerInfo(),
7789 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007790 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007791 OffsetSlot, MachinePointerInfo(),
7792 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007793 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7794 return Fild;
7795 }
7796
7797 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7798 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007799 StackSlot, MachinePointerInfo(),
7800 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007801 // For i64 source, we need to add the appropriate power of 2 if the input
7802 // was negative. This is the same as the optimization in
7803 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7804 // we must be careful to do the computation in x87 extended precision, not
7805 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007806 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7807 MachineMemOperand *MMO =
7808 DAG.getMachineFunction()
7809 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7810 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007811
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007812 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7813 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007814 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7815 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007816
7817 APInt FF(32, 0x5F800000ULL);
7818
7819 // Check whether the sign bit is set.
7820 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7821 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7822 ISD::SETLT);
7823
7824 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7825 SDValue FudgePtr = DAG.getConstantPool(
7826 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7827 getPointerTy());
7828
7829 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7830 SDValue Zero = DAG.getIntPtrConstant(0);
7831 SDValue Four = DAG.getIntPtrConstant(4);
7832 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7833 Zero, Four);
7834 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7835
7836 // Load the value out, extending it from f32 to f80.
7837 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007838 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007839 FudgePtr, MachinePointerInfo::getConstantPool(),
7840 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007841 // Extend everything to 80 bits to force it to be done on x87.
7842 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7843 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007844}
7845
Dan Gohman475871a2008-07-27 21:46:04 +00007846std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007847FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007848 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007849
Owen Andersone50ed302009-08-10 22:56:29 +00007850 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007851
7852 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007853 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7854 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007855 }
7856
Owen Anderson825b72b2009-08-11 20:47:22 +00007857 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7858 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007859 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007860
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007861 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007863 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007864 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007865 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007866 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007867 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007868 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007869
Evan Cheng87c89352007-10-15 20:11:21 +00007870 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7871 // stack slot.
7872 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007873 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007874 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007875 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007876
Michael J. Spencerec38de22010-10-10 22:04:20 +00007877
7878
Evan Cheng0db9fe62006-04-25 20:13:52 +00007879 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007881 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007882 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7883 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7884 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007885 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007886
Dan Gohman475871a2008-07-27 21:46:04 +00007887 SDValue Chain = DAG.getEntryNode();
7888 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007889 EVT TheVT = Op.getOperand(0).getValueType();
7890 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007891 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007892 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007893 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007894 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007895 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007896 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007897 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007898 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007899
Chris Lattner492a43e2010-09-22 01:28:21 +00007900 MachineMemOperand *MMO =
7901 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7902 MachineMemOperand::MOLoad, MemSize, MemSize);
7903 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7904 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007905 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007906 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007907 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7908 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007909
Chris Lattner07290932010-09-22 01:05:16 +00007910 MachineMemOperand *MMO =
7911 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7912 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007913
Evan Cheng0db9fe62006-04-25 20:13:52 +00007914 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007915 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007916 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7917 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007918
Chris Lattner27a6c732007-11-24 07:07:01 +00007919 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007920}
7921
Dan Gohmand858e902010-04-17 15:26:15 +00007922SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7923 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007924 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007925 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007926
Eli Friedman948e95a2009-05-23 09:59:16 +00007927 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007928 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007929 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7930 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007931
Chris Lattner27a6c732007-11-24 07:07:01 +00007932 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007933 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007934 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007935}
7936
Dan Gohmand858e902010-04-17 15:26:15 +00007937SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7938 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007939 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7940 SDValue FIST = Vals.first, StackSlot = Vals.second;
7941 assert(FIST.getNode() && "Unexpected failure");
7942
7943 // Load the result.
7944 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007945 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007946}
7947
Dan Gohmand858e902010-04-17 15:26:15 +00007948SDValue X86TargetLowering::LowerFABS(SDValue Op,
7949 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007950 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007951 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007952 EVT VT = Op.getValueType();
7953 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007954 if (VT.isVector())
7955 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007956 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007957 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007958 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007959 CV.push_back(C);
7960 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007961 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007962 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007963 CV.push_back(C);
7964 CV.push_back(C);
7965 CV.push_back(C);
7966 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007967 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007968 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007969 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007970 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007971 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007972 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007973 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007974}
7975
Dan Gohmand858e902010-04-17 15:26:15 +00007976SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007977 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007978 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007979 EVT VT = Op.getValueType();
7980 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007981 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007982 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007983 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007984 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007985 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007986 CV.push_back(C);
7987 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007988 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007989 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007990 CV.push_back(C);
7991 CV.push_back(C);
7992 CV.push_back(C);
7993 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007994 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007995 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007996 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007997 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007998 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007999 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008000 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008001 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008002 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008003 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008004 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008005 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008006 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008007 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008008 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008009}
8010
Dan Gohmand858e902010-04-17 15:26:15 +00008011SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008012 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008013 SDValue Op0 = Op.getOperand(0);
8014 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008015 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008016 EVT VT = Op.getValueType();
8017 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008018
8019 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008020 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008021 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008022 SrcVT = VT;
8023 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008024 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008025 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008026 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008027 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008028 }
8029
8030 // At this point the operands and the result should have the same
8031 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008032
Evan Cheng68c47cb2007-01-05 07:55:56 +00008033 // First get the sign bit of second operand.
8034 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008035 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008036 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8037 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008038 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008039 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8040 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8041 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8042 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008043 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008044 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008045 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008046 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008047 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008048 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008049 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008050
8051 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008052 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008053 // Op0 is MVT::f32, Op1 is MVT::f64.
8054 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8055 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8056 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008057 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008058 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008059 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008060 }
8061
Evan Cheng73d6cf12007-01-05 21:37:56 +00008062 // Clear first operand sign bit.
8063 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008064 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008065 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8066 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008067 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008068 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8069 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8070 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8071 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008072 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008073 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008074 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008075 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008076 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008077 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008078 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008079
8080 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008081 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008082}
8083
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008084SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8085 SDValue N0 = Op.getOperand(0);
8086 DebugLoc dl = Op.getDebugLoc();
8087 EVT VT = Op.getValueType();
8088
8089 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8090 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8091 DAG.getConstant(1, VT));
8092 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8093}
8094
Dan Gohman076aee32009-03-04 19:44:21 +00008095/// Emit nodes that will be selected as "test Op0,Op0", or something
8096/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008097SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008098 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008099 DebugLoc dl = Op.getDebugLoc();
8100
Dan Gohman31125812009-03-07 01:58:32 +00008101 // CF and OF aren't always set the way we want. Determine which
8102 // of these we need.
8103 bool NeedCF = false;
8104 bool NeedOF = false;
8105 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008106 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008107 case X86::COND_A: case X86::COND_AE:
8108 case X86::COND_B: case X86::COND_BE:
8109 NeedCF = true;
8110 break;
8111 case X86::COND_G: case X86::COND_GE:
8112 case X86::COND_L: case X86::COND_LE:
8113 case X86::COND_O: case X86::COND_NO:
8114 NeedOF = true;
8115 break;
Dan Gohman31125812009-03-07 01:58:32 +00008116 }
8117
Dan Gohman076aee32009-03-04 19:44:21 +00008118 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008119 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8120 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008121 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8122 // Emit a CMP with 0, which is the TEST pattern.
8123 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8124 DAG.getConstant(0, Op.getValueType()));
8125
8126 unsigned Opcode = 0;
8127 unsigned NumOperands = 0;
8128 switch (Op.getNode()->getOpcode()) {
8129 case ISD::ADD:
8130 // Due to an isel shortcoming, be conservative if this add is likely to be
8131 // selected as part of a load-modify-store instruction. When the root node
8132 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8133 // uses of other nodes in the match, such as the ADD in this case. This
8134 // leads to the ADD being left around and reselected, with the result being
8135 // two adds in the output. Alas, even if none our users are stores, that
8136 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8137 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8138 // climbing the DAG back to the root, and it doesn't seem to be worth the
8139 // effort.
8140 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008141 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008142 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8143 goto default_case;
8144
8145 if (ConstantSDNode *C =
8146 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8147 // An add of one will be selected as an INC.
8148 if (C->getAPIntValue() == 1) {
8149 Opcode = X86ISD::INC;
8150 NumOperands = 1;
8151 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008152 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008153
8154 // An add of negative one (subtract of one) will be selected as a DEC.
8155 if (C->getAPIntValue().isAllOnesValue()) {
8156 Opcode = X86ISD::DEC;
8157 NumOperands = 1;
8158 break;
8159 }
Dan Gohman076aee32009-03-04 19:44:21 +00008160 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008161
8162 // Otherwise use a regular EFLAGS-setting add.
8163 Opcode = X86ISD::ADD;
8164 NumOperands = 2;
8165 break;
8166 case ISD::AND: {
8167 // If the primary and result isn't used, don't bother using X86ISD::AND,
8168 // because a TEST instruction will be better.
8169 bool NonFlagUse = false;
8170 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8171 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8172 SDNode *User = *UI;
8173 unsigned UOpNo = UI.getOperandNo();
8174 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8175 // Look pass truncate.
8176 UOpNo = User->use_begin().getOperandNo();
8177 User = *User->use_begin();
8178 }
8179
8180 if (User->getOpcode() != ISD::BRCOND &&
8181 User->getOpcode() != ISD::SETCC &&
8182 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8183 NonFlagUse = true;
8184 break;
8185 }
Dan Gohman076aee32009-03-04 19:44:21 +00008186 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008187
8188 if (!NonFlagUse)
8189 break;
8190 }
8191 // FALL THROUGH
8192 case ISD::SUB:
8193 case ISD::OR:
8194 case ISD::XOR:
8195 // Due to the ISEL shortcoming noted above, be conservative if this op is
8196 // likely to be selected as part of a load-modify-store instruction.
8197 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8198 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8199 if (UI->getOpcode() == ISD::STORE)
8200 goto default_case;
8201
8202 // Otherwise use a regular EFLAGS-setting instruction.
8203 switch (Op.getNode()->getOpcode()) {
8204 default: llvm_unreachable("unexpected operator!");
8205 case ISD::SUB: Opcode = X86ISD::SUB; break;
8206 case ISD::OR: Opcode = X86ISD::OR; break;
8207 case ISD::XOR: Opcode = X86ISD::XOR; break;
8208 case ISD::AND: Opcode = X86ISD::AND; break;
8209 }
8210
8211 NumOperands = 2;
8212 break;
8213 case X86ISD::ADD:
8214 case X86ISD::SUB:
8215 case X86ISD::INC:
8216 case X86ISD::DEC:
8217 case X86ISD::OR:
8218 case X86ISD::XOR:
8219 case X86ISD::AND:
8220 return SDValue(Op.getNode(), 1);
8221 default:
8222 default_case:
8223 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008224 }
8225
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008226 if (Opcode == 0)
8227 // Emit a CMP with 0, which is the TEST pattern.
8228 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8229 DAG.getConstant(0, Op.getValueType()));
8230
8231 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8232 SmallVector<SDValue, 4> Ops;
8233 for (unsigned i = 0; i != NumOperands; ++i)
8234 Ops.push_back(Op.getOperand(i));
8235
8236 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8237 DAG.ReplaceAllUsesWith(Op, New);
8238 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008239}
8240
8241/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8242/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008243SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008244 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008245 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8246 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008247 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008248
8249 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008250 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008251}
8252
Evan Chengd40d03e2010-01-06 19:38:29 +00008253/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8254/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008255SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8256 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008257 SDValue Op0 = And.getOperand(0);
8258 SDValue Op1 = And.getOperand(1);
8259 if (Op0.getOpcode() == ISD::TRUNCATE)
8260 Op0 = Op0.getOperand(0);
8261 if (Op1.getOpcode() == ISD::TRUNCATE)
8262 Op1 = Op1.getOperand(0);
8263
Evan Chengd40d03e2010-01-06 19:38:29 +00008264 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008265 if (Op1.getOpcode() == ISD::SHL)
8266 std::swap(Op0, Op1);
8267 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008268 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8269 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008270 // If we looked past a truncate, check that it's only truncating away
8271 // known zeros.
8272 unsigned BitWidth = Op0.getValueSizeInBits();
8273 unsigned AndBitWidth = And.getValueSizeInBits();
8274 if (BitWidth > AndBitWidth) {
8275 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8276 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8277 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8278 return SDValue();
8279 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008280 LHS = Op1;
8281 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008282 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008283 } else if (Op1.getOpcode() == ISD::Constant) {
8284 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8285 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008286 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8287 LHS = AndLHS.getOperand(0);
8288 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008289 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008290 }
Evan Cheng0488db92007-09-25 01:57:46 +00008291
Evan Chengd40d03e2010-01-06 19:38:29 +00008292 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008293 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008294 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008295 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008296 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008297 // Also promote i16 to i32 for performance / code size reason.
8298 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008299 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008300 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008301
Evan Chengd40d03e2010-01-06 19:38:29 +00008302 // If the operand types disagree, extend the shift amount to match. Since
8303 // BT ignores high bits (like shifts) we can use anyextend.
8304 if (LHS.getValueType() != RHS.getValueType())
8305 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008306
Evan Chengd40d03e2010-01-06 19:38:29 +00008307 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8308 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8309 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8310 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008311 }
8312
Evan Cheng54de3ea2010-01-05 06:52:31 +00008313 return SDValue();
8314}
8315
Dan Gohmand858e902010-04-17 15:26:15 +00008316SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00008317 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8318 SDValue Op0 = Op.getOperand(0);
8319 SDValue Op1 = Op.getOperand(1);
8320 DebugLoc dl = Op.getDebugLoc();
8321 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8322
8323 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008324 // Lower (X & (1 << N)) == 0 to BT(X, N).
8325 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8326 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008327 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008328 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008329 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008330 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8331 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8332 if (NewSetCC.getNode())
8333 return NewSetCC;
8334 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008335
Chris Lattner481eebc2010-12-19 21:23:48 +00008336 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8337 // these.
8338 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008339 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008340 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8341 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008342
Chris Lattner481eebc2010-12-19 21:23:48 +00008343 // If the input is a setcc, then reuse the input setcc or use a new one with
8344 // the inverted condition.
8345 if (Op0.getOpcode() == X86ISD::SETCC) {
8346 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8347 bool Invert = (CC == ISD::SETNE) ^
8348 cast<ConstantSDNode>(Op1)->isNullValue();
8349 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008350
Evan Cheng2c755ba2010-02-27 07:36:59 +00008351 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008352 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8353 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8354 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008355 }
8356
Evan Chenge5b51ac2010-04-17 06:13:15 +00008357 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008358 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008359 if (X86CC == X86::COND_INVALID)
8360 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008361
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008362 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008363 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008364 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008365}
8366
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008367// Lower256IntVETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
8368// ones, and then concatenate the result back.
8369static SDValue Lower256IntVETCC(SDValue Op, SelectionDAG &DAG) {
8370 EVT VT = Op.getValueType();
8371
8372 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::VSETCC &&
8373 "Unsupported value type for operation");
8374
8375 int NumElems = VT.getVectorNumElements();
8376 DebugLoc dl = Op.getDebugLoc();
8377 SDValue CC = Op.getOperand(2);
8378 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8379 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8380
8381 // Extract the LHS vectors
8382 SDValue LHS = Op.getOperand(0);
8383 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8384 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8385
8386 // Extract the RHS vectors
8387 SDValue RHS = Op.getOperand(1);
8388 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8389 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8390
8391 // Issue the operation on the smaller types and concatenate the result back
8392 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8393 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8394 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8395 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8396 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8397}
8398
8399
Dan Gohmand858e902010-04-17 15:26:15 +00008400SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008401 SDValue Cond;
8402 SDValue Op0 = Op.getOperand(0);
8403 SDValue Op1 = Op.getOperand(1);
8404 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008405 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008406 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8407 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008408 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008409
8410 if (isFP) {
8411 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008412 EVT EltVT = Op0.getValueType().getVectorElementType();
8413 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8414
8415 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008416 bool Swap = false;
8417
8418 switch (SetCCOpcode) {
8419 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008420 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008421 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00008422 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00008423 case ISD::SETGT: Swap = true; // Fallthrough
8424 case ISD::SETLT:
8425 case ISD::SETOLT: SSECC = 1; break;
8426 case ISD::SETOGE:
8427 case ISD::SETGE: Swap = true; // Fallthrough
8428 case ISD::SETLE:
8429 case ISD::SETOLE: SSECC = 2; break;
8430 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008431 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008432 case ISD::SETNE: SSECC = 4; break;
8433 case ISD::SETULE: Swap = true;
8434 case ISD::SETUGE: SSECC = 5; break;
8435 case ISD::SETULT: Swap = true;
8436 case ISD::SETUGT: SSECC = 6; break;
8437 case ISD::SETO: SSECC = 7; break;
8438 }
8439 if (Swap)
8440 std::swap(Op0, Op1);
8441
Nate Begemanfb8ead02008-07-25 19:05:58 +00008442 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008443 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008444 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008445 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008446 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8447 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008448 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008449 }
8450 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008451 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008452 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8453 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008454 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008455 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008456 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008457 }
8458 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00008459 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008460 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008461
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008462 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008463 if (!isFP && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008464 return Lower256IntVETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008465
Nate Begeman30a0de92008-07-17 16:51:19 +00008466 // We are handling one of the integer comparisons here. Since SSE only has
8467 // GT and EQ comparisons for integer, swapping operands and multiple
8468 // operations may be required for some comparisons.
8469 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8470 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008471
Owen Anderson825b72b2009-08-11 20:47:22 +00008472 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008473 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008474 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008475 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008476 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8477 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008478 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008479
Nate Begeman30a0de92008-07-17 16:51:19 +00008480 switch (SetCCOpcode) {
8481 default: break;
8482 case ISD::SETNE: Invert = true;
8483 case ISD::SETEQ: Opc = EQOpc; break;
8484 case ISD::SETLT: Swap = true;
8485 case ISD::SETGT: Opc = GTOpc; break;
8486 case ISD::SETGE: Swap = true;
8487 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8488 case ISD::SETULT: Swap = true;
8489 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8490 case ISD::SETUGE: Swap = true;
8491 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8492 }
8493 if (Swap)
8494 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008495
Nate Begeman30a0de92008-07-17 16:51:19 +00008496 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8497 // bits of the inputs before performing those operations.
8498 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008499 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008500 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8501 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008502 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008503 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8504 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008505 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8506 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008507 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008508
Dale Johannesenace16102009-02-03 19:33:06 +00008509 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008510
8511 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008512 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008513 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008514
Nate Begeman30a0de92008-07-17 16:51:19 +00008515 return Result;
8516}
Evan Cheng0488db92007-09-25 01:57:46 +00008517
Evan Cheng370e5342008-12-03 08:38:43 +00008518// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008519static bool isX86LogicalCmp(SDValue Op) {
8520 unsigned Opc = Op.getNode()->getOpcode();
8521 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8522 return true;
8523 if (Op.getResNo() == 1 &&
8524 (Opc == X86ISD::ADD ||
8525 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008526 Opc == X86ISD::ADC ||
8527 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008528 Opc == X86ISD::SMUL ||
8529 Opc == X86ISD::UMUL ||
8530 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008531 Opc == X86ISD::DEC ||
8532 Opc == X86ISD::OR ||
8533 Opc == X86ISD::XOR ||
8534 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008535 return true;
8536
Chris Lattner9637d5b2010-12-05 07:49:54 +00008537 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8538 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008539
Dan Gohman076aee32009-03-04 19:44:21 +00008540 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008541}
8542
Chris Lattnera2b56002010-12-05 01:23:24 +00008543static bool isZero(SDValue V) {
8544 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8545 return C && C->isNullValue();
8546}
8547
Chris Lattner96908b12010-12-05 02:00:51 +00008548static bool isAllOnes(SDValue V) {
8549 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8550 return C && C->isAllOnesValue();
8551}
8552
Dan Gohmand858e902010-04-17 15:26:15 +00008553SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008554 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008555 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008556 SDValue Op1 = Op.getOperand(1);
8557 SDValue Op2 = Op.getOperand(2);
8558 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008559 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008560
Dan Gohman1a492952009-10-20 16:22:37 +00008561 if (Cond.getOpcode() == ISD::SETCC) {
8562 SDValue NewCond = LowerSETCC(Cond, DAG);
8563 if (NewCond.getNode())
8564 Cond = NewCond;
8565 }
Evan Cheng734503b2006-09-11 02:19:56 +00008566
Chris Lattnera2b56002010-12-05 01:23:24 +00008567 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008568 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008569 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008570 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008571 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008572 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8573 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008574 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008575
Chris Lattnera2b56002010-12-05 01:23:24 +00008576 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008577
8578 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008579 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8580 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008581
8582 SDValue CmpOp0 = Cmp.getOperand(0);
8583 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8584 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008585
Chris Lattner96908b12010-12-05 02:00:51 +00008586 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008587 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8588 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008589
Chris Lattner96908b12010-12-05 02:00:51 +00008590 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8591 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008592
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008593 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008594 if (N2C == 0 || !N2C->isNullValue())
8595 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8596 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008597 }
8598 }
8599
Chris Lattnera2b56002010-12-05 01:23:24 +00008600 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008601 if (Cond.getOpcode() == ISD::AND &&
8602 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8603 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008604 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008605 Cond = Cond.getOperand(0);
8606 }
8607
Evan Cheng3f41d662007-10-08 22:16:29 +00008608 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8609 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008610 if (Cond.getOpcode() == X86ISD::SETCC ||
8611 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008612 CC = Cond.getOperand(0);
8613
Dan Gohman475871a2008-07-27 21:46:04 +00008614 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008615 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008616 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008617
Evan Cheng3f41d662007-10-08 22:16:29 +00008618 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008619 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008620 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008621 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008622
Chris Lattnerd1980a52009-03-12 06:52:53 +00008623 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8624 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008625 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008626 addTest = false;
8627 }
8628 }
8629
8630 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008631 // Look pass the truncate.
8632 if (Cond.getOpcode() == ISD::TRUNCATE)
8633 Cond = Cond.getOperand(0);
8634
8635 // We know the result of AND is compared against zero. Try to match
8636 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008637 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008638 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008639 if (NewSetCC.getNode()) {
8640 CC = NewSetCC.getOperand(0);
8641 Cond = NewSetCC.getOperand(1);
8642 addTest = false;
8643 }
8644 }
8645 }
8646
8647 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008648 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008649 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008650 }
8651
Benjamin Kramere915ff32010-12-22 23:09:28 +00008652 // a < b ? -1 : 0 -> RES = ~setcc_carry
8653 // a < b ? 0 : -1 -> RES = setcc_carry
8654 // a >= b ? -1 : 0 -> RES = setcc_carry
8655 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8656 if (Cond.getOpcode() == X86ISD::CMP) {
8657 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8658
8659 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8660 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8661 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8662 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8663 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8664 return DAG.getNOT(DL, Res, Res.getValueType());
8665 return Res;
8666 }
8667 }
8668
Evan Cheng0488db92007-09-25 01:57:46 +00008669 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8670 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008671 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008672 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008673 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008674}
8675
Evan Cheng370e5342008-12-03 08:38:43 +00008676// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8677// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8678// from the AND / OR.
8679static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8680 Opc = Op.getOpcode();
8681 if (Opc != ISD::OR && Opc != ISD::AND)
8682 return false;
8683 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8684 Op.getOperand(0).hasOneUse() &&
8685 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8686 Op.getOperand(1).hasOneUse());
8687}
8688
Evan Cheng961d6d42009-02-02 08:19:07 +00008689// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8690// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008691static bool isXor1OfSetCC(SDValue Op) {
8692 if (Op.getOpcode() != ISD::XOR)
8693 return false;
8694 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8695 if (N1C && N1C->getAPIntValue() == 1) {
8696 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8697 Op.getOperand(0).hasOneUse();
8698 }
8699 return false;
8700}
8701
Dan Gohmand858e902010-04-17 15:26:15 +00008702SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008703 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008704 SDValue Chain = Op.getOperand(0);
8705 SDValue Cond = Op.getOperand(1);
8706 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008707 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008708 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008709
Dan Gohman1a492952009-10-20 16:22:37 +00008710 if (Cond.getOpcode() == ISD::SETCC) {
8711 SDValue NewCond = LowerSETCC(Cond, DAG);
8712 if (NewCond.getNode())
8713 Cond = NewCond;
8714 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008715#if 0
8716 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008717 else if (Cond.getOpcode() == X86ISD::ADD ||
8718 Cond.getOpcode() == X86ISD::SUB ||
8719 Cond.getOpcode() == X86ISD::SMUL ||
8720 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008721 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008722#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008723
Evan Chengad9c0a32009-12-15 00:53:42 +00008724 // Look pass (and (setcc_carry (cmp ...)), 1).
8725 if (Cond.getOpcode() == ISD::AND &&
8726 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8727 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008728 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008729 Cond = Cond.getOperand(0);
8730 }
8731
Evan Cheng3f41d662007-10-08 22:16:29 +00008732 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8733 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008734 if (Cond.getOpcode() == X86ISD::SETCC ||
8735 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008736 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008737
Dan Gohman475871a2008-07-27 21:46:04 +00008738 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008739 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008740 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008741 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008742 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008743 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008744 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008745 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008746 default: break;
8747 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008748 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008749 // These can only come from an arithmetic instruction with overflow,
8750 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008751 Cond = Cond.getNode()->getOperand(1);
8752 addTest = false;
8753 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008754 }
Evan Cheng0488db92007-09-25 01:57:46 +00008755 }
Evan Cheng370e5342008-12-03 08:38:43 +00008756 } else {
8757 unsigned CondOpc;
8758 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8759 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008760 if (CondOpc == ISD::OR) {
8761 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8762 // two branches instead of an explicit OR instruction with a
8763 // separate test.
8764 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008765 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008766 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008767 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008768 Chain, Dest, CC, Cmp);
8769 CC = Cond.getOperand(1).getOperand(0);
8770 Cond = Cmp;
8771 addTest = false;
8772 }
8773 } else { // ISD::AND
8774 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8775 // two branches instead of an explicit AND instruction with a
8776 // separate test. However, we only do this if this block doesn't
8777 // have a fall-through edge, because this requires an explicit
8778 // jmp when the condition is false.
8779 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008780 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008781 Op.getNode()->hasOneUse()) {
8782 X86::CondCode CCode =
8783 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8784 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008785 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008786 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008787 // Look for an unconditional branch following this conditional branch.
8788 // We need this because we need to reverse the successors in order
8789 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008790 if (User->getOpcode() == ISD::BR) {
8791 SDValue FalseBB = User->getOperand(1);
8792 SDNode *NewBR =
8793 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008794 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008795 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008796 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008797
Dale Johannesene4d209d2009-02-03 20:21:25 +00008798 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008799 Chain, Dest, CC, Cmp);
8800 X86::CondCode CCode =
8801 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8802 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008803 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008804 Cond = Cmp;
8805 addTest = false;
8806 }
8807 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008808 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008809 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8810 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8811 // It should be transformed during dag combiner except when the condition
8812 // is set by a arithmetics with overflow node.
8813 X86::CondCode CCode =
8814 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8815 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008816 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008817 Cond = Cond.getOperand(0).getOperand(1);
8818 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008819 }
Evan Cheng0488db92007-09-25 01:57:46 +00008820 }
8821
8822 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008823 // Look pass the truncate.
8824 if (Cond.getOpcode() == ISD::TRUNCATE)
8825 Cond = Cond.getOperand(0);
8826
8827 // We know the result of AND is compared against zero. Try to match
8828 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008829 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008830 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8831 if (NewSetCC.getNode()) {
8832 CC = NewSetCC.getOperand(0);
8833 Cond = NewSetCC.getOperand(1);
8834 addTest = false;
8835 }
8836 }
8837 }
8838
8839 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008840 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008841 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008842 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008843 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008844 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008845}
8846
Anton Korobeynikove060b532007-04-17 19:34:00 +00008847
8848// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8849// Calls to _alloca is needed to probe the stack when allocating more than 4k
8850// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8851// that the guard pages used by the OS virtual memory manager are allocated in
8852// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008853SDValue
8854X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008855 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008856 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8857 EnableSegmentedStacks) &&
8858 "This should be used only on Windows targets or when segmented stacks "
8859 "are being used.");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008860 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008861 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008862
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008863 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008864 SDValue Chain = Op.getOperand(0);
8865 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008866 // FIXME: Ensure alignment here
8867
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008868 bool Is64Bit = Subtarget->is64Bit();
8869 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008870
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008871 if (EnableSegmentedStacks) {
8872 MachineFunction &MF = DAG.getMachineFunction();
8873 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008874
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008875 if (Is64Bit) {
8876 // The 64 bit implementation of segmented stacks needs to clobber both r10
8877 // r11. This makes it impossible to use it along with nested paramenters.
8878 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008879
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008880 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8881 I != E; I++)
8882 if (I->hasNestAttr())
8883 report_fatal_error("Cannot use segmented stacks with functions that "
8884 "have nested arguments.");
8885 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008886
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008887 const TargetRegisterClass *AddrRegClass =
8888 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8889 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8890 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8891 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8892 DAG.getRegister(Vreg, SPTy));
8893 SDValue Ops1[2] = { Value, Chain };
8894 return DAG.getMergeValues(Ops1, 2, dl);
8895 } else {
8896 SDValue Flag;
8897 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008898
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008899 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8900 Flag = Chain.getValue(1);
8901 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008902
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008903 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8904 Flag = Chain.getValue(1);
8905
8906 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8907
8908 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8909 return DAG.getMergeValues(Ops1, 2, dl);
8910 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008911}
8912
Dan Gohmand858e902010-04-17 15:26:15 +00008913SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008914 MachineFunction &MF = DAG.getMachineFunction();
8915 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8916
Dan Gohman69de1932008-02-06 22:27:42 +00008917 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008918 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008919
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008920 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008921 // vastart just stores the address of the VarArgsFrameIndex slot into the
8922 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008923 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8924 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008925 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8926 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008927 }
8928
8929 // __va_list_tag:
8930 // gp_offset (0 - 6 * 8)
8931 // fp_offset (48 - 48 + 8 * 16)
8932 // overflow_arg_area (point to parameters coming in memory).
8933 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008934 SmallVector<SDValue, 8> MemOps;
8935 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008936 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008937 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008938 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8939 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008940 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008941 MemOps.push_back(Store);
8942
8943 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008944 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008945 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008946 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008947 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8948 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008949 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008950 MemOps.push_back(Store);
8951
8952 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008953 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008954 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008955 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8956 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008957 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8958 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008959 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008960 MemOps.push_back(Store);
8961
8962 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008963 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008964 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008965 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8966 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008967 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8968 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008969 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008970 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008971 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008972}
8973
Dan Gohmand858e902010-04-17 15:26:15 +00008974SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008975 assert(Subtarget->is64Bit() &&
8976 "LowerVAARG only handles 64-bit va_arg!");
8977 assert((Subtarget->isTargetLinux() ||
8978 Subtarget->isTargetDarwin()) &&
8979 "Unhandled target in LowerVAARG");
8980 assert(Op.getNode()->getNumOperands() == 4);
8981 SDValue Chain = Op.getOperand(0);
8982 SDValue SrcPtr = Op.getOperand(1);
8983 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8984 unsigned Align = Op.getConstantOperandVal(3);
8985 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008986
Dan Gohman320afb82010-10-12 18:00:49 +00008987 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008988 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00008989 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8990 uint8_t ArgMode;
8991
8992 // Decide which area this value should be read from.
8993 // TODO: Implement the AMD64 ABI in its entirety. This simple
8994 // selection mechanism works only for the basic types.
8995 if (ArgVT == MVT::f80) {
8996 llvm_unreachable("va_arg for f80 not yet implemented");
8997 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8998 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8999 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9000 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9001 } else {
9002 llvm_unreachable("Unhandled argument type in LowerVAARG");
9003 }
9004
9005 if (ArgMode == 2) {
9006 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009007 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009008 !(DAG.getMachineFunction()
9009 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009010 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009011 }
9012
9013 // Insert VAARG_64 node into the DAG
9014 // VAARG_64 returns two values: Variable Argument Address, Chain
9015 SmallVector<SDValue, 11> InstOps;
9016 InstOps.push_back(Chain);
9017 InstOps.push_back(SrcPtr);
9018 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9019 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9020 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9021 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9022 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9023 VTs, &InstOps[0], InstOps.size(),
9024 MVT::i64,
9025 MachinePointerInfo(SV),
9026 /*Align=*/0,
9027 /*Volatile=*/false,
9028 /*ReadMem=*/true,
9029 /*WriteMem=*/true);
9030 Chain = VAARG.getValue(1);
9031
9032 // Load the next argument and return it
9033 return DAG.getLoad(ArgVT, dl,
9034 Chain,
9035 VAARG,
9036 MachinePointerInfo(),
9037 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009038}
9039
Dan Gohmand858e902010-04-17 15:26:15 +00009040SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009041 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009042 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009043 SDValue Chain = Op.getOperand(0);
9044 SDValue DstPtr = Op.getOperand(1);
9045 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009046 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9047 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009048 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009049
Chris Lattnere72f2022010-09-21 05:40:29 +00009050 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009051 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009052 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009053 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009054}
9055
Dan Gohman475871a2008-07-27 21:46:04 +00009056SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009057X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009058 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009059 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009060 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009061 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009062 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009063 case Intrinsic::x86_sse_comieq_ss:
9064 case Intrinsic::x86_sse_comilt_ss:
9065 case Intrinsic::x86_sse_comile_ss:
9066 case Intrinsic::x86_sse_comigt_ss:
9067 case Intrinsic::x86_sse_comige_ss:
9068 case Intrinsic::x86_sse_comineq_ss:
9069 case Intrinsic::x86_sse_ucomieq_ss:
9070 case Intrinsic::x86_sse_ucomilt_ss:
9071 case Intrinsic::x86_sse_ucomile_ss:
9072 case Intrinsic::x86_sse_ucomigt_ss:
9073 case Intrinsic::x86_sse_ucomige_ss:
9074 case Intrinsic::x86_sse_ucomineq_ss:
9075 case Intrinsic::x86_sse2_comieq_sd:
9076 case Intrinsic::x86_sse2_comilt_sd:
9077 case Intrinsic::x86_sse2_comile_sd:
9078 case Intrinsic::x86_sse2_comigt_sd:
9079 case Intrinsic::x86_sse2_comige_sd:
9080 case Intrinsic::x86_sse2_comineq_sd:
9081 case Intrinsic::x86_sse2_ucomieq_sd:
9082 case Intrinsic::x86_sse2_ucomilt_sd:
9083 case Intrinsic::x86_sse2_ucomile_sd:
9084 case Intrinsic::x86_sse2_ucomigt_sd:
9085 case Intrinsic::x86_sse2_ucomige_sd:
9086 case Intrinsic::x86_sse2_ucomineq_sd: {
9087 unsigned Opc = 0;
9088 ISD::CondCode CC = ISD::SETCC_INVALID;
9089 switch (IntNo) {
9090 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009091 case Intrinsic::x86_sse_comieq_ss:
9092 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009093 Opc = X86ISD::COMI;
9094 CC = ISD::SETEQ;
9095 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009096 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009097 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009098 Opc = X86ISD::COMI;
9099 CC = ISD::SETLT;
9100 break;
9101 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009102 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009103 Opc = X86ISD::COMI;
9104 CC = ISD::SETLE;
9105 break;
9106 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009107 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009108 Opc = X86ISD::COMI;
9109 CC = ISD::SETGT;
9110 break;
9111 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009112 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009113 Opc = X86ISD::COMI;
9114 CC = ISD::SETGE;
9115 break;
9116 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009117 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009118 Opc = X86ISD::COMI;
9119 CC = ISD::SETNE;
9120 break;
9121 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009122 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009123 Opc = X86ISD::UCOMI;
9124 CC = ISD::SETEQ;
9125 break;
9126 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009127 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009128 Opc = X86ISD::UCOMI;
9129 CC = ISD::SETLT;
9130 break;
9131 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009132 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009133 Opc = X86ISD::UCOMI;
9134 CC = ISD::SETLE;
9135 break;
9136 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009137 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009138 Opc = X86ISD::UCOMI;
9139 CC = ISD::SETGT;
9140 break;
9141 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009142 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009143 Opc = X86ISD::UCOMI;
9144 CC = ISD::SETGE;
9145 break;
9146 case Intrinsic::x86_sse_ucomineq_ss:
9147 case Intrinsic::x86_sse2_ucomineq_sd:
9148 Opc = X86ISD::UCOMI;
9149 CC = ISD::SETNE;
9150 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009151 }
Evan Cheng734503b2006-09-11 02:19:56 +00009152
Dan Gohman475871a2008-07-27 21:46:04 +00009153 SDValue LHS = Op.getOperand(1);
9154 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009155 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009156 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009157 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9158 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9159 DAG.getConstant(X86CC, MVT::i8), Cond);
9160 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009161 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009162 // ptest and testp intrinsics. The intrinsic these come from are designed to
9163 // return an integer value, not just an instruction so lower it to the ptest
9164 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009165 case Intrinsic::x86_sse41_ptestz:
9166 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009167 case Intrinsic::x86_sse41_ptestnzc:
9168 case Intrinsic::x86_avx_ptestz_256:
9169 case Intrinsic::x86_avx_ptestc_256:
9170 case Intrinsic::x86_avx_ptestnzc_256:
9171 case Intrinsic::x86_avx_vtestz_ps:
9172 case Intrinsic::x86_avx_vtestc_ps:
9173 case Intrinsic::x86_avx_vtestnzc_ps:
9174 case Intrinsic::x86_avx_vtestz_pd:
9175 case Intrinsic::x86_avx_vtestc_pd:
9176 case Intrinsic::x86_avx_vtestnzc_pd:
9177 case Intrinsic::x86_avx_vtestz_ps_256:
9178 case Intrinsic::x86_avx_vtestc_ps_256:
9179 case Intrinsic::x86_avx_vtestnzc_ps_256:
9180 case Intrinsic::x86_avx_vtestz_pd_256:
9181 case Intrinsic::x86_avx_vtestc_pd_256:
9182 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9183 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009184 unsigned X86CC = 0;
9185 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009186 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009187 case Intrinsic::x86_avx_vtestz_ps:
9188 case Intrinsic::x86_avx_vtestz_pd:
9189 case Intrinsic::x86_avx_vtestz_ps_256:
9190 case Intrinsic::x86_avx_vtestz_pd_256:
9191 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009192 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009193 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009194 // ZF = 1
9195 X86CC = X86::COND_E;
9196 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009197 case Intrinsic::x86_avx_vtestc_ps:
9198 case Intrinsic::x86_avx_vtestc_pd:
9199 case Intrinsic::x86_avx_vtestc_ps_256:
9200 case Intrinsic::x86_avx_vtestc_pd_256:
9201 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009202 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009203 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009204 // CF = 1
9205 X86CC = X86::COND_B;
9206 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009207 case Intrinsic::x86_avx_vtestnzc_ps:
9208 case Intrinsic::x86_avx_vtestnzc_pd:
9209 case Intrinsic::x86_avx_vtestnzc_ps_256:
9210 case Intrinsic::x86_avx_vtestnzc_pd_256:
9211 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009212 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009213 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009214 // ZF and CF = 0
9215 X86CC = X86::COND_A;
9216 break;
9217 }
Eric Christopherfd179292009-08-27 18:07:15 +00009218
Eric Christopher71c67532009-07-29 00:28:05 +00009219 SDValue LHS = Op.getOperand(1);
9220 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009221 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9222 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009223 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9224 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9225 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009226 }
Evan Cheng5759f972008-05-04 09:15:50 +00009227
9228 // Fix vector shift instructions where the last operand is a non-immediate
9229 // i32 value.
9230 case Intrinsic::x86_sse2_pslli_w:
9231 case Intrinsic::x86_sse2_pslli_d:
9232 case Intrinsic::x86_sse2_pslli_q:
9233 case Intrinsic::x86_sse2_psrli_w:
9234 case Intrinsic::x86_sse2_psrli_d:
9235 case Intrinsic::x86_sse2_psrli_q:
9236 case Intrinsic::x86_sse2_psrai_w:
9237 case Intrinsic::x86_sse2_psrai_d:
9238 case Intrinsic::x86_mmx_pslli_w:
9239 case Intrinsic::x86_mmx_pslli_d:
9240 case Intrinsic::x86_mmx_pslli_q:
9241 case Intrinsic::x86_mmx_psrli_w:
9242 case Intrinsic::x86_mmx_psrli_d:
9243 case Intrinsic::x86_mmx_psrli_q:
9244 case Intrinsic::x86_mmx_psrai_w:
9245 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009246 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009247 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009248 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009249
9250 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009251 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009252 switch (IntNo) {
9253 case Intrinsic::x86_sse2_pslli_w:
9254 NewIntNo = Intrinsic::x86_sse2_psll_w;
9255 break;
9256 case Intrinsic::x86_sse2_pslli_d:
9257 NewIntNo = Intrinsic::x86_sse2_psll_d;
9258 break;
9259 case Intrinsic::x86_sse2_pslli_q:
9260 NewIntNo = Intrinsic::x86_sse2_psll_q;
9261 break;
9262 case Intrinsic::x86_sse2_psrli_w:
9263 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9264 break;
9265 case Intrinsic::x86_sse2_psrli_d:
9266 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9267 break;
9268 case Intrinsic::x86_sse2_psrli_q:
9269 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9270 break;
9271 case Intrinsic::x86_sse2_psrai_w:
9272 NewIntNo = Intrinsic::x86_sse2_psra_w;
9273 break;
9274 case Intrinsic::x86_sse2_psrai_d:
9275 NewIntNo = Intrinsic::x86_sse2_psra_d;
9276 break;
9277 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009278 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009279 switch (IntNo) {
9280 case Intrinsic::x86_mmx_pslli_w:
9281 NewIntNo = Intrinsic::x86_mmx_psll_w;
9282 break;
9283 case Intrinsic::x86_mmx_pslli_d:
9284 NewIntNo = Intrinsic::x86_mmx_psll_d;
9285 break;
9286 case Intrinsic::x86_mmx_pslli_q:
9287 NewIntNo = Intrinsic::x86_mmx_psll_q;
9288 break;
9289 case Intrinsic::x86_mmx_psrli_w:
9290 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9291 break;
9292 case Intrinsic::x86_mmx_psrli_d:
9293 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9294 break;
9295 case Intrinsic::x86_mmx_psrli_q:
9296 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9297 break;
9298 case Intrinsic::x86_mmx_psrai_w:
9299 NewIntNo = Intrinsic::x86_mmx_psra_w;
9300 break;
9301 case Intrinsic::x86_mmx_psrai_d:
9302 NewIntNo = Intrinsic::x86_mmx_psra_d;
9303 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009304 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009305 }
9306 break;
9307 }
9308 }
Mon P Wangefa42202009-09-03 19:56:25 +00009309
9310 // The vector shift intrinsics with scalars uses 32b shift amounts but
9311 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9312 // to be zero.
9313 SDValue ShOps[4];
9314 ShOps[0] = ShAmt;
9315 ShOps[1] = DAG.getConstant(0, MVT::i32);
9316 if (ShAmtVT == MVT::v4i32) {
9317 ShOps[2] = DAG.getUNDEF(MVT::i32);
9318 ShOps[3] = DAG.getUNDEF(MVT::i32);
9319 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9320 } else {
9321 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009322// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009323 }
9324
Owen Andersone50ed302009-08-10 22:56:29 +00009325 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009326 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009327 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009328 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009329 Op.getOperand(1), ShAmt);
9330 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009331 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009332}
Evan Cheng72261582005-12-20 06:22:03 +00009333
Dan Gohmand858e902010-04-17 15:26:15 +00009334SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9335 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009336 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9337 MFI->setReturnAddressIsTaken(true);
9338
Bill Wendling64e87322009-01-16 19:25:27 +00009339 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009340 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009341
9342 if (Depth > 0) {
9343 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9344 SDValue Offset =
9345 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009346 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009347 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009348 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009349 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009350 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009351 }
9352
9353 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009354 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009355 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009356 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009357}
9358
Dan Gohmand858e902010-04-17 15:26:15 +00009359SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009360 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9361 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009362
Owen Andersone50ed302009-08-10 22:56:29 +00009363 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009364 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009365 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9366 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009367 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009368 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009369 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9370 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009371 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009372 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009373}
9374
Dan Gohman475871a2008-07-27 21:46:04 +00009375SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009376 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009377 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009378}
9379
Dan Gohmand858e902010-04-17 15:26:15 +00009380SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009381 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009382 SDValue Chain = Op.getOperand(0);
9383 SDValue Offset = Op.getOperand(1);
9384 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009385 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009386
Dan Gohmand8816272010-08-11 18:14:00 +00009387 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9388 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9389 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009390 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009391
Dan Gohmand8816272010-08-11 18:14:00 +00009392 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9393 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009394 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009395 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9396 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009397 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009398 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009399
Dale Johannesene4d209d2009-02-03 20:21:25 +00009400 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009401 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009402 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009403}
9404
Dan Gohman475871a2008-07-27 21:46:04 +00009405SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009406 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009407 SDValue Root = Op.getOperand(0);
9408 SDValue Trmp = Op.getOperand(1); // trampoline
9409 SDValue FPtr = Op.getOperand(2); // nested function
9410 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009411 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009412
Dan Gohman69de1932008-02-06 22:27:42 +00009413 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009414
9415 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009416 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009417
9418 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009419 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9420 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009421
Evan Cheng0e6a0522011-07-18 20:57:22 +00009422 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9423 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009424
9425 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9426
9427 // Load the pointer to the nested function into R11.
9428 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009429 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009430 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009431 Addr, MachinePointerInfo(TrmpAddr),
9432 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009433
Owen Anderson825b72b2009-08-11 20:47:22 +00009434 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9435 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009436 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9437 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009438 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009439
9440 // Load the 'nest' parameter value into R10.
9441 // R10 is specified in X86CallingConv.td
9442 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009443 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9444 DAG.getConstant(10, MVT::i64));
9445 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009446 Addr, MachinePointerInfo(TrmpAddr, 10),
9447 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009448
Owen Anderson825b72b2009-08-11 20:47:22 +00009449 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9450 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009451 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9452 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009453 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009454
9455 // Jump to the nested function.
9456 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009457 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9458 DAG.getConstant(20, MVT::i64));
9459 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009460 Addr, MachinePointerInfo(TrmpAddr, 20),
9461 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009462
9463 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009464 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9465 DAG.getConstant(22, MVT::i64));
9466 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009467 MachinePointerInfo(TrmpAddr, 22),
9468 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009469
Dan Gohman475871a2008-07-27 21:46:04 +00009470 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00009471 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009472 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009473 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009474 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009475 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009476 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009477 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009478
9479 switch (CC) {
9480 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009481 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009482 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009483 case CallingConv::X86_StdCall: {
9484 // Pass 'nest' parameter in ECX.
9485 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009486 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009487
9488 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009489 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009490 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009491
Chris Lattner58d74912008-03-12 17:45:29 +00009492 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009493 unsigned InRegCount = 0;
9494 unsigned Idx = 1;
9495
9496 for (FunctionType::param_iterator I = FTy->param_begin(),
9497 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009498 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009499 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009500 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009501
9502 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009503 report_fatal_error("Nest register in use - reduce number of inreg"
9504 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009505 }
9506 }
9507 break;
9508 }
9509 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009510 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009511 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009512 // Pass 'nest' parameter in EAX.
9513 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009514 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009515 break;
9516 }
9517
Dan Gohman475871a2008-07-27 21:46:04 +00009518 SDValue OutChains[4];
9519 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009520
Owen Anderson825b72b2009-08-11 20:47:22 +00009521 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9522 DAG.getConstant(10, MVT::i32));
9523 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009524
Chris Lattnera62fe662010-02-05 19:20:30 +00009525 // This is storing the opcode for MOV32ri.
9526 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009527 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009528 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009529 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009530 Trmp, MachinePointerInfo(TrmpAddr),
9531 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009532
Owen Anderson825b72b2009-08-11 20:47:22 +00009533 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9534 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009535 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9536 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009537 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009538
Chris Lattnera62fe662010-02-05 19:20:30 +00009539 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009540 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9541 DAG.getConstant(5, MVT::i32));
9542 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009543 MachinePointerInfo(TrmpAddr, 5),
9544 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009545
Owen Anderson825b72b2009-08-11 20:47:22 +00009546 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9547 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009548 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9549 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009550 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009551
Dan Gohman475871a2008-07-27 21:46:04 +00009552 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00009553 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009554 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009555 }
9556}
9557
Dan Gohmand858e902010-04-17 15:26:15 +00009558SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9559 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009560 /*
9561 The rounding mode is in bits 11:10 of FPSR, and has the following
9562 settings:
9563 00 Round to nearest
9564 01 Round to -inf
9565 10 Round to +inf
9566 11 Round to 0
9567
9568 FLT_ROUNDS, on the other hand, expects the following:
9569 -1 Undefined
9570 0 Round to 0
9571 1 Round to nearest
9572 2 Round to +inf
9573 3 Round to -inf
9574
9575 To perform the conversion, we do:
9576 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9577 */
9578
9579 MachineFunction &MF = DAG.getMachineFunction();
9580 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009581 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009582 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009583 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009584 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009585
9586 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009587 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009588 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009589
Michael J. Spencerec38de22010-10-10 22:04:20 +00009590
Chris Lattner2156b792010-09-22 01:11:26 +00009591 MachineMemOperand *MMO =
9592 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9593 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009594
Chris Lattner2156b792010-09-22 01:11:26 +00009595 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9596 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9597 DAG.getVTList(MVT::Other),
9598 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009599
9600 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009601 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009602 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009603
9604 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009605 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009606 DAG.getNode(ISD::SRL, DL, MVT::i16,
9607 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009608 CWD, DAG.getConstant(0x800, MVT::i16)),
9609 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009610 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009611 DAG.getNode(ISD::SRL, DL, MVT::i16,
9612 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009613 CWD, DAG.getConstant(0x400, MVT::i16)),
9614 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009615
Dan Gohman475871a2008-07-27 21:46:04 +00009616 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009617 DAG.getNode(ISD::AND, DL, MVT::i16,
9618 DAG.getNode(ISD::ADD, DL, MVT::i16,
9619 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009620 DAG.getConstant(1, MVT::i16)),
9621 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009622
9623
Duncan Sands83ec4b62008-06-06 12:08:01 +00009624 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009625 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009626}
9627
Dan Gohmand858e902010-04-17 15:26:15 +00009628SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009629 EVT VT = Op.getValueType();
9630 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009631 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009632 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009633
9634 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009635 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009636 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009637 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009638 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009639 }
Evan Cheng18efe262007-12-14 02:13:44 +00009640
Evan Cheng152804e2007-12-14 08:30:15 +00009641 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009642 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009643 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009644
9645 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009646 SDValue Ops[] = {
9647 Op,
9648 DAG.getConstant(NumBits+NumBits-1, OpVT),
9649 DAG.getConstant(X86::COND_E, MVT::i8),
9650 Op.getValue(1)
9651 };
9652 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009653
9654 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009655 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009656
Owen Anderson825b72b2009-08-11 20:47:22 +00009657 if (VT == MVT::i8)
9658 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009659 return Op;
9660}
9661
Dan Gohmand858e902010-04-17 15:26:15 +00009662SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009663 EVT VT = Op.getValueType();
9664 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009665 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009666 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009667
9668 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009669 if (VT == MVT::i8) {
9670 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009671 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009672 }
Evan Cheng152804e2007-12-14 08:30:15 +00009673
9674 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009675 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009676 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009677
9678 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009679 SDValue Ops[] = {
9680 Op,
9681 DAG.getConstant(NumBits, OpVT),
9682 DAG.getConstant(X86::COND_E, MVT::i8),
9683 Op.getValue(1)
9684 };
9685 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009686
Owen Anderson825b72b2009-08-11 20:47:22 +00009687 if (VT == MVT::i8)
9688 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009689 return Op;
9690}
9691
Craig Topper13894fa2011-08-24 06:14:18 +00009692// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9693// ones, and then concatenate the result back.
9694static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009695 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009696
9697 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9698 "Unsupported value type for operation");
9699
9700 int NumElems = VT.getVectorNumElements();
9701 DebugLoc dl = Op.getDebugLoc();
9702 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9703 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9704
9705 // Extract the LHS vectors
9706 SDValue LHS = Op.getOperand(0);
9707 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9708 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9709
9710 // Extract the RHS vectors
9711 SDValue RHS = Op.getOperand(1);
9712 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9713 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9714
9715 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9716 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9717
9718 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9719 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9720 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9721}
9722
9723SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9724 assert(Op.getValueType().getSizeInBits() == 256 &&
9725 Op.getValueType().isInteger() &&
9726 "Only handle AVX 256-bit vector integer operation");
9727 return Lower256IntArith(Op, DAG);
9728}
9729
9730SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9731 assert(Op.getValueType().getSizeInBits() == 256 &&
9732 Op.getValueType().isInteger() &&
9733 "Only handle AVX 256-bit vector integer operation");
9734 return Lower256IntArith(Op, DAG);
9735}
9736
9737SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9738 EVT VT = Op.getValueType();
9739
9740 // Decompose 256-bit ops into smaller 128-bit ops.
9741 if (VT.getSizeInBits() == 256)
9742 return Lower256IntArith(Op, DAG);
9743
Owen Anderson825b72b2009-08-11 20:47:22 +00009744 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009745 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009746
Mon P Wangaf9b9522008-12-18 21:42:19 +00009747 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9748 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9749 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9750 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9751 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9752 //
9753 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9754 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9755 // return AloBlo + AloBhi + AhiBlo;
9756
9757 SDValue A = Op.getOperand(0);
9758 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009759
Dale Johannesene4d209d2009-02-03 20:21:25 +00009760 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009761 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9762 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009763 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009764 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9765 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009766 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009767 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009768 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009769 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009770 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009771 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009772 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009773 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009774 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009775 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009776 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9777 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009778 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009779 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9780 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009781 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9782 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009783 return Res;
9784}
9785
Nadav Rotem43012222011-05-11 08:12:09 +00009786SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9787
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009788 EVT VT = Op.getValueType();
9789 DebugLoc dl = Op.getDebugLoc();
9790 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009791 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009792 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009793
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009794 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
9795 return SDValue();
9796
9797 // Decompose 256-bit shifts into smaller 128-bit shifts.
9798 if (VT.getSizeInBits() == 256) {
9799 int NumElems = VT.getVectorNumElements();
9800 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9801 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9802
9803 // Extract the two vectors
9804 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9805 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9806 DAG, dl);
9807
9808 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009809 SDValue Amt1, Amt2;
9810 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9811 // Constant shift amount
9812 SmallVector<SDValue, 4> Amt1Csts;
9813 SmallVector<SDValue, 4> Amt2Csts;
9814 for (int i = 0; i < NumElems/2; ++i)
9815 Amt1Csts.push_back(Amt->getOperand(i));
9816 for (int i = NumElems/2; i < NumElems; ++i)
9817 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009818
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009819 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9820 &Amt1Csts[0], NumElems/2);
9821 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9822 &Amt2Csts[0], NumElems/2);
9823 } else {
9824 // Variable shift amount
9825 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9826 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9827 DAG, dl);
9828 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009829
9830 // Issue new vector shifts for the smaller types
9831 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9832 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9833
9834 // Concatenate the result back
9835 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9836 }
Nate Begeman51409212010-07-28 00:21:48 +00009837
Nadav Rotem43012222011-05-11 08:12:09 +00009838 // Optimize shl/srl/sra with constant shift amount.
9839 if (isSplatVector(Amt.getNode())) {
9840 SDValue SclrAmt = Amt->getOperand(0);
9841 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9842 uint64_t ShiftAmt = C->getZExtValue();
9843
9844 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9845 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9846 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9847 R, DAG.getConstant(ShiftAmt, MVT::i32));
9848
9849 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9850 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9851 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9852 R, DAG.getConstant(ShiftAmt, MVT::i32));
9853
9854 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9855 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9856 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9857 R, DAG.getConstant(ShiftAmt, MVT::i32));
9858
9859 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9860 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9861 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9862 R, DAG.getConstant(ShiftAmt, MVT::i32));
9863
9864 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9865 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9866 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9867 R, DAG.getConstant(ShiftAmt, MVT::i32));
9868
9869 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9870 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9871 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9872 R, DAG.getConstant(ShiftAmt, MVT::i32));
9873
9874 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9875 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9876 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9877 R, DAG.getConstant(ShiftAmt, MVT::i32));
9878
9879 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9880 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9881 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9882 R, DAG.getConstant(ShiftAmt, MVT::i32));
9883 }
9884 }
9885
9886 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009887 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009888 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9889 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9890 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9891
9892 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009893
Nate Begeman51409212010-07-28 00:21:48 +00009894 std::vector<Constant*> CV(4, CI);
9895 Constant *C = ConstantVector::get(CV);
9896 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9897 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009898 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009899 false, false, 16);
9900
9901 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009902 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009903 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9904 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9905 }
Nadav Rotem43012222011-05-11 08:12:09 +00009906 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009907 // a = a << 5;
9908 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9909 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9910 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9911
9912 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9913 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9914
9915 std::vector<Constant*> CVM1(16, CM1);
9916 std::vector<Constant*> CVM2(16, CM2);
9917 Constant *C = ConstantVector::get(CVM1);
9918 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9919 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009920 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009921 false, false, 16);
9922
9923 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9924 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9925 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9926 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9927 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009928 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009929 // a += a
9930 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009931
Nate Begeman51409212010-07-28 00:21:48 +00009932 C = ConstantVector::get(CVM2);
9933 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9934 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009935 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009936 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009937
Nate Begeman51409212010-07-28 00:21:48 +00009938 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9939 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9940 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9941 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9942 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009943 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009944 // a += a
9945 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009946
Nate Begeman51409212010-07-28 00:21:48 +00009947 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009948 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00009949 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
9950 return R;
9951 }
9952 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009953}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009954
Dan Gohmand858e902010-04-17 15:26:15 +00009955SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009956 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9957 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009958 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9959 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009960 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009961 SDValue LHS = N->getOperand(0);
9962 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009963 unsigned BaseOp = 0;
9964 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009965 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009966 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009967 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009968 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009969 // A subtract of one will be selected as a INC. Note that INC doesn't
9970 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009971 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9972 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009973 BaseOp = X86ISD::INC;
9974 Cond = X86::COND_O;
9975 break;
9976 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009977 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009978 Cond = X86::COND_O;
9979 break;
9980 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009981 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009982 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009983 break;
9984 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009985 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9986 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009987 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9988 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009989 BaseOp = X86ISD::DEC;
9990 Cond = X86::COND_O;
9991 break;
9992 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009993 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009994 Cond = X86::COND_O;
9995 break;
9996 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009997 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009998 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009999 break;
10000 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010001 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010002 Cond = X86::COND_O;
10003 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010004 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10005 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10006 MVT::i32);
10007 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010008
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010009 SDValue SetCC =
10010 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10011 DAG.getConstant(X86::COND_O, MVT::i32),
10012 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010013
Dan Gohman6e5fda22011-07-22 18:45:15 +000010014 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010015 }
Bill Wendling74c37652008-12-09 22:08:41 +000010016 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010017
Bill Wendling61edeb52008-12-02 01:06:39 +000010018 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010019 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010020 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010021
Bill Wendling61edeb52008-12-02 01:06:39 +000010022 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010023 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10024 DAG.getConstant(Cond, MVT::i32),
10025 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010026
Dan Gohman6e5fda22011-07-22 18:45:15 +000010027 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010028}
10029
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010030SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10031 DebugLoc dl = Op.getDebugLoc();
10032 SDNode* Node = Op.getNode();
10033 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10034 EVT VT = Node->getValueType(0);
10035
10036 if (Subtarget->hasSSE2() && VT.isVector()) {
10037 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10038 ExtraVT.getScalarType().getSizeInBits();
10039 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10040
10041 unsigned SHLIntrinsicsID = 0;
10042 unsigned SRAIntrinsicsID = 0;
10043 switch (VT.getSimpleVT().SimpleTy) {
10044 default:
10045 return SDValue();
10046 case MVT::v2i64: {
10047 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
10048 SRAIntrinsicsID = 0;
10049 break;
10050 }
10051 case MVT::v4i32: {
10052 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10053 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10054 break;
10055 }
10056 case MVT::v8i16: {
10057 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10058 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10059 break;
10060 }
10061 }
10062
10063 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10064 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10065 Node->getOperand(0), ShAmt);
10066
10067 // In case of 1 bit sext, no need to shr
10068 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10069
10070 if (SRAIntrinsicsID) {
10071 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10072 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10073 Tmp1, ShAmt);
10074 }
10075 return Tmp1;
10076 }
10077
10078 return SDValue();
10079}
10080
10081
Eric Christopher9a9d2752010-07-22 02:48:34 +000010082SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10083 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010084
Eric Christopher77ed1352011-07-08 00:04:56 +000010085 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10086 // There isn't any reason to disable it if the target processor supports it.
10087 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010088 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010089 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010090 SDValue Ops[] = {
10091 DAG.getRegister(X86::ESP, MVT::i32), // Base
10092 DAG.getTargetConstant(1, MVT::i8), // Scale
10093 DAG.getRegister(0, MVT::i32), // Index
10094 DAG.getTargetConstant(0, MVT::i32), // Disp
10095 DAG.getRegister(0, MVT::i32), // Segment.
10096 Zero,
10097 Chain
10098 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010099 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010100 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10101 array_lengthof(Ops));
10102 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010103 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010104
Eric Christopher9a9d2752010-07-22 02:48:34 +000010105 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010106 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010107 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010108
Chris Lattner132929a2010-08-14 17:26:09 +000010109 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10110 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10111 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10112 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010113
Chris Lattner132929a2010-08-14 17:26:09 +000010114 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10115 if (!Op1 && !Op2 && !Op3 && Op4)
10116 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010117
Chris Lattner132929a2010-08-14 17:26:09 +000010118 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10119 if (Op1 && !Op2 && !Op3 && !Op4)
10120 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010121
10122 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010123 // (MFENCE)>;
10124 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010125}
10126
Eli Friedman14648462011-07-27 22:21:52 +000010127SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10128 SelectionDAG &DAG) const {
10129 DebugLoc dl = Op.getDebugLoc();
10130 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10131 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10132 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10133 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10134
10135 // The only fence that needs an instruction is a sequentially-consistent
10136 // cross-thread fence.
10137 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10138 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10139 // no-sse2). There isn't any reason to disable it if the target processor
10140 // supports it.
10141 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
10142 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10143
10144 SDValue Chain = Op.getOperand(0);
10145 SDValue Zero = DAG.getConstant(0, MVT::i32);
10146 SDValue Ops[] = {
10147 DAG.getRegister(X86::ESP, MVT::i32), // Base
10148 DAG.getTargetConstant(1, MVT::i8), // Scale
10149 DAG.getRegister(0, MVT::i32), // Index
10150 DAG.getTargetConstant(0, MVT::i32), // Disp
10151 DAG.getRegister(0, MVT::i32), // Segment.
10152 Zero,
10153 Chain
10154 };
10155 SDNode *Res =
10156 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10157 array_lengthof(Ops));
10158 return SDValue(Res, 0);
10159 }
10160
10161 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10162 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10163}
10164
10165
Dan Gohmand858e902010-04-17 15:26:15 +000010166SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010167 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010168 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010169 unsigned Reg = 0;
10170 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010171 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010172 default:
10173 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010174 case MVT::i8: Reg = X86::AL; size = 1; break;
10175 case MVT::i16: Reg = X86::AX; size = 2; break;
10176 case MVT::i32: Reg = X86::EAX; size = 4; break;
10177 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010178 assert(Subtarget->is64Bit() && "Node not type legal!");
10179 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010180 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010181 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010182 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010183 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010184 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010185 Op.getOperand(1),
10186 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010187 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010188 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010189 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010190 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10191 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10192 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010193 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010194 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010195 return cpOut;
10196}
10197
Duncan Sands1607f052008-12-01 11:39:25 +000010198SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010199 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010200 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010201 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010202 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010203 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010204 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010205 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10206 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010207 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010208 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10209 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010210 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010211 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010212 rdx.getValue(1)
10213 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010214 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010215}
10216
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010217SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010218 SelectionDAG &DAG) const {
10219 EVT SrcVT = Op.getOperand(0).getValueType();
10220 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +000010221 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
10222 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010223 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010224 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010225 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010226 // i64 <=> MMX conversions are Legal.
10227 if (SrcVT==MVT::i64 && DstVT.isVector())
10228 return Op;
10229 if (DstVT==MVT::i64 && SrcVT.isVector())
10230 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010231 // MMX <=> MMX conversions are Legal.
10232 if (SrcVT.isVector() && DstVT.isVector())
10233 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010234 // All other conversions need to be expanded.
10235 return SDValue();
10236}
Chris Lattner5b856542010-12-20 00:59:46 +000010237
Dan Gohmand858e902010-04-17 15:26:15 +000010238SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010239 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010240 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010241 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010242 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010243 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010244 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010245 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010246 Node->getOperand(0),
10247 Node->getOperand(1), negOp,
10248 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010249 cast<AtomicSDNode>(Node)->getAlignment(),
10250 cast<AtomicSDNode>(Node)->getOrdering(),
10251 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010252}
10253
Eli Friedman327236c2011-08-24 20:50:09 +000010254static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10255 SDNode *Node = Op.getNode();
10256 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010257 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010258
10259 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010260 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10261 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10262 // (The only way to get a 16-byte store is cmpxchg16b)
10263 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10264 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10265 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010266 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10267 cast<AtomicSDNode>(Node)->getMemoryVT(),
10268 Node->getOperand(0),
10269 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010270 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010271 cast<AtomicSDNode>(Node)->getOrdering(),
10272 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010273 return Swap.getValue(1);
10274 }
10275 // Other atomic stores have a simple pattern.
10276 return Op;
10277}
10278
Chris Lattner5b856542010-12-20 00:59:46 +000010279static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10280 EVT VT = Op.getNode()->getValueType(0);
10281
10282 // Let legalize expand this if it isn't a legal type yet.
10283 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10284 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010285
Chris Lattner5b856542010-12-20 00:59:46 +000010286 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010287
Chris Lattner5b856542010-12-20 00:59:46 +000010288 unsigned Opc;
10289 bool ExtraOp = false;
10290 switch (Op.getOpcode()) {
10291 default: assert(0 && "Invalid code");
10292 case ISD::ADDC: Opc = X86ISD::ADD; break;
10293 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10294 case ISD::SUBC: Opc = X86ISD::SUB; break;
10295 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10296 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010297
Chris Lattner5b856542010-12-20 00:59:46 +000010298 if (!ExtraOp)
10299 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10300 Op.getOperand(1));
10301 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10302 Op.getOperand(1), Op.getOperand(2));
10303}
10304
Evan Cheng0db9fe62006-04-25 20:13:52 +000010305/// LowerOperation - Provide custom lowering hooks for some operations.
10306///
Dan Gohmand858e902010-04-17 15:26:15 +000010307SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010308 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010309 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010310 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010311 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010312 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010313 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10314 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010315 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010316 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010317 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010318 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10319 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10320 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010321 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010322 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010323 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10324 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10325 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010326 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010327 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010328 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010329 case ISD::SHL_PARTS:
10330 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010331 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010332 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010333 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010334 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010335 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010336 case ISD::FABS: return LowerFABS(Op, DAG);
10337 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010338 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010339 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010340 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +000010341 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010342 case ISD::SELECT: return LowerSELECT(Op, DAG);
10343 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010344 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010345 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010346 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010347 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010348 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010349 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10350 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010351 case ISD::FRAME_TO_ARGS_OFFSET:
10352 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010353 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010354 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010355 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010356 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010357 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10358 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010359 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010360 case ISD::SRA:
10361 case ISD::SRL:
10362 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010363 case ISD::SADDO:
10364 case ISD::UADDO:
10365 case ISD::SSUBO:
10366 case ISD::USUBO:
10367 case ISD::SMULO:
10368 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010369 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010370 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010371 case ISD::ADDC:
10372 case ISD::ADDE:
10373 case ISD::SUBC:
10374 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010375 case ISD::ADD: return LowerADD(Op, DAG);
10376 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010377 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010378}
10379
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010380static void ReplaceATOMIC_LOAD(SDNode *Node,
10381 SmallVectorImpl<SDValue> &Results,
10382 SelectionDAG &DAG) {
10383 DebugLoc dl = Node->getDebugLoc();
10384 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10385
10386 // Convert wide load -> cmpxchg8b/cmpxchg16b
10387 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10388 // (The only way to get a 16-byte load is cmpxchg16b)
10389 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010390 SDValue Zero = DAG.getConstant(0, VT);
10391 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010392 Node->getOperand(0),
10393 Node->getOperand(1), Zero, Zero,
10394 cast<AtomicSDNode>(Node)->getMemOperand(),
10395 cast<AtomicSDNode>(Node)->getOrdering(),
10396 cast<AtomicSDNode>(Node)->getSynchScope());
10397 Results.push_back(Swap.getValue(0));
10398 Results.push_back(Swap.getValue(1));
10399}
10400
Duncan Sands1607f052008-12-01 11:39:25 +000010401void X86TargetLowering::
10402ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010403 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010404 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010405 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010406 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010407
10408 SDValue Chain = Node->getOperand(0);
10409 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010410 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010411 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010412 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010413 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010414 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010415 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010416 SDValue Result =
10417 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10418 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010419 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010420 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010421 Results.push_back(Result.getValue(2));
10422}
10423
Duncan Sands126d9072008-07-04 11:47:58 +000010424/// ReplaceNodeResults - Replace a node with an illegal result type
10425/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010426void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10427 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010428 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010429 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010430 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010431 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010432 assert(false && "Do not know how to custom type legalize this operation!");
10433 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010434 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010435 case ISD::ADDC:
10436 case ISD::ADDE:
10437 case ISD::SUBC:
10438 case ISD::SUBE:
10439 // We don't want to expand or promote these.
10440 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010441 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010442 std::pair<SDValue,SDValue> Vals =
10443 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010444 SDValue FIST = Vals.first, StackSlot = Vals.second;
10445 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010446 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010447 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010448 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10449 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010450 }
10451 return;
10452 }
10453 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010454 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010455 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010456 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010457 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010458 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010459 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010460 eax.getValue(2));
10461 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10462 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010463 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010464 Results.push_back(edx.getValue(1));
10465 return;
10466 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010467 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010468 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010469 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010470 bool Regs64bit = T == MVT::i128;
10471 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010472 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010473 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10474 DAG.getConstant(0, HalfT));
10475 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10476 DAG.getConstant(1, HalfT));
10477 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10478 Regs64bit ? X86::RAX : X86::EAX,
10479 cpInL, SDValue());
10480 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10481 Regs64bit ? X86::RDX : X86::EDX,
10482 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010483 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010484 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10485 DAG.getConstant(0, HalfT));
10486 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10487 DAG.getConstant(1, HalfT));
10488 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10489 Regs64bit ? X86::RBX : X86::EBX,
10490 swapInL, cpInH.getValue(1));
10491 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10492 Regs64bit ? X86::RCX : X86::ECX,
10493 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010494 SDValue Ops[] = { swapInH.getValue(0),
10495 N->getOperand(1),
10496 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010497 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010498 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010499 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10500 X86ISD::LCMPXCHG8_DAG;
10501 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010502 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010503 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10504 Regs64bit ? X86::RAX : X86::EAX,
10505 HalfT, Result.getValue(1));
10506 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10507 Regs64bit ? X86::RDX : X86::EDX,
10508 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010509 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010510 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010511 Results.push_back(cpOutH.getValue(1));
10512 return;
10513 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010514 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010515 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10516 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010517 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010518 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10519 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010520 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010521 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10522 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010523 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010524 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10525 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010526 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010527 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10528 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010529 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010530 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10531 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010532 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010533 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10534 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010535 case ISD::ATOMIC_LOAD:
10536 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010537 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010538}
10539
Evan Cheng72261582005-12-20 06:22:03 +000010540const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10541 switch (Opcode) {
10542 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010543 case X86ISD::BSF: return "X86ISD::BSF";
10544 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010545 case X86ISD::SHLD: return "X86ISD::SHLD";
10546 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010547 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010548 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010549 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010550 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010551 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010552 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010553 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10554 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10555 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010556 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010557 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010558 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010559 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010560 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010561 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010562 case X86ISD::COMI: return "X86ISD::COMI";
10563 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010564 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010565 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010566 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10567 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010568 case X86ISD::CMOV: return "X86ISD::CMOV";
10569 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010570 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010571 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10572 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010573 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010574 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010575 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010576 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010577 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010578 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10579 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010580 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010581 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010582 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010583 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10584 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10585 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +000010586 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010587 case X86ISD::FMAX: return "X86ISD::FMAX";
10588 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010589 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10590 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010591 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010592 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010593 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010594 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010595 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010596 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10597 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010598 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10599 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10600 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10601 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10602 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10603 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010604 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10605 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010606 case X86ISD::VSHL: return "X86ISD::VSHL";
10607 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010608 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10609 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10610 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10611 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10612 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10613 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10614 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10615 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10616 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10617 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010618 case X86ISD::ADD: return "X86ISD::ADD";
10619 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010620 case X86ISD::ADC: return "X86ISD::ADC";
10621 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010622 case X86ISD::SMUL: return "X86ISD::SMUL";
10623 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010624 case X86ISD::INC: return "X86ISD::INC";
10625 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010626 case X86ISD::OR: return "X86ISD::OR";
10627 case X86ISD::XOR: return "X86ISD::XOR";
10628 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010629 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010630 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010631 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010632 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10633 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10634 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10635 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10636 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10637 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10638 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10639 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10640 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010641 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010642 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010643 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010644 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10645 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010646 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10647 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10648 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10649 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10650 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10651 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10652 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10653 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10654 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010655 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010656 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10657 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10658 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10659 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10660 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10661 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10662 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10663 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10664 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10665 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010666 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010667 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10668 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10669 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10670 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010671 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010672 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010673 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010674 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010675 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010676 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010677 }
10678}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010679
Chris Lattnerc9addb72007-03-30 23:15:24 +000010680// isLegalAddressingMode - Return true if the addressing mode represented
10681// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010682bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010683 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010684 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010685 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010686 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010687
Chris Lattnerc9addb72007-03-30 23:15:24 +000010688 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010689 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010690 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010691
Chris Lattnerc9addb72007-03-30 23:15:24 +000010692 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010693 unsigned GVFlags =
10694 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010695
Chris Lattnerdfed4132009-07-10 07:38:24 +000010696 // If a reference to this global requires an extra load, we can't fold it.
10697 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010698 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010699
Chris Lattnerdfed4132009-07-10 07:38:24 +000010700 // If BaseGV requires a register for the PIC base, we cannot also have a
10701 // BaseReg specified.
10702 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010703 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010704
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010705 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010706 if ((M != CodeModel::Small || R != Reloc::Static) &&
10707 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010708 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010709 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010710
Chris Lattnerc9addb72007-03-30 23:15:24 +000010711 switch (AM.Scale) {
10712 case 0:
10713 case 1:
10714 case 2:
10715 case 4:
10716 case 8:
10717 // These scales always work.
10718 break;
10719 case 3:
10720 case 5:
10721 case 9:
10722 // These scales are formed with basereg+scalereg. Only accept if there is
10723 // no basereg yet.
10724 if (AM.HasBaseReg)
10725 return false;
10726 break;
10727 default: // Other stuff never works.
10728 return false;
10729 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010730
Chris Lattnerc9addb72007-03-30 23:15:24 +000010731 return true;
10732}
10733
10734
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010735bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010736 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010737 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010738 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10739 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010740 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010741 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010742 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010743}
10744
Owen Andersone50ed302009-08-10 22:56:29 +000010745bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010746 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010747 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010748 unsigned NumBits1 = VT1.getSizeInBits();
10749 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010750 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010751 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010752 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010753}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010754
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010755bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010756 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010757 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010758}
10759
Owen Andersone50ed302009-08-10 22:56:29 +000010760bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010761 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010762 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010763}
10764
Owen Andersone50ed302009-08-10 22:56:29 +000010765bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010766 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010767 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010768}
10769
Evan Cheng60c07e12006-07-05 22:17:51 +000010770/// isShuffleMaskLegal - Targets can use this to indicate that they only
10771/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10772/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10773/// are assumed to be legal.
10774bool
Eric Christopherfd179292009-08-27 18:07:15 +000010775X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010776 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010777 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010778 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +000010779 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +000010780
Nate Begemana09008b2009-10-19 02:17:23 +000010781 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010782 return (VT.getVectorNumElements() == 2 ||
10783 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10784 isMOVLMask(M, VT) ||
10785 isSHUFPMask(M, VT) ||
10786 isPSHUFDMask(M, VT) ||
10787 isPSHUFHWMask(M, VT) ||
10788 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +000010789 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010790 isUNPCKLMask(M, VT) ||
10791 isUNPCKHMask(M, VT) ||
10792 isUNPCKL_v_undef_Mask(M, VT) ||
10793 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010794}
10795
Dan Gohman7d8143f2008-04-09 20:09:42 +000010796bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010797X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010798 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010799 unsigned NumElts = VT.getVectorNumElements();
10800 // FIXME: This collection of masks seems suspect.
10801 if (NumElts == 2)
10802 return true;
10803 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10804 return (isMOVLMask(Mask, VT) ||
10805 isCommutedMOVLMask(Mask, VT, true) ||
10806 isSHUFPMask(Mask, VT) ||
10807 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010808 }
10809 return false;
10810}
10811
10812//===----------------------------------------------------------------------===//
10813// X86 Scheduler Hooks
10814//===----------------------------------------------------------------------===//
10815
Mon P Wang63307c32008-05-05 19:05:59 +000010816// private utility function
10817MachineBasicBlock *
10818X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10819 MachineBasicBlock *MBB,
10820 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010821 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010822 unsigned LoadOpc,
10823 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010824 unsigned notOpc,
10825 unsigned EAXreg,
10826 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010827 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010828 // For the atomic bitwise operator, we generate
10829 // thisMBB:
10830 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010831 // ld t1 = [bitinstr.addr]
10832 // op t2 = t1, [bitinstr.val]
10833 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010834 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10835 // bz newMBB
10836 // fallthrough -->nextMBB
10837 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10838 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010839 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010840 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010841
Mon P Wang63307c32008-05-05 19:05:59 +000010842 /// First build the CFG
10843 MachineFunction *F = MBB->getParent();
10844 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010845 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10846 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10847 F->insert(MBBIter, newMBB);
10848 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010849
Dan Gohman14152b42010-07-06 20:24:04 +000010850 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10851 nextMBB->splice(nextMBB->begin(), thisMBB,
10852 llvm::next(MachineBasicBlock::iterator(bInstr)),
10853 thisMBB->end());
10854 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010855
Mon P Wang63307c32008-05-05 19:05:59 +000010856 // Update thisMBB to fall through to newMBB
10857 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010858
Mon P Wang63307c32008-05-05 19:05:59 +000010859 // newMBB jumps to itself and fall through to nextMBB
10860 newMBB->addSuccessor(nextMBB);
10861 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010862
Mon P Wang63307c32008-05-05 19:05:59 +000010863 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010864 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010865 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010866 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010867 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010868 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010869 int numArgs = bInstr->getNumOperands() - 1;
10870 for (int i=0; i < numArgs; ++i)
10871 argOpers[i] = &bInstr->getOperand(i+1);
10872
10873 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010874 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010875 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010876
Dale Johannesen140be2d2008-08-19 18:47:28 +000010877 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010878 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010879 for (int i=0; i <= lastAddrIndx; ++i)
10880 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010881
Dale Johannesen140be2d2008-08-19 18:47:28 +000010882 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010883 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010884 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010885 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010886 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010887 tt = t1;
10888
Dale Johannesen140be2d2008-08-19 18:47:28 +000010889 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010890 assert((argOpers[valArgIndx]->isReg() ||
10891 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010892 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010893 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010894 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010895 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010896 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010897 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010898 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010899
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010900 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010901 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010902
Dale Johannesene4d209d2009-02-03 20:21:25 +000010903 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010904 for (int i=0; i <= lastAddrIndx; ++i)
10905 (*MIB).addOperand(*argOpers[i]);
10906 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010907 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010908 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10909 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010910
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010911 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010912 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010913
Mon P Wang63307c32008-05-05 19:05:59 +000010914 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010915 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010916
Dan Gohman14152b42010-07-06 20:24:04 +000010917 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010918 return nextMBB;
10919}
10920
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010921// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010922MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010923X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10924 MachineBasicBlock *MBB,
10925 unsigned regOpcL,
10926 unsigned regOpcH,
10927 unsigned immOpcL,
10928 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010929 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010930 // For the atomic bitwise operator, we generate
10931 // thisMBB (instructions are in pairs, except cmpxchg8b)
10932 // ld t1,t2 = [bitinstr.addr]
10933 // newMBB:
10934 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
10935 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000010936 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010937 // mov ECX, EBX <- t5, t6
10938 // mov EAX, EDX <- t1, t2
10939 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
10940 // mov t3, t4 <- EAX, EDX
10941 // bz newMBB
10942 // result in out1, out2
10943 // fallthrough -->nextMBB
10944
10945 const TargetRegisterClass *RC = X86::GR32RegisterClass;
10946 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010947 const unsigned NotOpc = X86::NOT32r;
10948 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10949 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10950 MachineFunction::iterator MBBIter = MBB;
10951 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010952
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010953 /// First build the CFG
10954 MachineFunction *F = MBB->getParent();
10955 MachineBasicBlock *thisMBB = MBB;
10956 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10957 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10958 F->insert(MBBIter, newMBB);
10959 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010960
Dan Gohman14152b42010-07-06 20:24:04 +000010961 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10962 nextMBB->splice(nextMBB->begin(), thisMBB,
10963 llvm::next(MachineBasicBlock::iterator(bInstr)),
10964 thisMBB->end());
10965 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010966
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010967 // Update thisMBB to fall through to newMBB
10968 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010969
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010970 // newMBB jumps to itself and fall through to nextMBB
10971 newMBB->addSuccessor(nextMBB);
10972 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010973
Dale Johannesene4d209d2009-02-03 20:21:25 +000010974 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010975 // Insert instructions into newMBB based on incoming instruction
10976 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010977 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010978 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010979 MachineOperand& dest1Oper = bInstr->getOperand(0);
10980 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010981 MachineOperand* argOpers[2 + X86::AddrNumOperands];
10982 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010983 argOpers[i] = &bInstr->getOperand(i+2);
10984
Dan Gohman71ea4e52010-05-14 21:01:44 +000010985 // We use some of the operands multiple times, so conservatively just
10986 // clear any kill flags that might be present.
10987 if (argOpers[i]->isReg() && argOpers[i]->isUse())
10988 argOpers[i]->setIsKill(false);
10989 }
10990
Evan Chengad5b52f2010-01-08 19:14:57 +000010991 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010992 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000010993
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010994 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010995 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010996 for (int i=0; i <= lastAddrIndx; ++i)
10997 (*MIB).addOperand(*argOpers[i]);
10998 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010999 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011000 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011001 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011002 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011003 MachineOperand newOp3 = *(argOpers[3]);
11004 if (newOp3.isImm())
11005 newOp3.setImm(newOp3.getImm()+4);
11006 else
11007 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011008 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011009 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011010
11011 // t3/4 are defined later, at the bottom of the loop
11012 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11013 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011014 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011015 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011016 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011017 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11018
Evan Cheng306b4ca2010-01-08 23:41:50 +000011019 // The subsequent operations should be using the destination registers of
11020 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011021 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011022 t1 = F->getRegInfo().createVirtualRegister(RC);
11023 t2 = F->getRegInfo().createVirtualRegister(RC);
11024 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11025 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011026 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011027 t1 = dest1Oper.getReg();
11028 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011029 }
11030
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011031 int valArgIndx = lastAddrIndx + 1;
11032 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011033 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011034 "invalid operand");
11035 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11036 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011037 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011038 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011039 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011040 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011041 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011042 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011043 (*MIB).addOperand(*argOpers[valArgIndx]);
11044 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011045 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011046 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011047 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011048 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011049 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011050 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011051 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011052 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011053 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011054 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011055
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011056 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011057 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011058 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011059 MIB.addReg(t2);
11060
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011061 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011062 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011063 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011064 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011065
Dale Johannesene4d209d2009-02-03 20:21:25 +000011066 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011067 for (int i=0; i <= lastAddrIndx; ++i)
11068 (*MIB).addOperand(*argOpers[i]);
11069
11070 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011071 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11072 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011073
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011074 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011075 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011076 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011077 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011078
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011079 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011080 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011081
Dan Gohman14152b42010-07-06 20:24:04 +000011082 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011083 return nextMBB;
11084}
11085
11086// private utility function
11087MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011088X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11089 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011090 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011091 // For the atomic min/max operator, we generate
11092 // thisMBB:
11093 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011094 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011095 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011096 // cmp t1, t2
11097 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011098 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011099 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11100 // bz newMBB
11101 // fallthrough -->nextMBB
11102 //
11103 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11104 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011105 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011106 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011107
Mon P Wang63307c32008-05-05 19:05:59 +000011108 /// First build the CFG
11109 MachineFunction *F = MBB->getParent();
11110 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011111 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11112 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11113 F->insert(MBBIter, newMBB);
11114 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011115
Dan Gohman14152b42010-07-06 20:24:04 +000011116 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11117 nextMBB->splice(nextMBB->begin(), thisMBB,
11118 llvm::next(MachineBasicBlock::iterator(mInstr)),
11119 thisMBB->end());
11120 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011121
Mon P Wang63307c32008-05-05 19:05:59 +000011122 // Update thisMBB to fall through to newMBB
11123 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011124
Mon P Wang63307c32008-05-05 19:05:59 +000011125 // newMBB jumps to newMBB and fall through to nextMBB
11126 newMBB->addSuccessor(nextMBB);
11127 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011128
Dale Johannesene4d209d2009-02-03 20:21:25 +000011129 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011130 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011131 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011132 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011133 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011134 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011135 int numArgs = mInstr->getNumOperands() - 1;
11136 for (int i=0; i < numArgs; ++i)
11137 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011138
Mon P Wang63307c32008-05-05 19:05:59 +000011139 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011140 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011141 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011142
Mon P Wangab3e7472008-05-05 22:56:23 +000011143 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011144 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011145 for (int i=0; i <= lastAddrIndx; ++i)
11146 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011147
Mon P Wang63307c32008-05-05 19:05:59 +000011148 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011149 assert((argOpers[valArgIndx]->isReg() ||
11150 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011151 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011152
11153 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011154 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011155 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011156 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011157 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011158 (*MIB).addOperand(*argOpers[valArgIndx]);
11159
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011160 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011161 MIB.addReg(t1);
11162
Dale Johannesene4d209d2009-02-03 20:21:25 +000011163 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011164 MIB.addReg(t1);
11165 MIB.addReg(t2);
11166
11167 // Generate movc
11168 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011169 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011170 MIB.addReg(t2);
11171 MIB.addReg(t1);
11172
11173 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011174 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011175 for (int i=0; i <= lastAddrIndx; ++i)
11176 (*MIB).addOperand(*argOpers[i]);
11177 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011178 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011179 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11180 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011181
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011182 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011183 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011184
Mon P Wang63307c32008-05-05 19:05:59 +000011185 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011186 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011187
Dan Gohman14152b42010-07-06 20:24:04 +000011188 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011189 return nextMBB;
11190}
11191
Eric Christopherf83a5de2009-08-27 18:08:16 +000011192// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011193// or XMM0_V32I8 in AVX all of this code can be replaced with that
11194// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011195MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011196X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011197 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011198 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11199 "Target must have SSE4.2 or AVX features enabled");
11200
Eric Christopherb120ab42009-08-18 22:50:32 +000011201 DebugLoc dl = MI->getDebugLoc();
11202 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011203 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011204 if (!Subtarget->hasAVX()) {
11205 if (memArg)
11206 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11207 else
11208 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11209 } else {
11210 if (memArg)
11211 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11212 else
11213 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11214 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011215
Eric Christopher41c902f2010-11-30 08:20:21 +000011216 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011217 for (unsigned i = 0; i < numArgs; ++i) {
11218 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011219 if (!(Op.isReg() && Op.isImplicit()))
11220 MIB.addOperand(Op);
11221 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011222 BuildMI(*BB, MI, dl,
11223 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11224 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011225 .addReg(X86::XMM0);
11226
Dan Gohman14152b42010-07-06 20:24:04 +000011227 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011228 return BB;
11229}
11230
11231MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011232X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011233 DebugLoc dl = MI->getDebugLoc();
11234 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011235
Eric Christopher228232b2010-11-30 07:20:12 +000011236 // Address into RAX/EAX, other two args into ECX, EDX.
11237 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11238 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11239 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11240 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011241 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011242
Eric Christopher228232b2010-11-30 07:20:12 +000011243 unsigned ValOps = X86::AddrNumOperands;
11244 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11245 .addReg(MI->getOperand(ValOps).getReg());
11246 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11247 .addReg(MI->getOperand(ValOps+1).getReg());
11248
11249 // The instruction doesn't actually take any operands though.
11250 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011251
Eric Christopher228232b2010-11-30 07:20:12 +000011252 MI->eraseFromParent(); // The pseudo is gone now.
11253 return BB;
11254}
11255
11256MachineBasicBlock *
11257X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011258 DebugLoc dl = MI->getDebugLoc();
11259 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011260
Eric Christopher228232b2010-11-30 07:20:12 +000011261 // First arg in ECX, the second in EAX.
11262 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11263 .addReg(MI->getOperand(0).getReg());
11264 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11265 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011266
Eric Christopher228232b2010-11-30 07:20:12 +000011267 // The instruction doesn't actually take any operands though.
11268 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011269
Eric Christopher228232b2010-11-30 07:20:12 +000011270 MI->eraseFromParent(); // The pseudo is gone now.
11271 return BB;
11272}
11273
11274MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011275X86TargetLowering::EmitVAARG64WithCustomInserter(
11276 MachineInstr *MI,
11277 MachineBasicBlock *MBB) const {
11278 // Emit va_arg instruction on X86-64.
11279
11280 // Operands to this pseudo-instruction:
11281 // 0 ) Output : destination address (reg)
11282 // 1-5) Input : va_list address (addr, i64mem)
11283 // 6 ) ArgSize : Size (in bytes) of vararg type
11284 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11285 // 8 ) Align : Alignment of type
11286 // 9 ) EFLAGS (implicit-def)
11287
11288 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11289 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11290
11291 unsigned DestReg = MI->getOperand(0).getReg();
11292 MachineOperand &Base = MI->getOperand(1);
11293 MachineOperand &Scale = MI->getOperand(2);
11294 MachineOperand &Index = MI->getOperand(3);
11295 MachineOperand &Disp = MI->getOperand(4);
11296 MachineOperand &Segment = MI->getOperand(5);
11297 unsigned ArgSize = MI->getOperand(6).getImm();
11298 unsigned ArgMode = MI->getOperand(7).getImm();
11299 unsigned Align = MI->getOperand(8).getImm();
11300
11301 // Memory Reference
11302 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11303 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11304 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11305
11306 // Machine Information
11307 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11308 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11309 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11310 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11311 DebugLoc DL = MI->getDebugLoc();
11312
11313 // struct va_list {
11314 // i32 gp_offset
11315 // i32 fp_offset
11316 // i64 overflow_area (address)
11317 // i64 reg_save_area (address)
11318 // }
11319 // sizeof(va_list) = 24
11320 // alignment(va_list) = 8
11321
11322 unsigned TotalNumIntRegs = 6;
11323 unsigned TotalNumXMMRegs = 8;
11324 bool UseGPOffset = (ArgMode == 1);
11325 bool UseFPOffset = (ArgMode == 2);
11326 unsigned MaxOffset = TotalNumIntRegs * 8 +
11327 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11328
11329 /* Align ArgSize to a multiple of 8 */
11330 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11331 bool NeedsAlign = (Align > 8);
11332
11333 MachineBasicBlock *thisMBB = MBB;
11334 MachineBasicBlock *overflowMBB;
11335 MachineBasicBlock *offsetMBB;
11336 MachineBasicBlock *endMBB;
11337
11338 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11339 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11340 unsigned OffsetReg = 0;
11341
11342 if (!UseGPOffset && !UseFPOffset) {
11343 // If we only pull from the overflow region, we don't create a branch.
11344 // We don't need to alter control flow.
11345 OffsetDestReg = 0; // unused
11346 OverflowDestReg = DestReg;
11347
11348 offsetMBB = NULL;
11349 overflowMBB = thisMBB;
11350 endMBB = thisMBB;
11351 } else {
11352 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11353 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11354 // If not, pull from overflow_area. (branch to overflowMBB)
11355 //
11356 // thisMBB
11357 // | .
11358 // | .
11359 // offsetMBB overflowMBB
11360 // | .
11361 // | .
11362 // endMBB
11363
11364 // Registers for the PHI in endMBB
11365 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11366 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11367
11368 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11369 MachineFunction *MF = MBB->getParent();
11370 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11371 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11372 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11373
11374 MachineFunction::iterator MBBIter = MBB;
11375 ++MBBIter;
11376
11377 // Insert the new basic blocks
11378 MF->insert(MBBIter, offsetMBB);
11379 MF->insert(MBBIter, overflowMBB);
11380 MF->insert(MBBIter, endMBB);
11381
11382 // Transfer the remainder of MBB and its successor edges to endMBB.
11383 endMBB->splice(endMBB->begin(), thisMBB,
11384 llvm::next(MachineBasicBlock::iterator(MI)),
11385 thisMBB->end());
11386 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11387
11388 // Make offsetMBB and overflowMBB successors of thisMBB
11389 thisMBB->addSuccessor(offsetMBB);
11390 thisMBB->addSuccessor(overflowMBB);
11391
11392 // endMBB is a successor of both offsetMBB and overflowMBB
11393 offsetMBB->addSuccessor(endMBB);
11394 overflowMBB->addSuccessor(endMBB);
11395
11396 // Load the offset value into a register
11397 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11398 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11399 .addOperand(Base)
11400 .addOperand(Scale)
11401 .addOperand(Index)
11402 .addDisp(Disp, UseFPOffset ? 4 : 0)
11403 .addOperand(Segment)
11404 .setMemRefs(MMOBegin, MMOEnd);
11405
11406 // Check if there is enough room left to pull this argument.
11407 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11408 .addReg(OffsetReg)
11409 .addImm(MaxOffset + 8 - ArgSizeA8);
11410
11411 // Branch to "overflowMBB" if offset >= max
11412 // Fall through to "offsetMBB" otherwise
11413 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11414 .addMBB(overflowMBB);
11415 }
11416
11417 // In offsetMBB, emit code to use the reg_save_area.
11418 if (offsetMBB) {
11419 assert(OffsetReg != 0);
11420
11421 // Read the reg_save_area address.
11422 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11423 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11424 .addOperand(Base)
11425 .addOperand(Scale)
11426 .addOperand(Index)
11427 .addDisp(Disp, 16)
11428 .addOperand(Segment)
11429 .setMemRefs(MMOBegin, MMOEnd);
11430
11431 // Zero-extend the offset
11432 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11433 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11434 .addImm(0)
11435 .addReg(OffsetReg)
11436 .addImm(X86::sub_32bit);
11437
11438 // Add the offset to the reg_save_area to get the final address.
11439 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11440 .addReg(OffsetReg64)
11441 .addReg(RegSaveReg);
11442
11443 // Compute the offset for the next argument
11444 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11445 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11446 .addReg(OffsetReg)
11447 .addImm(UseFPOffset ? 16 : 8);
11448
11449 // Store it back into the va_list.
11450 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11451 .addOperand(Base)
11452 .addOperand(Scale)
11453 .addOperand(Index)
11454 .addDisp(Disp, UseFPOffset ? 4 : 0)
11455 .addOperand(Segment)
11456 .addReg(NextOffsetReg)
11457 .setMemRefs(MMOBegin, MMOEnd);
11458
11459 // Jump to endMBB
11460 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11461 .addMBB(endMBB);
11462 }
11463
11464 //
11465 // Emit code to use overflow area
11466 //
11467
11468 // Load the overflow_area address into a register.
11469 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11470 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11471 .addOperand(Base)
11472 .addOperand(Scale)
11473 .addOperand(Index)
11474 .addDisp(Disp, 8)
11475 .addOperand(Segment)
11476 .setMemRefs(MMOBegin, MMOEnd);
11477
11478 // If we need to align it, do so. Otherwise, just copy the address
11479 // to OverflowDestReg.
11480 if (NeedsAlign) {
11481 // Align the overflow address
11482 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11483 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11484
11485 // aligned_addr = (addr + (align-1)) & ~(align-1)
11486 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11487 .addReg(OverflowAddrReg)
11488 .addImm(Align-1);
11489
11490 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11491 .addReg(TmpReg)
11492 .addImm(~(uint64_t)(Align-1));
11493 } else {
11494 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11495 .addReg(OverflowAddrReg);
11496 }
11497
11498 // Compute the next overflow address after this argument.
11499 // (the overflow address should be kept 8-byte aligned)
11500 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11501 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11502 .addReg(OverflowDestReg)
11503 .addImm(ArgSizeA8);
11504
11505 // Store the new overflow address.
11506 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11507 .addOperand(Base)
11508 .addOperand(Scale)
11509 .addOperand(Index)
11510 .addDisp(Disp, 8)
11511 .addOperand(Segment)
11512 .addReg(NextAddrReg)
11513 .setMemRefs(MMOBegin, MMOEnd);
11514
11515 // If we branched, emit the PHI to the front of endMBB.
11516 if (offsetMBB) {
11517 BuildMI(*endMBB, endMBB->begin(), DL,
11518 TII->get(X86::PHI), DestReg)
11519 .addReg(OffsetDestReg).addMBB(offsetMBB)
11520 .addReg(OverflowDestReg).addMBB(overflowMBB);
11521 }
11522
11523 // Erase the pseudo instruction
11524 MI->eraseFromParent();
11525
11526 return endMBB;
11527}
11528
11529MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011530X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11531 MachineInstr *MI,
11532 MachineBasicBlock *MBB) const {
11533 // Emit code to save XMM registers to the stack. The ABI says that the
11534 // number of registers to save is given in %al, so it's theoretically
11535 // possible to do an indirect jump trick to avoid saving all of them,
11536 // however this code takes a simpler approach and just executes all
11537 // of the stores if %al is non-zero. It's less code, and it's probably
11538 // easier on the hardware branch predictor, and stores aren't all that
11539 // expensive anyway.
11540
11541 // Create the new basic blocks. One block contains all the XMM stores,
11542 // and one block is the final destination regardless of whether any
11543 // stores were performed.
11544 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11545 MachineFunction *F = MBB->getParent();
11546 MachineFunction::iterator MBBIter = MBB;
11547 ++MBBIter;
11548 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11549 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11550 F->insert(MBBIter, XMMSaveMBB);
11551 F->insert(MBBIter, EndMBB);
11552
Dan Gohman14152b42010-07-06 20:24:04 +000011553 // Transfer the remainder of MBB and its successor edges to EndMBB.
11554 EndMBB->splice(EndMBB->begin(), MBB,
11555 llvm::next(MachineBasicBlock::iterator(MI)),
11556 MBB->end());
11557 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11558
Dan Gohmand6708ea2009-08-15 01:38:56 +000011559 // The original block will now fall through to the XMM save block.
11560 MBB->addSuccessor(XMMSaveMBB);
11561 // The XMMSaveMBB will fall through to the end block.
11562 XMMSaveMBB->addSuccessor(EndMBB);
11563
11564 // Now add the instructions.
11565 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11566 DebugLoc DL = MI->getDebugLoc();
11567
11568 unsigned CountReg = MI->getOperand(0).getReg();
11569 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11570 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11571
11572 if (!Subtarget->isTargetWin64()) {
11573 // If %al is 0, branch around the XMM save block.
11574 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011575 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011576 MBB->addSuccessor(EndMBB);
11577 }
11578
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011579 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011580 // In the XMM save block, save all the XMM argument registers.
11581 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11582 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011583 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011584 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011585 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011586 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011587 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011588 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011589 .addFrameIndex(RegSaveFrameIndex)
11590 .addImm(/*Scale=*/1)
11591 .addReg(/*IndexReg=*/0)
11592 .addImm(/*Disp=*/Offset)
11593 .addReg(/*Segment=*/0)
11594 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011595 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011596 }
11597
Dan Gohman14152b42010-07-06 20:24:04 +000011598 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011599
11600 return EndMBB;
11601}
Mon P Wang63307c32008-05-05 19:05:59 +000011602
Evan Cheng60c07e12006-07-05 22:17:51 +000011603MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011604X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011605 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011606 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11607 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011608
Chris Lattner52600972009-09-02 05:57:00 +000011609 // To "insert" a SELECT_CC instruction, we actually have to insert the
11610 // diamond control-flow pattern. The incoming instruction knows the
11611 // destination vreg to set, the condition code register to branch on, the
11612 // true/false values to select between, and a branch opcode to use.
11613 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11614 MachineFunction::iterator It = BB;
11615 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011616
Chris Lattner52600972009-09-02 05:57:00 +000011617 // thisMBB:
11618 // ...
11619 // TrueVal = ...
11620 // cmpTY ccX, r1, r2
11621 // bCC copy1MBB
11622 // fallthrough --> copy0MBB
11623 MachineBasicBlock *thisMBB = BB;
11624 MachineFunction *F = BB->getParent();
11625 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11626 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011627 F->insert(It, copy0MBB);
11628 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011629
Bill Wendling730c07e2010-06-25 20:48:10 +000011630 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11631 // live into the sink and copy blocks.
11632 const MachineFunction *MF = BB->getParent();
11633 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
11634 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000011635
Dan Gohman14152b42010-07-06 20:24:04 +000011636 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
11637 const MachineOperand &MO = MI->getOperand(I);
11638 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000011639 unsigned Reg = MO.getReg();
11640 if (Reg != X86::EFLAGS) continue;
11641 copy0MBB->addLiveIn(Reg);
11642 sinkMBB->addLiveIn(Reg);
11643 }
11644
Dan Gohman14152b42010-07-06 20:24:04 +000011645 // Transfer the remainder of BB and its successor edges to sinkMBB.
11646 sinkMBB->splice(sinkMBB->begin(), BB,
11647 llvm::next(MachineBasicBlock::iterator(MI)),
11648 BB->end());
11649 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11650
11651 // Add the true and fallthrough blocks as its successors.
11652 BB->addSuccessor(copy0MBB);
11653 BB->addSuccessor(sinkMBB);
11654
11655 // Create the conditional branch instruction.
11656 unsigned Opc =
11657 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11658 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11659
Chris Lattner52600972009-09-02 05:57:00 +000011660 // copy0MBB:
11661 // %FalseValue = ...
11662 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011663 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011664
Chris Lattner52600972009-09-02 05:57:00 +000011665 // sinkMBB:
11666 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11667 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011668 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11669 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011670 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11671 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11672
Dan Gohman14152b42010-07-06 20:24:04 +000011673 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011674 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011675}
11676
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011677MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011678X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11679 bool Is64Bit) const {
11680 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11681 DebugLoc DL = MI->getDebugLoc();
11682 MachineFunction *MF = BB->getParent();
11683 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11684
11685 assert(EnableSegmentedStacks);
11686
11687 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11688 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11689
11690 // BB:
11691 // ... [Till the alloca]
11692 // If stacklet is not large enough, jump to mallocMBB
11693 //
11694 // bumpMBB:
11695 // Allocate by subtracting from RSP
11696 // Jump to continueMBB
11697 //
11698 // mallocMBB:
11699 // Allocate by call to runtime
11700 //
11701 // continueMBB:
11702 // ...
11703 // [rest of original BB]
11704 //
11705
11706 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11707 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11708 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11709
11710 MachineRegisterInfo &MRI = MF->getRegInfo();
11711 const TargetRegisterClass *AddrRegClass =
11712 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11713
11714 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11715 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11716 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11717 sizeVReg = MI->getOperand(1).getReg(),
11718 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11719
11720 MachineFunction::iterator MBBIter = BB;
11721 ++MBBIter;
11722
11723 MF->insert(MBBIter, bumpMBB);
11724 MF->insert(MBBIter, mallocMBB);
11725 MF->insert(MBBIter, continueMBB);
11726
11727 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11728 (MachineBasicBlock::iterator(MI)), BB->end());
11729 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11730
11731 // Add code to the main basic block to check if the stack limit has been hit,
11732 // and if so, jump to mallocMBB otherwise to bumpMBB.
11733 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11734 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11735 .addReg(tmpSPVReg).addReg(sizeVReg);
11736 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11737 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11738 .addReg(tmpSPVReg);
11739 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11740
11741 // bumpMBB simply decreases the stack pointer, since we know the current
11742 // stacklet has enough space.
11743 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11744 .addReg(tmpSPVReg);
11745 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11746 .addReg(tmpSPVReg);
11747 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11748
11749 // Calls into a routine in libgcc to allocate more space from the heap.
11750 if (Is64Bit) {
11751 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11752 .addReg(sizeVReg);
11753 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11754 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11755 } else {
11756 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11757 .addImm(12);
11758 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11759 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11760 .addExternalSymbol("__morestack_allocate_stack_space");
11761 }
11762
11763 if (!Is64Bit)
11764 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11765 .addImm(16);
11766
11767 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11768 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11769 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11770
11771 // Set up the CFG correctly.
11772 BB->addSuccessor(bumpMBB);
11773 BB->addSuccessor(mallocMBB);
11774 mallocMBB->addSuccessor(continueMBB);
11775 bumpMBB->addSuccessor(continueMBB);
11776
11777 // Take care of the PHI nodes.
11778 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11779 MI->getOperand(0).getReg())
11780 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11781 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11782
11783 // Delete the original pseudo instruction.
11784 MI->eraseFromParent();
11785
11786 // And we're done.
11787 return continueMBB;
11788}
11789
11790MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011791X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011792 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011793 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11794 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011795
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011796 assert(!Subtarget->isTargetEnvMacho());
11797
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011798 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11799 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011800
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011801 if (Subtarget->isTargetWin64()) {
11802 if (Subtarget->isTargetCygMing()) {
11803 // ___chkstk(Mingw64):
11804 // Clobbers R10, R11, RAX and EFLAGS.
11805 // Updates RSP.
11806 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11807 .addExternalSymbol("___chkstk")
11808 .addReg(X86::RAX, RegState::Implicit)
11809 .addReg(X86::RSP, RegState::Implicit)
11810 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11811 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11812 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11813 } else {
11814 // __chkstk(MSVCRT): does not update stack pointer.
11815 // Clobbers R10, R11 and EFLAGS.
11816 // FIXME: RAX(allocated size) might be reused and not killed.
11817 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11818 .addExternalSymbol("__chkstk")
11819 .addReg(X86::RAX, RegState::Implicit)
11820 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11821 // RAX has the offset to subtracted from RSP.
11822 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11823 .addReg(X86::RSP)
11824 .addReg(X86::RAX);
11825 }
11826 } else {
11827 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011828 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11829
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011830 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11831 .addExternalSymbol(StackProbeSymbol)
11832 .addReg(X86::EAX, RegState::Implicit)
11833 .addReg(X86::ESP, RegState::Implicit)
11834 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11835 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11836 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11837 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011838
Dan Gohman14152b42010-07-06 20:24:04 +000011839 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011840 return BB;
11841}
Chris Lattner52600972009-09-02 05:57:00 +000011842
11843MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011844X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11845 MachineBasicBlock *BB) const {
11846 // This is pretty easy. We're taking the value that we received from
11847 // our load from the relocation, sticking it in either RDI (x86-64)
11848 // or EAX and doing an indirect call. The return value will then
11849 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011850 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011851 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011852 DebugLoc DL = MI->getDebugLoc();
11853 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011854
11855 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011856 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011857
Eric Christopher30ef0e52010-06-03 04:07:48 +000011858 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011859 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11860 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011861 .addReg(X86::RIP)
11862 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011863 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011864 MI->getOperand(3).getTargetFlags())
11865 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011866 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011867 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011868 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011869 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11870 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011871 .addReg(0)
11872 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011873 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011874 MI->getOperand(3).getTargetFlags())
11875 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011876 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011877 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011878 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011879 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11880 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011881 .addReg(TII->getGlobalBaseReg(F))
11882 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011883 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011884 MI->getOperand(3).getTargetFlags())
11885 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011886 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011887 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011888 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011889
Dan Gohman14152b42010-07-06 20:24:04 +000011890 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011891 return BB;
11892}
11893
11894MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011895X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011896 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011897 switch (MI->getOpcode()) {
11898 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011899 case X86::TAILJMPd64:
11900 case X86::TAILJMPr64:
11901 case X86::TAILJMPm64:
11902 assert(!"TAILJMP64 would not be touched here.");
11903 case X86::TCRETURNdi64:
11904 case X86::TCRETURNri64:
11905 case X86::TCRETURNmi64:
11906 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11907 // On AMD64, additional defs should be added before register allocation.
11908 if (!Subtarget->isTargetWin64()) {
11909 MI->addRegisterDefined(X86::RSI);
11910 MI->addRegisterDefined(X86::RDI);
11911 MI->addRegisterDefined(X86::XMM6);
11912 MI->addRegisterDefined(X86::XMM7);
11913 MI->addRegisterDefined(X86::XMM8);
11914 MI->addRegisterDefined(X86::XMM9);
11915 MI->addRegisterDefined(X86::XMM10);
11916 MI->addRegisterDefined(X86::XMM11);
11917 MI->addRegisterDefined(X86::XMM12);
11918 MI->addRegisterDefined(X86::XMM13);
11919 MI->addRegisterDefined(X86::XMM14);
11920 MI->addRegisterDefined(X86::XMM15);
11921 }
11922 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011923 case X86::WIN_ALLOCA:
11924 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011925 case X86::SEG_ALLOCA_32:
11926 return EmitLoweredSegAlloca(MI, BB, false);
11927 case X86::SEG_ALLOCA_64:
11928 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011929 case X86::TLSCall_32:
11930 case X86::TLSCall_64:
11931 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011932 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011933 case X86::CMOV_FR32:
11934 case X86::CMOV_FR64:
11935 case X86::CMOV_V4F32:
11936 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011937 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011938 case X86::CMOV_V8F32:
11939 case X86::CMOV_V4F64:
11940 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000011941 case X86::CMOV_GR16:
11942 case X86::CMOV_GR32:
11943 case X86::CMOV_RFP32:
11944 case X86::CMOV_RFP64:
11945 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011946 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011947
Dale Johannesen849f2142007-07-03 00:53:03 +000011948 case X86::FP32_TO_INT16_IN_MEM:
11949 case X86::FP32_TO_INT32_IN_MEM:
11950 case X86::FP32_TO_INT64_IN_MEM:
11951 case X86::FP64_TO_INT16_IN_MEM:
11952 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000011953 case X86::FP64_TO_INT64_IN_MEM:
11954 case X86::FP80_TO_INT16_IN_MEM:
11955 case X86::FP80_TO_INT32_IN_MEM:
11956 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000011957 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11958 DebugLoc DL = MI->getDebugLoc();
11959
Evan Cheng60c07e12006-07-05 22:17:51 +000011960 // Change the floating point control register to use "round towards zero"
11961 // mode when truncating to an integer value.
11962 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000011963 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000011964 addFrameReference(BuildMI(*BB, MI, DL,
11965 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011966
11967 // Load the old value of the high byte of the control word...
11968 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000011969 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000011970 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000011971 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011972
11973 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000011974 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000011975 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000011976
11977 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000011978 addFrameReference(BuildMI(*BB, MI, DL,
11979 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011980
11981 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000011982 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000011983 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000011984
11985 // Get the X86 opcode to use.
11986 unsigned Opc;
11987 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011988 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000011989 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
11990 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
11991 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
11992 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
11993 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
11994 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000011995 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
11996 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
11997 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000011998 }
11999
12000 X86AddressMode AM;
12001 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012002 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012003 AM.BaseType = X86AddressMode::RegBase;
12004 AM.Base.Reg = Op.getReg();
12005 } else {
12006 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012007 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012008 }
12009 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012010 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012011 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012012 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012013 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012014 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012015 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012016 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012017 AM.GV = Op.getGlobal();
12018 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012019 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012020 }
Dan Gohman14152b42010-07-06 20:24:04 +000012021 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012022 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012023
12024 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012025 addFrameReference(BuildMI(*BB, MI, DL,
12026 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012027
Dan Gohman14152b42010-07-06 20:24:04 +000012028 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012029 return BB;
12030 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012031 // String/text processing lowering.
12032 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012033 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012034 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12035 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012036 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012037 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12038 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012039 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012040 return EmitPCMP(MI, BB, 5, false /* in mem */);
12041 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012042 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012043 return EmitPCMP(MI, BB, 5, true /* in mem */);
12044
Eric Christopher228232b2010-11-30 07:20:12 +000012045 // Thread synchronization.
12046 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012047 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012048 case X86::MWAIT:
12049 return EmitMwait(MI, BB);
12050
Eric Christopherb120ab42009-08-18 22:50:32 +000012051 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012052 case X86::ATOMAND32:
12053 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012054 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012055 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012056 X86::NOT32r, X86::EAX,
12057 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012058 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012059 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12060 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012061 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012062 X86::NOT32r, X86::EAX,
12063 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012064 case X86::ATOMXOR32:
12065 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012066 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012067 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012068 X86::NOT32r, X86::EAX,
12069 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012070 case X86::ATOMNAND32:
12071 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012072 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012073 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012074 X86::NOT32r, X86::EAX,
12075 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012076 case X86::ATOMMIN32:
12077 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12078 case X86::ATOMMAX32:
12079 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12080 case X86::ATOMUMIN32:
12081 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12082 case X86::ATOMUMAX32:
12083 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012084
12085 case X86::ATOMAND16:
12086 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12087 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012088 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012089 X86::NOT16r, X86::AX,
12090 X86::GR16RegisterClass);
12091 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012092 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012093 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012094 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012095 X86::NOT16r, X86::AX,
12096 X86::GR16RegisterClass);
12097 case X86::ATOMXOR16:
12098 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12099 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012100 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012101 X86::NOT16r, X86::AX,
12102 X86::GR16RegisterClass);
12103 case X86::ATOMNAND16:
12104 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12105 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012106 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012107 X86::NOT16r, X86::AX,
12108 X86::GR16RegisterClass, true);
12109 case X86::ATOMMIN16:
12110 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12111 case X86::ATOMMAX16:
12112 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12113 case X86::ATOMUMIN16:
12114 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12115 case X86::ATOMUMAX16:
12116 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12117
12118 case X86::ATOMAND8:
12119 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12120 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012121 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012122 X86::NOT8r, X86::AL,
12123 X86::GR8RegisterClass);
12124 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012125 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012126 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012127 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012128 X86::NOT8r, X86::AL,
12129 X86::GR8RegisterClass);
12130 case X86::ATOMXOR8:
12131 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12132 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012133 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012134 X86::NOT8r, X86::AL,
12135 X86::GR8RegisterClass);
12136 case X86::ATOMNAND8:
12137 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12138 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012139 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012140 X86::NOT8r, X86::AL,
12141 X86::GR8RegisterClass, true);
12142 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012143 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012144 case X86::ATOMAND64:
12145 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012146 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012147 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012148 X86::NOT64r, X86::RAX,
12149 X86::GR64RegisterClass);
12150 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012151 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12152 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012153 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012154 X86::NOT64r, X86::RAX,
12155 X86::GR64RegisterClass);
12156 case X86::ATOMXOR64:
12157 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012158 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012159 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012160 X86::NOT64r, X86::RAX,
12161 X86::GR64RegisterClass);
12162 case X86::ATOMNAND64:
12163 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12164 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012165 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012166 X86::NOT64r, X86::RAX,
12167 X86::GR64RegisterClass, true);
12168 case X86::ATOMMIN64:
12169 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12170 case X86::ATOMMAX64:
12171 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12172 case X86::ATOMUMIN64:
12173 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12174 case X86::ATOMUMAX64:
12175 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012176
12177 // This group does 64-bit operations on a 32-bit host.
12178 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012179 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012180 X86::AND32rr, X86::AND32rr,
12181 X86::AND32ri, X86::AND32ri,
12182 false);
12183 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012184 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012185 X86::OR32rr, X86::OR32rr,
12186 X86::OR32ri, X86::OR32ri,
12187 false);
12188 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012189 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012190 X86::XOR32rr, X86::XOR32rr,
12191 X86::XOR32ri, X86::XOR32ri,
12192 false);
12193 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012194 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012195 X86::AND32rr, X86::AND32rr,
12196 X86::AND32ri, X86::AND32ri,
12197 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012198 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012199 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012200 X86::ADD32rr, X86::ADC32rr,
12201 X86::ADD32ri, X86::ADC32ri,
12202 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012203 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012204 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012205 X86::SUB32rr, X86::SBB32rr,
12206 X86::SUB32ri, X86::SBB32ri,
12207 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012208 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012209 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012210 X86::MOV32rr, X86::MOV32rr,
12211 X86::MOV32ri, X86::MOV32ri,
12212 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012213 case X86::VASTART_SAVE_XMM_REGS:
12214 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012215
12216 case X86::VAARG_64:
12217 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012218 }
12219}
12220
12221//===----------------------------------------------------------------------===//
12222// X86 Optimization Hooks
12223//===----------------------------------------------------------------------===//
12224
Dan Gohman475871a2008-07-27 21:46:04 +000012225void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012226 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012227 APInt &KnownZero,
12228 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012229 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012230 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012231 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012232 assert((Opc >= ISD::BUILTIN_OP_END ||
12233 Opc == ISD::INTRINSIC_WO_CHAIN ||
12234 Opc == ISD::INTRINSIC_W_CHAIN ||
12235 Opc == ISD::INTRINSIC_VOID) &&
12236 "Should use MaskedValueIsZero if you don't know whether Op"
12237 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012238
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012239 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012240 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012241 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012242 case X86ISD::ADD:
12243 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012244 case X86ISD::ADC:
12245 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012246 case X86ISD::SMUL:
12247 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012248 case X86ISD::INC:
12249 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012250 case X86ISD::OR:
12251 case X86ISD::XOR:
12252 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012253 // These nodes' second result is a boolean.
12254 if (Op.getResNo() == 0)
12255 break;
12256 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012257 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012258 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12259 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012260 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012261 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012262}
Chris Lattner259e97c2006-01-31 19:43:35 +000012263
Owen Andersonbc146b02010-09-21 20:42:50 +000012264unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12265 unsigned Depth) const {
12266 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12267 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12268 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012269
Owen Andersonbc146b02010-09-21 20:42:50 +000012270 // Fallback case.
12271 return 1;
12272}
12273
Evan Cheng206ee9d2006-07-07 08:33:52 +000012274/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012275/// node is a GlobalAddress + offset.
12276bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012277 const GlobalValue* &GA,
12278 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012279 if (N->getOpcode() == X86ISD::Wrapper) {
12280 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012281 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012282 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012283 return true;
12284 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012285 }
Evan Chengad4196b2008-05-12 19:56:52 +000012286 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012287}
12288
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012289/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12290/// same as extracting the high 128-bit part of 256-bit vector and then
12291/// inserting the result into the low part of a new 256-bit vector
12292static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12293 EVT VT = SVOp->getValueType(0);
12294 int NumElems = VT.getVectorNumElements();
12295
12296 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12297 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12298 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12299 SVOp->getMaskElt(j) >= 0)
12300 return false;
12301
12302 return true;
12303}
12304
12305/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12306/// same as extracting the low 128-bit part of 256-bit vector and then
12307/// inserting the result into the high part of a new 256-bit vector
12308static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12309 EVT VT = SVOp->getValueType(0);
12310 int NumElems = VT.getVectorNumElements();
12311
12312 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12313 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12314 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12315 SVOp->getMaskElt(j) >= 0)
12316 return false;
12317
12318 return true;
12319}
12320
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012321/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12322static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12323 TargetLowering::DAGCombinerInfo &DCI) {
12324 DebugLoc dl = N->getDebugLoc();
12325 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12326 SDValue V1 = SVOp->getOperand(0);
12327 SDValue V2 = SVOp->getOperand(1);
12328 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012329 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012330
12331 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12332 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12333 //
12334 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012335 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012336 // V UNDEF BUILD_VECTOR UNDEF
12337 // \ / \ /
12338 // CONCAT_VECTOR CONCAT_VECTOR
12339 // \ /
12340 // \ /
12341 // RESULT: V + zero extended
12342 //
12343 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12344 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12345 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12346 return SDValue();
12347
12348 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12349 return SDValue();
12350
12351 // To match the shuffle mask, the first half of the mask should
12352 // be exactly the first vector, and all the rest a splat with the
12353 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012354 for (int i = 0; i < NumElems/2; ++i)
12355 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12356 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12357 return SDValue();
12358
12359 // Emit a zeroed vector and insert the desired subvector on its
12360 // first half.
12361 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, DAG, dl);
12362 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12363 DAG.getConstant(0, MVT::i32), DAG, dl);
12364 return DCI.CombineTo(N, InsV);
12365 }
12366
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012367 //===--------------------------------------------------------------------===//
12368 // Combine some shuffles into subvector extracts and inserts:
12369 //
12370
12371 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12372 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12373 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12374 DAG, dl);
12375 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12376 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12377 return DCI.CombineTo(N, InsV);
12378 }
12379
12380 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12381 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12382 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12383 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12384 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12385 return DCI.CombineTo(N, InsV);
12386 }
12387
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012388 return SDValue();
12389}
12390
12391/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012392static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012393 TargetLowering::DAGCombinerInfo &DCI,
12394 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012395 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012396 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012397
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012398 // Don't create instructions with illegal types after legalize types has run.
12399 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12400 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12401 return SDValue();
12402
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012403 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12404 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12405 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012406 return PerformShuffleCombine256(N, DAG, DCI);
12407
12408 // Only handle 128 wide vector from here on.
12409 if (VT.getSizeInBits() != 128)
12410 return SDValue();
12411
12412 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12413 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12414 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012415 SmallVector<SDValue, 16> Elts;
12416 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012417 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012418
Nate Begemanfdea31a2010-03-24 20:49:50 +000012419 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012420}
Evan Chengd880b972008-05-09 21:53:03 +000012421
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012422/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12423/// generation and convert it from being a bunch of shuffles and extracts
12424/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012425static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12426 const TargetLowering &TLI) {
12427 SDValue InputVector = N->getOperand(0);
12428
12429 // Only operate on vectors of 4 elements, where the alternative shuffling
12430 // gets to be more expensive.
12431 if (InputVector.getValueType() != MVT::v4i32)
12432 return SDValue();
12433
12434 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12435 // single use which is a sign-extend or zero-extend, and all elements are
12436 // used.
12437 SmallVector<SDNode *, 4> Uses;
12438 unsigned ExtractedElements = 0;
12439 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12440 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12441 if (UI.getUse().getResNo() != InputVector.getResNo())
12442 return SDValue();
12443
12444 SDNode *Extract = *UI;
12445 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12446 return SDValue();
12447
12448 if (Extract->getValueType(0) != MVT::i32)
12449 return SDValue();
12450 if (!Extract->hasOneUse())
12451 return SDValue();
12452 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12453 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12454 return SDValue();
12455 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12456 return SDValue();
12457
12458 // Record which element was extracted.
12459 ExtractedElements |=
12460 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12461
12462 Uses.push_back(Extract);
12463 }
12464
12465 // If not all the elements were used, this may not be worthwhile.
12466 if (ExtractedElements != 15)
12467 return SDValue();
12468
12469 // Ok, we've now decided to do the transformation.
12470 DebugLoc dl = InputVector.getDebugLoc();
12471
12472 // Store the value to a temporary stack slot.
12473 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012474 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12475 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012476
12477 // Replace each use (extract) with a load of the appropriate element.
12478 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12479 UE = Uses.end(); UI != UE; ++UI) {
12480 SDNode *Extract = *UI;
12481
Nadav Rotem86694292011-05-17 08:31:57 +000012482 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012483 SDValue Idx = Extract->getOperand(1);
12484 unsigned EltSize =
12485 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12486 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12487 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12488
Nadav Rotem86694292011-05-17 08:31:57 +000012489 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012490 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012491
12492 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012493 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012494 ScalarAddr, MachinePointerInfo(),
12495 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012496
12497 // Replace the exact with the load.
12498 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12499 }
12500
12501 // The replacement was made in place; don't return anything.
12502 return SDValue();
12503}
12504
Chris Lattner83e6c992006-10-04 06:57:07 +000012505/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012506static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012507 const X86Subtarget *Subtarget) {
12508 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012509 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012510 // Get the LHS/RHS of the select.
12511 SDValue LHS = N->getOperand(1);
12512 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000012513
Dan Gohman670e5392009-09-21 18:03:22 +000012514 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012515 // instructions match the semantics of the common C idiom x<y?x:y but not
12516 // x<=y?x:y, because of how they handle negative zero (which can be
12517 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000012518 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000012519 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000012520 Cond.getOpcode() == ISD::SETCC) {
12521 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012522
Chris Lattner47b4ce82009-03-11 05:48:52 +000012523 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012524 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012525 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12526 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012527 switch (CC) {
12528 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012529 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012530 // Converting this to a min would handle NaNs incorrectly, and swapping
12531 // the operands would cause it to handle comparisons between positive
12532 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012533 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012534 if (!UnsafeFPMath &&
12535 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12536 break;
12537 std::swap(LHS, RHS);
12538 }
Dan Gohman670e5392009-09-21 18:03:22 +000012539 Opcode = X86ISD::FMIN;
12540 break;
12541 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012542 // Converting this to a min would handle comparisons between positive
12543 // and negative zero incorrectly.
12544 if (!UnsafeFPMath &&
12545 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12546 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012547 Opcode = X86ISD::FMIN;
12548 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012549 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012550 // Converting this to a min would handle both negative zeros and NaNs
12551 // incorrectly, but we can swap the operands to fix both.
12552 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012553 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012554 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012555 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012556 Opcode = X86ISD::FMIN;
12557 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012558
Dan Gohman670e5392009-09-21 18:03:22 +000012559 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012560 // Converting this to a max would handle comparisons between positive
12561 // and negative zero incorrectly.
12562 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012563 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012564 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012565 Opcode = X86ISD::FMAX;
12566 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012567 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012568 // Converting this to a max would handle NaNs incorrectly, and swapping
12569 // the operands would cause it to handle comparisons between positive
12570 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012571 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012572 if (!UnsafeFPMath &&
12573 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12574 break;
12575 std::swap(LHS, RHS);
12576 }
Dan Gohman670e5392009-09-21 18:03:22 +000012577 Opcode = X86ISD::FMAX;
12578 break;
12579 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012580 // Converting this to a max would handle both negative zeros and NaNs
12581 // incorrectly, but we can swap the operands to fix both.
12582 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012583 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012584 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012585 case ISD::SETGE:
12586 Opcode = X86ISD::FMAX;
12587 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012588 }
Dan Gohman670e5392009-09-21 18:03:22 +000012589 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012590 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12591 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012592 switch (CC) {
12593 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012594 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012595 // Converting this to a min would handle comparisons between positive
12596 // and negative zero incorrectly, and swapping the operands would
12597 // cause it to handle NaNs incorrectly.
12598 if (!UnsafeFPMath &&
12599 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012600 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012601 break;
12602 std::swap(LHS, RHS);
12603 }
Dan Gohman670e5392009-09-21 18:03:22 +000012604 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012605 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012606 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012607 // Converting this to a min would handle NaNs incorrectly.
12608 if (!UnsafeFPMath &&
12609 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12610 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012611 Opcode = X86ISD::FMIN;
12612 break;
12613 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012614 // Converting this to a min would handle both negative zeros and NaNs
12615 // incorrectly, but we can swap the operands to fix both.
12616 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012617 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012618 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012619 case ISD::SETGE:
12620 Opcode = X86ISD::FMIN;
12621 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012622
Dan Gohman670e5392009-09-21 18:03:22 +000012623 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012624 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012625 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012626 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012627 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012628 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012629 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012630 // Converting this to a max would handle comparisons between positive
12631 // and negative zero incorrectly, and swapping the operands would
12632 // cause it to handle NaNs incorrectly.
12633 if (!UnsafeFPMath &&
12634 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012635 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012636 break;
12637 std::swap(LHS, RHS);
12638 }
Dan Gohman670e5392009-09-21 18:03:22 +000012639 Opcode = X86ISD::FMAX;
12640 break;
12641 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012642 // Converting this to a max would handle both negative zeros and NaNs
12643 // incorrectly, but we can swap the operands to fix both.
12644 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012645 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012646 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012647 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012648 Opcode = X86ISD::FMAX;
12649 break;
12650 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012651 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012652
Chris Lattner47b4ce82009-03-11 05:48:52 +000012653 if (Opcode)
12654 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012655 }
Eric Christopherfd179292009-08-27 18:07:15 +000012656
Chris Lattnerd1980a52009-03-12 06:52:53 +000012657 // If this is a select between two integer constants, try to do some
12658 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012659 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12660 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012661 // Don't do this for crazy integer types.
12662 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12663 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012664 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012665 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012666
Chris Lattnercee56e72009-03-13 05:53:31 +000012667 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012668 // Efficiently invertible.
12669 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12670 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12671 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12672 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012673 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012674 }
Eric Christopherfd179292009-08-27 18:07:15 +000012675
Chris Lattnerd1980a52009-03-12 06:52:53 +000012676 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012677 if (FalseC->getAPIntValue() == 0 &&
12678 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012679 if (NeedsCondInvert) // Invert the condition if needed.
12680 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12681 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012682
Chris Lattnerd1980a52009-03-12 06:52:53 +000012683 // Zero extend the condition if needed.
12684 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012685
Chris Lattnercee56e72009-03-13 05:53:31 +000012686 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012687 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012688 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012689 }
Eric Christopherfd179292009-08-27 18:07:15 +000012690
Chris Lattner97a29a52009-03-13 05:22:11 +000012691 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012692 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012693 if (NeedsCondInvert) // Invert the condition if needed.
12694 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12695 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012696
Chris Lattner97a29a52009-03-13 05:22:11 +000012697 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012698 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12699 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012700 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012701 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012702 }
Eric Christopherfd179292009-08-27 18:07:15 +000012703
Chris Lattnercee56e72009-03-13 05:53:31 +000012704 // Optimize cases that will turn into an LEA instruction. This requires
12705 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012706 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012707 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012708 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012709
Chris Lattnercee56e72009-03-13 05:53:31 +000012710 bool isFastMultiplier = false;
12711 if (Diff < 10) {
12712 switch ((unsigned char)Diff) {
12713 default: break;
12714 case 1: // result = add base, cond
12715 case 2: // result = lea base( , cond*2)
12716 case 3: // result = lea base(cond, cond*2)
12717 case 4: // result = lea base( , cond*4)
12718 case 5: // result = lea base(cond, cond*4)
12719 case 8: // result = lea base( , cond*8)
12720 case 9: // result = lea base(cond, cond*8)
12721 isFastMultiplier = true;
12722 break;
12723 }
12724 }
Eric Christopherfd179292009-08-27 18:07:15 +000012725
Chris Lattnercee56e72009-03-13 05:53:31 +000012726 if (isFastMultiplier) {
12727 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12728 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 Lattnercee56e72009-03-13 05:53:31 +000012732 // Zero extend the condition if needed.
12733 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12734 Cond);
12735 // Scale the condition by the difference.
12736 if (Diff != 1)
12737 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12738 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012739
Chris Lattnercee56e72009-03-13 05:53:31 +000012740 // Add the base if non-zero.
12741 if (FalseC->getAPIntValue() != 0)
12742 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12743 SDValue(FalseC, 0));
12744 return Cond;
12745 }
Eric Christopherfd179292009-08-27 18:07:15 +000012746 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012747 }
12748 }
Eric Christopherfd179292009-08-27 18:07:15 +000012749
Dan Gohman475871a2008-07-27 21:46:04 +000012750 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012751}
12752
Chris Lattnerd1980a52009-03-12 06:52:53 +000012753/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12754static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12755 TargetLowering::DAGCombinerInfo &DCI) {
12756 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012757
Chris Lattnerd1980a52009-03-12 06:52:53 +000012758 // If the flag operand isn't dead, don't touch this CMOV.
12759 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12760 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012761
Evan Chengb5a55d92011-05-24 01:48:22 +000012762 SDValue FalseOp = N->getOperand(0);
12763 SDValue TrueOp = N->getOperand(1);
12764 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12765 SDValue Cond = N->getOperand(3);
12766 if (CC == X86::COND_E || CC == X86::COND_NE) {
12767 switch (Cond.getOpcode()) {
12768 default: break;
12769 case X86ISD::BSR:
12770 case X86ISD::BSF:
12771 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12772 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12773 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12774 }
12775 }
12776
Chris Lattnerd1980a52009-03-12 06:52:53 +000012777 // If this is a select between two integer constants, try to do some
12778 // optimizations. Note that the operands are ordered the opposite of SELECT
12779 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012780 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12781 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012782 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12783 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012784 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12785 CC = X86::GetOppositeBranchCondition(CC);
12786 std::swap(TrueC, FalseC);
12787 }
Eric Christopherfd179292009-08-27 18:07:15 +000012788
Chris Lattnerd1980a52009-03-12 06:52:53 +000012789 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012790 // This is efficient for any integer data type (including i8/i16) and
12791 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012792 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012793 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12794 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012795
Chris Lattnerd1980a52009-03-12 06:52:53 +000012796 // Zero extend the condition if needed.
12797 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012798
Chris Lattnerd1980a52009-03-12 06:52:53 +000012799 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12800 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012801 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012802 if (N->getNumValues() == 2) // Dead flag value?
12803 return DCI.CombineTo(N, Cond, SDValue());
12804 return Cond;
12805 }
Eric Christopherfd179292009-08-27 18:07:15 +000012806
Chris Lattnercee56e72009-03-13 05:53:31 +000012807 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12808 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012809 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012810 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12811 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012812
Chris Lattner97a29a52009-03-13 05:22:11 +000012813 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012814 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12815 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012816 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12817 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012818
Chris Lattner97a29a52009-03-13 05:22:11 +000012819 if (N->getNumValues() == 2) // Dead flag value?
12820 return DCI.CombineTo(N, Cond, SDValue());
12821 return Cond;
12822 }
Eric Christopherfd179292009-08-27 18:07:15 +000012823
Chris Lattnercee56e72009-03-13 05:53:31 +000012824 // Optimize cases that will turn into an LEA instruction. This requires
12825 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012826 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012827 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012828 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012829
Chris Lattnercee56e72009-03-13 05:53:31 +000012830 bool isFastMultiplier = false;
12831 if (Diff < 10) {
12832 switch ((unsigned char)Diff) {
12833 default: break;
12834 case 1: // result = add base, cond
12835 case 2: // result = lea base( , cond*2)
12836 case 3: // result = lea base(cond, cond*2)
12837 case 4: // result = lea base( , cond*4)
12838 case 5: // result = lea base(cond, cond*4)
12839 case 8: // result = lea base( , cond*8)
12840 case 9: // result = lea base(cond, cond*8)
12841 isFastMultiplier = true;
12842 break;
12843 }
12844 }
Eric Christopherfd179292009-08-27 18:07:15 +000012845
Chris Lattnercee56e72009-03-13 05:53:31 +000012846 if (isFastMultiplier) {
12847 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012848 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12849 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012850 // Zero extend the condition if needed.
12851 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12852 Cond);
12853 // Scale the condition by the difference.
12854 if (Diff != 1)
12855 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12856 DAG.getConstant(Diff, Cond.getValueType()));
12857
12858 // Add the base if non-zero.
12859 if (FalseC->getAPIntValue() != 0)
12860 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12861 SDValue(FalseC, 0));
12862 if (N->getNumValues() == 2) // Dead flag value?
12863 return DCI.CombineTo(N, Cond, SDValue());
12864 return Cond;
12865 }
Eric Christopherfd179292009-08-27 18:07:15 +000012866 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012867 }
12868 }
12869 return SDValue();
12870}
12871
12872
Evan Cheng0b0cd912009-03-28 05:57:29 +000012873/// PerformMulCombine - Optimize a single multiply with constant into two
12874/// in order to implement it with two cheaper instructions, e.g.
12875/// LEA + SHL, LEA + LEA.
12876static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12877 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012878 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12879 return SDValue();
12880
Owen Andersone50ed302009-08-10 22:56:29 +000012881 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012882 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012883 return SDValue();
12884
12885 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12886 if (!C)
12887 return SDValue();
12888 uint64_t MulAmt = C->getZExtValue();
12889 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12890 return SDValue();
12891
12892 uint64_t MulAmt1 = 0;
12893 uint64_t MulAmt2 = 0;
12894 if ((MulAmt % 9) == 0) {
12895 MulAmt1 = 9;
12896 MulAmt2 = MulAmt / 9;
12897 } else if ((MulAmt % 5) == 0) {
12898 MulAmt1 = 5;
12899 MulAmt2 = MulAmt / 5;
12900 } else if ((MulAmt % 3) == 0) {
12901 MulAmt1 = 3;
12902 MulAmt2 = MulAmt / 3;
12903 }
12904 if (MulAmt2 &&
12905 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12906 DebugLoc DL = N->getDebugLoc();
12907
12908 if (isPowerOf2_64(MulAmt2) &&
12909 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12910 // If second multiplifer is pow2, issue it first. We want the multiply by
12911 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12912 // is an add.
12913 std::swap(MulAmt1, MulAmt2);
12914
12915 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012916 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012917 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012918 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012919 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012920 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012921 DAG.getConstant(MulAmt1, VT));
12922
Eric Christopherfd179292009-08-27 18:07:15 +000012923 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012924 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000012925 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000012926 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012927 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000012928 DAG.getConstant(MulAmt2, VT));
12929
12930 // Do not add new nodes to DAG combiner worklist.
12931 DCI.CombineTo(N, NewMul, false);
12932 }
12933 return SDValue();
12934}
12935
Evan Chengad9c0a32009-12-15 00:53:42 +000012936static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
12937 SDValue N0 = N->getOperand(0);
12938 SDValue N1 = N->getOperand(1);
12939 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
12940 EVT VT = N0.getValueType();
12941
12942 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
12943 // since the result of setcc_c is all zero's or all ones.
12944 if (N1C && N0.getOpcode() == ISD::AND &&
12945 N0.getOperand(1).getOpcode() == ISD::Constant) {
12946 SDValue N00 = N0.getOperand(0);
12947 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
12948 ((N00.getOpcode() == ISD::ANY_EXTEND ||
12949 N00.getOpcode() == ISD::ZERO_EXTEND) &&
12950 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
12951 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
12952 APInt ShAmt = N1C->getAPIntValue();
12953 Mask = Mask.shl(ShAmt);
12954 if (Mask != 0)
12955 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
12956 N00, DAG.getConstant(Mask, VT));
12957 }
12958 }
12959
12960 return SDValue();
12961}
Evan Cheng0b0cd912009-03-28 05:57:29 +000012962
Nate Begeman740ab032009-01-26 00:52:55 +000012963/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
12964/// when possible.
12965static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
12966 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000012967 EVT VT = N->getValueType(0);
12968 if (!VT.isVector() && VT.isInteger() &&
12969 N->getOpcode() == ISD::SHL)
12970 return PerformSHLCombine(N, DAG);
12971
Nate Begeman740ab032009-01-26 00:52:55 +000012972 // On X86 with SSE2 support, we can transform this to a vector shift if
12973 // all elements are shifted by the same amount. We can't do this in legalize
12974 // because the a constant vector is typically transformed to a constant pool
12975 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000012976 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012977 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000012978
Owen Anderson825b72b2009-08-11 20:47:22 +000012979 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012980 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000012981
Mon P Wang3becd092009-01-28 08:12:05 +000012982 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000012983 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000012984 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000012985 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000012986 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
12987 unsigned NumElts = VT.getVectorNumElements();
12988 unsigned i = 0;
12989 for (; i != NumElts; ++i) {
12990 SDValue Arg = ShAmtOp.getOperand(i);
12991 if (Arg.getOpcode() == ISD::UNDEF) continue;
12992 BaseShAmt = Arg;
12993 break;
12994 }
12995 for (; i != NumElts; ++i) {
12996 SDValue Arg = ShAmtOp.getOperand(i);
12997 if (Arg.getOpcode() == ISD::UNDEF) continue;
12998 if (Arg != BaseShAmt) {
12999 return SDValue();
13000 }
13001 }
13002 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013003 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013004 SDValue InVec = ShAmtOp.getOperand(0);
13005 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13006 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13007 unsigned i = 0;
13008 for (; i != NumElts; ++i) {
13009 SDValue Arg = InVec.getOperand(i);
13010 if (Arg.getOpcode() == ISD::UNDEF) continue;
13011 BaseShAmt = Arg;
13012 break;
13013 }
13014 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13015 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013016 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013017 if (C->getZExtValue() == SplatIdx)
13018 BaseShAmt = InVec.getOperand(1);
13019 }
13020 }
13021 if (BaseShAmt.getNode() == 0)
13022 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13023 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013024 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013025 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013026
Mon P Wangefa42202009-09-03 19:56:25 +000013027 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013028 if (EltVT.bitsGT(MVT::i32))
13029 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13030 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013031 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013032
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013033 // The shift amount is identical so we can do a vector shift.
13034 SDValue ValOp = N->getOperand(0);
13035 switch (N->getOpcode()) {
13036 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013037 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013038 break;
13039 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013040 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013041 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013042 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013043 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013044 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013045 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013046 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013047 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013048 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013049 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013050 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013051 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013052 break;
13053 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013054 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013055 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013056 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013057 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013058 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013059 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013060 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013061 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013062 break;
13063 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013064 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013065 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013066 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013067 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013068 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013069 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013070 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013071 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013072 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013073 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013074 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013075 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013076 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013077 }
13078 return SDValue();
13079}
13080
Nate Begemanb65c1752010-12-17 22:55:37 +000013081
Stuart Hastings865f0932011-06-03 23:53:54 +000013082// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13083// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13084// and friends. Likewise for OR -> CMPNEQSS.
13085static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13086 TargetLowering::DAGCombinerInfo &DCI,
13087 const X86Subtarget *Subtarget) {
13088 unsigned opcode;
13089
13090 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13091 // we're requiring SSE2 for both.
13092 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
13093 SDValue N0 = N->getOperand(0);
13094 SDValue N1 = N->getOperand(1);
13095 SDValue CMP0 = N0->getOperand(1);
13096 SDValue CMP1 = N1->getOperand(1);
13097 DebugLoc DL = N->getDebugLoc();
13098
13099 // The SETCCs should both refer to the same CMP.
13100 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13101 return SDValue();
13102
13103 SDValue CMP00 = CMP0->getOperand(0);
13104 SDValue CMP01 = CMP0->getOperand(1);
13105 EVT VT = CMP00.getValueType();
13106
13107 if (VT == MVT::f32 || VT == MVT::f64) {
13108 bool ExpectingFlags = false;
13109 // Check for any users that want flags:
13110 for (SDNode::use_iterator UI = N->use_begin(),
13111 UE = N->use_end();
13112 !ExpectingFlags && UI != UE; ++UI)
13113 switch (UI->getOpcode()) {
13114 default:
13115 case ISD::BR_CC:
13116 case ISD::BRCOND:
13117 case ISD::SELECT:
13118 ExpectingFlags = true;
13119 break;
13120 case ISD::CopyToReg:
13121 case ISD::SIGN_EXTEND:
13122 case ISD::ZERO_EXTEND:
13123 case ISD::ANY_EXTEND:
13124 break;
13125 }
13126
13127 if (!ExpectingFlags) {
13128 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13129 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13130
13131 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13132 X86::CondCode tmp = cc0;
13133 cc0 = cc1;
13134 cc1 = tmp;
13135 }
13136
13137 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13138 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13139 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13140 X86ISD::NodeType NTOperator = is64BitFP ?
13141 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13142 // FIXME: need symbolic constants for these magic numbers.
13143 // See X86ATTInstPrinter.cpp:printSSECC().
13144 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13145 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13146 DAG.getConstant(x86cc, MVT::i8));
13147 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13148 OnesOrZeroesF);
13149 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13150 DAG.getConstant(1, MVT::i32));
13151 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13152 return OneBitOfTruth;
13153 }
13154 }
13155 }
13156 }
13157 return SDValue();
13158}
13159
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013160/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13161/// so it can be folded inside ANDNP.
13162static bool CanFoldXORWithAllOnes(const SDNode *N) {
13163 EVT VT = N->getValueType(0);
13164
13165 // Match direct AllOnes for 128 and 256-bit vectors
13166 if (ISD::isBuildVectorAllOnes(N))
13167 return true;
13168
13169 // Look through a bit convert.
13170 if (N->getOpcode() == ISD::BITCAST)
13171 N = N->getOperand(0).getNode();
13172
13173 // Sometimes the operand may come from a insert_subvector building a 256-bit
13174 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013175 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013176 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13177 SDValue V1 = N->getOperand(0);
13178 SDValue V2 = N->getOperand(1);
13179
13180 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13181 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13182 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13183 ISD::isBuildVectorAllOnes(V2.getNode()))
13184 return true;
13185 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013186
13187 return false;
13188}
13189
Nate Begemanb65c1752010-12-17 22:55:37 +000013190static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13191 TargetLowering::DAGCombinerInfo &DCI,
13192 const X86Subtarget *Subtarget) {
13193 if (DCI.isBeforeLegalizeOps())
13194 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013195
Stuart Hastings865f0932011-06-03 23:53:54 +000013196 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13197 if (R.getNode())
13198 return R;
13199
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013200 // Want to form ANDNP nodes:
13201 // 1) In the hopes of then easily combining them with OR and AND nodes
13202 // to form PBLEND/PSIGN.
13203 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013204 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013205 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013206 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013207
Nate Begemanb65c1752010-12-17 22:55:37 +000013208 SDValue N0 = N->getOperand(0);
13209 SDValue N1 = N->getOperand(1);
13210 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013211
Nate Begemanb65c1752010-12-17 22:55:37 +000013212 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013213 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013214 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13215 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013216 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013217
13218 // Check RHS for vnot
13219 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013220 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13221 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013222 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013223
Nate Begemanb65c1752010-12-17 22:55:37 +000013224 return SDValue();
13225}
13226
Evan Cheng760d1942010-01-04 21:22:48 +000013227static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013228 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013229 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013230 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013231 return SDValue();
13232
Stuart Hastings865f0932011-06-03 23:53:54 +000013233 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13234 if (R.getNode())
13235 return R;
13236
Evan Cheng760d1942010-01-04 21:22:48 +000013237 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013238 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013239 return SDValue();
13240
Evan Cheng760d1942010-01-04 21:22:48 +000013241 SDValue N0 = N->getOperand(0);
13242 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013243
Nate Begemanb65c1752010-12-17 22:55:37 +000013244 // look for psign/blend
13245 if (Subtarget->hasSSSE3()) {
13246 if (VT == MVT::v2i64) {
13247 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013248 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013249 std::swap(N0, N1);
13250 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013251 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013252 SDValue Mask = N1.getOperand(0);
13253 SDValue X = N1.getOperand(1);
13254 SDValue Y;
13255 if (N0.getOperand(0) == Mask)
13256 Y = N0.getOperand(1);
13257 if (N0.getOperand(1) == Mask)
13258 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013259
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013260 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013261 if (!Y.getNode())
13262 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013263
Nate Begemanb65c1752010-12-17 22:55:37 +000013264 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13265 if (Mask.getOpcode() != ISD::BITCAST ||
13266 X.getOpcode() != ISD::BITCAST ||
13267 Y.getOpcode() != ISD::BITCAST)
13268 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013269
Nate Begemanb65c1752010-12-17 22:55:37 +000013270 // Look through mask bitcast.
13271 Mask = Mask.getOperand(0);
13272 EVT MaskVT = Mask.getValueType();
13273
13274 // Validate that the Mask operand is a vector sra node. The sra node
13275 // will be an intrinsic.
13276 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13277 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013278
Nate Begemanb65c1752010-12-17 22:55:37 +000013279 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13280 // there is no psrai.b
13281 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13282 case Intrinsic::x86_sse2_psrai_w:
13283 case Intrinsic::x86_sse2_psrai_d:
13284 break;
13285 default: return SDValue();
13286 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013287
Nate Begemanb65c1752010-12-17 22:55:37 +000013288 // Check that the SRA is all signbits.
13289 SDValue SraC = Mask.getOperand(2);
13290 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13291 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13292 if ((SraAmt + 1) != EltBits)
13293 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013294
Nate Begemanb65c1752010-12-17 22:55:37 +000013295 DebugLoc DL = N->getDebugLoc();
13296
13297 // Now we know we at least have a plendvb with the mask val. See if
13298 // we can form a psignb/w/d.
13299 // psign = x.type == y.type == mask.type && y = sub(0, x);
13300 X = X.getOperand(0);
13301 Y = Y.getOperand(0);
13302 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13303 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13304 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13305 unsigned Opc = 0;
13306 switch (EltBits) {
13307 case 8: Opc = X86ISD::PSIGNB; break;
13308 case 16: Opc = X86ISD::PSIGNW; break;
13309 case 32: Opc = X86ISD::PSIGND; break;
13310 default: break;
13311 }
13312 if (Opc) {
13313 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13314 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13315 }
13316 }
13317 // PBLENDVB only available on SSE 4.1
13318 if (!Subtarget->hasSSE41())
13319 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013320
Nate Begemanb65c1752010-12-17 22:55:37 +000013321 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13322 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13323 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000013324 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013325 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13326 }
13327 }
13328 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013329
Nate Begemanb65c1752010-12-17 22:55:37 +000013330 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013331 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13332 std::swap(N0, N1);
13333 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13334 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013335 if (!N0.hasOneUse() || !N1.hasOneUse())
13336 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013337
13338 SDValue ShAmt0 = N0.getOperand(1);
13339 if (ShAmt0.getValueType() != MVT::i8)
13340 return SDValue();
13341 SDValue ShAmt1 = N1.getOperand(1);
13342 if (ShAmt1.getValueType() != MVT::i8)
13343 return SDValue();
13344 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13345 ShAmt0 = ShAmt0.getOperand(0);
13346 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13347 ShAmt1 = ShAmt1.getOperand(0);
13348
13349 DebugLoc DL = N->getDebugLoc();
13350 unsigned Opc = X86ISD::SHLD;
13351 SDValue Op0 = N0.getOperand(0);
13352 SDValue Op1 = N1.getOperand(0);
13353 if (ShAmt0.getOpcode() == ISD::SUB) {
13354 Opc = X86ISD::SHRD;
13355 std::swap(Op0, Op1);
13356 std::swap(ShAmt0, ShAmt1);
13357 }
13358
Evan Cheng8b1190a2010-04-28 01:18:01 +000013359 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013360 if (ShAmt1.getOpcode() == ISD::SUB) {
13361 SDValue Sum = ShAmt1.getOperand(0);
13362 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013363 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13364 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13365 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13366 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013367 return DAG.getNode(Opc, DL, VT,
13368 Op0, Op1,
13369 DAG.getNode(ISD::TRUNCATE, DL,
13370 MVT::i8, ShAmt0));
13371 }
13372 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13373 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13374 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013375 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013376 return DAG.getNode(Opc, DL, VT,
13377 N0.getOperand(0), N1.getOperand(0),
13378 DAG.getNode(ISD::TRUNCATE, DL,
13379 MVT::i8, ShAmt0));
13380 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013381
Evan Cheng760d1942010-01-04 21:22:48 +000013382 return SDValue();
13383}
13384
Chris Lattner149a4e52008-02-22 02:09:43 +000013385/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013386static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013387 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013388 StoreSDNode *St = cast<StoreSDNode>(N);
13389 EVT VT = St->getValue().getValueType();
13390 EVT StVT = St->getMemoryVT();
13391 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013392 SDValue StoredVal = St->getOperand(1);
13393 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13394
13395 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013396 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13397 // 128-bit ones. If in the future the cost becomes only one memory access the
13398 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013399 if (VT.getSizeInBits() == 256 &&
13400 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13401 StoredVal.getNumOperands() == 2) {
13402
13403 SDValue Value0 = StoredVal.getOperand(0);
13404 SDValue Value1 = StoredVal.getOperand(1);
13405
13406 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13407 SDValue Ptr0 = St->getBasePtr();
13408 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13409
13410 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13411 St->getPointerInfo(), St->isVolatile(),
13412 St->isNonTemporal(), St->getAlignment());
13413 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13414 St->getPointerInfo(), St->isVolatile(),
13415 St->isNonTemporal(), St->getAlignment());
13416 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13417 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013418
13419 // Optimize trunc store (of multiple scalars) to shuffle and store.
13420 // First, pack all of the elements in one place. Next, store to memory
13421 // in fewer chunks.
13422 if (St->isTruncatingStore() && VT.isVector()) {
13423 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13424 unsigned NumElems = VT.getVectorNumElements();
13425 assert(StVT != VT && "Cannot truncate to the same type");
13426 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13427 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13428
13429 // From, To sizes and ElemCount must be pow of two
13430 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
13431 // We are going to use the original vector elt for storing.
13432 // accumulated smaller vector elements must be a multiple of bigger size.
13433 if (0 != (NumElems * ToSz) % FromSz) return SDValue();
13434 unsigned SizeRatio = FromSz / ToSz;
13435
13436 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13437
13438 // Create a type on which we perform the shuffle
13439 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13440 StVT.getScalarType(), NumElems*SizeRatio);
13441
13442 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13443
13444 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13445 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13446 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13447
13448 // Can't shuffle using an illegal type
13449 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13450
13451 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13452 DAG.getUNDEF(WideVec.getValueType()),
13453 ShuffleVec.data());
13454 // At this point all of the data is stored at the bottom of the
13455 // register. We now need to save it to mem.
13456
13457 // Find the largest store unit
13458 MVT StoreType = MVT::i8;
13459 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13460 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13461 MVT Tp = (MVT::SimpleValueType)tp;
13462 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13463 StoreType = Tp;
13464 }
13465
13466 // Bitcast the original vector into a vector of store-size units
13467 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13468 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13469 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13470 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13471 SmallVector<SDValue, 8> Chains;
13472 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13473 TLI.getPointerTy());
13474 SDValue Ptr = St->getBasePtr();
13475
13476 // Perform one or more big stores into memory.
13477 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13478 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13479 StoreType, ShuffWide,
13480 DAG.getIntPtrConstant(i));
13481 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13482 St->getPointerInfo(), St->isVolatile(),
13483 St->isNonTemporal(), St->getAlignment());
13484 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13485 Chains.push_back(Ch);
13486 }
13487
13488 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13489 Chains.size());
13490 }
13491
13492
Chris Lattner149a4e52008-02-22 02:09:43 +000013493 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13494 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013495 // A preferable solution to the general problem is to figure out the right
13496 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013497
13498 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013499 if (VT.getSizeInBits() != 64)
13500 return SDValue();
13501
Devang Patel578efa92009-06-05 21:57:13 +000013502 const Function *F = DAG.getMachineFunction().getFunction();
13503 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013504 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000013505 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000013506 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013507 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013508 isa<LoadSDNode>(St->getValue()) &&
13509 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13510 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013511 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013512 LoadSDNode *Ld = 0;
13513 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013514 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013515 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013516 // Must be a store of a load. We currently handle two cases: the load
13517 // is a direct child, and it's under an intervening TokenFactor. It is
13518 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013519 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013520 Ld = cast<LoadSDNode>(St->getChain());
13521 else if (St->getValue().hasOneUse() &&
13522 ChainVal->getOpcode() == ISD::TokenFactor) {
13523 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013524 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013525 TokenFactorIndex = i;
13526 Ld = cast<LoadSDNode>(St->getValue());
13527 } else
13528 Ops.push_back(ChainVal->getOperand(i));
13529 }
13530 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013531
Evan Cheng536e6672009-03-12 05:59:15 +000013532 if (!Ld || !ISD::isNormalLoad(Ld))
13533 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013534
Evan Cheng536e6672009-03-12 05:59:15 +000013535 // If this is not the MMX case, i.e. we are just turning i64 load/store
13536 // into f64 load/store, avoid the transformation if there are multiple
13537 // uses of the loaded value.
13538 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13539 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013540
Evan Cheng536e6672009-03-12 05:59:15 +000013541 DebugLoc LdDL = Ld->getDebugLoc();
13542 DebugLoc StDL = N->getDebugLoc();
13543 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13544 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13545 // pair instead.
13546 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013547 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013548 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13549 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013550 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013551 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013552 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013553 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013554 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013555 Ops.size());
13556 }
Evan Cheng536e6672009-03-12 05:59:15 +000013557 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013558 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013559 St->isVolatile(), St->isNonTemporal(),
13560 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013561 }
Evan Cheng536e6672009-03-12 05:59:15 +000013562
13563 // Otherwise, lower to two pairs of 32-bit loads / stores.
13564 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013565 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13566 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013567
Owen Anderson825b72b2009-08-11 20:47:22 +000013568 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013569 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013570 Ld->isVolatile(), Ld->isNonTemporal(),
13571 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013572 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013573 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013574 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013575 MinAlign(Ld->getAlignment(), 4));
13576
13577 SDValue NewChain = LoLd.getValue(1);
13578 if (TokenFactorIndex != -1) {
13579 Ops.push_back(LoLd);
13580 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013581 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013582 Ops.size());
13583 }
13584
13585 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013586 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13587 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013588
13589 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013590 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013591 St->isVolatile(), St->isNonTemporal(),
13592 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013593 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013594 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013595 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013596 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013597 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013598 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013599 }
Dan Gohman475871a2008-07-27 21:46:04 +000013600 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013601}
13602
Chris Lattner6cf73262008-01-25 06:14:17 +000013603/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13604/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013605static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013606 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13607 // F[X]OR(0.0, x) -> x
13608 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013609 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13610 if (C->getValueAPF().isPosZero())
13611 return N->getOperand(1);
13612 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13613 if (C->getValueAPF().isPosZero())
13614 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013615 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013616}
13617
13618/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013619static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013620 // FAND(0.0, x) -> 0.0
13621 // FAND(x, 0.0) -> 0.0
13622 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13623 if (C->getValueAPF().isPosZero())
13624 return N->getOperand(0);
13625 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13626 if (C->getValueAPF().isPosZero())
13627 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013628 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013629}
13630
Dan Gohmane5af2d32009-01-29 01:59:02 +000013631static SDValue PerformBTCombine(SDNode *N,
13632 SelectionDAG &DAG,
13633 TargetLowering::DAGCombinerInfo &DCI) {
13634 // BT ignores high bits in the bit index operand.
13635 SDValue Op1 = N->getOperand(1);
13636 if (Op1.hasOneUse()) {
13637 unsigned BitWidth = Op1.getValueSizeInBits();
13638 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13639 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013640 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13641 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013642 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013643 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13644 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13645 DCI.CommitTargetLoweringOpt(TLO);
13646 }
13647 return SDValue();
13648}
Chris Lattner83e6c992006-10-04 06:57:07 +000013649
Eli Friedman7a5e5552009-06-07 06:52:44 +000013650static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13651 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013652 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013653 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013654 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013655 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013656 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013657 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013658 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013659 }
13660 return SDValue();
13661}
13662
Evan Cheng2e489c42009-12-16 00:53:11 +000013663static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13664 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13665 // (and (i32 x86isd::setcc_carry), 1)
13666 // This eliminates the zext. This transformation is necessary because
13667 // ISD::SETCC is always legalized to i8.
13668 DebugLoc dl = N->getDebugLoc();
13669 SDValue N0 = N->getOperand(0);
13670 EVT VT = N->getValueType(0);
13671 if (N0.getOpcode() == ISD::AND &&
13672 N0.hasOneUse() &&
13673 N0.getOperand(0).hasOneUse()) {
13674 SDValue N00 = N0.getOperand(0);
13675 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13676 return SDValue();
13677 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13678 if (!C || C->getZExtValue() != 1)
13679 return SDValue();
13680 return DAG.getNode(ISD::AND, dl, VT,
13681 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13682 N00.getOperand(0), N00.getOperand(1)),
13683 DAG.getConstant(1, VT));
13684 }
13685
13686 return SDValue();
13687}
13688
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013689// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13690static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13691 unsigned X86CC = N->getConstantOperandVal(0);
13692 SDValue EFLAG = N->getOperand(1);
13693 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013694
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013695 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
13696 // a zext and produces an all-ones bit which is more useful than 0/1 in some
13697 // cases.
13698 if (X86CC == X86::COND_B)
13699 return DAG.getNode(ISD::AND, DL, MVT::i8,
13700 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
13701 DAG.getConstant(X86CC, MVT::i8), EFLAG),
13702 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013703
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013704 return SDValue();
13705}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013706
Benjamin Kramer1396c402011-06-18 11:09:41 +000013707static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
13708 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013709 SDValue Op0 = N->getOperand(0);
13710 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
13711 // a 32-bit target where SSE doesn't support i64->FP operations.
13712 if (Op0.getOpcode() == ISD::LOAD) {
13713 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
13714 EVT VT = Ld->getValueType(0);
13715 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
13716 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
13717 !XTLI->getSubtarget()->is64Bit() &&
13718 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000013719 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
13720 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013721 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
13722 return FILDChain;
13723 }
13724 }
13725 return SDValue();
13726}
13727
Chris Lattner23a01992010-12-20 01:37:09 +000013728// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
13729static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
13730 X86TargetLowering::DAGCombinerInfo &DCI) {
13731 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
13732 // the result is either zero or one (depending on the input carry bit).
13733 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
13734 if (X86::isZeroNode(N->getOperand(0)) &&
13735 X86::isZeroNode(N->getOperand(1)) &&
13736 // We don't have a good way to replace an EFLAGS use, so only do this when
13737 // dead right now.
13738 SDValue(N, 1).use_empty()) {
13739 DebugLoc DL = N->getDebugLoc();
13740 EVT VT = N->getValueType(0);
13741 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
13742 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
13743 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
13744 DAG.getConstant(X86::COND_B,MVT::i8),
13745 N->getOperand(2)),
13746 DAG.getConstant(1, VT));
13747 return DCI.CombineTo(N, Res1, CarryOut);
13748 }
13749
13750 return SDValue();
13751}
13752
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013753// fold (add Y, (sete X, 0)) -> adc 0, Y
13754// (add Y, (setne X, 0)) -> sbb -1, Y
13755// (sub (sete X, 0), Y) -> sbb 0, Y
13756// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013757static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013758 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013759
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013760 // Look through ZExts.
13761 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
13762 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
13763 return SDValue();
13764
13765 SDValue SetCC = Ext.getOperand(0);
13766 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
13767 return SDValue();
13768
13769 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
13770 if (CC != X86::COND_E && CC != X86::COND_NE)
13771 return SDValue();
13772
13773 SDValue Cmp = SetCC.getOperand(1);
13774 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000013775 !X86::isZeroNode(Cmp.getOperand(1)) ||
13776 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013777 return SDValue();
13778
13779 SDValue CmpOp0 = Cmp.getOperand(0);
13780 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
13781 DAG.getConstant(1, CmpOp0.getValueType()));
13782
13783 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
13784 if (CC == X86::COND_NE)
13785 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
13786 DL, OtherVal.getValueType(), OtherVal,
13787 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
13788 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
13789 DL, OtherVal.getValueType(), OtherVal,
13790 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
13791}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013792
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013793static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
13794 SDValue Op0 = N->getOperand(0);
13795 SDValue Op1 = N->getOperand(1);
13796
13797 // X86 can't encode an immediate LHS of a sub. See if we can push the
13798 // negation into a preceding instruction.
13799 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013800 // If the RHS of the sub is a XOR with one use and a constant, invert the
13801 // immediate. Then add one to the LHS of the sub so we can turn
13802 // X-Y -> X+~Y+1, saving one register.
13803 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
13804 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000013805 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013806 EVT VT = Op0.getValueType();
13807 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
13808 Op1.getOperand(0),
13809 DAG.getConstant(~XorC, VT));
13810 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000013811 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013812 }
13813 }
13814
13815 return OptimizeConditionalInDecrement(N, DAG);
13816}
13817
Dan Gohman475871a2008-07-27 21:46:04 +000013818SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000013819 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013820 SelectionDAG &DAG = DCI.DAG;
13821 switch (N->getOpcode()) {
13822 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013823 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013824 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000013825 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013826 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013827 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
13828 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000013829 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000013830 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000013831 case ISD::SHL:
13832 case ISD::SRA:
13833 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000013834 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000013835 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000013836 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013837 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000013838 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000013839 case X86ISD::FOR: return PerformFORCombine(N, DAG);
13840 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000013841 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013842 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000013843 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013844 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013845 case X86ISD::SHUFPS: // Handle all target specific shuffles
13846 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000013847 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013848 case X86ISD::PUNPCKHBW:
13849 case X86ISD::PUNPCKHWD:
13850 case X86ISD::PUNPCKHDQ:
13851 case X86ISD::PUNPCKHQDQ:
13852 case X86ISD::UNPCKHPS:
13853 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000013854 case X86ISD::VUNPCKHPSY:
13855 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013856 case X86ISD::PUNPCKLBW:
13857 case X86ISD::PUNPCKLWD:
13858 case X86ISD::PUNPCKLDQ:
13859 case X86ISD::PUNPCKLQDQ:
13860 case X86ISD::UNPCKLPS:
13861 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000013862 case X86ISD::VUNPCKLPSY:
13863 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013864 case X86ISD::MOVHLPS:
13865 case X86ISD::MOVLHPS:
13866 case X86ISD::PSHUFD:
13867 case X86ISD::PSHUFHW:
13868 case X86ISD::PSHUFLW:
13869 case X86ISD::MOVSS:
13870 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000013871 case X86ISD::VPERMILPS:
13872 case X86ISD::VPERMILPSY:
13873 case X86ISD::VPERMILPD:
13874 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000013875 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013876 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013877 }
13878
Dan Gohman475871a2008-07-27 21:46:04 +000013879 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013880}
13881
Evan Chenge5b51ac2010-04-17 06:13:15 +000013882/// isTypeDesirableForOp - Return true if the target has native support for
13883/// the specified value type and it is 'desirable' to use the type for the
13884/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
13885/// instruction encodings are longer and some i16 instructions are slow.
13886bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
13887 if (!isTypeLegal(VT))
13888 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013889 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000013890 return true;
13891
13892 switch (Opc) {
13893 default:
13894 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000013895 case ISD::LOAD:
13896 case ISD::SIGN_EXTEND:
13897 case ISD::ZERO_EXTEND:
13898 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013899 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013900 case ISD::SRL:
13901 case ISD::SUB:
13902 case ISD::ADD:
13903 case ISD::MUL:
13904 case ISD::AND:
13905 case ISD::OR:
13906 case ISD::XOR:
13907 return false;
13908 }
13909}
13910
13911/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000013912/// beneficial for dag combiner to promote the specified node. If true, it
13913/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000013914bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013915 EVT VT = Op.getValueType();
13916 if (VT != MVT::i16)
13917 return false;
13918
Evan Cheng4c26e932010-04-19 19:29:22 +000013919 bool Promote = false;
13920 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013921 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000013922 default: break;
13923 case ISD::LOAD: {
13924 LoadSDNode *LD = cast<LoadSDNode>(Op);
13925 // If the non-extending load has a single use and it's not live out, then it
13926 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013927 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
13928 Op.hasOneUse()*/) {
13929 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
13930 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
13931 // The only case where we'd want to promote LOAD (rather then it being
13932 // promoted as an operand is when it's only use is liveout.
13933 if (UI->getOpcode() != ISD::CopyToReg)
13934 return false;
13935 }
13936 }
Evan Cheng4c26e932010-04-19 19:29:22 +000013937 Promote = true;
13938 break;
13939 }
13940 case ISD::SIGN_EXTEND:
13941 case ISD::ZERO_EXTEND:
13942 case ISD::ANY_EXTEND:
13943 Promote = true;
13944 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013945 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013946 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000013947 SDValue N0 = Op.getOperand(0);
13948 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000013949 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000013950 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000013951 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013952 break;
13953 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000013954 case ISD::ADD:
13955 case ISD::MUL:
13956 case ISD::AND:
13957 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000013958 case ISD::XOR:
13959 Commute = true;
13960 // fallthrough
13961 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013962 SDValue N0 = Op.getOperand(0);
13963 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000013964 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013965 return false;
13966 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000013967 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013968 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000013969 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013970 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000013971 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013972 }
13973 }
13974
13975 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000013976 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013977}
13978
Evan Cheng60c07e12006-07-05 22:17:51 +000013979//===----------------------------------------------------------------------===//
13980// X86 Inline Assembly Support
13981//===----------------------------------------------------------------------===//
13982
Chris Lattnerb8105652009-07-20 17:51:36 +000013983bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
13984 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000013985
13986 std::string AsmStr = IA->getAsmString();
13987
13988 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000013989 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000013990 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000013991
13992 switch (AsmPieces.size()) {
13993 default: return false;
13994 case 1:
13995 AsmStr = AsmPieces[0];
13996 AsmPieces.clear();
13997 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
13998
Chris Lattner7a2bdde2011-04-15 05:18:47 +000013999 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014000 // we will turn this bswap into something that will be lowered to logical ops
14001 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14002 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014003 // bswap $0
14004 if (AsmPieces.size() == 2 &&
14005 (AsmPieces[0] == "bswap" ||
14006 AsmPieces[0] == "bswapq" ||
14007 AsmPieces[0] == "bswapl") &&
14008 (AsmPieces[1] == "$0" ||
14009 AsmPieces[1] == "${0:q}")) {
14010 // No need to check constraints, nothing other than the equivalent of
14011 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014012 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014013 if (!Ty || Ty->getBitWidth() % 16 != 0)
14014 return false;
14015 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014016 }
14017 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014018 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014019 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014020 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014021 AsmPieces[1] == "$$8," &&
14022 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014023 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14024 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014025 const std::string &ConstraintsStr = IA->getConstraintString();
14026 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014027 std::sort(AsmPieces.begin(), AsmPieces.end());
14028 if (AsmPieces.size() == 4 &&
14029 AsmPieces[0] == "~{cc}" &&
14030 AsmPieces[1] == "~{dirflag}" &&
14031 AsmPieces[2] == "~{flags}" &&
14032 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014033 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014034 if (!Ty || Ty->getBitWidth() % 16 != 0)
14035 return false;
14036 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014037 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014038 }
14039 break;
14040 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014041 if (CI->getType()->isIntegerTy(32) &&
14042 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14043 SmallVector<StringRef, 4> Words;
14044 SplitString(AsmPieces[0], Words, " \t,");
14045 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14046 Words[2] == "${0:w}") {
14047 Words.clear();
14048 SplitString(AsmPieces[1], Words, " \t,");
14049 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14050 Words[2] == "$0") {
14051 Words.clear();
14052 SplitString(AsmPieces[2], Words, " \t,");
14053 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14054 Words[2] == "${0:w}") {
14055 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014056 const std::string &ConstraintsStr = IA->getConstraintString();
14057 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014058 std::sort(AsmPieces.begin(), AsmPieces.end());
14059 if (AsmPieces.size() == 4 &&
14060 AsmPieces[0] == "~{cc}" &&
14061 AsmPieces[1] == "~{dirflag}" &&
14062 AsmPieces[2] == "~{flags}" &&
14063 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014064 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014065 if (!Ty || Ty->getBitWidth() % 16 != 0)
14066 return false;
14067 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014068 }
14069 }
14070 }
14071 }
14072 }
Evan Cheng55d42002011-01-08 01:24:27 +000014073
14074 if (CI->getType()->isIntegerTy(64)) {
14075 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14076 if (Constraints.size() >= 2 &&
14077 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14078 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14079 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14080 SmallVector<StringRef, 4> Words;
14081 SplitString(AsmPieces[0], Words, " \t");
14082 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014083 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014084 SplitString(AsmPieces[1], Words, " \t");
14085 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14086 Words.clear();
14087 SplitString(AsmPieces[2], Words, " \t,");
14088 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14089 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014090 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014091 if (!Ty || Ty->getBitWidth() % 16 != 0)
14092 return false;
14093 return IntrinsicLowering::LowerToByteSwap(CI);
14094 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014095 }
14096 }
14097 }
14098 }
14099 break;
14100 }
14101 return false;
14102}
14103
14104
14105
Chris Lattnerf4dff842006-07-11 02:54:03 +000014106/// getConstraintType - Given a constraint letter, return the type of
14107/// constraint it is for this target.
14108X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014109X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14110 if (Constraint.size() == 1) {
14111 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014112 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014113 case 'q':
14114 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014115 case 'f':
14116 case 't':
14117 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014118 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014119 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014120 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014121 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014122 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014123 case 'a':
14124 case 'b':
14125 case 'c':
14126 case 'd':
14127 case 'S':
14128 case 'D':
14129 case 'A':
14130 return C_Register;
14131 case 'I':
14132 case 'J':
14133 case 'K':
14134 case 'L':
14135 case 'M':
14136 case 'N':
14137 case 'G':
14138 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014139 case 'e':
14140 case 'Z':
14141 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014142 default:
14143 break;
14144 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014145 }
Chris Lattner4234f572007-03-25 02:14:49 +000014146 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014147}
14148
John Thompson44ab89e2010-10-29 17:29:13 +000014149/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014150/// This object must already have been set up with the operand type
14151/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014152TargetLowering::ConstraintWeight
14153 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014154 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014155 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014156 Value *CallOperandVal = info.CallOperandVal;
14157 // If we don't have a value, we can't do a match,
14158 // but allow it at the lowest weight.
14159 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014160 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014161 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014162 // Look at the constraint type.
14163 switch (*constraint) {
14164 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014165 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14166 case 'R':
14167 case 'q':
14168 case 'Q':
14169 case 'a':
14170 case 'b':
14171 case 'c':
14172 case 'd':
14173 case 'S':
14174 case 'D':
14175 case 'A':
14176 if (CallOperandVal->getType()->isIntegerTy())
14177 weight = CW_SpecificReg;
14178 break;
14179 case 'f':
14180 case 't':
14181 case 'u':
14182 if (type->isFloatingPointTy())
14183 weight = CW_SpecificReg;
14184 break;
14185 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014186 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014187 weight = CW_SpecificReg;
14188 break;
14189 case 'x':
14190 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014191 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014192 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014193 break;
14194 case 'I':
14195 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14196 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014197 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014198 }
14199 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014200 case 'J':
14201 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14202 if (C->getZExtValue() <= 63)
14203 weight = CW_Constant;
14204 }
14205 break;
14206 case 'K':
14207 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14208 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14209 weight = CW_Constant;
14210 }
14211 break;
14212 case 'L':
14213 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14214 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14215 weight = CW_Constant;
14216 }
14217 break;
14218 case 'M':
14219 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14220 if (C->getZExtValue() <= 3)
14221 weight = CW_Constant;
14222 }
14223 break;
14224 case 'N':
14225 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14226 if (C->getZExtValue() <= 0xff)
14227 weight = CW_Constant;
14228 }
14229 break;
14230 case 'G':
14231 case 'C':
14232 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14233 weight = CW_Constant;
14234 }
14235 break;
14236 case 'e':
14237 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14238 if ((C->getSExtValue() >= -0x80000000LL) &&
14239 (C->getSExtValue() <= 0x7fffffffLL))
14240 weight = CW_Constant;
14241 }
14242 break;
14243 case 'Z':
14244 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14245 if (C->getZExtValue() <= 0xffffffff)
14246 weight = CW_Constant;
14247 }
14248 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014249 }
14250 return weight;
14251}
14252
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014253/// LowerXConstraint - try to replace an X constraint, which matches anything,
14254/// with another that has more specific requirements based on the type of the
14255/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014256const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014257LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014258 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14259 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014260 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014261 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014262 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014263 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014264 return "x";
14265 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014266
Chris Lattner5e764232008-04-26 23:02:14 +000014267 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014268}
14269
Chris Lattner48884cd2007-08-25 00:47:38 +000014270/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14271/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014272void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014273 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014274 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014275 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014276 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014277
Eric Christopher100c8332011-06-02 23:16:42 +000014278 // Only support length 1 constraints for now.
14279 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014280
Eric Christopher100c8332011-06-02 23:16:42 +000014281 char ConstraintLetter = Constraint[0];
14282 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014283 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014284 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014285 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014286 if (C->getZExtValue() <= 31) {
14287 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014288 break;
14289 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014290 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014291 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014292 case 'J':
14293 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014294 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014295 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14296 break;
14297 }
14298 }
14299 return;
14300 case 'K':
14301 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014302 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014303 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14304 break;
14305 }
14306 }
14307 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014308 case 'N':
14309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014310 if (C->getZExtValue() <= 255) {
14311 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014312 break;
14313 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014314 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014315 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014316 case 'e': {
14317 // 32-bit signed value
14318 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014319 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14320 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014321 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014322 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014323 break;
14324 }
14325 // FIXME gcc accepts some relocatable values here too, but only in certain
14326 // memory models; it's complicated.
14327 }
14328 return;
14329 }
14330 case 'Z': {
14331 // 32-bit unsigned value
14332 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014333 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14334 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014335 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14336 break;
14337 }
14338 }
14339 // FIXME gcc accepts some relocatable values here too, but only in certain
14340 // memory models; it's complicated.
14341 return;
14342 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014343 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014344 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014345 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014346 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014347 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014348 break;
14349 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014350
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014351 // In any sort of PIC mode addresses need to be computed at runtime by
14352 // adding in a register or some sort of table lookup. These can't
14353 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014354 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014355 return;
14356
Chris Lattnerdc43a882007-05-03 16:52:29 +000014357 // If we are in non-pic codegen mode, we allow the address of a global (with
14358 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014359 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014360 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014361
Chris Lattner49921962009-05-08 18:23:14 +000014362 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14363 while (1) {
14364 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14365 Offset += GA->getOffset();
14366 break;
14367 } else if (Op.getOpcode() == ISD::ADD) {
14368 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14369 Offset += C->getZExtValue();
14370 Op = Op.getOperand(0);
14371 continue;
14372 }
14373 } else if (Op.getOpcode() == ISD::SUB) {
14374 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14375 Offset += -C->getZExtValue();
14376 Op = Op.getOperand(0);
14377 continue;
14378 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014379 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014380
Chris Lattner49921962009-05-08 18:23:14 +000014381 // Otherwise, this isn't something we can handle, reject it.
14382 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014383 }
Eric Christopherfd179292009-08-27 18:07:15 +000014384
Dan Gohman46510a72010-04-15 01:51:59 +000014385 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014386 // If we require an extra load to get this address, as in PIC mode, we
14387 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014388 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14389 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014390 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014391
Devang Patel0d881da2010-07-06 22:08:15 +000014392 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14393 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014394 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014395 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014396 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014397
Gabor Greifba36cb52008-08-28 21:40:38 +000014398 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014399 Ops.push_back(Result);
14400 return;
14401 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014402 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014403}
14404
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014405std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014406X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014407 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014408 // First, see if this is a constraint that directly corresponds to an LLVM
14409 // register class.
14410 if (Constraint.size() == 1) {
14411 // GCC Constraint Letters
14412 switch (Constraint[0]) {
14413 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014414 // TODO: Slight differences here in allocation order and leaving
14415 // RIP in the class. Do they matter any more here than they do
14416 // in the normal allocation?
14417 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14418 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014419 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014420 return std::make_pair(0U, X86::GR32RegisterClass);
14421 else if (VT == MVT::i16)
14422 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014423 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014424 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014425 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014426 return std::make_pair(0U, X86::GR64RegisterClass);
14427 break;
14428 }
14429 // 32-bit fallthrough
14430 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014431 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014432 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14433 else if (VT == MVT::i16)
14434 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014435 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014436 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14437 else if (VT == MVT::i64)
14438 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14439 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014440 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014441 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014442 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014443 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014444 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014445 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014446 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014447 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014448 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014449 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014450 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014451 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14452 if (VT == MVT::i16)
14453 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14454 if (VT == MVT::i32 || !Subtarget->is64Bit())
14455 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14456 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014457 case 'f': // FP Stack registers.
14458 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14459 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014460 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014461 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014462 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014463 return std::make_pair(0U, X86::RFP64RegisterClass);
14464 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014465 case 'y': // MMX_REGS if MMX allowed.
14466 if (!Subtarget->hasMMX()) break;
14467 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014468 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014469 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014470 // FALL THROUGH.
14471 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014472 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014473
Owen Anderson825b72b2009-08-11 20:47:22 +000014474 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014475 default: break;
14476 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014477 case MVT::f32:
14478 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014479 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014480 case MVT::f64:
14481 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014482 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014483 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014484 case MVT::v16i8:
14485 case MVT::v8i16:
14486 case MVT::v4i32:
14487 case MVT::v2i64:
14488 case MVT::v4f32:
14489 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014490 return std::make_pair(0U, X86::VR128RegisterClass);
14491 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014492 break;
14493 }
14494 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014495
Chris Lattnerf76d1802006-07-31 23:26:50 +000014496 // Use the default implementation in TargetLowering to convert the register
14497 // constraint into a member of a register class.
14498 std::pair<unsigned, const TargetRegisterClass*> Res;
14499 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014500
14501 // Not found as a standard register?
14502 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014503 // Map st(0) -> st(7) -> ST0
14504 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14505 tolower(Constraint[1]) == 's' &&
14506 tolower(Constraint[2]) == 't' &&
14507 Constraint[3] == '(' &&
14508 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14509 Constraint[5] == ')' &&
14510 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014511
Chris Lattner56d77c72009-09-13 22:41:48 +000014512 Res.first = X86::ST0+Constraint[4]-'0';
14513 Res.second = X86::RFP80RegisterClass;
14514 return Res;
14515 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014516
Chris Lattner56d77c72009-09-13 22:41:48 +000014517 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014518 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014519 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014520 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014521 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014522 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014523
14524 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014525 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014526 Res.first = X86::EFLAGS;
14527 Res.second = X86::CCRRegisterClass;
14528 return Res;
14529 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014530
Dale Johannesen330169f2008-11-13 21:52:36 +000014531 // 'A' means EAX + EDX.
14532 if (Constraint == "A") {
14533 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014534 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014535 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014536 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014537 return Res;
14538 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014539
Chris Lattnerf76d1802006-07-31 23:26:50 +000014540 // Otherwise, check to see if this is a register class of the wrong value
14541 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14542 // turn into {ax},{dx}.
14543 if (Res.second->hasType(VT))
14544 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014545
Chris Lattnerf76d1802006-07-31 23:26:50 +000014546 // All of the single-register GCC register classes map their values onto
14547 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14548 // really want an 8-bit or 32-bit register, map to the appropriate register
14549 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014550 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014551 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014552 unsigned DestReg = 0;
14553 switch (Res.first) {
14554 default: break;
14555 case X86::AX: DestReg = X86::AL; break;
14556 case X86::DX: DestReg = X86::DL; break;
14557 case X86::CX: DestReg = X86::CL; break;
14558 case X86::BX: DestReg = X86::BL; break;
14559 }
14560 if (DestReg) {
14561 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014562 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014563 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014564 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014565 unsigned DestReg = 0;
14566 switch (Res.first) {
14567 default: break;
14568 case X86::AX: DestReg = X86::EAX; break;
14569 case X86::DX: DestReg = X86::EDX; break;
14570 case X86::CX: DestReg = X86::ECX; break;
14571 case X86::BX: DestReg = X86::EBX; break;
14572 case X86::SI: DestReg = X86::ESI; break;
14573 case X86::DI: DestReg = X86::EDI; break;
14574 case X86::BP: DestReg = X86::EBP; break;
14575 case X86::SP: DestReg = X86::ESP; break;
14576 }
14577 if (DestReg) {
14578 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014579 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014580 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014581 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014582 unsigned DestReg = 0;
14583 switch (Res.first) {
14584 default: break;
14585 case X86::AX: DestReg = X86::RAX; break;
14586 case X86::DX: DestReg = X86::RDX; break;
14587 case X86::CX: DestReg = X86::RCX; break;
14588 case X86::BX: DestReg = X86::RBX; break;
14589 case X86::SI: DestReg = X86::RSI; break;
14590 case X86::DI: DestReg = X86::RDI; break;
14591 case X86::BP: DestReg = X86::RBP; break;
14592 case X86::SP: DestReg = X86::RSP; break;
14593 }
14594 if (DestReg) {
14595 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014596 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014597 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014598 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014599 } else if (Res.second == X86::FR32RegisterClass ||
14600 Res.second == X86::FR64RegisterClass ||
14601 Res.second == X86::VR128RegisterClass) {
14602 // Handle references to XMM physical registers that got mapped into the
14603 // wrong class. This can happen with constraints like {xmm0} where the
14604 // target independent register mapper will just pick the first match it can
14605 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014606 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014607 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014608 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014609 Res.second = X86::FR64RegisterClass;
14610 else if (X86::VR128RegisterClass->hasType(VT))
14611 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014612 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014613
Chris Lattnerf76d1802006-07-31 23:26:50 +000014614 return Res;
14615}