blob: d220c59430afa2e1946505a54505cd0289ca4e1f [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
Duncan Sands4a544a72011-09-06 13:37:06 +0000507 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
508 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000509
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000511
Nate Begemanacc398c2006-01-25 18:21:52 +0000512 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::VASTART , MVT::Other, Custom);
514 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000515 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::VAARG , MVT::Other, Custom);
517 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000518 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::VAARG , MVT::Other, Expand);
520 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000521 }
Evan Chengae642192007-03-02 23:16:35 +0000522
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
524 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000525
526 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
527 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
528 MVT::i64 : MVT::i32, Custom);
529 else if (EnableSegmentedStacks)
530 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
531 MVT::i64 : MVT::i32, Custom);
532 else
533 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
534 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000535
Evan Chengc7ce29b2009-02-13 22:36:38 +0000536 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000537 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000538 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
540 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000541
Evan Cheng223547a2006-01-31 22:28:30 +0000542 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::FABS , MVT::f64, Custom);
544 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000545
546 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::FNEG , MVT::f64, Custom);
548 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000549
Evan Cheng68c47cb2007-01-05 07:55:56 +0000550 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
552 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000553
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000554 // Lower this to FGETSIGNx86 plus an AND.
555 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
556 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
557
Evan Chengd25e9e82006-02-02 00:28:23 +0000558 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::FSIN , MVT::f64, Expand);
560 setOperationAction(ISD::FCOS , MVT::f64, Expand);
561 setOperationAction(ISD::FSIN , MVT::f32, Expand);
562 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000563
Chris Lattnera54aa942006-01-29 06:26:08 +0000564 // Expand FP immediates into loads from the stack, except for the special
565 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000566 addLegalFPImmediate(APFloat(+0.0)); // xorpd
567 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000568 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000569 // Use SSE for f32, x87 for f64.
570 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
572 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000573
574 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000575 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000576
577 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000579
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581
582 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
584 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585
586 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setOperationAction(ISD::FSIN , MVT::f32, Expand);
588 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000589
Nate Begemane1795842008-02-14 08:57:00 +0000590 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591 addLegalFPImmediate(APFloat(+0.0f)); // xorps
592 addLegalFPImmediate(APFloat(+0.0)); // FLD0
593 addLegalFPImmediate(APFloat(+1.0)); // FLD1
594 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
595 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
596
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000597 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
599 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000601 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000603 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
605 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000606
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
608 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
609 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
610 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000611
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000612 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
614 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000615 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000616 addLegalFPImmediate(APFloat(+0.0)); // FLD0
617 addLegalFPImmediate(APFloat(+1.0)); // FLD1
618 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
619 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
621 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
622 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
623 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000624 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000625
Cameron Zwarich33390842011-07-08 21:39:21 +0000626 // We don't support FMA.
627 setOperationAction(ISD::FMA, MVT::f64, Expand);
628 setOperationAction(ISD::FMA, MVT::f32, Expand);
629
Dale Johannesen59a58732007-08-05 18:49:15 +0000630 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000631 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
633 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
634 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000635 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000636 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000637 addLegalFPImmediate(TmpFlt); // FLD0
638 TmpFlt.changeSign();
639 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000640
641 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000642 APFloat TmpFlt2(+1.0);
643 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
644 &ignored);
645 addLegalFPImmediate(TmpFlt2); // FLD1
646 TmpFlt2.changeSign();
647 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
648 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000649
Evan Chengc7ce29b2009-02-13 22:36:38 +0000650 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
652 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000654
655 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000656 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000657
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000658 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
660 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
661 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000662
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FLOG, MVT::f80, Expand);
664 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
665 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
666 setOperationAction(ISD::FEXP, MVT::f80, Expand);
667 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000668
Mon P Wangf007a8b2008-11-06 05:31:54 +0000669 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000670 // (for widening) or expand (for scalarization). Then we will selectively
671 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
673 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
674 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
675 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
676 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
677 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000690 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
691 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000723 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000724 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
728 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
729 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
730 setTruncStoreAction((MVT::SimpleValueType)VT,
731 (MVT::SimpleValueType)InnerVT, Expand);
732 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
733 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
734 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000735 }
736
Evan Chengc7ce29b2009-02-13 22:36:38 +0000737 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
738 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000739 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000740 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000741 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000742 }
743
Dale Johannesen0488fb62010-09-30 23:57:10 +0000744 // MMX-sized vectors (other than x86mmx) are expected to be expanded
745 // into smaller operations.
746 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
747 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
748 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
749 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
750 setOperationAction(ISD::AND, MVT::v8i8, Expand);
751 setOperationAction(ISD::AND, MVT::v4i16, Expand);
752 setOperationAction(ISD::AND, MVT::v2i32, Expand);
753 setOperationAction(ISD::AND, MVT::v1i64, Expand);
754 setOperationAction(ISD::OR, MVT::v8i8, Expand);
755 setOperationAction(ISD::OR, MVT::v4i16, Expand);
756 setOperationAction(ISD::OR, MVT::v2i32, Expand);
757 setOperationAction(ISD::OR, MVT::v1i64, Expand);
758 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
759 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
760 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
761 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
762 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
763 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
764 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
765 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
766 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
767 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
768 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
769 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
770 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000771 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
772 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
773 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
774 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000775
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000776 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000778
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
780 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
781 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
782 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
783 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
784 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
785 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
786 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
787 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
788 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
789 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
790 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791 }
792
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000793 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000795
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000796 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
797 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
799 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
800 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
801 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000802
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
804 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
805 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
806 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
807 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
808 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
809 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
810 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
811 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
812 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
813 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
814 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
815 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
816 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
817 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
818 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000819
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
821 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
822 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
823 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000824
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
826 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
827 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
828 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
829 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000830
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000831 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
832 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
833 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
834 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
835 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
836
Evan Cheng2c3ae372006-04-12 21:21:57 +0000837 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
839 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000840 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000841 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000842 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000843 // Do not attempt to custom lower non-128-bit vectors
844 if (!VT.is128BitVector())
845 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 setOperationAction(ISD::BUILD_VECTOR,
847 VT.getSimpleVT().SimpleTy, Custom);
848 setOperationAction(ISD::VECTOR_SHUFFLE,
849 VT.getSimpleVT().SimpleTy, Custom);
850 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
851 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000852 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000853
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
855 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
856 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
857 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
858 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
859 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000860
Nate Begemancdd1eec2008-02-12 22:51:28 +0000861 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000864 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000866 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
868 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000869 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000870
871 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000872 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000873 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000874
Owen Andersond6662ad2009-08-10 20:46:15 +0000875 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000876 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000877 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000879 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000881 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000883 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000885 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000886
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000888
Evan Cheng2c3ae372006-04-12 21:21:57 +0000889 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
891 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
892 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
893 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000894
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
896 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000897 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000898
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000899 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000900 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
901 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
902 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
903 setOperationAction(ISD::FRINT, MVT::f32, Legal);
904 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
905 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
906 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
907 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
908 setOperationAction(ISD::FRINT, MVT::f64, Legal);
909 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
910
Nate Begeman14d12ca2008-02-11 04:19:36 +0000911 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000913
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000914 // Can turn SHL into an integer multiply.
915 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000916 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000917
Nate Begeman14d12ca2008-02-11 04:19:36 +0000918 // i8 and i16 vectors are custom , because the source register and source
919 // source memory operand types are not the same width. f32 vectors are
920 // custom since the immediate controlling the insert encodes additional
921 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
923 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
924 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
925 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
928 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
929 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
930 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000931
932 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
934 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000935 }
936 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000937
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000938 if (Subtarget->hasSSE2() || Subtarget->hasAVX()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000939 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
940 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
941 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000942 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000943
944 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
945 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
946 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
947
948 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
949 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
950 }
951
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000952 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000954
David Greene9b9838d2009-06-29 16:47:10 +0000955 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000956 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
957 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
958 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
959 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
960 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
961 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000962
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
965 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000966
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
968 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
969 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
970 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
971 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
972 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000973
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
975 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
976 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
977 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
978 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
979 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000980
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000981 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
982 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000983 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000984
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000985 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
986 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
987 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
988 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
989 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
990 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
991
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000992 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
993 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
994 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
995 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
996
997 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
998 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
999 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1000 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1001
1002 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1003 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1004
Craig Toppera5347802011-08-23 04:36:33 +00001005 setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
1006 setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001007 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
1008 setOperationAction(ISD::VSETCC, MVT::v4i64, Custom);
1009
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001010 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1011 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1012 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1013
Craig Topper13894fa2011-08-24 06:14:18 +00001014 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1015 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1016 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1017 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1018
1019 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1020 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1021 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1022 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1023
1024 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1025 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1026 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1027 // Don't lower v32i8 because there is no 128-bit byte mul
1028
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001029 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001030 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001031 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1032 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1033 EVT VT = SVT;
1034
1035 // Extract subvector is special because the value type
1036 // (result) is 128-bit but the source is 256-bit wide.
1037 if (VT.is128BitVector())
1038 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1039
1040 // Do not attempt to custom lower other non-256-bit vectors
1041 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001042 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001043
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001044 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1045 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1046 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1047 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001048 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001049 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001050 }
1051
David Greene54d8eba2011-01-27 22:38:56 +00001052 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001053 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1054 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1055 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001056
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001057 // Do not attempt to promote non-256-bit vectors
1058 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001059 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001060
1061 setOperationAction(ISD::AND, SVT, Promote);
1062 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1063 setOperationAction(ISD::OR, SVT, Promote);
1064 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1065 setOperationAction(ISD::XOR, SVT, Promote);
1066 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1067 setOperationAction(ISD::LOAD, SVT, Promote);
1068 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1069 setOperationAction(ISD::SELECT, SVT, Promote);
1070 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001071 }
David Greene9b9838d2009-06-29 16:47:10 +00001072 }
1073
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001074 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1075 // of this type with custom code.
1076 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1077 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1078 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1079 }
1080
Evan Cheng6be2c582006-04-05 23:38:46 +00001081 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001083
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001084
Eli Friedman962f5492010-06-02 19:35:46 +00001085 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1086 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001087 //
Eli Friedman962f5492010-06-02 19:35:46 +00001088 // FIXME: We really should do custom legalization for addition and
1089 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1090 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001091 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1092 // Add/Sub/Mul with overflow operations are custom lowered.
1093 MVT VT = IntVTs[i];
1094 setOperationAction(ISD::SADDO, VT, Custom);
1095 setOperationAction(ISD::UADDO, VT, Custom);
1096 setOperationAction(ISD::SSUBO, VT, Custom);
1097 setOperationAction(ISD::USUBO, VT, Custom);
1098 setOperationAction(ISD::SMULO, VT, Custom);
1099 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001100 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001101
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001102 // There are no 8-bit 3-address imul/mul instructions
1103 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1104 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001105
Evan Chengd54f2d52009-03-31 19:38:51 +00001106 if (!Subtarget->is64Bit()) {
1107 // These libcalls are not available in 32-bit.
1108 setLibcallName(RTLIB::SHL_I128, 0);
1109 setLibcallName(RTLIB::SRL_I128, 0);
1110 setLibcallName(RTLIB::SRA_I128, 0);
1111 }
1112
Evan Cheng206ee9d2006-07-07 08:33:52 +00001113 // We have target-specific dag combine patterns for the following nodes:
1114 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001115 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001116 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001117 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001118 setTargetDAGCombine(ISD::SHL);
1119 setTargetDAGCombine(ISD::SRA);
1120 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001121 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001122 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001123 setTargetDAGCombine(ISD::ADD);
1124 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001125 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001126 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001127 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001128 if (Subtarget->is64Bit())
1129 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001130
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001131 computeRegisterProperties();
1132
Evan Cheng05219282011-01-06 06:52:41 +00001133 // On Darwin, -Os means optimize for size without hurting performance,
1134 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001135 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001136 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001137 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001138 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1139 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1140 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001141 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001142 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001143
1144 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001145}
1146
Scott Michel5b8f82e2008-03-10 15:42:14 +00001147
Owen Anderson825b72b2009-08-11 20:47:22 +00001148MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1149 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001150}
1151
1152
Evan Cheng29286502008-01-23 23:17:41 +00001153/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1154/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001155static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001156 if (MaxAlign == 16)
1157 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001158 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001159 if (VTy->getBitWidth() == 128)
1160 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001161 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001162 unsigned EltAlign = 0;
1163 getMaxByValAlign(ATy->getElementType(), EltAlign);
1164 if (EltAlign > MaxAlign)
1165 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001166 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001167 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1168 unsigned EltAlign = 0;
1169 getMaxByValAlign(STy->getElementType(i), EltAlign);
1170 if (EltAlign > MaxAlign)
1171 MaxAlign = EltAlign;
1172 if (MaxAlign == 16)
1173 break;
1174 }
1175 }
1176 return;
1177}
1178
1179/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1180/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001181/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1182/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001183unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001184 if (Subtarget->is64Bit()) {
1185 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001186 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001187 if (TyAlign > 8)
1188 return TyAlign;
1189 return 8;
1190 }
1191
Evan Cheng29286502008-01-23 23:17:41 +00001192 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001193 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001194 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001195 return Align;
1196}
Chris Lattner2b02a442007-02-25 08:29:00 +00001197
Evan Chengf0df0312008-05-15 08:39:06 +00001198/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001199/// and store operations as a result of memset, memcpy, and memmove
1200/// lowering. If DstAlign is zero that means it's safe to destination
1201/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1202/// means there isn't a need to check it against alignment requirement,
1203/// probably because the source does not need to be loaded. If
1204/// 'NonScalarIntSafe' is true, that means it's safe to return a
1205/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1206/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1207/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001208/// It returns EVT::Other if the type should be determined using generic
1209/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001210EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001211X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1212 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001213 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001214 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001215 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001216 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1217 // linux. This is because the stack realignment code can't handle certain
1218 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001219 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001220 if (NonScalarIntSafe &&
1221 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001222 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001223 (Subtarget->isUnalignedMemAccessFast() ||
1224 ((DstAlign == 0 || DstAlign >= 16) &&
1225 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001226 Subtarget->getStackAlignment() >= 16) {
1227 if (Subtarget->hasSSE2())
1228 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001229 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001230 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001231 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001232 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001233 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001234 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001235 // Do not use f64 to lower memcpy if source is string constant. It's
1236 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001237 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001238 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001239 }
Evan Chengf0df0312008-05-15 08:39:06 +00001240 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001241 return MVT::i64;
1242 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001243}
1244
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001245/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1246/// current function. The returned value is a member of the
1247/// MachineJumpTableInfo::JTEntryKind enum.
1248unsigned X86TargetLowering::getJumpTableEncoding() const {
1249 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1250 // symbol.
1251 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1252 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001253 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001254
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001255 // Otherwise, use the normal jump table encoding heuristics.
1256 return TargetLowering::getJumpTableEncoding();
1257}
1258
Chris Lattnerc64daab2010-01-26 05:02:42 +00001259const MCExpr *
1260X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1261 const MachineBasicBlock *MBB,
1262 unsigned uid,MCContext &Ctx) const{
1263 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1264 Subtarget->isPICStyleGOT());
1265 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1266 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001267 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1268 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001269}
1270
Evan Chengcc415862007-11-09 01:32:10 +00001271/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1272/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001273SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001274 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001275 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001276 // This doesn't have DebugLoc associated with it, but is not really the
1277 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001278 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001279 return Table;
1280}
1281
Chris Lattner589c6f62010-01-26 06:28:43 +00001282/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1283/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1284/// MCExpr.
1285const MCExpr *X86TargetLowering::
1286getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1287 MCContext &Ctx) const {
1288 // X86-64 uses RIP relative addressing based on the jump table label.
1289 if (Subtarget->isPICStyleRIPRel())
1290 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1291
1292 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001293 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001294}
1295
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001296// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001297std::pair<const TargetRegisterClass*, uint8_t>
1298X86TargetLowering::findRepresentativeClass(EVT VT) const{
1299 const TargetRegisterClass *RRC = 0;
1300 uint8_t Cost = 1;
1301 switch (VT.getSimpleVT().SimpleTy) {
1302 default:
1303 return TargetLowering::findRepresentativeClass(VT);
1304 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1305 RRC = (Subtarget->is64Bit()
1306 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1307 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001308 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001309 RRC = X86::VR64RegisterClass;
1310 break;
1311 case MVT::f32: case MVT::f64:
1312 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1313 case MVT::v4f32: case MVT::v2f64:
1314 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1315 case MVT::v4f64:
1316 RRC = X86::VR128RegisterClass;
1317 break;
1318 }
1319 return std::make_pair(RRC, Cost);
1320}
1321
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001322bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1323 unsigned &Offset) const {
1324 if (!Subtarget->isTargetLinux())
1325 return false;
1326
1327 if (Subtarget->is64Bit()) {
1328 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1329 Offset = 0x28;
1330 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1331 AddressSpace = 256;
1332 else
1333 AddressSpace = 257;
1334 } else {
1335 // %gs:0x14 on i386
1336 Offset = 0x14;
1337 AddressSpace = 256;
1338 }
1339 return true;
1340}
1341
1342
Chris Lattner2b02a442007-02-25 08:29:00 +00001343//===----------------------------------------------------------------------===//
1344// Return Value Calling Convention Implementation
1345//===----------------------------------------------------------------------===//
1346
Chris Lattner59ed56b2007-02-28 04:55:35 +00001347#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001348
Michael J. Spencerec38de22010-10-10 22:04:20 +00001349bool
Eric Christopher471e4222011-06-08 23:55:35 +00001350X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1351 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001352 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001353 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001354 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001355 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001356 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001357 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001358}
1359
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360SDValue
1361X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001362 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001363 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001364 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001365 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001366 MachineFunction &MF = DAG.getMachineFunction();
1367 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001368
Chris Lattner9774c912007-02-27 05:28:59 +00001369 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001370 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001371 RVLocs, *DAG.getContext());
1372 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001373
Evan Chengdcea1632010-02-04 02:40:39 +00001374 // Add the regs to the liveout set for the function.
1375 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1376 for (unsigned i = 0; i != RVLocs.size(); ++i)
1377 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1378 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001379
Dan Gohman475871a2008-07-27 21:46:04 +00001380 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001381
Dan Gohman475871a2008-07-27 21:46:04 +00001382 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001383 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1384 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001385 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1386 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001387
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001388 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001389 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1390 CCValAssign &VA = RVLocs[i];
1391 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001392 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001393 EVT ValVT = ValToCopy.getValueType();
1394
Dale Johannesenc4510512010-09-24 19:05:48 +00001395 // If this is x86-64, and we disabled SSE, we can't return FP values,
1396 // or SSE or MMX vectors.
1397 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1398 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001399 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001400 report_fatal_error("SSE register return with SSE disabled");
1401 }
1402 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1403 // llvm-gcc has never done it right and no one has noticed, so this
1404 // should be OK for now.
1405 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001406 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001407 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001408
Chris Lattner447ff682008-03-11 03:23:40 +00001409 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1410 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001411 if (VA.getLocReg() == X86::ST0 ||
1412 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001413 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1414 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001415 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001417 RetOps.push_back(ValToCopy);
1418 // Don't emit a copytoreg.
1419 continue;
1420 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001421
Evan Cheng242b38b2009-02-23 09:03:22 +00001422 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1423 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001424 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001425 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001426 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001427 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001428 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1429 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001430 // If we don't have SSE2 available, convert to v4f32 so the generated
1431 // register is legal.
1432 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001433 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001434 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001435 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001436 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001437
Dale Johannesendd64c412009-02-04 00:33:20 +00001438 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001439 Flag = Chain.getValue(1);
1440 }
Dan Gohman61a92132008-04-21 23:59:07 +00001441
1442 // The x86-64 ABI for returning structs by value requires that we copy
1443 // the sret argument into %rax for the return. We saved the argument into
1444 // a virtual register in the entry block, so now we copy the value out
1445 // and into %rax.
1446 if (Subtarget->is64Bit() &&
1447 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1448 MachineFunction &MF = DAG.getMachineFunction();
1449 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1450 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001451 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001452 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001453 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001454
Dale Johannesendd64c412009-02-04 00:33:20 +00001455 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001456 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001457
1458 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001459 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001460 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001461
Chris Lattner447ff682008-03-11 03:23:40 +00001462 RetOps[0] = Chain; // Update chain.
1463
1464 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001465 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001466 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001467
1468 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001470}
1471
Evan Cheng3d2125c2010-11-30 23:55:39 +00001472bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1473 if (N->getNumValues() != 1)
1474 return false;
1475 if (!N->hasNUsesOfValue(1, 0))
1476 return false;
1477
1478 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001479 if (Copy->getOpcode() != ISD::CopyToReg &&
1480 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001481 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001482
1483 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001484 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001485 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001486 if (UI->getOpcode() != X86ISD::RET_FLAG)
1487 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001488 HasRet = true;
1489 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001490
Evan Cheng1bf891a2010-12-01 22:59:46 +00001491 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001492}
1493
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001494EVT
1495X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001496 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001497 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001498 // TODO: Is this also valid on 32-bit?
1499 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001500 ReturnMVT = MVT::i8;
1501 else
1502 ReturnMVT = MVT::i32;
1503
1504 EVT MinVT = getRegisterType(Context, ReturnMVT);
1505 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001506}
1507
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508/// LowerCallResult - Lower the result values of a call into the
1509/// appropriate copies out of appropriate physical registers.
1510///
1511SDValue
1512X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001513 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001514 const SmallVectorImpl<ISD::InputArg> &Ins,
1515 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001516 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001517
Chris Lattnere32bbf62007-02-28 07:09:55 +00001518 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001519 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001520 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001521 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1522 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001523 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001524
Chris Lattner3085e152007-02-25 08:59:22 +00001525 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001526 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001527 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001528 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Torok Edwin3f142c32009-02-01 18:15:56 +00001530 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001532 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001533 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001534 }
1535
Evan Cheng79fb3b42009-02-20 20:43:02 +00001536 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001537
1538 // If this is a call to a function that returns an fp value on the floating
1539 // point stack, we must guarantee the the value is popped from the stack, so
1540 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001541 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001542 // instead.
1543 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1544 // If we prefer to use the value in xmm registers, copy it out as f80 and
1545 // use a truncate to move it from fp stack reg to xmm reg.
1546 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001547 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001548 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1549 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001550 Val = Chain.getValue(0);
1551
1552 // Round the f80 to the right size, which also moves it to the appropriate
1553 // xmm register.
1554 if (CopyVT != VA.getValVT())
1555 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1556 // This truncation won't change the value.
1557 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001558 } else {
1559 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1560 CopyVT, InFlag).getValue(1);
1561 Val = Chain.getValue(0);
1562 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001563 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001565 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001566
Dan Gohman98ca4f22009-08-05 01:29:28 +00001567 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001568}
1569
1570
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001571//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001572// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001573//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001574// StdCall calling convention seems to be standard for many Windows' API
1575// routines and around. It differs from C calling convention just a little:
1576// callee should clean up the stack, not caller. Symbols should be also
1577// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001578// For info on fast calling convention see Fast Calling Convention (tail call)
1579// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001580
Dan Gohman98ca4f22009-08-05 01:29:28 +00001581/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001582/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001583static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1584 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001585 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001586
Dan Gohman98ca4f22009-08-05 01:29:28 +00001587 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001588}
1589
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001590/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001591/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001592static bool
1593ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1594 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001595 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001596
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001598}
1599
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001600/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1601/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001602/// the specific parameter attribute. The copy will be passed as a byval
1603/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001604static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001605CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001606 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1607 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001608 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001609
Dale Johannesendd64c412009-02-04 00:33:20 +00001610 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001611 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001612 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001613}
1614
Chris Lattner29689432010-03-11 00:22:57 +00001615/// IsTailCallConvention - Return true if the calling convention is one that
1616/// supports tail call optimization.
1617static bool IsTailCallConvention(CallingConv::ID CC) {
1618 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1619}
1620
Evan Cheng485fafc2011-03-21 01:19:09 +00001621bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1622 if (!CI->isTailCall())
1623 return false;
1624
1625 CallSite CS(CI);
1626 CallingConv::ID CalleeCC = CS.getCallingConv();
1627 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1628 return false;
1629
1630 return true;
1631}
1632
Evan Cheng0c439eb2010-01-27 00:07:07 +00001633/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1634/// a tailcall target by changing its ABI.
1635static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001636 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001637}
1638
Dan Gohman98ca4f22009-08-05 01:29:28 +00001639SDValue
1640X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001641 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001642 const SmallVectorImpl<ISD::InputArg> &Ins,
1643 DebugLoc dl, SelectionDAG &DAG,
1644 const CCValAssign &VA,
1645 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001646 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001647 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001648 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001649 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001650 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001651 EVT ValVT;
1652
1653 // If value is passed by pointer we have address passed instead of the value
1654 // itself.
1655 if (VA.getLocInfo() == CCValAssign::Indirect)
1656 ValVT = VA.getLocVT();
1657 else
1658 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001659
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001660 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001661 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001662 // In case of tail call optimization mark all arguments mutable. Since they
1663 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001664 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001665 unsigned Bytes = Flags.getByValSize();
1666 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1667 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001668 return DAG.getFrameIndex(FI, getPointerTy());
1669 } else {
1670 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001671 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001672 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1673 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001674 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001675 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001676 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001677}
1678
Dan Gohman475871a2008-07-27 21:46:04 +00001679SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001680X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001681 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001682 bool isVarArg,
1683 const SmallVectorImpl<ISD::InputArg> &Ins,
1684 DebugLoc dl,
1685 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001686 SmallVectorImpl<SDValue> &InVals)
1687 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001688 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001689 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001690
Gordon Henriksen86737662008-01-05 16:56:59 +00001691 const Function* Fn = MF.getFunction();
1692 if (Fn->hasExternalLinkage() &&
1693 Subtarget->isTargetCygMing() &&
1694 Fn->getName() == "main")
1695 FuncInfo->setForceFramePointer(true);
1696
Evan Cheng1bc78042006-04-26 01:20:17 +00001697 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001698 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001699 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001700
Chris Lattner29689432010-03-11 00:22:57 +00001701 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1702 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001703
Chris Lattner638402b2007-02-28 07:00:42 +00001704 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001705 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001706 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001707 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001708
1709 // Allocate shadow area for Win64
1710 if (IsWin64) {
1711 CCInfo.AllocateStack(32, 8);
1712 }
1713
Duncan Sands45907662010-10-31 13:21:44 +00001714 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001715
Chris Lattnerf39f7712007-02-28 05:46:49 +00001716 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001717 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001718 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1719 CCValAssign &VA = ArgLocs[i];
1720 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1721 // places.
1722 assert(VA.getValNo() != LastVal &&
1723 "Don't support value assigned to multiple locs yet");
1724 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001725
Chris Lattnerf39f7712007-02-28 05:46:49 +00001726 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001727 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001728 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001729 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001730 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001731 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001732 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001733 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001734 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001735 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001736 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001737 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1738 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001739 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001740 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001741 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001742 RC = X86::VR64RegisterClass;
1743 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001744 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001745
Devang Patel68e6bee2011-02-21 23:21:26 +00001746 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001748
Chris Lattnerf39f7712007-02-28 05:46:49 +00001749 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1750 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1751 // right size.
1752 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001753 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001754 DAG.getValueType(VA.getValVT()));
1755 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001756 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001757 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001758 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001759 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001760
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001761 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001762 // Handle MMX values passed in XMM regs.
1763 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001764 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1765 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001766 } else
1767 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001768 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001769 } else {
1770 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001771 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001772 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001773
1774 // If value is passed via pointer - do a load.
1775 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001776 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1777 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001778
Dan Gohman98ca4f22009-08-05 01:29:28 +00001779 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001780 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001781
Dan Gohman61a92132008-04-21 23:59:07 +00001782 // The x86-64 ABI for returning structs by value requires that we copy
1783 // the sret argument into %rax for the return. Save the argument into
1784 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001785 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001786 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1787 unsigned Reg = FuncInfo->getSRetReturnReg();
1788 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001790 FuncInfo->setSRetReturnReg(Reg);
1791 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001793 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001794 }
1795
Chris Lattnerf39f7712007-02-28 05:46:49 +00001796 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001797 // Align stack specially for tail calls.
1798 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001799 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001800
Evan Cheng1bc78042006-04-26 01:20:17 +00001801 // If the function takes variable number of arguments, make a frame index for
1802 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001803 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001804 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1805 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001806 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001807 }
1808 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001809 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1810
1811 // FIXME: We should really autogenerate these arrays
1812 static const unsigned GPR64ArgRegsWin64[] = {
1813 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001814 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001815 static const unsigned GPR64ArgRegs64Bit[] = {
1816 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1817 };
1818 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001819 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1820 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1821 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001822 const unsigned *GPR64ArgRegs;
1823 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001824
1825 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001826 // The XMM registers which might contain var arg parameters are shadowed
1827 // in their paired GPR. So we only need to save the GPR to their home
1828 // slots.
1829 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001830 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001831 } else {
1832 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1833 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001834
1835 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001836 }
1837 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1838 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001839
Devang Patel578efa92009-06-05 21:57:13 +00001840 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001841 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001842 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001843 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001844 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001845 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001846 // Kernel mode asks for SSE to be disabled, so don't push them
1847 // on the stack.
1848 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001849
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001850 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001851 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001852 // Get to the caller-allocated home save location. Add 8 to account
1853 // for the return address.
1854 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001855 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001856 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001857 // Fixup to set vararg frame on shadow area (4 x i64).
1858 if (NumIntRegs < 4)
1859 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001860 } else {
1861 // For X86-64, if there are vararg parameters that are passed via
1862 // registers, then we must store them to their spots on the stack so they
1863 // may be loaded by deferencing the result of va_next.
1864 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1865 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1866 FuncInfo->setRegSaveFrameIndex(
1867 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001868 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001869 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001870
Gordon Henriksen86737662008-01-05 16:56:59 +00001871 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001872 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001873 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1874 getPointerTy());
1875 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001876 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001877 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1878 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001879 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001880 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001882 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001883 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001884 MachinePointerInfo::getFixedStack(
1885 FuncInfo->getRegSaveFrameIndex(), Offset),
1886 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001887 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001888 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001889 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001890
Dan Gohmanface41a2009-08-16 21:24:25 +00001891 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1892 // Now store the XMM (fp + vector) parameter registers.
1893 SmallVector<SDValue, 11> SaveXMMOps;
1894 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001895
Devang Patel68e6bee2011-02-21 23:21:26 +00001896 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001897 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1898 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001899
Dan Gohman1e93df62010-04-17 14:41:14 +00001900 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1901 FuncInfo->getRegSaveFrameIndex()));
1902 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1903 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001904
Dan Gohmanface41a2009-08-16 21:24:25 +00001905 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001906 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001907 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001908 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1909 SaveXMMOps.push_back(Val);
1910 }
1911 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1912 MVT::Other,
1913 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001914 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001915
1916 if (!MemOps.empty())
1917 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1918 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001919 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001920 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001921
Gordon Henriksen86737662008-01-05 16:56:59 +00001922 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001923 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001924 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001925 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001926 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001927 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001928 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001929 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001930 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001931
Gordon Henriksen86737662008-01-05 16:56:59 +00001932 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001933 // RegSaveFrameIndex is X86-64 only.
1934 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001935 if (CallConv == CallingConv::X86_FastCall ||
1936 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001937 // fastcc functions can't have varargs.
1938 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001939 }
Evan Cheng25caf632006-05-23 21:06:34 +00001940
Rafael Espindola76927d752011-08-30 19:39:58 +00001941 FuncInfo->setArgumentStackSize(StackSize);
1942
Dan Gohman98ca4f22009-08-05 01:29:28 +00001943 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001944}
1945
Dan Gohman475871a2008-07-27 21:46:04 +00001946SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001947X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1948 SDValue StackPtr, SDValue Arg,
1949 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001950 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001951 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001952 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001953 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001954 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001955 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001956 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001957
1958 return DAG.getStore(Chain, dl, Arg, PtrOff,
1959 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001960 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001961}
1962
Bill Wendling64e87322009-01-16 19:25:27 +00001963/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001964/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001965SDValue
1966X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001967 SDValue &OutRetAddr, SDValue Chain,
1968 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001969 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001970 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001971 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001972 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001973
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001974 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001975 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1976 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001977 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001978}
1979
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001980/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001981/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001982static SDValue
1983EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001984 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001985 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001986 // Store the return address to the appropriate stack slot.
1987 if (!FPDiff) return Chain;
1988 // Calculate the new stack slot for the return address.
1989 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001990 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001991 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001992 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001993 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001994 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001995 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001996 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001997 return Chain;
1998}
1999
Dan Gohman98ca4f22009-08-05 01:29:28 +00002000SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002001X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002002 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002003 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002004 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002005 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002006 const SmallVectorImpl<ISD::InputArg> &Ins,
2007 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002008 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002009 MachineFunction &MF = DAG.getMachineFunction();
2010 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002011 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002012 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002013 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002014
Evan Cheng5f941932010-02-05 02:21:12 +00002015 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002016 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002017 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2018 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002019 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002020
2021 // Sibcalls are automatically detected tailcalls which do not require
2022 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002023 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002024 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002025
2026 if (isTailCall)
2027 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002028 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002029
Chris Lattner29689432010-03-11 00:22:57 +00002030 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2031 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002032
Chris Lattner638402b2007-02-28 07:00:42 +00002033 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002034 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002035 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002036 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002037
2038 // Allocate shadow area for Win64
2039 if (IsWin64) {
2040 CCInfo.AllocateStack(32, 8);
2041 }
2042
Duncan Sands45907662010-10-31 13:21:44 +00002043 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002044
Chris Lattner423c5f42007-02-28 05:31:48 +00002045 // Get a count of how many bytes are to be pushed on the stack.
2046 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002047 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002048 // This is a sibcall. The memory operands are available in caller's
2049 // own caller's stack.
2050 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002051 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002052 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002053
Gordon Henriksen86737662008-01-05 16:56:59 +00002054 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002055 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002056 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002057 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002058 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2059 FPDiff = NumBytesCallerPushed - NumBytes;
2060
2061 // Set the delta of movement of the returnaddr stackslot.
2062 // But only set if delta is greater than previous delta.
2063 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2064 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2065 }
2066
Evan Chengf22f9b32010-02-06 03:28:46 +00002067 if (!IsSibcall)
2068 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002069
Dan Gohman475871a2008-07-27 21:46:04 +00002070 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002071 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002072 if (isTailCall && FPDiff)
2073 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2074 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002075
Dan Gohman475871a2008-07-27 21:46:04 +00002076 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2077 SmallVector<SDValue, 8> MemOpChains;
2078 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002079
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002080 // Walk the register/memloc assignments, inserting copies/loads. In the case
2081 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002082 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2083 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002084 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002085 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002086 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002087 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Chris Lattner423c5f42007-02-28 05:31:48 +00002089 // Promote the value if needed.
2090 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002091 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002092 case CCValAssign::Full: break;
2093 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002094 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002095 break;
2096 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002097 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002098 break;
2099 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002100 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2101 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002102 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2104 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002105 } else
2106 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2107 break;
2108 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002109 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002110 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002111 case CCValAssign::Indirect: {
2112 // Store the argument.
2113 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002114 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002115 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002116 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002117 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002118 Arg = SpillSlot;
2119 break;
2120 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002122
Chris Lattner423c5f42007-02-28 05:31:48 +00002123 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002124 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2125 if (isVarArg && IsWin64) {
2126 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2127 // shadow reg if callee is a varargs function.
2128 unsigned ShadowReg = 0;
2129 switch (VA.getLocReg()) {
2130 case X86::XMM0: ShadowReg = X86::RCX; break;
2131 case X86::XMM1: ShadowReg = X86::RDX; break;
2132 case X86::XMM2: ShadowReg = X86::R8; break;
2133 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002134 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002135 if (ShadowReg)
2136 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002137 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002138 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002139 assert(VA.isMemLoc());
2140 if (StackPtr.getNode() == 0)
2141 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2142 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2143 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002144 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002145 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002146
Evan Cheng32fe1032006-05-25 00:59:30 +00002147 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002148 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002149 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002150
Evan Cheng347d5f72006-04-28 21:29:37 +00002151 // Build a sequence of copy-to-reg nodes chained together with token chain
2152 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002153 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002154 // Tail call byval lowering might overwrite argument registers so in case of
2155 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002156 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002157 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002158 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002159 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002160 InFlag = Chain.getValue(1);
2161 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002162
Chris Lattner88e1fd52009-07-09 04:24:46 +00002163 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002164 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2165 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002167 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2168 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002169 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002170 InFlag);
2171 InFlag = Chain.getValue(1);
2172 } else {
2173 // If we are tail calling and generating PIC/GOT style code load the
2174 // address of the callee into ECX. The value in ecx is used as target of
2175 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2176 // for tail calls on PIC/GOT architectures. Normally we would just put the
2177 // address of GOT into ebx and then call target@PLT. But for tail calls
2178 // ebx would be restored (since ebx is callee saved) before jumping to the
2179 // target@PLT.
2180
2181 // Note: The actual moving to ECX is done further down.
2182 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2183 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2184 !G->getGlobal()->hasProtectedVisibility())
2185 Callee = LowerGlobalAddress(Callee, DAG);
2186 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002187 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002188 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002189 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002190
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002191 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002192 // From AMD64 ABI document:
2193 // For calls that may call functions that use varargs or stdargs
2194 // (prototype-less calls or calls to functions containing ellipsis (...) in
2195 // the declaration) %al is used as hidden argument to specify the number
2196 // of SSE registers used. The contents of %al do not need to match exactly
2197 // the number of registers, but must be an ubound on the number of SSE
2198 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002199
Gordon Henriksen86737662008-01-05 16:56:59 +00002200 // Count the number of XMM registers allocated.
2201 static const unsigned XMMArgRegs[] = {
2202 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2203 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2204 };
2205 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002206 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002207 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002208
Dale Johannesendd64c412009-02-04 00:33:20 +00002209 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002210 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002211 InFlag = Chain.getValue(1);
2212 }
2213
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002214
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002215 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002216 if (isTailCall) {
2217 // Force all the incoming stack arguments to be loaded from the stack
2218 // before any new outgoing arguments are stored to the stack, because the
2219 // outgoing stack slots may alias the incoming argument stack slots, and
2220 // the alias isn't otherwise explicit. This is slightly more conservative
2221 // than necessary, because it means that each store effectively depends
2222 // on every argument instead of just those arguments it would clobber.
2223 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2224
Dan Gohman475871a2008-07-27 21:46:04 +00002225 SmallVector<SDValue, 8> MemOpChains2;
2226 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002227 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002228 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002229 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002230 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002231 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2232 CCValAssign &VA = ArgLocs[i];
2233 if (VA.isRegLoc())
2234 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002235 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002236 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002237 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 // Create frame index.
2239 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002240 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002241 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002242 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002243
Duncan Sands276dcbd2008-03-21 09:14:45 +00002244 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002245 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002246 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002247 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002248 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002249 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002250 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002251
Dan Gohman98ca4f22009-08-05 01:29:28 +00002252 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2253 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002254 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002255 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002256 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002257 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002258 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002259 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002260 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002261 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002262 }
2263 }
2264
2265 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002266 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002267 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002268
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002269 // Copy arguments to their registers.
2270 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002271 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002272 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002273 InFlag = Chain.getValue(1);
2274 }
Dan Gohman475871a2008-07-27 21:46:04 +00002275 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002276
Gordon Henriksen86737662008-01-05 16:56:59 +00002277 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002278 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002279 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002280 }
2281
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002282 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2283 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2284 // In the 64-bit large code model, we have to make all calls
2285 // through a register, since the call instruction's 32-bit
2286 // pc-relative offset may not be large enough to hold the whole
2287 // address.
2288 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002289 // If the callee is a GlobalAddress node (quite common, every direct call
2290 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2291 // it.
2292
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002293 // We should use extra load for direct calls to dllimported functions in
2294 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002295 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002296 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002297 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002298 bool ExtraLoad = false;
2299 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002300
Chris Lattner48a7d022009-07-09 05:02:21 +00002301 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2302 // external symbols most go through the PLT in PIC mode. If the symbol
2303 // has hidden or protected visibility, or if it is static or local, then
2304 // we don't need to use the PLT - we can directly call it.
2305 if (Subtarget->isTargetELF() &&
2306 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002307 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002308 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002309 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002310 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002311 (!Subtarget->getTargetTriple().isMacOSX() ||
2312 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002313 // PC-relative references to external symbols should go through $stub,
2314 // unless we're building with the leopard linker or later, which
2315 // automatically synthesizes these stubs.
2316 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002317 } else if (Subtarget->isPICStyleRIPRel() &&
2318 isa<Function>(GV) &&
2319 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2320 // If the function is marked as non-lazy, generate an indirect call
2321 // which loads from the GOT directly. This avoids runtime overhead
2322 // at the cost of eager binding (and one extra byte of encoding).
2323 OpFlags = X86II::MO_GOTPCREL;
2324 WrapperKind = X86ISD::WrapperRIP;
2325 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002326 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002327
Devang Patel0d881da2010-07-06 22:08:15 +00002328 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002329 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002330
2331 // Add a wrapper if needed.
2332 if (WrapperKind != ISD::DELETED_NODE)
2333 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2334 // Add extra indirection if needed.
2335 if (ExtraLoad)
2336 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2337 MachinePointerInfo::getGOT(),
2338 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002339 }
Bill Wendling056292f2008-09-16 21:48:12 +00002340 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002341 unsigned char OpFlags = 0;
2342
Evan Cheng1bf891a2010-12-01 22:59:46 +00002343 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2344 // external symbols should go through the PLT.
2345 if (Subtarget->isTargetELF() &&
2346 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2347 OpFlags = X86II::MO_PLT;
2348 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002349 (!Subtarget->getTargetTriple().isMacOSX() ||
2350 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002351 // PC-relative references to external symbols should go through $stub,
2352 // unless we're building with the leopard linker or later, which
2353 // automatically synthesizes these stubs.
2354 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002355 }
Eric Christopherfd179292009-08-27 18:07:15 +00002356
Chris Lattner48a7d022009-07-09 05:02:21 +00002357 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2358 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002359 }
2360
Chris Lattnerd96d0722007-02-25 06:40:16 +00002361 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002362 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002363 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002364
Evan Chengf22f9b32010-02-06 03:28:46 +00002365 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002366 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2367 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002368 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002369 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002370
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002371 Ops.push_back(Chain);
2372 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002373
Dan Gohman98ca4f22009-08-05 01:29:28 +00002374 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002375 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002376
Gordon Henriksen86737662008-01-05 16:56:59 +00002377 // Add argument registers to the end of the list so that they are known live
2378 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002379 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2380 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2381 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002382
Evan Cheng586ccac2008-03-18 23:36:35 +00002383 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002384 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002385 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2386
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002387 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002388 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002389 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002390
Gabor Greifba36cb52008-08-28 21:40:38 +00002391 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002392 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002393
Dan Gohman98ca4f22009-08-05 01:29:28 +00002394 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002395 // We used to do:
2396 //// If this is the first return lowered for this function, add the regs
2397 //// to the liveout set for the function.
2398 // This isn't right, although it's probably harmless on x86; liveouts
2399 // should be computed from returns not tail calls. Consider a void
2400 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002401 return DAG.getNode(X86ISD::TC_RETURN, dl,
2402 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002403 }
2404
Dale Johannesenace16102009-02-03 19:33:06 +00002405 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002406 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002407
Chris Lattner2d297092006-05-23 18:50:38 +00002408 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002409 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002410 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002411 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002412 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002413 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002414 // pops the hidden struct pointer, so we have to push it back.
2415 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002416 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002417 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002418 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002419
Gordon Henriksenae636f82008-01-03 16:47:34 +00002420 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002421 if (!IsSibcall) {
2422 Chain = DAG.getCALLSEQ_END(Chain,
2423 DAG.getIntPtrConstant(NumBytes, true),
2424 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2425 true),
2426 InFlag);
2427 InFlag = Chain.getValue(1);
2428 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002429
Chris Lattner3085e152007-02-25 08:59:22 +00002430 // Handle result values, copying them out of physregs into vregs that we
2431 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002432 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2433 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002434}
2435
Evan Cheng25ab6902006-09-08 06:48:29 +00002436
2437//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002438// Fast Calling Convention (tail call) implementation
2439//===----------------------------------------------------------------------===//
2440
2441// Like std call, callee cleans arguments, convention except that ECX is
2442// reserved for storing the tail called function address. Only 2 registers are
2443// free for argument passing (inreg). Tail call optimization is performed
2444// provided:
2445// * tailcallopt is enabled
2446// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002447// On X86_64 architecture with GOT-style position independent code only local
2448// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002449// To keep the stack aligned according to platform abi the function
2450// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2451// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002452// If a tail called function callee has more arguments than the caller the
2453// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002454// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002455// original REtADDR, but before the saved framepointer or the spilled registers
2456// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2457// stack layout:
2458// arg1
2459// arg2
2460// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002461// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002462// move area ]
2463// (possible EBP)
2464// ESI
2465// EDI
2466// local1 ..
2467
2468/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2469/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002470unsigned
2471X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2472 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002473 MachineFunction &MF = DAG.getMachineFunction();
2474 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002475 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002476 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002477 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002478 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002479 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002480 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2481 // Number smaller than 12 so just add the difference.
2482 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2483 } else {
2484 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002485 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002486 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002487 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002488 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002489}
2490
Evan Cheng5f941932010-02-05 02:21:12 +00002491/// MatchingStackOffset - Return true if the given stack call argument is
2492/// already available in the same position (relatively) of the caller's
2493/// incoming argument stack.
2494static
2495bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2496 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2497 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002498 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2499 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002500 if (Arg.getOpcode() == ISD::CopyFromReg) {
2501 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002502 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002503 return false;
2504 MachineInstr *Def = MRI->getVRegDef(VR);
2505 if (!Def)
2506 return false;
2507 if (!Flags.isByVal()) {
2508 if (!TII->isLoadFromStackSlot(Def, FI))
2509 return false;
2510 } else {
2511 unsigned Opcode = Def->getOpcode();
2512 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2513 Def->getOperand(1).isFI()) {
2514 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002515 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002516 } else
2517 return false;
2518 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002519 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2520 if (Flags.isByVal())
2521 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002522 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002523 // define @foo(%struct.X* %A) {
2524 // tail call @bar(%struct.X* byval %A)
2525 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002526 return false;
2527 SDValue Ptr = Ld->getBasePtr();
2528 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2529 if (!FINode)
2530 return false;
2531 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002532 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002533 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002534 FI = FINode->getIndex();
2535 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002536 } else
2537 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002538
Evan Cheng4cae1332010-03-05 08:38:04 +00002539 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002540 if (!MFI->isFixedObjectIndex(FI))
2541 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002542 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002543}
2544
Dan Gohman98ca4f22009-08-05 01:29:28 +00002545/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2546/// for tail call optimization. Targets which want to do tail call
2547/// optimization should implement this function.
2548bool
2549X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002550 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002551 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002552 bool isCalleeStructRet,
2553 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002554 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002555 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002556 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002557 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002558 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002559 CalleeCC != CallingConv::C)
2560 return false;
2561
Evan Cheng7096ae42010-01-29 06:45:59 +00002562 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002563 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002564 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002565 CallingConv::ID CallerCC = CallerF->getCallingConv();
2566 bool CCMatch = CallerCC == CalleeCC;
2567
Dan Gohman1797ed52010-02-08 20:27:50 +00002568 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002569 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002570 return true;
2571 return false;
2572 }
2573
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002574 // Look for obvious safe cases to perform tail call optimization that do not
2575 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002576
Evan Cheng2c12cb42010-03-26 16:26:03 +00002577 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2578 // emit a special epilogue.
2579 if (RegInfo->needsStackRealignment(MF))
2580 return false;
2581
Evan Chenga375d472010-03-15 18:54:48 +00002582 // Also avoid sibcall optimization if either caller or callee uses struct
2583 // return semantics.
2584 if (isCalleeStructRet || isCallerStructRet)
2585 return false;
2586
Chad Rosier2416da32011-06-24 21:15:36 +00002587 // An stdcall caller is expected to clean up its arguments; the callee
2588 // isn't going to do that.
2589 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2590 return false;
2591
Chad Rosier871f6642011-05-18 19:59:50 +00002592 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002593 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002594 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002595
2596 // Optimizing for varargs on Win64 is unlikely to be safe without
2597 // additional testing.
2598 if (Subtarget->isTargetWin64())
2599 return false;
2600
Chad Rosier871f6642011-05-18 19:59:50 +00002601 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002602 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2603 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002604
Chad Rosier871f6642011-05-18 19:59:50 +00002605 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2606 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2607 if (!ArgLocs[i].isRegLoc())
2608 return false;
2609 }
2610
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002611 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2612 // Therefore if it's not used by the call it is not safe to optimize this into
2613 // a sibcall.
2614 bool Unused = false;
2615 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2616 if (!Ins[i].Used) {
2617 Unused = true;
2618 break;
2619 }
2620 }
2621 if (Unused) {
2622 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002623 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2624 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002625 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002626 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002627 CCValAssign &VA = RVLocs[i];
2628 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2629 return false;
2630 }
2631 }
2632
Evan Cheng13617962010-04-30 01:12:32 +00002633 // If the calling conventions do not match, then we'd better make sure the
2634 // results are returned in the same way as what the caller expects.
2635 if (!CCMatch) {
2636 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002637 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2638 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002639 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2640
2641 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002642 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2643 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002644 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2645
2646 if (RVLocs1.size() != RVLocs2.size())
2647 return false;
2648 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2649 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2650 return false;
2651 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2652 return false;
2653 if (RVLocs1[i].isRegLoc()) {
2654 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2655 return false;
2656 } else {
2657 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2658 return false;
2659 }
2660 }
2661 }
2662
Evan Chenga6bff982010-01-30 01:22:00 +00002663 // If the callee takes no arguments then go on to check the results of the
2664 // call.
2665 if (!Outs.empty()) {
2666 // Check if stack adjustment is needed. For now, do not do this if any
2667 // argument is passed on the stack.
2668 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002669 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2670 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002671
2672 // Allocate shadow area for Win64
2673 if (Subtarget->isTargetWin64()) {
2674 CCInfo.AllocateStack(32, 8);
2675 }
2676
Duncan Sands45907662010-10-31 13:21:44 +00002677 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002678 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002679 MachineFunction &MF = DAG.getMachineFunction();
2680 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2681 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002682
2683 // Check if the arguments are already laid out in the right way as
2684 // the caller's fixed stack objects.
2685 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002686 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2687 const X86InstrInfo *TII =
2688 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002689 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2690 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002691 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002692 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002693 if (VA.getLocInfo() == CCValAssign::Indirect)
2694 return false;
2695 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002696 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2697 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002698 return false;
2699 }
2700 }
2701 }
Evan Cheng9c044672010-05-29 01:35:22 +00002702
2703 // If the tailcall address may be in a register, then make sure it's
2704 // possible to register allocate for it. In 32-bit, the call address can
2705 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002706 // callee-saved registers are restored. These happen to be the same
2707 // registers used to pass 'inreg' arguments so watch out for those.
2708 if (!Subtarget->is64Bit() &&
2709 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002710 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002711 unsigned NumInRegs = 0;
2712 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2713 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002714 if (!VA.isRegLoc())
2715 continue;
2716 unsigned Reg = VA.getLocReg();
2717 switch (Reg) {
2718 default: break;
2719 case X86::EAX: case X86::EDX: case X86::ECX:
2720 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002721 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002722 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002723 }
2724 }
2725 }
Evan Chenga6bff982010-01-30 01:22:00 +00002726 }
Evan Chengb1712452010-01-27 06:25:16 +00002727
Evan Cheng86809cc2010-02-03 03:28:02 +00002728 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002729}
2730
Dan Gohman3df24e62008-09-03 23:12:08 +00002731FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002732X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2733 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002734}
2735
2736
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002737//===----------------------------------------------------------------------===//
2738// Other Lowering Hooks
2739//===----------------------------------------------------------------------===//
2740
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002741static bool MayFoldLoad(SDValue Op) {
2742 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2743}
2744
2745static bool MayFoldIntoStore(SDValue Op) {
2746 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2747}
2748
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002749static bool isTargetShuffle(unsigned Opcode) {
2750 switch(Opcode) {
2751 default: return false;
2752 case X86ISD::PSHUFD:
2753 case X86ISD::PSHUFHW:
2754 case X86ISD::PSHUFLW:
2755 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002756 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002757 case X86ISD::SHUFPS:
2758 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002759 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002760 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002761 case X86ISD::MOVLPS:
2762 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002763 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002764 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002765 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002766 case X86ISD::MOVSS:
2767 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002768 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002769 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002770 case X86ISD::VUNPCKLPSY:
2771 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002772 case X86ISD::PUNPCKLWD:
2773 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002774 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002775 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002776 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002777 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002778 case X86ISD::VUNPCKHPSY:
2779 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002780 case X86ISD::PUNPCKHWD:
2781 case X86ISD::PUNPCKHBW:
2782 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002783 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002784 case X86ISD::VPERMILPS:
2785 case X86ISD::VPERMILPSY:
2786 case X86ISD::VPERMILPD:
2787 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002788 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002789 return true;
2790 }
2791 return false;
2792}
2793
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002794static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002795 SDValue V1, SelectionDAG &DAG) {
2796 switch(Opc) {
2797 default: llvm_unreachable("Unknown x86 shuffle node");
2798 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002799 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002800 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002801 return DAG.getNode(Opc, dl, VT, V1);
2802 }
2803
2804 return SDValue();
2805}
2806
2807static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002808 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002809 switch(Opc) {
2810 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002811 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002812 case X86ISD::PSHUFHW:
2813 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002814 case X86ISD::VPERMILPS:
2815 case X86ISD::VPERMILPSY:
2816 case X86ISD::VPERMILPD:
2817 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002818 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2819 }
2820
2821 return SDValue();
2822}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002823
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002824static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2825 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2826 switch(Opc) {
2827 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002828 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002829 case X86ISD::SHUFPD:
2830 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002831 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002832 return DAG.getNode(Opc, dl, VT, V1, V2,
2833 DAG.getConstant(TargetMask, MVT::i8));
2834 }
2835 return SDValue();
2836}
2837
2838static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2839 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2840 switch(Opc) {
2841 default: llvm_unreachable("Unknown x86 shuffle node");
2842 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002843 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002844 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002845 case X86ISD::MOVLPS:
2846 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002847 case X86ISD::MOVSS:
2848 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002849 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002850 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002851 case X86ISD::VUNPCKLPSY:
2852 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002853 case X86ISD::PUNPCKLWD:
2854 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002855 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002856 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002857 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002858 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002859 case X86ISD::VUNPCKHPSY:
2860 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002861 case X86ISD::PUNPCKHWD:
2862 case X86ISD::PUNPCKHBW:
2863 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002864 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002865 return DAG.getNode(Opc, dl, VT, V1, V2);
2866 }
2867 return SDValue();
2868}
2869
Dan Gohmand858e902010-04-17 15:26:15 +00002870SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002871 MachineFunction &MF = DAG.getMachineFunction();
2872 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2873 int ReturnAddrIndex = FuncInfo->getRAIndex();
2874
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002875 if (ReturnAddrIndex == 0) {
2876 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002877 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002878 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002879 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002880 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002881 }
2882
Evan Cheng25ab6902006-09-08 06:48:29 +00002883 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002884}
2885
2886
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002887bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2888 bool hasSymbolicDisplacement) {
2889 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002890 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002891 return false;
2892
2893 // If we don't have a symbolic displacement - we don't have any extra
2894 // restrictions.
2895 if (!hasSymbolicDisplacement)
2896 return true;
2897
2898 // FIXME: Some tweaks might be needed for medium code model.
2899 if (M != CodeModel::Small && M != CodeModel::Kernel)
2900 return false;
2901
2902 // For small code model we assume that latest object is 16MB before end of 31
2903 // bits boundary. We may also accept pretty large negative constants knowing
2904 // that all objects are in the positive half of address space.
2905 if (M == CodeModel::Small && Offset < 16*1024*1024)
2906 return true;
2907
2908 // For kernel code model we know that all object resist in the negative half
2909 // of 32bits address space. We may not accept negative offsets, since they may
2910 // be just off and we may accept pretty large positive ones.
2911 if (M == CodeModel::Kernel && Offset > 0)
2912 return true;
2913
2914 return false;
2915}
2916
Evan Chengef41ff62011-06-23 17:54:54 +00002917/// isCalleePop - Determines whether the callee is required to pop its
2918/// own arguments. Callee pop is necessary to support tail calls.
2919bool X86::isCalleePop(CallingConv::ID CallingConv,
2920 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2921 if (IsVarArg)
2922 return false;
2923
2924 switch (CallingConv) {
2925 default:
2926 return false;
2927 case CallingConv::X86_StdCall:
2928 return !is64Bit;
2929 case CallingConv::X86_FastCall:
2930 return !is64Bit;
2931 case CallingConv::X86_ThisCall:
2932 return !is64Bit;
2933 case CallingConv::Fast:
2934 return TailCallOpt;
2935 case CallingConv::GHC:
2936 return TailCallOpt;
2937 }
2938}
2939
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002940/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2941/// specific condition code, returning the condition code and the LHS/RHS of the
2942/// comparison to make.
2943static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2944 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002945 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002946 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2947 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2948 // X > -1 -> X == 0, jump !sign.
2949 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002950 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002951 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2952 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002953 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002954 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002955 // X < 1 -> X <= 0
2956 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002957 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002958 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002959 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002960
Evan Chengd9558e02006-01-06 00:43:03 +00002961 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002962 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002963 case ISD::SETEQ: return X86::COND_E;
2964 case ISD::SETGT: return X86::COND_G;
2965 case ISD::SETGE: return X86::COND_GE;
2966 case ISD::SETLT: return X86::COND_L;
2967 case ISD::SETLE: return X86::COND_LE;
2968 case ISD::SETNE: return X86::COND_NE;
2969 case ISD::SETULT: return X86::COND_B;
2970 case ISD::SETUGT: return X86::COND_A;
2971 case ISD::SETULE: return X86::COND_BE;
2972 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002973 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002974 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002975
Chris Lattner4c78e022008-12-23 23:42:27 +00002976 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002977
Chris Lattner4c78e022008-12-23 23:42:27 +00002978 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002979 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2980 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002981 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2982 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002983 }
2984
Chris Lattner4c78e022008-12-23 23:42:27 +00002985 switch (SetCCOpcode) {
2986 default: break;
2987 case ISD::SETOLT:
2988 case ISD::SETOLE:
2989 case ISD::SETUGT:
2990 case ISD::SETUGE:
2991 std::swap(LHS, RHS);
2992 break;
2993 }
2994
2995 // On a floating point condition, the flags are set as follows:
2996 // ZF PF CF op
2997 // 0 | 0 | 0 | X > Y
2998 // 0 | 0 | 1 | X < Y
2999 // 1 | 0 | 0 | X == Y
3000 // 1 | 1 | 1 | unordered
3001 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003002 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003003 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003004 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003005 case ISD::SETOLT: // flipped
3006 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003007 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003008 case ISD::SETOLE: // flipped
3009 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003010 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003011 case ISD::SETUGT: // flipped
3012 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003013 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003014 case ISD::SETUGE: // flipped
3015 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003016 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003017 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003018 case ISD::SETNE: return X86::COND_NE;
3019 case ISD::SETUO: return X86::COND_P;
3020 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003021 case ISD::SETOEQ:
3022 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003023 }
Evan Chengd9558e02006-01-06 00:43:03 +00003024}
3025
Evan Cheng4a460802006-01-11 00:33:36 +00003026/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3027/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003028/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003029static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003030 switch (X86CC) {
3031 default:
3032 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003033 case X86::COND_B:
3034 case X86::COND_BE:
3035 case X86::COND_E:
3036 case X86::COND_P:
3037 case X86::COND_A:
3038 case X86::COND_AE:
3039 case X86::COND_NE:
3040 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003041 return true;
3042 }
3043}
3044
Evan Chengeb2f9692009-10-27 19:56:55 +00003045/// isFPImmLegal - Returns true if the target can instruction select the
3046/// specified FP immediate natively. If false, the legalizer will
3047/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003048bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003049 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3050 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3051 return true;
3052 }
3053 return false;
3054}
3055
Nate Begeman9008ca62009-04-27 18:41:29 +00003056/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3057/// the specified range (L, H].
3058static bool isUndefOrInRange(int Val, int Low, int Hi) {
3059 return (Val < 0) || (Val >= Low && Val < Hi);
3060}
3061
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003062/// isUndefOrInRange - Return true if every element in Mask, begining
3063/// from position Pos and ending in Pos+Size, falls within the specified
3064/// range (L, L+Pos]. or is undef.
3065static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3066 int Pos, int Size, int Low, int Hi) {
3067 for (int i = Pos, e = Pos+Size; i != e; ++i)
3068 if (!isUndefOrInRange(Mask[i], Low, Hi))
3069 return false;
3070 return true;
3071}
3072
Nate Begeman9008ca62009-04-27 18:41:29 +00003073/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3074/// specified value.
3075static bool isUndefOrEqual(int Val, int CmpVal) {
3076 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003077 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003078 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003079}
3080
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003081/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3082/// from position Pos and ending in Pos+Size, falls within the specified
3083/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003084static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3085 int Pos, int Size, int Low) {
3086 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3087 if (!isUndefOrEqual(Mask[i], Low))
3088 return false;
3089 return true;
3090}
3091
Nate Begeman9008ca62009-04-27 18:41:29 +00003092/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3093/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3094/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003095static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003096 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003097 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003098 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003099 return (Mask[0] < 2 && Mask[1] < 2);
3100 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003101}
3102
Nate Begeman9008ca62009-04-27 18:41:29 +00003103bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003104 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003105 N->getMask(M);
3106 return ::isPSHUFDMask(M, N->getValueType(0));
3107}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003108
Nate Begeman9008ca62009-04-27 18:41:29 +00003109/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3110/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003111static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003112 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003113 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003114
Nate Begeman9008ca62009-04-27 18:41:29 +00003115 // Lower quadword copied in order or undef.
3116 for (int i = 0; i != 4; ++i)
3117 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003118 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003119
Evan Cheng506d3df2006-03-29 23:07:14 +00003120 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003121 for (int i = 4; i != 8; ++i)
3122 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003123 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003124
Evan Cheng506d3df2006-03-29 23:07:14 +00003125 return true;
3126}
3127
Nate Begeman9008ca62009-04-27 18:41:29 +00003128bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003129 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003130 N->getMask(M);
3131 return ::isPSHUFHWMask(M, N->getValueType(0));
3132}
Evan Cheng506d3df2006-03-29 23:07:14 +00003133
Nate Begeman9008ca62009-04-27 18:41:29 +00003134/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3135/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003136static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003138 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003139
Rafael Espindola15684b22009-04-24 12:40:33 +00003140 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003141 for (int i = 4; i != 8; ++i)
3142 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003143 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003144
Rafael Espindola15684b22009-04-24 12:40:33 +00003145 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003146 for (int i = 0; i != 4; ++i)
3147 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003148 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003149
Rafael Espindola15684b22009-04-24 12:40:33 +00003150 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003151}
3152
Nate Begeman9008ca62009-04-27 18:41:29 +00003153bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003154 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 N->getMask(M);
3156 return ::isPSHUFLWMask(M, N->getValueType(0));
3157}
3158
Nate Begemana09008b2009-10-19 02:17:23 +00003159/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3160/// is suitable for input to PALIGNR.
3161static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3162 bool hasSSSE3) {
3163 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003164 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3165 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003166
Nate Begemana09008b2009-10-19 02:17:23 +00003167 // Do not handle v2i64 / v2f64 shuffles with palignr.
3168 if (e < 4 || !hasSSSE3)
3169 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003170
Nate Begemana09008b2009-10-19 02:17:23 +00003171 for (i = 0; i != e; ++i)
3172 if (Mask[i] >= 0)
3173 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003174
Nate Begemana09008b2009-10-19 02:17:23 +00003175 // All undef, not a palignr.
3176 if (i == e)
3177 return false;
3178
Eli Friedman63f8dde2011-07-25 21:36:45 +00003179 // Make sure we're shifting in the right direction.
3180 if (Mask[i] <= i)
3181 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003182
3183 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003184
Nate Begemana09008b2009-10-19 02:17:23 +00003185 // Check the rest of the elements to see if they are consecutive.
3186 for (++i; i != e; ++i) {
3187 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003188 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003189 return false;
3190 }
3191 return true;
3192}
3193
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003194/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3195/// specifies a shuffle of elements that is suitable for input to 256-bit
3196/// VSHUFPSY.
3197static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3198 const X86Subtarget *Subtarget) {
3199 int NumElems = VT.getVectorNumElements();
3200
3201 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3202 return false;
3203
3204 if (NumElems != 8)
3205 return false;
3206
3207 // VSHUFPSY divides the resulting vector into 4 chunks.
3208 // The sources are also splitted into 4 chunks, and each destination
3209 // chunk must come from a different source chunk.
3210 //
3211 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3212 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3213 //
3214 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3215 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3216 //
3217 int QuarterSize = NumElems/4;
3218 int HalfSize = QuarterSize*2;
3219 for (int i = 0; i < QuarterSize; ++i)
3220 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3221 return false;
3222 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3223 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3224 return false;
3225
3226 // The mask of the second half must be the same as the first but with
3227 // the appropriate offsets. This works in the same way as VPERMILPS
3228 // works with masks.
3229 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3230 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3231 return false;
3232 int FstHalfIdx = i-HalfSize;
3233 if (Mask[FstHalfIdx] < 0)
3234 continue;
3235 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3236 return false;
3237 }
3238 for (int i = QuarterSize*3; i < NumElems; ++i) {
3239 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3240 return false;
3241 int FstHalfIdx = i-HalfSize;
3242 if (Mask[FstHalfIdx] < 0)
3243 continue;
3244 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3245 return false;
3246
3247 }
3248
3249 return true;
3250}
3251
3252/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3253/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3254static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3255 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3256 EVT VT = SVOp->getValueType(0);
3257 int NumElems = VT.getVectorNumElements();
3258
3259 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3260 "Only supports v8i32 and v8f32 types");
3261
3262 int HalfSize = NumElems/2;
3263 unsigned Mask = 0;
3264 for (int i = 0; i != NumElems ; ++i) {
3265 if (SVOp->getMaskElt(i) < 0)
3266 continue;
3267 // The mask of the first half must be equal to the second one.
3268 unsigned Shamt = (i%HalfSize)*2;
3269 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3270 Mask |= Elt << Shamt;
3271 }
3272
3273 return Mask;
3274}
3275
3276/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3277/// specifies a shuffle of elements that is suitable for input to 256-bit
3278/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3279/// version and the mask of the second half isn't binded with the first
3280/// one.
3281static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3282 const X86Subtarget *Subtarget) {
3283 int NumElems = VT.getVectorNumElements();
3284
3285 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3286 return false;
3287
3288 if (NumElems != 4)
3289 return false;
3290
3291 // VSHUFPSY divides the resulting vector into 4 chunks.
3292 // The sources are also splitted into 4 chunks, and each destination
3293 // chunk must come from a different source chunk.
3294 //
3295 // SRC1 => X3 X2 X1 X0
3296 // SRC2 => Y3 Y2 Y1 Y0
3297 //
3298 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3299 //
3300 int QuarterSize = NumElems/4;
3301 int HalfSize = QuarterSize*2;
3302 for (int i = 0; i < QuarterSize; ++i)
3303 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3304 return false;
3305 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3306 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3307 return false;
3308 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3309 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3310 return false;
3311 for (int i = QuarterSize*3; i < NumElems; ++i)
3312 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3313 return false;
3314
3315 return true;
3316}
3317
3318/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3319/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3320static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3321 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3322 EVT VT = SVOp->getValueType(0);
3323 int NumElems = VT.getVectorNumElements();
3324
3325 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3326 "Only supports v4i64 and v4f64 types");
3327
3328 int HalfSize = NumElems/2;
3329 unsigned Mask = 0;
3330 for (int i = 0; i != NumElems ; ++i) {
3331 if (SVOp->getMaskElt(i) < 0)
3332 continue;
3333 int Elt = SVOp->getMaskElt(i) % HalfSize;
3334 Mask |= Elt << i;
3335 }
3336
3337 return Mask;
3338}
3339
Evan Cheng14aed5e2006-03-24 01:18:28 +00003340/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003341/// specifies a shuffle of elements that is suitable for input to 128-bit
3342/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003343static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003345
3346 if (VT.getSizeInBits() != 128)
3347 return false;
3348
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 if (NumElems != 2 && NumElems != 4)
3350 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003351
Nate Begeman9008ca62009-04-27 18:41:29 +00003352 int Half = NumElems / 2;
3353 for (int i = 0; i < Half; ++i)
3354 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003355 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003356 for (int i = Half; i < NumElems; ++i)
3357 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003358 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003359
Evan Cheng14aed5e2006-03-24 01:18:28 +00003360 return true;
3361}
3362
Nate Begeman9008ca62009-04-27 18:41:29 +00003363bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3364 SmallVector<int, 8> M;
3365 N->getMask(M);
3366 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003367}
3368
Evan Cheng213d2cf2007-05-17 18:45:50 +00003369/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003370/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3371/// half elements to come from vector 1 (which would equal the dest.) and
3372/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003373static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003375
3376 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003378
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 int Half = NumElems / 2;
3380 for (int i = 0; i < Half; ++i)
3381 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003382 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003383 for (int i = Half; i < NumElems; ++i)
3384 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003385 return false;
3386 return true;
3387}
3388
Nate Begeman9008ca62009-04-27 18:41:29 +00003389static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3390 SmallVector<int, 8> M;
3391 N->getMask(M);
3392 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003393}
3394
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003395/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3396/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003397bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003398 EVT VT = N->getValueType(0);
3399 unsigned NumElems = VT.getVectorNumElements();
3400
3401 if (VT.getSizeInBits() != 128)
3402 return false;
3403
3404 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003405 return false;
3406
Evan Cheng2064a2b2006-03-28 06:50:32 +00003407 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003408 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3409 isUndefOrEqual(N->getMaskElt(1), 7) &&
3410 isUndefOrEqual(N->getMaskElt(2), 2) &&
3411 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003412}
3413
Nate Begeman0b10b912009-11-07 23:17:15 +00003414/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3415/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3416/// <2, 3, 2, 3>
3417bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003418 EVT VT = N->getValueType(0);
3419 unsigned NumElems = VT.getVectorNumElements();
3420
3421 if (VT.getSizeInBits() != 128)
3422 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003423
Nate Begeman0b10b912009-11-07 23:17:15 +00003424 if (NumElems != 4)
3425 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003426
Nate Begeman0b10b912009-11-07 23:17:15 +00003427 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003428 isUndefOrEqual(N->getMaskElt(1), 3) &&
3429 isUndefOrEqual(N->getMaskElt(2), 2) &&
3430 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003431}
3432
Evan Cheng5ced1d82006-04-06 23:23:56 +00003433/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3434/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003435bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3436 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003437
Evan Cheng5ced1d82006-04-06 23:23:56 +00003438 if (NumElems != 2 && NumElems != 4)
3439 return false;
3440
Evan Chengc5cdff22006-04-07 21:53:05 +00003441 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003443 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003444
Evan Chengc5cdff22006-04-07 21:53:05 +00003445 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003447 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003448
3449 return true;
3450}
3451
Nate Begeman0b10b912009-11-07 23:17:15 +00003452/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3453/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3454bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003455 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003456
David Greenea20244d2011-03-02 17:23:43 +00003457 if ((NumElems != 2 && NumElems != 4)
3458 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003459 return false;
3460
Evan Chengc5cdff22006-04-07 21:53:05 +00003461 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003463 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003464
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 for (unsigned i = 0; i < NumElems/2; ++i)
3466 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003467 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003468
3469 return true;
3470}
3471
Evan Cheng0038e592006-03-28 00:39:58 +00003472/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3473/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003474static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003475 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003477
3478 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3479 "Unsupported vector type for unpckh");
3480
3481 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003482 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003483
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003484 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3485 // independently on 128-bit lanes.
3486 unsigned NumLanes = VT.getSizeInBits()/128;
3487 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003488
3489 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003490 unsigned End = NumLaneElts;
3491 for (unsigned s = 0; s < NumLanes; ++s) {
3492 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003493 i != End;
3494 i += 2, ++j) {
3495 int BitI = Mask[i];
3496 int BitI1 = Mask[i+1];
3497 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003498 return false;
David Greenea20244d2011-03-02 17:23:43 +00003499 if (V2IsSplat) {
3500 if (!isUndefOrEqual(BitI1, NumElts))
3501 return false;
3502 } else {
3503 if (!isUndefOrEqual(BitI1, j + NumElts))
3504 return false;
3505 }
Evan Cheng39623da2006-04-20 08:58:49 +00003506 }
David Greenea20244d2011-03-02 17:23:43 +00003507 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003508 Start += NumLaneElts;
3509 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003510 }
David Greenea20244d2011-03-02 17:23:43 +00003511
Evan Cheng0038e592006-03-28 00:39:58 +00003512 return true;
3513}
3514
Nate Begeman9008ca62009-04-27 18:41:29 +00003515bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3516 SmallVector<int, 8> M;
3517 N->getMask(M);
3518 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003519}
3520
Evan Cheng4fcb9222006-03-28 02:43:26 +00003521/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3522/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003523static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003524 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003525 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003526
3527 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3528 "Unsupported vector type for unpckh");
3529
3530 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003531 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003532
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003533 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3534 // independently on 128-bit lanes.
3535 unsigned NumLanes = VT.getSizeInBits()/128;
3536 unsigned NumLaneElts = NumElts/NumLanes;
3537
3538 unsigned Start = 0;
3539 unsigned End = NumLaneElts;
3540 for (unsigned l = 0; l != NumLanes; ++l) {
3541 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3542 i != End; i += 2, ++j) {
3543 int BitI = Mask[i];
3544 int BitI1 = Mask[i+1];
3545 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003546 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003547 if (V2IsSplat) {
3548 if (isUndefOrEqual(BitI1, NumElts))
3549 return false;
3550 } else {
3551 if (!isUndefOrEqual(BitI1, j+NumElts))
3552 return false;
3553 }
Evan Cheng39623da2006-04-20 08:58:49 +00003554 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003555 // Process the next 128 bits.
3556 Start += NumLaneElts;
3557 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003558 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003559 return true;
3560}
3561
Nate Begeman9008ca62009-04-27 18:41:29 +00003562bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3563 SmallVector<int, 8> M;
3564 N->getMask(M);
3565 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003566}
3567
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003568/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3569/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3570/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003571static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003572 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003573 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003574 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003575
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003576 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3577 // FIXME: Need a better way to get rid of this, there's no latency difference
3578 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3579 // the former later. We should also remove the "_undef" special mask.
3580 if (NumElems == 4 && VT.getSizeInBits() == 256)
3581 return false;
3582
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003583 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3584 // independently on 128-bit lanes.
3585 unsigned NumLanes = VT.getSizeInBits() / 128;
3586 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003587
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003588 for (unsigned s = 0; s < NumLanes; ++s) {
3589 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3590 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003591 i += 2, ++j) {
3592 int BitI = Mask[i];
3593 int BitI1 = Mask[i+1];
3594
3595 if (!isUndefOrEqual(BitI, j))
3596 return false;
3597 if (!isUndefOrEqual(BitI1, j))
3598 return false;
3599 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003600 }
David Greenea20244d2011-03-02 17:23:43 +00003601
Rafael Espindola15684b22009-04-24 12:40:33 +00003602 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003603}
3604
Nate Begeman9008ca62009-04-27 18:41:29 +00003605bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3606 SmallVector<int, 8> M;
3607 N->getMask(M);
3608 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3609}
3610
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003611/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3612/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3613/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003614static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003615 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003616 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3617 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003618
Nate Begeman9008ca62009-04-27 18:41:29 +00003619 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3620 int BitI = Mask[i];
3621 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003622 if (!isUndefOrEqual(BitI, j))
3623 return false;
3624 if (!isUndefOrEqual(BitI1, j))
3625 return false;
3626 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003627 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003628}
3629
Nate Begeman9008ca62009-04-27 18:41:29 +00003630bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3631 SmallVector<int, 8> M;
3632 N->getMask(M);
3633 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3634}
3635
Evan Cheng017dcc62006-04-21 01:05:10 +00003636/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3637/// specifies a shuffle of elements that is suitable for input to MOVSS,
3638/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003639static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003640 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003641 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003642
3643 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003644
Nate Begeman9008ca62009-04-27 18:41:29 +00003645 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003646 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003647
Nate Begeman9008ca62009-04-27 18:41:29 +00003648 for (int i = 1; i < NumElts; ++i)
3649 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003650 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003651
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003652 return true;
3653}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003654
Nate Begeman9008ca62009-04-27 18:41:29 +00003655bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3656 SmallVector<int, 8> M;
3657 N->getMask(M);
3658 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003659}
3660
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003661/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3662/// as permutations between 128-bit chunks or halves. As an example: this
3663/// shuffle bellow:
3664/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3665/// The first half comes from the second half of V1 and the second half from the
3666/// the second half of V2.
3667static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3668 const X86Subtarget *Subtarget) {
3669 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3670 return false;
3671
3672 // The shuffle result is divided into half A and half B. In total the two
3673 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3674 // B must come from C, D, E or F.
3675 int HalfSize = VT.getVectorNumElements()/2;
3676 bool MatchA = false, MatchB = false;
3677
3678 // Check if A comes from one of C, D, E, F.
3679 for (int Half = 0; Half < 4; ++Half) {
3680 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3681 MatchA = true;
3682 break;
3683 }
3684 }
3685
3686 // Check if B comes from one of C, D, E, F.
3687 for (int Half = 0; Half < 4; ++Half) {
3688 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3689 MatchB = true;
3690 break;
3691 }
3692 }
3693
3694 return MatchA && MatchB;
3695}
3696
3697/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3698/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3699static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3700 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3701 EVT VT = SVOp->getValueType(0);
3702
3703 int HalfSize = VT.getVectorNumElements()/2;
3704
3705 int FstHalf = 0, SndHalf = 0;
3706 for (int i = 0; i < HalfSize; ++i) {
3707 if (SVOp->getMaskElt(i) > 0) {
3708 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3709 break;
3710 }
3711 }
3712 for (int i = HalfSize; i < HalfSize*2; ++i) {
3713 if (SVOp->getMaskElt(i) > 0) {
3714 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3715 break;
3716 }
3717 }
3718
3719 return (FstHalf | (SndHalf << 4));
3720}
3721
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003722/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3723/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3724/// Note that VPERMIL mask matching is different depending whether theunderlying
3725/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3726/// to the same elements of the low, but to the higher half of the source.
3727/// In VPERMILPD the two lanes could be shuffled independently of each other
3728/// with the same restriction that lanes can't be crossed.
3729static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3730 const X86Subtarget *Subtarget) {
3731 int NumElts = VT.getVectorNumElements();
3732 int NumLanes = VT.getSizeInBits()/128;
3733
3734 if (!Subtarget->hasAVX())
3735 return false;
3736
3737 // Match any permutation of 128-bit vector with 64-bit types
3738 if (NumLanes == 1 && NumElts != 2)
3739 return false;
3740
3741 // Only match 256-bit with 32 types
3742 if (VT.getSizeInBits() == 256 && NumElts != 4)
3743 return false;
3744
3745 // The mask on the high lane is independent of the low. Both can match
3746 // any element in inside its own lane, but can't cross.
3747 int LaneSize = NumElts/NumLanes;
3748 for (int l = 0; l < NumLanes; ++l)
3749 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3750 int LaneStart = l*LaneSize;
3751 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3752 return false;
3753 }
3754
3755 return true;
3756}
3757
3758/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3759/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3760/// Note that VPERMIL mask matching is different depending whether theunderlying
3761/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3762/// to the same elements of the low, but to the higher half of the source.
3763/// In VPERMILPD the two lanes could be shuffled independently of each other
3764/// with the same restriction that lanes can't be crossed.
3765static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3766 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003767 unsigned NumElts = VT.getVectorNumElements();
3768 unsigned NumLanes = VT.getSizeInBits()/128;
3769
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003770 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003771 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003772
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003773 // Match any permutation of 128-bit vector with 32-bit types
3774 if (NumLanes == 1 && NumElts != 4)
3775 return false;
3776
3777 // Only match 256-bit with 32 types
3778 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003779 return false;
3780
3781 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003782 // they can differ if any of the corresponding index in a lane is undef
3783 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003784 int LaneSize = NumElts/NumLanes;
3785 for (int i = 0; i < LaneSize; ++i) {
3786 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003787 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3788 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3789
3790 if (!HighValid || !LowValid)
3791 return false;
3792 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003793 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003794 if (Mask[HighElt]-Mask[i] != LaneSize)
3795 return false;
3796 }
3797
3798 return true;
3799}
3800
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003801/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3802/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3803static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003804 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3805 EVT VT = SVOp->getValueType(0);
3806
3807 int NumElts = VT.getVectorNumElements();
3808 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003809 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003810
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003811 // Although the mask is equal for both lanes do it twice to get the cases
3812 // where a mask will match because the same mask element is undef on the
3813 // first half but valid on the second. This would get pathological cases
3814 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003815 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003816 for (int l = 0; l < NumLanes; ++l) {
3817 for (int i = 0; i < LaneSize; ++i) {
3818 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3819 if (MaskElt < 0)
3820 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003821 if (MaskElt >= LaneSize)
3822 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003823 Mask |= MaskElt << (i*2);
3824 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003825 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003826
3827 return Mask;
3828}
3829
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003830/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3831/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3832static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3833 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3834 EVT VT = SVOp->getValueType(0);
3835
3836 int NumElts = VT.getVectorNumElements();
3837 int NumLanes = VT.getSizeInBits()/128;
3838
3839 unsigned Mask = 0;
3840 int LaneSize = NumElts/NumLanes;
3841 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003842 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3843 int MaskElt = SVOp->getMaskElt(i);
3844 if (MaskElt < 0)
3845 continue;
3846 Mask |= (MaskElt-l*LaneSize) << i;
3847 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003848
3849 return Mask;
3850}
3851
Evan Cheng017dcc62006-04-21 01:05:10 +00003852/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3853/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003854/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003855static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 bool V2IsSplat = false, bool V2IsUndef = false) {
3857 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003858 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003859 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003860
Nate Begeman9008ca62009-04-27 18:41:29 +00003861 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003862 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003863
Nate Begeman9008ca62009-04-27 18:41:29 +00003864 for (int i = 1; i < NumOps; ++i)
3865 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3866 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3867 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003868 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003869
Evan Cheng39623da2006-04-20 08:58:49 +00003870 return true;
3871}
3872
Nate Begeman9008ca62009-04-27 18:41:29 +00003873static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003874 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003875 SmallVector<int, 8> M;
3876 N->getMask(M);
3877 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003878}
3879
Evan Chengd9539472006-04-14 21:59:03 +00003880/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3881/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003882/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3883bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3884 const X86Subtarget *Subtarget) {
3885 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003886 return false;
3887
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003888 // The second vector must be undef
3889 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3890 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003891
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003892 EVT VT = N->getValueType(0);
3893 unsigned NumElems = VT.getVectorNumElements();
3894
3895 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3896 (VT.getSizeInBits() == 256 && NumElems != 8))
3897 return false;
3898
3899 // "i+1" is the value the indexed mask element must have
3900 for (unsigned i = 0; i < NumElems; i += 2)
3901 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3902 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003903 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003904
3905 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003906}
3907
3908/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3909/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003910/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3911bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3912 const X86Subtarget *Subtarget) {
3913 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003914 return false;
3915
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003916 // The second vector must be undef
3917 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3918 return false;
3919
3920 EVT VT = N->getValueType(0);
3921 unsigned NumElems = VT.getVectorNumElements();
3922
3923 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3924 (VT.getSizeInBits() == 256 && NumElems != 8))
3925 return false;
3926
3927 // "i" is the value the indexed mask element must have
3928 for (unsigned i = 0; i < NumElems; i += 2)
3929 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3930 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003932
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003933 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003934}
3935
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003936/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3937/// specifies a shuffle of elements that is suitable for input to 256-bit
3938/// version of MOVDDUP.
3939static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3940 const X86Subtarget *Subtarget) {
3941 EVT VT = N->getValueType(0);
3942 int NumElts = VT.getVectorNumElements();
3943 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3944
3945 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3946 !V2IsUndef || NumElts != 4)
3947 return false;
3948
3949 for (int i = 0; i != NumElts/2; ++i)
3950 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3951 return false;
3952 for (int i = NumElts/2; i != NumElts; ++i)
3953 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3954 return false;
3955 return true;
3956}
3957
Evan Cheng0b457f02008-09-25 20:50:48 +00003958/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003959/// specifies a shuffle of elements that is suitable for input to 128-bit
3960/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003961bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003962 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003963
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003964 if (VT.getSizeInBits() != 128)
3965 return false;
3966
3967 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 for (int i = 0; i < e; ++i)
3969 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003970 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003971 for (int i = 0; i < e; ++i)
3972 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003973 return false;
3974 return true;
3975}
3976
David Greenec38a03e2011-02-03 15:50:00 +00003977/// isVEXTRACTF128Index - Return true if the specified
3978/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3979/// suitable for input to VEXTRACTF128.
3980bool X86::isVEXTRACTF128Index(SDNode *N) {
3981 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3982 return false;
3983
3984 // The index should be aligned on a 128-bit boundary.
3985 uint64_t Index =
3986 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3987
3988 unsigned VL = N->getValueType(0).getVectorNumElements();
3989 unsigned VBits = N->getValueType(0).getSizeInBits();
3990 unsigned ElSize = VBits / VL;
3991 bool Result = (Index * ElSize) % 128 == 0;
3992
3993 return Result;
3994}
3995
David Greeneccacdc12011-02-04 16:08:29 +00003996/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3997/// operand specifies a subvector insert that is suitable for input to
3998/// VINSERTF128.
3999bool X86::isVINSERTF128Index(SDNode *N) {
4000 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4001 return false;
4002
4003 // The index should be aligned on a 128-bit boundary.
4004 uint64_t Index =
4005 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4006
4007 unsigned VL = N->getValueType(0).getVectorNumElements();
4008 unsigned VBits = N->getValueType(0).getSizeInBits();
4009 unsigned ElSize = VBits / VL;
4010 bool Result = (Index * ElSize) % 128 == 0;
4011
4012 return Result;
4013}
4014
Evan Cheng63d33002006-03-22 08:01:21 +00004015/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004016/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004017unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004018 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4019 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4020
Evan Chengb9df0ca2006-03-22 02:53:00 +00004021 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4022 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004023 for (int i = 0; i < NumOperands; ++i) {
4024 int Val = SVOp->getMaskElt(NumOperands-i-1);
4025 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004026 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004027 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004028 if (i != NumOperands - 1)
4029 Mask <<= Shift;
4030 }
Evan Cheng63d33002006-03-22 08:01:21 +00004031 return Mask;
4032}
4033
Evan Cheng506d3df2006-03-29 23:07:14 +00004034/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004035/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004036unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004037 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004038 unsigned Mask = 0;
4039 // 8 nodes, but we only care about the last 4.
4040 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004041 int Val = SVOp->getMaskElt(i);
4042 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004043 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004044 if (i != 4)
4045 Mask <<= 2;
4046 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004047 return Mask;
4048}
4049
4050/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004051/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004052unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004053 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004054 unsigned Mask = 0;
4055 // 8 nodes, but we only care about the first 4.
4056 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004057 int Val = SVOp->getMaskElt(i);
4058 if (Val >= 0)
4059 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004060 if (i != 0)
4061 Mask <<= 2;
4062 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004063 return Mask;
4064}
4065
Nate Begemana09008b2009-10-19 02:17:23 +00004066/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4067/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4068unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4069 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4070 EVT VVT = N->getValueType(0);
4071 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4072 int Val = 0;
4073
4074 unsigned i, e;
4075 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4076 Val = SVOp->getMaskElt(i);
4077 if (Val >= 0)
4078 break;
4079 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004080 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004081 return (Val - i) * EltSize;
4082}
4083
David Greenec38a03e2011-02-03 15:50:00 +00004084/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4085/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4086/// instructions.
4087unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4088 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4089 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4090
4091 uint64_t Index =
4092 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4093
4094 EVT VecVT = N->getOperand(0).getValueType();
4095 EVT ElVT = VecVT.getVectorElementType();
4096
4097 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004098 return Index / NumElemsPerChunk;
4099}
4100
David Greeneccacdc12011-02-04 16:08:29 +00004101/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4102/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4103/// instructions.
4104unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4105 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4106 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4107
4108 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004109 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004110
4111 EVT VecVT = N->getValueType(0);
4112 EVT ElVT = VecVT.getVectorElementType();
4113
4114 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004115 return Index / NumElemsPerChunk;
4116}
4117
Evan Cheng37b73872009-07-30 08:33:02 +00004118/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4119/// constant +0.0.
4120bool X86::isZeroNode(SDValue Elt) {
4121 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004122 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004123 (isa<ConstantFPSDNode>(Elt) &&
4124 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4125}
4126
Nate Begeman9008ca62009-04-27 18:41:29 +00004127/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4128/// their permute mask.
4129static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4130 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004131 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004132 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004133 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004134
Nate Begeman5a5ca152009-04-29 05:20:52 +00004135 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004136 int idx = SVOp->getMaskElt(i);
4137 if (idx < 0)
4138 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004139 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004140 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004141 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004142 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004143 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004144 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4145 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004146}
4147
Evan Cheng779ccea2007-12-07 21:30:01 +00004148/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4149/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004150static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004151 unsigned NumElems = VT.getVectorNumElements();
4152 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004153 int idx = Mask[i];
4154 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004155 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004156 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004157 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004158 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004159 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004160 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004161}
4162
Evan Cheng533a0aa2006-04-19 20:35:22 +00004163/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4164/// match movhlps. The lower half elements should come from upper half of
4165/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004166/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004167static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004168 EVT VT = Op->getValueType(0);
4169 if (VT.getSizeInBits() != 128)
4170 return false;
4171 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004172 return false;
4173 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004174 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004175 return false;
4176 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004177 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004178 return false;
4179 return true;
4180}
4181
Evan Cheng5ced1d82006-04-06 23:23:56 +00004182/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004183/// is promoted to a vector. It also returns the LoadSDNode by reference if
4184/// required.
4185static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004186 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4187 return false;
4188 N = N->getOperand(0).getNode();
4189 if (!ISD::isNON_EXTLoad(N))
4190 return false;
4191 if (LD)
4192 *LD = cast<LoadSDNode>(N);
4193 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004194}
4195
Evan Cheng533a0aa2006-04-19 20:35:22 +00004196/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4197/// match movlp{s|d}. The lower half elements should come from lower half of
4198/// V1 (and in order), and the upper half elements should come from the upper
4199/// half of V2 (and in order). And since V1 will become the source of the
4200/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004201static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4202 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004203 EVT VT = Op->getValueType(0);
4204 if (VT.getSizeInBits() != 128)
4205 return false;
4206
Evan Cheng466685d2006-10-09 20:57:25 +00004207 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004208 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004209 // Is V2 is a vector load, don't do this transformation. We will try to use
4210 // load folding shufps op.
4211 if (ISD::isNON_EXTLoad(V2))
4212 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004213
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004214 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004215
Evan Cheng533a0aa2006-04-19 20:35:22 +00004216 if (NumElems != 2 && NumElems != 4)
4217 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004218 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004219 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004220 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004221 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004222 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004223 return false;
4224 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004225}
4226
Evan Cheng39623da2006-04-20 08:58:49 +00004227/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4228/// all the same.
4229static bool isSplatVector(SDNode *N) {
4230 if (N->getOpcode() != ISD::BUILD_VECTOR)
4231 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004232
Dan Gohman475871a2008-07-27 21:46:04 +00004233 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004234 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4235 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004236 return false;
4237 return true;
4238}
4239
Evan Cheng213d2cf2007-05-17 18:45:50 +00004240/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004241/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004242/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004243static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004244 SDValue V1 = N->getOperand(0);
4245 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004246 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4247 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004248 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004249 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004250 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004251 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4252 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004253 if (Opc != ISD::BUILD_VECTOR ||
4254 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004255 return false;
4256 } else if (Idx >= 0) {
4257 unsigned Opc = V1.getOpcode();
4258 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4259 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004260 if (Opc != ISD::BUILD_VECTOR ||
4261 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004262 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004263 }
4264 }
4265 return true;
4266}
4267
4268/// getZeroVector - Returns a vector of specified type with all zero elements.
4269///
Owen Andersone50ed302009-08-10 22:56:29 +00004270static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004271 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004272 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004273
Dale Johannesen0488fb62010-09-30 23:57:10 +00004274 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004275 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004276 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004277 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004278 if (HasSSE2) { // SSE2
4279 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4280 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4281 } else { // SSE1
4282 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4283 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4284 }
4285 } else if (VT.getSizeInBits() == 256) { // AVX
4286 // 256-bit logic and arithmetic instructions in AVX are
4287 // all floating-point, no support for integer ops. Default
4288 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004289 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004290 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4291 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004292 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004293 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004294}
4295
Chris Lattner8a594482007-11-25 00:24:49 +00004296/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004297/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4298/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4299/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004300static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004301 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004302 assert((VT.is128BitVector() || VT.is256BitVector())
4303 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004304
Owen Anderson825b72b2009-08-11 20:47:22 +00004305 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004306 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4307 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004308
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004309 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004310 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4311 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4312 Vec = Insert128BitVector(InsV, Vec,
4313 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4314 }
4315
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004316 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004317}
4318
Evan Cheng39623da2006-04-20 08:58:49 +00004319/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4320/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004321static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004322 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004323 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004324
Evan Cheng39623da2006-04-20 08:58:49 +00004325 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 SmallVector<int, 8> MaskVec;
4327 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004328
Nate Begeman5a5ca152009-04-29 05:20:52 +00004329 for (unsigned i = 0; i != NumElems; ++i) {
4330 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004331 MaskVec[i] = NumElems;
4332 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004333 }
Evan Cheng39623da2006-04-20 08:58:49 +00004334 }
Evan Cheng39623da2006-04-20 08:58:49 +00004335 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004336 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4337 SVOp->getOperand(1), &MaskVec[0]);
4338 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004339}
4340
Evan Cheng017dcc62006-04-21 01:05:10 +00004341/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4342/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004343static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004344 SDValue V2) {
4345 unsigned NumElems = VT.getVectorNumElements();
4346 SmallVector<int, 8> Mask;
4347 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004348 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004349 Mask.push_back(i);
4350 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004351}
4352
Nate Begeman9008ca62009-04-27 18:41:29 +00004353/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004354static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004355 SDValue V2) {
4356 unsigned NumElems = VT.getVectorNumElements();
4357 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004358 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 Mask.push_back(i);
4360 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004361 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004363}
4364
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004365/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004366static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 SDValue V2) {
4368 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004369 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004371 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 Mask.push_back(i + Half);
4373 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004374 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004375 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004376}
4377
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004378// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004379// a generic shuffle instruction because the target has no such instructions.
4380// Generate shuffles which repeat i16 and i8 several times until they can be
4381// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004382static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004383 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004385 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004386
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 while (NumElems > 4) {
4388 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004389 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004391 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 EltNo -= NumElems/2;
4393 }
4394 NumElems >>= 1;
4395 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004396 return V;
4397}
Eric Christopherfd179292009-08-27 18:07:15 +00004398
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004399/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4400static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4401 EVT VT = V.getValueType();
4402 DebugLoc dl = V.getDebugLoc();
4403 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4404 && "Vector size not supported");
4405
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004406 if (VT.getSizeInBits() == 128) {
4407 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004408 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004409 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4410 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004411 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004412 // To use VPERMILPS to splat scalars, the second half of indicies must
4413 // refer to the higher part, which is a duplication of the lower one,
4414 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004415 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4416 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004417
4418 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4419 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4420 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004421 }
4422
4423 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4424}
4425
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004426/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004427static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4428 EVT SrcVT = SV->getValueType(0);
4429 SDValue V1 = SV->getOperand(0);
4430 DebugLoc dl = SV->getDebugLoc();
4431
4432 int EltNo = SV->getSplatIndex();
4433 int NumElems = SrcVT.getVectorNumElements();
4434 unsigned Size = SrcVT.getSizeInBits();
4435
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004436 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4437 "Unknown how to promote splat for type");
4438
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004439 // Extract the 128-bit part containing the splat element and update
4440 // the splat element index when it refers to the higher register.
4441 if (Size == 256) {
4442 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4443 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4444 if (Idx > 0)
4445 EltNo -= NumElems/2;
4446 }
4447
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004448 // All i16 and i8 vector types can't be used directly by a generic shuffle
4449 // instruction because the target has no such instruction. Generate shuffles
4450 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004451 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004452 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004453 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004454 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004455
4456 // Recreate the 256-bit vector and place the same 128-bit vector
4457 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004458 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004459 if (Size == 256) {
4460 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4461 DAG.getConstant(0, MVT::i32), DAG, dl);
4462 V1 = Insert128BitVector(InsV, V1,
4463 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4464 }
4465
4466 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004467}
4468
Evan Chengba05f722006-04-21 23:03:30 +00004469/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004470/// vector of zero or undef vector. This produces a shuffle where the low
4471/// element of V2 is swizzled into the zero/undef vector, landing at element
4472/// 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 +00004473static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00004474 bool isZero, bool HasSSE2,
4475 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004476 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004477 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4479 unsigned NumElems = VT.getVectorNumElements();
4480 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004481 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 // If this is the insertion idx, put the low elt of V2 here.
4483 MaskVec.push_back(i == Idx ? NumElems : i);
4484 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004485}
4486
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004487/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4488/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004489static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4490 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004491 if (Depth == 6)
4492 return SDValue(); // Limit search depth.
4493
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004494 SDValue V = SDValue(N, 0);
4495 EVT VT = V.getValueType();
4496 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004497
4498 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4499 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4500 Index = SV->getMaskElt(Index);
4501
4502 if (Index < 0)
4503 return DAG.getUNDEF(VT.getVectorElementType());
4504
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004505 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004506 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004507 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004508 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004509
4510 // Recurse into target specific vector shuffles to find scalars.
4511 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004512 int NumElems = VT.getVectorNumElements();
4513 SmallVector<unsigned, 16> ShuffleMask;
4514 SDValue ImmN;
4515
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004516 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004517 case X86ISD::SHUFPS:
4518 case X86ISD::SHUFPD:
4519 ImmN = N->getOperand(N->getNumOperands()-1);
4520 DecodeSHUFPSMask(NumElems,
4521 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4522 ShuffleMask);
4523 break;
4524 case X86ISD::PUNPCKHBW:
4525 case X86ISD::PUNPCKHWD:
4526 case X86ISD::PUNPCKHDQ:
4527 case X86ISD::PUNPCKHQDQ:
4528 DecodePUNPCKHMask(NumElems, ShuffleMask);
4529 break;
4530 case X86ISD::UNPCKHPS:
4531 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004532 case X86ISD::VUNPCKHPSY:
4533 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004534 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4535 break;
4536 case X86ISD::PUNPCKLBW:
4537 case X86ISD::PUNPCKLWD:
4538 case X86ISD::PUNPCKLDQ:
4539 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004540 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004541 break;
4542 case X86ISD::UNPCKLPS:
4543 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004544 case X86ISD::VUNPCKLPSY:
4545 case X86ISD::VUNPCKLPDY:
4546 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004547 break;
4548 case X86ISD::MOVHLPS:
4549 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4550 break;
4551 case X86ISD::MOVLHPS:
4552 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4553 break;
4554 case X86ISD::PSHUFD:
4555 ImmN = N->getOperand(N->getNumOperands()-1);
4556 DecodePSHUFMask(NumElems,
4557 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4558 ShuffleMask);
4559 break;
4560 case X86ISD::PSHUFHW:
4561 ImmN = N->getOperand(N->getNumOperands()-1);
4562 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4563 ShuffleMask);
4564 break;
4565 case X86ISD::PSHUFLW:
4566 ImmN = N->getOperand(N->getNumOperands()-1);
4567 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4568 ShuffleMask);
4569 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004570 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004571 case X86ISD::MOVSD: {
4572 // The index 0 always comes from the first element of the second source,
4573 // this is why MOVSS and MOVSD are used in the first place. The other
4574 // elements come from the other positions of the first source vector.
4575 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004576 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4577 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004578 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004579 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004580 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004581 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004582 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004583 break;
4584 case X86ISD::VPERMILPSY:
4585 ImmN = N->getOperand(N->getNumOperands()-1);
4586 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4587 ShuffleMask);
4588 break;
4589 case X86ISD::VPERMILPD:
4590 ImmN = N->getOperand(N->getNumOperands()-1);
4591 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4592 ShuffleMask);
4593 break;
4594 case X86ISD::VPERMILPDY:
4595 ImmN = N->getOperand(N->getNumOperands()-1);
4596 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4597 ShuffleMask);
4598 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004599 case X86ISD::VPERM2F128:
4600 ImmN = N->getOperand(N->getNumOperands()-1);
4601 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4602 ShuffleMask);
4603 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004604 default:
4605 assert("not implemented for target shuffle node");
4606 return SDValue();
4607 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004608
4609 Index = ShuffleMask[Index];
4610 if (Index < 0)
4611 return DAG.getUNDEF(VT.getVectorElementType());
4612
4613 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4614 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4615 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004616 }
4617
4618 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004619 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004620 V = V.getOperand(0);
4621 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004622 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004623
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004624 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004625 return SDValue();
4626 }
4627
4628 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4629 return (Index == 0) ? V.getOperand(0)
4630 : DAG.getUNDEF(VT.getVectorElementType());
4631
4632 if (V.getOpcode() == ISD::BUILD_VECTOR)
4633 return V.getOperand(Index);
4634
4635 return SDValue();
4636}
4637
4638/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4639/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004640/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004641static
4642unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4643 bool ZerosFromLeft, SelectionDAG &DAG) {
4644 int i = 0;
4645
4646 while (i < NumElems) {
4647 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004648 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649 if (!(Elt.getNode() &&
4650 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4651 break;
4652 ++i;
4653 }
4654
4655 return i;
4656}
4657
4658/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4659/// MaskE correspond consecutively to elements from one of the vector operands,
4660/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4661static
4662bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4663 int OpIdx, int NumElems, unsigned &OpNum) {
4664 bool SeenV1 = false;
4665 bool SeenV2 = false;
4666
4667 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4668 int Idx = SVOp->getMaskElt(i);
4669 // Ignore undef indicies
4670 if (Idx < 0)
4671 continue;
4672
4673 if (Idx < NumElems)
4674 SeenV1 = true;
4675 else
4676 SeenV2 = true;
4677
4678 // Only accept consecutive elements from the same vector
4679 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4680 return false;
4681 }
4682
4683 OpNum = SeenV1 ? 0 : 1;
4684 return true;
4685}
4686
4687/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4688/// logical left shift of a vector.
4689static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4690 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4691 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4692 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4693 false /* check zeros from right */, DAG);
4694 unsigned OpSrc;
4695
4696 if (!NumZeros)
4697 return false;
4698
4699 // Considering the elements in the mask that are not consecutive zeros,
4700 // check if they consecutively come from only one of the source vectors.
4701 //
4702 // V1 = {X, A, B, C} 0
4703 // \ \ \ /
4704 // vector_shuffle V1, V2 <1, 2, 3, X>
4705 //
4706 if (!isShuffleMaskConsecutive(SVOp,
4707 0, // Mask Start Index
4708 NumElems-NumZeros-1, // Mask End Index
4709 NumZeros, // Where to start looking in the src vector
4710 NumElems, // Number of elements in vector
4711 OpSrc)) // Which source operand ?
4712 return false;
4713
4714 isLeft = false;
4715 ShAmt = NumZeros;
4716 ShVal = SVOp->getOperand(OpSrc);
4717 return true;
4718}
4719
4720/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4721/// logical left shift of a vector.
4722static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4723 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4724 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4725 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4726 true /* check zeros from left */, DAG);
4727 unsigned OpSrc;
4728
4729 if (!NumZeros)
4730 return false;
4731
4732 // Considering the elements in the mask that are not consecutive zeros,
4733 // check if they consecutively come from only one of the source vectors.
4734 //
4735 // 0 { A, B, X, X } = V2
4736 // / \ / /
4737 // vector_shuffle V1, V2 <X, X, 4, 5>
4738 //
4739 if (!isShuffleMaskConsecutive(SVOp,
4740 NumZeros, // Mask Start Index
4741 NumElems-1, // Mask End Index
4742 0, // Where to start looking in the src vector
4743 NumElems, // Number of elements in vector
4744 OpSrc)) // Which source operand ?
4745 return false;
4746
4747 isLeft = true;
4748 ShAmt = NumZeros;
4749 ShVal = SVOp->getOperand(OpSrc);
4750 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004751}
4752
4753/// isVectorShift - Returns true if the shuffle can be implemented as a
4754/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004755static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004756 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004757 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4758 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4759 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004760
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004761 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004762}
4763
Evan Chengc78d3b42006-04-24 18:01:45 +00004764/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4765///
Dan Gohman475871a2008-07-27 21:46:04 +00004766static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004767 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004768 SelectionDAG &DAG,
4769 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004770 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004771 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004772
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004773 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004774 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004775 bool First = true;
4776 for (unsigned i = 0; i < 16; ++i) {
4777 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4778 if (ThisIsNonZero && First) {
4779 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004780 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004781 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004782 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004783 First = false;
4784 }
4785
4786 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004787 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004788 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4789 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004790 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004791 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004792 }
4793 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004794 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4795 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4796 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004797 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004798 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004799 } else
4800 ThisElt = LastElt;
4801
Gabor Greifba36cb52008-08-28 21:40:38 +00004802 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004803 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004804 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004805 }
4806 }
4807
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004808 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809}
4810
Bill Wendlinga348c562007-03-22 18:42:45 +00004811/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004812///
Dan Gohman475871a2008-07-27 21:46:04 +00004813static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004814 unsigned NumNonZero, unsigned NumZero,
4815 SelectionDAG &DAG,
4816 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004817 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004818 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004819
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004820 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004821 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004822 bool First = true;
4823 for (unsigned i = 0; i < 8; ++i) {
4824 bool isNonZero = (NonZeros & (1 << i)) != 0;
4825 if (isNonZero) {
4826 if (First) {
4827 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004828 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004829 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004830 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004831 First = false;
4832 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004833 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004835 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004836 }
4837 }
4838
4839 return V;
4840}
4841
Evan Chengf26ffe92008-05-29 08:22:04 +00004842/// getVShift - Return a vector logical shift node.
4843///
Owen Andersone50ed302009-08-10 22:56:29 +00004844static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004845 unsigned NumBits, SelectionDAG &DAG,
4846 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004847 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004848 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004849 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4850 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004851 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004852 DAG.getConstant(NumBits,
4853 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004854}
4855
Dan Gohman475871a2008-07-27 21:46:04 +00004856SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004857X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004858 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004859
Evan Chengc3630942009-12-09 21:00:30 +00004860 // Check if the scalar load can be widened into a vector load. And if
4861 // the address is "base + cst" see if the cst can be "absorbed" into
4862 // the shuffle mask.
4863 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4864 SDValue Ptr = LD->getBasePtr();
4865 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4866 return SDValue();
4867 EVT PVT = LD->getValueType(0);
4868 if (PVT != MVT::i32 && PVT != MVT::f32)
4869 return SDValue();
4870
4871 int FI = -1;
4872 int64_t Offset = 0;
4873 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4874 FI = FINode->getIndex();
4875 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004876 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004877 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4878 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4879 Offset = Ptr.getConstantOperandVal(1);
4880 Ptr = Ptr.getOperand(0);
4881 } else {
4882 return SDValue();
4883 }
4884
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004885 // FIXME: 256-bit vector instructions don't require a strict alignment,
4886 // improve this code to support it better.
4887 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004888 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004889 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004890 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004891 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004892 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004893 // Can't change the alignment. FIXME: It's possible to compute
4894 // the exact stack offset and reference FI + adjust offset instead.
4895 // If someone *really* cares about this. That's the way to implement it.
4896 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004897 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004898 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004899 }
4900 }
4901
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004902 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004903 // Ptr + (Offset & ~15).
4904 if (Offset < 0)
4905 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004906 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004907 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004908 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004909 if (StartOffset)
4910 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4911 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4912
4913 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004914 int NumElems = VT.getVectorNumElements();
4915
4916 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4917 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4918 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004919 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004920 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004921
4922 // Canonicalize it to a v4i32 or v8i32 shuffle.
4923 SmallVector<int, 8> Mask;
4924 for (int i = 0; i < NumElems; ++i)
4925 Mask.push_back(EltNo);
4926
4927 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4928 return DAG.getNode(ISD::BITCAST, dl, NVT,
4929 DAG.getVectorShuffle(CanonVT, dl, V1,
4930 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004931 }
4932
4933 return SDValue();
4934}
4935
Michael J. Spencerec38de22010-10-10 22:04:20 +00004936/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4937/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004938/// load which has the same value as a build_vector whose operands are 'elts'.
4939///
4940/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004941///
Nate Begeman1449f292010-03-24 22:19:06 +00004942/// FIXME: we'd also like to handle the case where the last elements are zero
4943/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4944/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004945static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004946 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004947 EVT EltVT = VT.getVectorElementType();
4948 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004949
Nate Begemanfdea31a2010-03-24 20:49:50 +00004950 LoadSDNode *LDBase = NULL;
4951 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004952
Nate Begeman1449f292010-03-24 22:19:06 +00004953 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004954 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004955 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004956 for (unsigned i = 0; i < NumElems; ++i) {
4957 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004958
Nate Begemanfdea31a2010-03-24 20:49:50 +00004959 if (!Elt.getNode() ||
4960 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4961 return SDValue();
4962 if (!LDBase) {
4963 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4964 return SDValue();
4965 LDBase = cast<LoadSDNode>(Elt.getNode());
4966 LastLoadedElt = i;
4967 continue;
4968 }
4969 if (Elt.getOpcode() == ISD::UNDEF)
4970 continue;
4971
4972 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4973 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4974 return SDValue();
4975 LastLoadedElt = i;
4976 }
Nate Begeman1449f292010-03-24 22:19:06 +00004977
4978 // If we have found an entire vector of loads and undefs, then return a large
4979 // load of the entire vector width starting at the base pointer. If we found
4980 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004981 if (LastLoadedElt == NumElems - 1) {
4982 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004983 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004984 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004985 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004986 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004987 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004988 LDBase->isVolatile(), LDBase->isNonTemporal(),
4989 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004990 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4991 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004992 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4993 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004994 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4995 Ops, 2, MVT::i32,
4996 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004997 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004998 }
4999 return SDValue();
5000}
5001
Evan Chengc3630942009-12-09 21:00:30 +00005002SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005003X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005004 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005005
David Greenef125a292011-02-08 19:04:41 +00005006 EVT VT = Op.getValueType();
5007 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005008 unsigned NumElems = Op.getNumOperands();
5009
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005010 // Vectors containing all zeros can be matched by pxor and xorps later
5011 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5012 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5013 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005014 if (Op.getValueType() == MVT::v4i32 ||
5015 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005016 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005017
Dale Johannesenace16102009-02-03 19:33:06 +00005018 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005019 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005020
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005021 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5022 // vectors or broken into v4i32 operations on 256-bit vectors.
5023 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5024 if (Op.getValueType() == MVT::v4i32)
5025 return Op;
5026
5027 return getOnesVector(Op.getValueType(), DAG, dl);
5028 }
5029
Owen Andersone50ed302009-08-10 22:56:29 +00005030 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005031
Evan Cheng0db9fe62006-04-25 20:13:52 +00005032 unsigned NumZero = 0;
5033 unsigned NumNonZero = 0;
5034 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005035 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005036 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005037 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005038 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005039 if (Elt.getOpcode() == ISD::UNDEF)
5040 continue;
5041 Values.insert(Elt);
5042 if (Elt.getOpcode() != ISD::Constant &&
5043 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005044 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005045 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005046 NumZero++;
5047 else {
5048 NonZeros |= (1 << i);
5049 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005050 }
5051 }
5052
Chris Lattner97a2a562010-08-26 05:24:29 +00005053 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5054 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005055 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005056
Chris Lattner67f453a2008-03-09 05:42:06 +00005057 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005058 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005059 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005060 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005061
Chris Lattner62098042008-03-09 01:05:04 +00005062 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5063 // the value are obviously zero, truncate the value to i32 and do the
5064 // insertion that way. Only do this if the value is non-constant or if the
5065 // value is a constant being inserted into element 0. It is cheaper to do
5066 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005067 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005068 (!IsAllConstants || Idx == 0)) {
5069 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005070 // Handle SSE only.
5071 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5072 EVT VecVT = MVT::v4i32;
5073 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005074
Chris Lattner62098042008-03-09 01:05:04 +00005075 // Truncate the value (which may itself be a constant) to i32, and
5076 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005077 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005078 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005079 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5080 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005081
Chris Lattner62098042008-03-09 01:05:04 +00005082 // Now we have our 32-bit value zero extended in the low element of
5083 // a vector. If Idx != 0, swizzle it into place.
5084 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005085 SmallVector<int, 4> Mask;
5086 Mask.push_back(Idx);
5087 for (unsigned i = 1; i != VecElts; ++i)
5088 Mask.push_back(i);
5089 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005090 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005091 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005092 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005093 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005094 }
5095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005096
Chris Lattner19f79692008-03-08 22:59:52 +00005097 // If we have a constant or non-constant insertion into the low element of
5098 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5099 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005100 // depending on what the source datatype is.
5101 if (Idx == 0) {
5102 if (NumZero == 0) {
5103 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005104 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5105 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005106 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5107 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
5108 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
5109 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5111 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005112 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5113 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005114 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5115 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5116 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005117 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005118 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005119 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005120
5121 // Is it a vector logical left shift?
5122 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005123 X86::isZeroNode(Op.getOperand(0)) &&
5124 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005125 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005126 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005127 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005128 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005129 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005130 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005131
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005132 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005133 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005134
Chris Lattner19f79692008-03-08 22:59:52 +00005135 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5136 // is a non-constant being inserted into an element other than the low one,
5137 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5138 // movd/movss) to move this into the low element, then shuffle it into
5139 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005140 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005141 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005142
Evan Cheng0db9fe62006-04-25 20:13:52 +00005143 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005144 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
5145 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005146 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005147 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005148 MaskVec.push_back(i == Idx ? 0 : 1);
5149 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005150 }
5151 }
5152
Chris Lattner67f453a2008-03-09 05:42:06 +00005153 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005154 if (Values.size() == 1) {
5155 if (EVTBits == 32) {
5156 // Instead of a shuffle like this:
5157 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5158 // Check if it's possible to issue this instead.
5159 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5160 unsigned Idx = CountTrailingZeros_32(NonZeros);
5161 SDValue Item = Op.getOperand(Idx);
5162 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5163 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5164 }
Dan Gohman475871a2008-07-27 21:46:04 +00005165 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005166 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005167
Dan Gohmana3941172007-07-24 22:55:08 +00005168 // A vector full of immediates; various special cases are already
5169 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005170 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005171 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005172
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005173 // For AVX-length vectors, build the individual 128-bit pieces and use
5174 // shuffles to put them in place.
5175 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5176 SmallVector<SDValue, 32> V;
5177 for (unsigned i = 0; i < NumElems; ++i)
5178 V.push_back(Op.getOperand(i));
5179
5180 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5181
5182 // Build both the lower and upper subvector.
5183 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5184 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5185 NumElems/2);
5186
5187 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005188 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5189 DAG.getConstant(0, MVT::i32), DAG, dl);
5190 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005191 DAG, dl);
5192 }
5193
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005194 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005195 if (EVTBits == 64) {
5196 if (NumNonZero == 1) {
5197 // One half is zero or undef.
5198 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005199 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005200 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005201 return getShuffleVectorZeroOrUndef(V2, Idx, true,
5202 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005203 }
Dan Gohman475871a2008-07-27 21:46:04 +00005204 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005205 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005206
5207 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005208 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005209 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005210 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005211 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005212 }
5213
Bill Wendling826f36f2007-03-28 00:57:11 +00005214 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005215 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005216 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005217 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005218 }
5219
5220 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005221 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005222 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005223 if (NumElems == 4 && NumZero > 0) {
5224 for (unsigned i = 0; i < 4; ++i) {
5225 bool isZero = !(NonZeros & (1 << i));
5226 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00005227 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005228 else
Dale Johannesenace16102009-02-03 19:33:06 +00005229 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005230 }
5231
5232 for (unsigned i = 0; i < 2; ++i) {
5233 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5234 default: break;
5235 case 0:
5236 V[i] = V[i*2]; // Must be a zero vector.
5237 break;
5238 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005239 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005240 break;
5241 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005242 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005243 break;
5244 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005245 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005246 break;
5247 }
5248 }
5249
Nate Begeman9008ca62009-04-27 18:41:29 +00005250 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005251 bool Reverse = (NonZeros & 0x3) == 2;
5252 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005253 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005254 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5255 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005256 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5257 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005258 }
5259
Nate Begemanfdea31a2010-03-24 20:49:50 +00005260 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5261 // Check for a build vector of consecutive loads.
5262 for (unsigned i = 0; i < NumElems; ++i)
5263 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005264
Nate Begemanfdea31a2010-03-24 20:49:50 +00005265 // Check for elements which are consecutive loads.
5266 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5267 if (LD.getNode())
5268 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005269
5270 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005271 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005272 SDValue Result;
5273 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5274 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5275 else
5276 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005277
Chris Lattner24faf612010-08-28 17:59:08 +00005278 for (unsigned i = 1; i < NumElems; ++i) {
5279 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5280 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005281 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005282 }
5283 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005284 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005285
Chris Lattner6e80e442010-08-28 17:15:43 +00005286 // Otherwise, expand into a number of unpckl*, start by extending each of
5287 // our (non-undef) elements to the full vector width with the element in the
5288 // bottom slot of the vector (which generates no code for SSE).
5289 for (unsigned i = 0; i < NumElems; ++i) {
5290 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5291 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5292 else
5293 V[i] = DAG.getUNDEF(VT);
5294 }
5295
5296 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005297 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5298 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5299 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005300 unsigned EltStride = NumElems >> 1;
5301 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005302 for (unsigned i = 0; i < EltStride; ++i) {
5303 // If V[i+EltStride] is undef and this is the first round of mixing,
5304 // then it is safe to just drop this shuffle: V[i] is already in the
5305 // right place, the one element (since it's the first round) being
5306 // inserted as undef can be dropped. This isn't safe for successive
5307 // rounds because they will permute elements within both vectors.
5308 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5309 EltStride == NumElems/2)
5310 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005311
Chris Lattner6e80e442010-08-28 17:15:43 +00005312 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005313 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005314 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005315 }
5316 return V[0];
5317 }
Dan Gohman475871a2008-07-27 21:46:04 +00005318 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005319}
5320
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005321// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5322// them in a MMX register. This is better than doing a stack convert.
5323static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005324 DebugLoc dl = Op.getDebugLoc();
5325 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005326
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005327 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5328 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5329 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005330 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005331 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5332 InVec = Op.getOperand(1);
5333 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5334 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005335 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005336 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5337 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5338 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005339 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005340 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5341 Mask[0] = 0; Mask[1] = 2;
5342 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5343 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005344 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005345}
5346
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005347// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5348// to create 256-bit vectors from two other 128-bit ones.
5349static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5350 DebugLoc dl = Op.getDebugLoc();
5351 EVT ResVT = Op.getValueType();
5352
5353 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5354
5355 SDValue V1 = Op.getOperand(0);
5356 SDValue V2 = Op.getOperand(1);
5357 unsigned NumElems = ResVT.getVectorNumElements();
5358
5359 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5360 DAG.getConstant(0, MVT::i32), DAG, dl);
5361 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5362 DAG, dl);
5363}
5364
5365SDValue
5366X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005367 EVT ResVT = Op.getValueType();
5368
5369 assert(Op.getNumOperands() == 2);
5370 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5371 "Unsupported CONCAT_VECTORS for value type");
5372
5373 // We support concatenate two MMX registers and place them in a MMX register.
5374 // This is better than doing a stack convert.
5375 if (ResVT.is128BitVector())
5376 return LowerMMXCONCAT_VECTORS(Op, DAG);
5377
5378 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5379 // from two other 128-bit ones.
5380 return LowerAVXCONCAT_VECTORS(Op, DAG);
5381}
5382
Nate Begemanb9a47b82009-02-23 08:49:38 +00005383// v8i16 shuffles - Prefer shuffles in the following order:
5384// 1. [all] pshuflw, pshufhw, optional move
5385// 2. [ssse3] 1 x pshufb
5386// 3. [ssse3] 2 x pshufb + 1 x por
5387// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005388SDValue
5389X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5390 SelectionDAG &DAG) const {
5391 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005392 SDValue V1 = SVOp->getOperand(0);
5393 SDValue V2 = SVOp->getOperand(1);
5394 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005395 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005396
Nate Begemanb9a47b82009-02-23 08:49:38 +00005397 // Determine if more than 1 of the words in each of the low and high quadwords
5398 // of the result come from the same quadword of one of the two inputs. Undef
5399 // mask values count as coming from any quadword, for better codegen.
5400 SmallVector<unsigned, 4> LoQuad(4);
5401 SmallVector<unsigned, 4> HiQuad(4);
5402 BitVector InputQuads(4);
5403 for (unsigned i = 0; i < 8; ++i) {
5404 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005405 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005406 MaskVals.push_back(EltIdx);
5407 if (EltIdx < 0) {
5408 ++Quad[0];
5409 ++Quad[1];
5410 ++Quad[2];
5411 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005412 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005413 }
5414 ++Quad[EltIdx / 4];
5415 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005416 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005417
Nate Begemanb9a47b82009-02-23 08:49:38 +00005418 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005419 unsigned MaxQuad = 1;
5420 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005421 if (LoQuad[i] > MaxQuad) {
5422 BestLoQuad = i;
5423 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005424 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005425 }
5426
Nate Begemanb9a47b82009-02-23 08:49:38 +00005427 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005428 MaxQuad = 1;
5429 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005430 if (HiQuad[i] > MaxQuad) {
5431 BestHiQuad = i;
5432 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005433 }
5434 }
5435
Nate Begemanb9a47b82009-02-23 08:49:38 +00005436 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005437 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005438 // single pshufb instruction is necessary. If There are more than 2 input
5439 // quads, disable the next transformation since it does not help SSSE3.
5440 bool V1Used = InputQuads[0] || InputQuads[1];
5441 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005442 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005443 if (InputQuads.count() == 2 && V1Used && V2Used) {
5444 BestLoQuad = InputQuads.find_first();
5445 BestHiQuad = InputQuads.find_next(BestLoQuad);
5446 }
5447 if (InputQuads.count() > 2) {
5448 BestLoQuad = -1;
5449 BestHiQuad = -1;
5450 }
5451 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005452
Nate Begemanb9a47b82009-02-23 08:49:38 +00005453 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5454 // the shuffle mask. If a quad is scored as -1, that means that it contains
5455 // words from all 4 input quadwords.
5456 SDValue NewV;
5457 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005458 SmallVector<int, 8> MaskV;
5459 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5460 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005461 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005462 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5463 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5464 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005465
Nate Begemanb9a47b82009-02-23 08:49:38 +00005466 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5467 // source words for the shuffle, to aid later transformations.
5468 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005469 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005470 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005471 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005472 if (idx != (int)i)
5473 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005474 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005475 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005476 AllWordsInNewV = false;
5477 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005478 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005479
Nate Begemanb9a47b82009-02-23 08:49:38 +00005480 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5481 if (AllWordsInNewV) {
5482 for (int i = 0; i != 8; ++i) {
5483 int idx = MaskVals[i];
5484 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005485 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005486 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005487 if ((idx != i) && idx < 4)
5488 pshufhw = false;
5489 if ((idx != i) && idx > 3)
5490 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005491 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005492 V1 = NewV;
5493 V2Used = false;
5494 BestLoQuad = 0;
5495 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005496 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005497
Nate Begemanb9a47b82009-02-23 08:49:38 +00005498 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5499 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005500 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005501 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5502 unsigned TargetMask = 0;
5503 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005504 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005505 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5506 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5507 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005508 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005509 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005510 }
Eric Christopherfd179292009-08-27 18:07:15 +00005511
Nate Begemanb9a47b82009-02-23 08:49:38 +00005512 // If we have SSSE3, and all words of the result are from 1 input vector,
5513 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5514 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005515 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005516 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005517
Nate Begemanb9a47b82009-02-23 08:49:38 +00005518 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005519 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005520 // mask, and elements that come from V1 in the V2 mask, so that the two
5521 // results can be OR'd together.
5522 bool TwoInputs = V1Used && V2Used;
5523 for (unsigned i = 0; i != 8; ++i) {
5524 int EltIdx = MaskVals[i] * 2;
5525 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005526 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5527 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005528 continue;
5529 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005530 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5531 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005532 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005533 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005534 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005535 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005536 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005537 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005538 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005539
Nate Begemanb9a47b82009-02-23 08:49:38 +00005540 // Calculate the shuffle mask for the second input, shuffle it, and
5541 // OR it with the first shuffled input.
5542 pshufbMask.clear();
5543 for (unsigned i = 0; i != 8; ++i) {
5544 int EltIdx = MaskVals[i] * 2;
5545 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005546 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5547 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005548 continue;
5549 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005550 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5551 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005552 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005553 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005554 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005555 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005556 MVT::v16i8, &pshufbMask[0], 16));
5557 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005558 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005559 }
5560
5561 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5562 // and update MaskVals with new element order.
5563 BitVector InOrder(8);
5564 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005565 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005566 for (int i = 0; i != 4; ++i) {
5567 int idx = MaskVals[i];
5568 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005569 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005570 InOrder.set(i);
5571 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005572 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005573 InOrder.set(i);
5574 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005575 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005576 }
5577 }
5578 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005579 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005580 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005581 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005582
5583 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5584 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5585 NewV.getOperand(0),
5586 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5587 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005588 }
Eric Christopherfd179292009-08-27 18:07:15 +00005589
Nate Begemanb9a47b82009-02-23 08:49:38 +00005590 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5591 // and update MaskVals with the new element order.
5592 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005593 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005594 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005595 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005596 for (unsigned i = 4; i != 8; ++i) {
5597 int idx = MaskVals[i];
5598 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005599 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005600 InOrder.set(i);
5601 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005602 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005603 InOrder.set(i);
5604 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005605 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005606 }
5607 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005608 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005609 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005610
5611 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5612 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5613 NewV.getOperand(0),
5614 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5615 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005616 }
Eric Christopherfd179292009-08-27 18:07:15 +00005617
Nate Begemanb9a47b82009-02-23 08:49:38 +00005618 // In case BestHi & BestLo were both -1, which means each quadword has a word
5619 // from each of the four input quadwords, calculate the InOrder bitvector now
5620 // before falling through to the insert/extract cleanup.
5621 if (BestLoQuad == -1 && BestHiQuad == -1) {
5622 NewV = V1;
5623 for (int i = 0; i != 8; ++i)
5624 if (MaskVals[i] < 0 || MaskVals[i] == i)
5625 InOrder.set(i);
5626 }
Eric Christopherfd179292009-08-27 18:07:15 +00005627
Nate Begemanb9a47b82009-02-23 08:49:38 +00005628 // The other elements are put in the right place using pextrw and pinsrw.
5629 for (unsigned i = 0; i != 8; ++i) {
5630 if (InOrder[i])
5631 continue;
5632 int EltIdx = MaskVals[i];
5633 if (EltIdx < 0)
5634 continue;
5635 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005636 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005638 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005639 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005640 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005641 DAG.getIntPtrConstant(i));
5642 }
5643 return NewV;
5644}
5645
5646// v16i8 shuffles - Prefer shuffles in the following order:
5647// 1. [ssse3] 1 x pshufb
5648// 2. [ssse3] 2 x pshufb + 1 x por
5649// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5650static
Nate Begeman9008ca62009-04-27 18:41:29 +00005651SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005652 SelectionDAG &DAG,
5653 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005654 SDValue V1 = SVOp->getOperand(0);
5655 SDValue V2 = SVOp->getOperand(1);
5656 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005657 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005658 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005659
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005661 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 // present, fall back to case 3.
5663 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5664 bool V1Only = true;
5665 bool V2Only = true;
5666 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005667 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005668 if (EltIdx < 0)
5669 continue;
5670 if (EltIdx < 16)
5671 V2Only = false;
5672 else
5673 V1Only = false;
5674 }
Eric Christopherfd179292009-08-27 18:07:15 +00005675
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5677 if (TLI.getSubtarget()->hasSSSE3()) {
5678 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005679
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005681 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005682 //
5683 // Otherwise, we have elements from both input vectors, and must zero out
5684 // elements that come from V2 in the first mask, and V1 in the second mask
5685 // so that we can OR them together.
5686 bool TwoInputs = !(V1Only || V2Only);
5687 for (unsigned i = 0; i != 16; ++i) {
5688 int EltIdx = MaskVals[i];
5689 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005690 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005691 continue;
5692 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005693 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005694 }
5695 // If all the elements are from V2, assign it to V1 and return after
5696 // building the first pshufb.
5697 if (V2Only)
5698 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005699 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005700 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005701 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 if (!TwoInputs)
5703 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005704
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 // Calculate the shuffle mask for the second input, shuffle it, and
5706 // OR it with the first shuffled input.
5707 pshufbMask.clear();
5708 for (unsigned i = 0; i != 16; ++i) {
5709 int EltIdx = MaskVals[i];
5710 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005711 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 continue;
5713 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005714 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005715 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005716 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005717 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005718 MVT::v16i8, &pshufbMask[0], 16));
5719 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 }
Eric Christopherfd179292009-08-27 18:07:15 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 // No SSSE3 - Calculate in place words and then fix all out of place words
5723 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5724 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005725 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5726 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005727 SDValue NewV = V2Only ? V2 : V1;
5728 for (int i = 0; i != 8; ++i) {
5729 int Elt0 = MaskVals[i*2];
5730 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005731
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 // This word of the result is all undef, skip it.
5733 if (Elt0 < 0 && Elt1 < 0)
5734 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005735
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 // This word of the result is already in the correct place, skip it.
5737 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5738 continue;
5739 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5740 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005741
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5743 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5744 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005745
5746 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5747 // using a single extract together, load it and store it.
5748 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005749 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005750 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005751 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005752 DAG.getIntPtrConstant(i));
5753 continue;
5754 }
5755
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005757 // source byte is not also odd, shift the extracted word left 8 bits
5758 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 DAG.getIntPtrConstant(Elt1 / 2));
5762 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005763 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005764 DAG.getConstant(8,
5765 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005766 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005767 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5768 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 }
5770 // If Elt0 is defined, extract it from the appropriate source. If the
5771 // source byte is not also even, shift the extracted word right 8 bits. If
5772 // Elt1 was also defined, OR the extracted values together before
5773 // inserting them in the result.
5774 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005775 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5777 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005779 DAG.getConstant(8,
5780 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005781 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005782 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5783 DAG.getConstant(0x00FF, MVT::i16));
5784 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 : InsElt0;
5786 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005787 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005788 DAG.getIntPtrConstant(i));
5789 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005790 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005791}
5792
Evan Cheng7a831ce2007-12-15 03:00:47 +00005793/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005794/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005795/// done when every pair / quad of shuffle mask elements point to elements in
5796/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005797/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005798static
Nate Begeman9008ca62009-04-27 18:41:29 +00005799SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005800 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005801 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005802 SDValue V1 = SVOp->getOperand(0);
5803 SDValue V2 = SVOp->getOperand(1);
5804 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005805 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005806 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005807 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005808 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 case MVT::v4f32: NewVT = MVT::v2f64; break;
5810 case MVT::v4i32: NewVT = MVT::v2i64; break;
5811 case MVT::v8i16: NewVT = MVT::v4i32; break;
5812 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005813 }
5814
Nate Begeman9008ca62009-04-27 18:41:29 +00005815 int Scale = NumElems / NewWidth;
5816 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005817 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005818 int StartIdx = -1;
5819 for (int j = 0; j < Scale; ++j) {
5820 int EltIdx = SVOp->getMaskElt(i+j);
5821 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005822 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005823 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005824 StartIdx = EltIdx - (EltIdx % Scale);
5825 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005826 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005827 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005828 if (StartIdx == -1)
5829 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005830 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005831 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005832 }
5833
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005834 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5835 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005836 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005837}
5838
Evan Chengd880b972008-05-09 21:53:03 +00005839/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005840///
Owen Andersone50ed302009-08-10 22:56:29 +00005841static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005842 SDValue SrcOp, SelectionDAG &DAG,
5843 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005844 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005845 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005846 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005847 LD = dyn_cast<LoadSDNode>(SrcOp);
5848 if (!LD) {
5849 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5850 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005851 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005852 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005853 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005854 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005855 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005856 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005857 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005858 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005859 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5860 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5861 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005862 SrcOp.getOperand(0)
5863 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005864 }
5865 }
5866 }
5867
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005868 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005869 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005870 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005871 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005872}
5873
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005874/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5875/// shuffle node referes to only one lane in the sources.
5876static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5877 EVT VT = SVOp->getValueType(0);
5878 int NumElems = VT.getVectorNumElements();
5879 int HalfSize = NumElems/2;
5880 SmallVector<int, 16> M;
5881 SVOp->getMask(M);
5882 bool MatchA = false, MatchB = false;
5883
5884 for (int l = 0; l < NumElems*2; l += HalfSize) {
5885 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5886 MatchA = true;
5887 break;
5888 }
5889 }
5890
5891 for (int l = 0; l < NumElems*2; l += HalfSize) {
5892 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5893 MatchB = true;
5894 break;
5895 }
5896 }
5897
5898 return MatchA && MatchB;
5899}
5900
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005901/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5902/// which could not be matched by any known target speficic shuffle
5903static SDValue
5904LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005905 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5906 // If each half of a vector shuffle node referes to only one lane in the
5907 // source vectors, extract each used 128-bit lane and shuffle them using
5908 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5909 // the work to the legalizer.
5910 DebugLoc dl = SVOp->getDebugLoc();
5911 EVT VT = SVOp->getValueType(0);
5912 int NumElems = VT.getVectorNumElements();
5913 int HalfSize = NumElems/2;
5914
5915 // Extract the reference for each half
5916 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5917 int FstVecOpNum = 0, SndVecOpNum = 0;
5918 for (int i = 0; i < HalfSize; ++i) {
5919 int Elt = SVOp->getMaskElt(i);
5920 if (SVOp->getMaskElt(i) < 0)
5921 continue;
5922 FstVecOpNum = Elt/NumElems;
5923 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5924 break;
5925 }
5926 for (int i = HalfSize; i < NumElems; ++i) {
5927 int Elt = SVOp->getMaskElt(i);
5928 if (SVOp->getMaskElt(i) < 0)
5929 continue;
5930 SndVecOpNum = Elt/NumElems;
5931 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5932 break;
5933 }
5934
5935 // Extract the subvectors
5936 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5937 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5938 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5939 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5940
5941 // Generate 128-bit shuffles
5942 SmallVector<int, 16> MaskV1, MaskV2;
5943 for (int i = 0; i < HalfSize; ++i) {
5944 int Elt = SVOp->getMaskElt(i);
5945 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5946 }
5947 for (int i = HalfSize; i < NumElems; ++i) {
5948 int Elt = SVOp->getMaskElt(i);
5949 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5950 }
5951
5952 EVT NVT = V1.getValueType();
5953 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5954 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5955
5956 // Concatenate the result back
5957 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5958 DAG.getConstant(0, MVT::i32), DAG, dl);
5959 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5960 DAG, dl);
5961 }
5962
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005963 return SDValue();
5964}
5965
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005966/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5967/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005968static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005969LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005970 SDValue V1 = SVOp->getOperand(0);
5971 SDValue V2 = SVOp->getOperand(1);
5972 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005973 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005974
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005975 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5976
Evan Chengace3c172008-07-22 21:13:36 +00005977 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005978 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005979 SmallVector<int, 8> Mask1(4U, -1);
5980 SmallVector<int, 8> PermMask;
5981 SVOp->getMask(PermMask);
5982
Evan Chengace3c172008-07-22 21:13:36 +00005983 unsigned NumHi = 0;
5984 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005985 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005986 int Idx = PermMask[i];
5987 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005988 Locs[i] = std::make_pair(-1, -1);
5989 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005990 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5991 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005992 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005993 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005994 NumLo++;
5995 } else {
5996 Locs[i] = std::make_pair(1, NumHi);
5997 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005998 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005999 NumHi++;
6000 }
6001 }
6002 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006003
Evan Chengace3c172008-07-22 21:13:36 +00006004 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006005 // If no more than two elements come from either vector. This can be
6006 // implemented with two shuffles. First shuffle gather the elements.
6007 // The second shuffle, which takes the first shuffle as both of its
6008 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006009 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006010
Nate Begeman9008ca62009-04-27 18:41:29 +00006011 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006012
Evan Chengace3c172008-07-22 21:13:36 +00006013 for (unsigned i = 0; i != 4; ++i) {
6014 if (Locs[i].first == -1)
6015 continue;
6016 else {
6017 unsigned Idx = (i < 2) ? 0 : 4;
6018 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006019 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006020 }
6021 }
6022
Nate Begeman9008ca62009-04-27 18:41:29 +00006023 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006024 } else if (NumLo == 3 || NumHi == 3) {
6025 // Otherwise, we must have three elements from one vector, call it X, and
6026 // one element from the other, call it Y. First, use a shufps to build an
6027 // intermediate vector with the one element from Y and the element from X
6028 // that will be in the same half in the final destination (the indexes don't
6029 // matter). Then, use a shufps to build the final vector, taking the half
6030 // containing the element from Y from the intermediate, and the other half
6031 // from X.
6032 if (NumHi == 3) {
6033 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006034 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006035 std::swap(V1, V2);
6036 }
6037
6038 // Find the element from V2.
6039 unsigned HiIndex;
6040 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006041 int Val = PermMask[HiIndex];
6042 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006043 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006044 if (Val >= 4)
6045 break;
6046 }
6047
Nate Begeman9008ca62009-04-27 18:41:29 +00006048 Mask1[0] = PermMask[HiIndex];
6049 Mask1[1] = -1;
6050 Mask1[2] = PermMask[HiIndex^1];
6051 Mask1[3] = -1;
6052 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006053
6054 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006055 Mask1[0] = PermMask[0];
6056 Mask1[1] = PermMask[1];
6057 Mask1[2] = HiIndex & 1 ? 6 : 4;
6058 Mask1[3] = HiIndex & 1 ? 4 : 6;
6059 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006060 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006061 Mask1[0] = HiIndex & 1 ? 2 : 0;
6062 Mask1[1] = HiIndex & 1 ? 0 : 2;
6063 Mask1[2] = PermMask[2];
6064 Mask1[3] = PermMask[3];
6065 if (Mask1[2] >= 0)
6066 Mask1[2] += 4;
6067 if (Mask1[3] >= 0)
6068 Mask1[3] += 4;
6069 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006070 }
Evan Chengace3c172008-07-22 21:13:36 +00006071 }
6072
6073 // Break it into (shuffle shuffle_hi, shuffle_lo).
6074 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006075 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006076 SmallVector<int,8> LoMask(4U, -1);
6077 SmallVector<int,8> HiMask(4U, -1);
6078
6079 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006080 unsigned MaskIdx = 0;
6081 unsigned LoIdx = 0;
6082 unsigned HiIdx = 2;
6083 for (unsigned i = 0; i != 4; ++i) {
6084 if (i == 2) {
6085 MaskPtr = &HiMask;
6086 MaskIdx = 1;
6087 LoIdx = 0;
6088 HiIdx = 2;
6089 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006090 int Idx = PermMask[i];
6091 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006092 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006093 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006094 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006095 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006096 LoIdx++;
6097 } else {
6098 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006100 HiIdx++;
6101 }
6102 }
6103
Nate Begeman9008ca62009-04-27 18:41:29 +00006104 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6105 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6106 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006107 for (unsigned i = 0; i != 4; ++i) {
6108 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006109 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006110 } else {
6111 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006112 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006113 }
6114 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006115 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006116}
6117
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006118static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006119 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006120 V = V.getOperand(0);
6121 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6122 V = V.getOperand(0);
6123 if (MayFoldLoad(V))
6124 return true;
6125 return false;
6126}
6127
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006128// FIXME: the version above should always be used. Since there's
6129// a bug where several vector shuffles can't be folded because the
6130// DAG is not updated during lowering and a node claims to have two
6131// uses while it only has one, use this version, and let isel match
6132// another instruction if the load really happens to have more than
6133// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006134// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006135static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006136 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006137 V = V.getOperand(0);
6138 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6139 V = V.getOperand(0);
6140 if (ISD::isNormalLoad(V.getNode()))
6141 return true;
6142 return false;
6143}
6144
6145/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6146/// a vector extract, and if both can be later optimized into a single load.
6147/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6148/// here because otherwise a target specific shuffle node is going to be
6149/// emitted for this shuffle, and the optimization not done.
6150/// FIXME: This is probably not the best approach, but fix the problem
6151/// until the right path is decided.
6152static
6153bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6154 const TargetLowering &TLI) {
6155 EVT VT = V.getValueType();
6156 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6157
6158 // Be sure that the vector shuffle is present in a pattern like this:
6159 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6160 if (!V.hasOneUse())
6161 return false;
6162
6163 SDNode *N = *V.getNode()->use_begin();
6164 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6165 return false;
6166
6167 SDValue EltNo = N->getOperand(1);
6168 if (!isa<ConstantSDNode>(EltNo))
6169 return false;
6170
6171 // If the bit convert changed the number of elements, it is unsafe
6172 // to examine the mask.
6173 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006174 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006175 EVT SrcVT = V.getOperand(0).getValueType();
6176 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6177 return false;
6178 V = V.getOperand(0);
6179 HasShuffleIntoBitcast = true;
6180 }
6181
6182 // Select the input vector, guarding against out of range extract vector.
6183 unsigned NumElems = VT.getVectorNumElements();
6184 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6185 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6186 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6187
6188 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006189 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006190 V = V.getOperand(0);
6191
6192 if (ISD::isNormalLoad(V.getNode())) {
6193 // Is the original load suitable?
6194 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6195
6196 // FIXME: avoid the multi-use bug that is preventing lots of
6197 // of foldings to be detected, this is still wrong of course, but
6198 // give the temporary desired behavior, and if it happens that
6199 // the load has real more uses, during isel it will not fold, and
6200 // will generate poor code.
6201 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6202 return false;
6203
6204 if (!HasShuffleIntoBitcast)
6205 return true;
6206
6207 // If there's a bitcast before the shuffle, check if the load type and
6208 // alignment is valid.
6209 unsigned Align = LN0->getAlignment();
6210 unsigned NewAlign =
6211 TLI.getTargetData()->getABITypeAlignment(
6212 VT.getTypeForEVT(*DAG.getContext()));
6213
6214 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6215 return false;
6216 }
6217
6218 return true;
6219}
6220
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006221static
Evan Cheng835580f2010-10-07 20:50:20 +00006222SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6223 EVT VT = Op.getValueType();
6224
6225 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006226 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6227 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006228 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6229 V1, DAG));
6230}
6231
6232static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006233SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
6234 bool HasSSE2) {
6235 SDValue V1 = Op.getOperand(0);
6236 SDValue V2 = Op.getOperand(1);
6237 EVT VT = Op.getValueType();
6238
6239 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6240
6241 if (HasSSE2 && VT == MVT::v2f64)
6242 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6243
Evan Cheng0899f5c2011-08-31 02:05:24 +00006244 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6245 return DAG.getNode(ISD::BITCAST, dl, VT,
6246 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6247 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6248 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006249}
6250
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006251static
6252SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6253 SDValue V1 = Op.getOperand(0);
6254 SDValue V2 = Op.getOperand(1);
6255 EVT VT = Op.getValueType();
6256
6257 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6258 "unsupported shuffle type");
6259
6260 if (V2.getOpcode() == ISD::UNDEF)
6261 V2 = V1;
6262
6263 // v4i32 or v4f32
6264 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6265}
6266
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006267static inline unsigned getSHUFPOpcode(EVT VT) {
6268 switch(VT.getSimpleVT().SimpleTy) {
6269 case MVT::v8i32: // Use fp unit for int unpack.
6270 case MVT::v8f32:
6271 case MVT::v4i32: // Use fp unit for int unpack.
6272 case MVT::v4f32: return X86ISD::SHUFPS;
6273 case MVT::v4i64: // Use fp unit for int unpack.
6274 case MVT::v4f64:
6275 case MVT::v2i64: // Use fp unit for int unpack.
6276 case MVT::v2f64: return X86ISD::SHUFPD;
6277 default:
6278 llvm_unreachable("Unknown type for shufp*");
6279 }
6280 return 0;
6281}
6282
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006283static
6284SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
6285 SDValue V1 = Op.getOperand(0);
6286 SDValue V2 = Op.getOperand(1);
6287 EVT VT = Op.getValueType();
6288 unsigned NumElems = VT.getVectorNumElements();
6289
6290 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6291 // operand of these instructions is only memory, so check if there's a
6292 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6293 // same masks.
6294 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006295
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006296 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006297 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006298 CanFoldLoad = true;
6299
6300 // When V1 is a load, it can be folded later into a store in isel, example:
6301 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6302 // turns into:
6303 // (MOVLPSmr addr:$src1, VR128:$src2)
6304 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006305 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006306 CanFoldLoad = true;
6307
Eric Christopher893a8822011-02-20 05:04:42 +00006308 // Both of them can't be memory operations though.
6309 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6310 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006311
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006312 if (CanFoldLoad) {
6313 if (HasSSE2 && NumElems == 2)
6314 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6315
6316 if (NumElems == 4)
6317 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6318 }
6319
6320 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6321 // movl and movlp will both match v2i64, but v2i64 is never matched by
6322 // movl earlier because we make it strict to avoid messing with the movlp load
6323 // folding logic (see the code above getMOVLP call). Match it here then,
6324 // this is horrible, but will stay like this until we move all shuffle
6325 // matching to x86 specific nodes. Note that for the 1st condition all
6326 // types are matched with movsd.
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006327 if (HasSSE2) {
6328 if (NumElems == 2)
6329 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006330 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006331 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006332
6333 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6334
6335 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006336 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006337 X86::getShuffleSHUFImmediate(SVOp), DAG);
6338}
6339
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006340static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006341 switch(VT.getSimpleVT().SimpleTy) {
6342 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6343 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006344 case MVT::v4f32: return X86ISD::UNPCKLPS;
6345 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006346 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006347 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006348 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006349 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006350 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6351 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6352 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006353 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006354 }
6355 return 0;
6356}
6357
6358static inline unsigned getUNPCKHOpcode(EVT VT) {
6359 switch(VT.getSimpleVT().SimpleTy) {
6360 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6361 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6362 case MVT::v4f32: return X86ISD::UNPCKHPS;
6363 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006364 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006365 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006366 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006367 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006368 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6369 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6370 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006371 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006372 }
6373 return 0;
6374}
6375
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006376static inline unsigned getVPERMILOpcode(EVT VT) {
6377 switch(VT.getSimpleVT().SimpleTy) {
6378 case MVT::v4i32:
6379 case MVT::v4f32: return X86ISD::VPERMILPS;
6380 case MVT::v2i64:
6381 case MVT::v2f64: return X86ISD::VPERMILPD;
6382 case MVT::v8i32:
6383 case MVT::v8f32: return X86ISD::VPERMILPSY;
6384 case MVT::v4i64:
6385 case MVT::v4f64: return X86ISD::VPERMILPDY;
6386 default:
6387 llvm_unreachable("Unknown type for vpermil");
6388 }
6389 return 0;
6390}
6391
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006392/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6393/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6394/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6395static bool isVectorBroadcast(SDValue &Op) {
6396 EVT VT = Op.getValueType();
6397 bool Is256 = VT.getSizeInBits() == 256;
6398
6399 assert((VT.getSizeInBits() == 128 || Is256) &&
6400 "Unsupported type for vbroadcast node");
6401
6402 SDValue V = Op;
6403 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6404 V = V.getOperand(0);
6405
6406 if (Is256 && !(V.hasOneUse() &&
6407 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6408 V.getOperand(0).getOpcode() == ISD::UNDEF))
6409 return false;
6410
6411 if (Is256)
6412 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006413
6414 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006415 return false;
6416
6417 // Check the source scalar_to_vector type. 256-bit broadcasts are
6418 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6419 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006420 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6421 return false;
6422
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006423 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6424 if (ScalarSize != 32 && ScalarSize != 64)
6425 return false;
6426 if (!Is256 && ScalarSize == 64)
6427 return false;
6428
6429 V = V.getOperand(0);
6430 if (!MayFoldLoad(V))
6431 return false;
6432
6433 // Return the load node
6434 Op = V;
6435 return true;
6436}
6437
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006438static
6439SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006440 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006441 const X86Subtarget *Subtarget) {
6442 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6443 EVT VT = Op.getValueType();
6444 DebugLoc dl = Op.getDebugLoc();
6445 SDValue V1 = Op.getOperand(0);
6446 SDValue V2 = Op.getOperand(1);
6447
6448 if (isZeroShuffle(SVOp))
6449 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
6450
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006451 // Handle splat operations
6452 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006453 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006454 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006455 // Special case, this is the only place now where it's allowed to return
6456 // a vector_shuffle operation without using a target specific node, because
6457 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6458 // this be moved to DAGCombine instead?
6459 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006460 return Op;
6461
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006462 // Use vbroadcast whenever the splat comes from a foldable load
6463 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6464 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6465
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006466 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006467 if ((Size == 128 && NumElem <= 4) ||
6468 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006469 return SDValue();
6470
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006471 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006472 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006473 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006474
6475 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6476 // do it!
6477 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6478 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6479 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006480 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006481 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
6482 // FIXME: Figure out a cleaner way to do this.
6483 // Try to make use of movq to zero out the top part.
6484 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6485 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6486 if (NewOp.getNode()) {
6487 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6488 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6489 DAG, Subtarget, dl);
6490 }
6491 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6492 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6493 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6494 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6495 DAG, Subtarget, dl);
6496 }
6497 }
6498 return SDValue();
6499}
6500
Dan Gohman475871a2008-07-27 21:46:04 +00006501SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006502X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006503 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006504 SDValue V1 = Op.getOperand(0);
6505 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006506 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006507 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006508 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006509 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006510 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6511 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006512 bool V1IsSplat = false;
6513 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006514 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006515 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006516 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006517 MachineFunction &MF = DAG.getMachineFunction();
6518 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006519
Dale Johannesen0488fb62010-09-30 23:57:10 +00006520 // Shuffle operations on MMX not supported.
6521 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006522 return Op;
6523
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006524 // Vector shuffle lowering takes 3 steps:
6525 //
6526 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6527 // narrowing and commutation of operands should be handled.
6528 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6529 // shuffle nodes.
6530 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6531 // so the shuffle can be broken into other shuffles and the legalizer can
6532 // try the lowering again.
6533 //
6534 // The general ideia is that no vector_shuffle operation should be left to
6535 // be matched during isel, all of them must be converted to a target specific
6536 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006537
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006538 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6539 // narrowing and commutation of operands should be handled. The actual code
6540 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006541 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006542 if (NewOp.getNode())
6543 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006544
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006545 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6546 // unpckh_undef). Only use pshufd if speed is more important than size.
6547 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006548 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006549 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006550 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006551
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006552 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00006553 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006554 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006555
Dale Johannesen0488fb62010-09-30 23:57:10 +00006556 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006557 return getMOVHighToLow(Op, dl, DAG);
6558
6559 // Use to match splats
6560 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
6561 (VT == MVT::v2f64 || VT == MVT::v2i64))
6562 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6563
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006564 if (X86::isPSHUFDMask(SVOp)) {
6565 // The actual implementation will match the mask in the if above and then
6566 // during isel it can match several different instructions, not only pshufd
6567 // as its name says, sad but true, emulate the behavior for now...
6568 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6569 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6570
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006571 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6572
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006573 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006574 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6575
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006576 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6577 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006578 }
Eric Christopherfd179292009-08-27 18:07:15 +00006579
Evan Chengf26ffe92008-05-29 08:22:04 +00006580 // Check if this can be converted into a logical shift.
6581 bool isLeft = false;
6582 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006583 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00006584 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00006585 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006586 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006587 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006588 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006589 EVT EltVT = VT.getVectorElementType();
6590 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006591 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006592 }
Eric Christopherfd179292009-08-27 18:07:15 +00006593
Nate Begeman9008ca62009-04-27 18:41:29 +00006594 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006595 if (V1IsUndef)
6596 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006597 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006598 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006599 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006600 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006601 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6602
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006603 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006604 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6605 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006606 }
Eric Christopherfd179292009-08-27 18:07:15 +00006607
Nate Begeman9008ca62009-04-27 18:41:29 +00006608 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006609 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
6610 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006611
Dale Johannesen0488fb62010-09-30 23:57:10 +00006612 if (X86::isMOVHLPSMask(SVOp))
6613 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006614
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006615 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006616 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006617
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006618 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006619 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006620
Dale Johannesen0488fb62010-09-30 23:57:10 +00006621 if (X86::isMOVLPMask(SVOp))
6622 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006623
Nate Begeman9008ca62009-04-27 18:41:29 +00006624 if (ShouldXformToMOVHLPS(SVOp) ||
6625 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6626 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006627
Evan Chengf26ffe92008-05-29 08:22:04 +00006628 if (isShift) {
6629 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006630 EVT EltVT = VT.getVectorElementType();
6631 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006632 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006633 }
Eric Christopherfd179292009-08-27 18:07:15 +00006634
Evan Cheng9eca5e82006-10-25 21:49:50 +00006635 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006636 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6637 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006638 V1IsSplat = isSplatVector(V1.getNode());
6639 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006640
Chris Lattner8a594482007-11-25 00:24:49 +00006641 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006642 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006643 Op = CommuteVectorShuffle(SVOp, DAG);
6644 SVOp = cast<ShuffleVectorSDNode>(Op);
6645 V1 = SVOp->getOperand(0);
6646 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006647 std::swap(V1IsSplat, V2IsSplat);
6648 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006649 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006650 }
6651
Nate Begeman9008ca62009-04-27 18:41:29 +00006652 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6653 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006654 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006655 return V1;
6656 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6657 // the instruction selector will not match, so get a canonical MOVL with
6658 // swapped operands to undo the commute.
6659 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006660 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006661
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006662 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006663 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006664
6665 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006666 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006667
Evan Cheng9bbbb982006-10-25 20:48:19 +00006668 if (V2IsSplat) {
6669 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006670 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006671 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006672 SDValue NewMask = NormalizeMask(SVOp, DAG);
6673 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6674 if (NSVOp != SVOp) {
6675 if (X86::isUNPCKLMask(NSVOp, true)) {
6676 return NewMask;
6677 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6678 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006679 }
6680 }
6681 }
6682
Evan Cheng9eca5e82006-10-25 21:49:50 +00006683 if (Commuted) {
6684 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006685 // FIXME: this seems wrong.
6686 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6687 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006688
6689 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006690 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006691
6692 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006693 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006694 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006695
Nate Begeman9008ca62009-04-27 18:41:29 +00006696 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006697 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006698 return CommuteVectorShuffle(SVOp, DAG);
6699
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006700 // The checks below are all present in isShuffleMaskLegal, but they are
6701 // inlined here right now to enable us to directly emit target specific
6702 // nodes, and remove one by one until they don't return Op anymore.
6703 SmallVector<int, 16> M;
6704 SVOp->getMask(M);
6705
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006706 if (isPALIGNRMask(M, VT, HasSSSE3))
6707 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6708 X86::getShufflePALIGNRImmediate(SVOp),
6709 DAG);
6710
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006711 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6712 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006713 if (VT == MVT::v2f64)
6714 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006715 if (VT == MVT::v2i64)
6716 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6717 }
6718
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006719 if (isPSHUFHWMask(M, VT))
6720 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6721 X86::getShufflePSHUFHWImmediate(SVOp),
6722 DAG);
6723
6724 if (isPSHUFLWMask(M, VT))
6725 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6726 X86::getShufflePSHUFLWImmediate(SVOp),
6727 DAG);
6728
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006729 if (isSHUFPMask(M, VT))
6730 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6731 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006732
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006733 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006734 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006735 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006736 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006737
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006738 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006739 // Generate target specific nodes for 128 or 256-bit shuffles only
6740 // supported in the AVX instruction set.
6741 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006742
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006743 // Handle VMOVDDUPY permutations
6744 if (isMOVDDUPYMask(SVOp, Subtarget))
6745 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6746
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006747 // Handle VPERMILPS* permutations
6748 if (isVPERMILPSMask(M, VT, Subtarget))
6749 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6750 getShuffleVPERMILPSImmediate(SVOp), DAG);
6751
6752 // Handle VPERMILPD* permutations
6753 if (isVPERMILPDMask(M, VT, Subtarget))
6754 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6755 getShuffleVPERMILPDImmediate(SVOp), DAG);
6756
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006757 // Handle VPERM2F128 permutations
6758 if (isVPERM2F128Mask(M, VT, Subtarget))
6759 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6760 getShuffleVPERM2F128Immediate(SVOp), DAG);
6761
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006762 // Handle VSHUFPSY permutations
6763 if (isVSHUFPSYMask(M, VT, Subtarget))
6764 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6765 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6766
6767 // Handle VSHUFPDY permutations
6768 if (isVSHUFPDYMask(M, VT, Subtarget))
6769 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6770 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6771
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006772 //===--------------------------------------------------------------------===//
6773 // Since no target specific shuffle was selected for this generic one,
6774 // lower it into other known shuffles. FIXME: this isn't true yet, but
6775 // this is the plan.
6776 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006777
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006778 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6779 if (VT == MVT::v8i16) {
6780 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6781 if (NewOp.getNode())
6782 return NewOp;
6783 }
6784
6785 if (VT == MVT::v16i8) {
6786 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6787 if (NewOp.getNode())
6788 return NewOp;
6789 }
6790
6791 // Handle all 128-bit wide vectors with 4 elements, and match them with
6792 // several different shuffle types.
6793 if (NumElems == 4 && VT.getSizeInBits() == 128)
6794 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6795
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006796 // Handle general 256-bit shuffles
6797 if (VT.is256BitVector())
6798 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6799
Dan Gohman475871a2008-07-27 21:46:04 +00006800 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801}
6802
Dan Gohman475871a2008-07-27 21:46:04 +00006803SDValue
6804X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006805 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006806 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006807 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006808
6809 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6810 return SDValue();
6811
Duncan Sands83ec4b62008-06-06 12:08:01 +00006812 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006813 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006814 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006815 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006816 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006817 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006818 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006819 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6820 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6821 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006822 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6823 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006824 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006825 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006826 Op.getOperand(0)),
6827 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006828 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006829 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006830 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006831 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006832 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006833 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006834 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6835 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006836 // result has a single use which is a store or a bitcast to i32. And in
6837 // the case of a store, it's not worth it if the index is a constant 0,
6838 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006839 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006840 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006841 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006842 if ((User->getOpcode() != ISD::STORE ||
6843 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6844 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006845 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006846 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006847 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006848 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006849 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006850 Op.getOperand(0)),
6851 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006852 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006853 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006854 // ExtractPS works with constant index.
6855 if (isa<ConstantSDNode>(Op.getOperand(1)))
6856 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006857 }
Dan Gohman475871a2008-07-27 21:46:04 +00006858 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006859}
6860
6861
Dan Gohman475871a2008-07-27 21:46:04 +00006862SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006863X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6864 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006865 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006866 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006867
David Greene74a579d2011-02-10 16:57:36 +00006868 SDValue Vec = Op.getOperand(0);
6869 EVT VecVT = Vec.getValueType();
6870
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006871 // If this is a 256-bit vector result, first extract the 128-bit vector and
6872 // then extract the element from the 128-bit vector.
6873 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006874 DebugLoc dl = Op.getNode()->getDebugLoc();
6875 unsigned NumElems = VecVT.getVectorNumElements();
6876 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006877 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6878
6879 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006880 bool Upper = IdxVal >= NumElems/2;
6881 Vec = Extract128BitVector(Vec,
6882 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006883
David Greene74a579d2011-02-10 16:57:36 +00006884 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006885 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006886 }
6887
6888 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6889
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006890 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006891 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006892 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006893 return Res;
6894 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006895
Owen Andersone50ed302009-08-10 22:56:29 +00006896 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006897 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006898 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006899 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006900 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006901 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006902 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006903 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6904 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006905 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006906 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006907 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006908 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006909 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006910 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006911 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006912 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006913 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006914 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006915 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006916 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006917 if (Idx == 0)
6918 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006919
Evan Cheng0db9fe62006-04-25 20:13:52 +00006920 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006921 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006922 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006923 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006924 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006925 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006926 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006927 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006928 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6929 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6930 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006931 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006932 if (Idx == 0)
6933 return Op;
6934
6935 // UNPCKHPD the element to the lowest double word, then movsd.
6936 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6937 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006938 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006939 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006940 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006941 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006942 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006943 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006944 }
6945
Dan Gohman475871a2008-07-27 21:46:04 +00006946 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006947}
6948
Dan Gohman475871a2008-07-27 21:46:04 +00006949SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006950X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6951 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006952 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006953 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006954 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006955
Dan Gohman475871a2008-07-27 21:46:04 +00006956 SDValue N0 = Op.getOperand(0);
6957 SDValue N1 = Op.getOperand(1);
6958 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006959
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006960 if (VT.getSizeInBits() == 256)
6961 return SDValue();
6962
Dan Gohman8a55ce42009-09-23 21:02:20 +00006963 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006964 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006965 unsigned Opc;
6966 if (VT == MVT::v8i16)
6967 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006968 else if (VT == MVT::v16i8)
6969 Opc = X86ISD::PINSRB;
6970 else
6971 Opc = X86ISD::PINSRB;
6972
Nate Begeman14d12ca2008-02-11 04:19:36 +00006973 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6974 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006975 if (N1.getValueType() != MVT::i32)
6976 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6977 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006978 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006979 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006980 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006981 // Bits [7:6] of the constant are the source select. This will always be
6982 // zero here. The DAG Combiner may combine an extract_elt index into these
6983 // bits. For example (insert (extract, 3), 2) could be matched by putting
6984 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006985 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006986 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006987 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006988 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006989 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006990 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006991 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006992 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006993 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006994 // PINSR* works with constant index.
6995 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006996 }
Dan Gohman475871a2008-07-27 21:46:04 +00006997 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006998}
6999
Dan Gohman475871a2008-07-27 21:46:04 +00007000SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007001X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007002 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007003 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007004
David Greene6b381262011-02-09 15:32:06 +00007005 DebugLoc dl = Op.getDebugLoc();
7006 SDValue N0 = Op.getOperand(0);
7007 SDValue N1 = Op.getOperand(1);
7008 SDValue N2 = Op.getOperand(2);
7009
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007010 // If this is a 256-bit vector result, first extract the 128-bit vector,
7011 // insert the element into the extracted half and then place it back.
7012 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007013 if (!isa<ConstantSDNode>(N2))
7014 return SDValue();
7015
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007016 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007017 unsigned NumElems = VT.getVectorNumElements();
7018 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007019 bool Upper = IdxVal >= NumElems/2;
7020 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7021 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007022
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007023 // Insert the element into the desired half.
7024 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7025 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007026
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007027 // Insert the changed part back to the 256-bit vector
7028 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007029 }
7030
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007031 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007032 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7033
Dan Gohman8a55ce42009-09-23 21:02:20 +00007034 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007035 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007036
Dan Gohman8a55ce42009-09-23 21:02:20 +00007037 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007038 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7039 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007040 if (N1.getValueType() != MVT::i32)
7041 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7042 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007043 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007044 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007045 }
Dan Gohman475871a2008-07-27 21:46:04 +00007046 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007047}
7048
Dan Gohman475871a2008-07-27 21:46:04 +00007049SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007050X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007051 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007052 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007053 EVT OpVT = Op.getValueType();
7054
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007055 // If this is a 256-bit vector result, first insert into a 128-bit
7056 // vector and then insert into the 256-bit vector.
7057 if (OpVT.getSizeInBits() > 128) {
7058 // Insert into a 128-bit vector.
7059 EVT VT128 = EVT::getVectorVT(*Context,
7060 OpVT.getVectorElementType(),
7061 OpVT.getVectorNumElements() / 2);
7062
7063 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7064
7065 // Insert the 128-bit vector.
7066 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7067 DAG.getConstant(0, MVT::i32),
7068 DAG, dl);
7069 }
7070
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007071 if (Op.getValueType() == MVT::v1i64 &&
7072 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007074
Owen Anderson825b72b2009-08-11 20:47:22 +00007075 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007076 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7077 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007078 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007079 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007080}
7081
David Greene91585092011-01-26 15:38:49 +00007082// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7083// a simple subregister reference or explicit instructions to grab
7084// upper bits of a vector.
7085SDValue
7086X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7087 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007088 DebugLoc dl = Op.getNode()->getDebugLoc();
7089 SDValue Vec = Op.getNode()->getOperand(0);
7090 SDValue Idx = Op.getNode()->getOperand(1);
7091
7092 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7093 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7094 return Extract128BitVector(Vec, Idx, DAG, dl);
7095 }
David Greene91585092011-01-26 15:38:49 +00007096 }
7097 return SDValue();
7098}
7099
David Greenecfe33c42011-01-26 19:13:22 +00007100// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7101// simple superregister reference or explicit instructions to insert
7102// the upper bits of a vector.
7103SDValue
7104X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7105 if (Subtarget->hasAVX()) {
7106 DebugLoc dl = Op.getNode()->getDebugLoc();
7107 SDValue Vec = Op.getNode()->getOperand(0);
7108 SDValue SubVec = Op.getNode()->getOperand(1);
7109 SDValue Idx = Op.getNode()->getOperand(2);
7110
7111 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7112 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007113 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007114 }
7115 }
7116 return SDValue();
7117}
7118
Bill Wendling056292f2008-09-16 21:48:12 +00007119// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7120// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7121// one of the above mentioned nodes. It has to be wrapped because otherwise
7122// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7123// be used to form addressing mode. These wrapped nodes will be selected
7124// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007125SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007126X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007127 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007128
Chris Lattner41621a22009-06-26 19:22:52 +00007129 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7130 // global base reg.
7131 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007132 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007133 CodeModel::Model M = getTargetMachine().getCodeModel();
7134
Chris Lattner4f066492009-07-11 20:29:19 +00007135 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007136 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007137 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007138 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007139 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007140 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007141 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007142
Evan Cheng1606e8e2009-03-13 07:51:59 +00007143 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007144 CP->getAlignment(),
7145 CP->getOffset(), OpFlag);
7146 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007147 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007148 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007149 if (OpFlag) {
7150 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007151 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007152 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007153 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154 }
7155
7156 return Result;
7157}
7158
Dan Gohmand858e902010-04-17 15:26:15 +00007159SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007160 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007161
Chris Lattner18c59872009-06-27 04:16:01 +00007162 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7163 // global base reg.
7164 unsigned char OpFlag = 0;
7165 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007166 CodeModel::Model M = getTargetMachine().getCodeModel();
7167
Chris Lattner4f066492009-07-11 20:29:19 +00007168 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007169 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007170 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007171 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007172 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007173 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007174 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007175
Chris Lattner18c59872009-06-27 04:16:01 +00007176 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7177 OpFlag);
7178 DebugLoc DL = JT->getDebugLoc();
7179 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007180
Chris Lattner18c59872009-06-27 04:16:01 +00007181 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007182 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007183 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7184 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007185 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007186 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007187
Chris Lattner18c59872009-06-27 04:16:01 +00007188 return Result;
7189}
7190
7191SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007192X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007193 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007194
Chris Lattner18c59872009-06-27 04:16:01 +00007195 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7196 // global base reg.
7197 unsigned char OpFlag = 0;
7198 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007199 CodeModel::Model M = getTargetMachine().getCodeModel();
7200
Chris Lattner4f066492009-07-11 20:29:19 +00007201 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007202 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7203 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7204 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007205 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007206 } else if (Subtarget->isPICStyleGOT()) {
7207 OpFlag = X86II::MO_GOT;
7208 } else if (Subtarget->isPICStyleStubPIC()) {
7209 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7210 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7211 OpFlag = X86II::MO_DARWIN_NONLAZY;
7212 }
Eric Christopherfd179292009-08-27 18:07:15 +00007213
Chris Lattner18c59872009-06-27 04:16:01 +00007214 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007215
Chris Lattner18c59872009-06-27 04:16:01 +00007216 DebugLoc DL = Op.getDebugLoc();
7217 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007218
7219
Chris Lattner18c59872009-06-27 04:16:01 +00007220 // With PIC, the address is actually $g + Offset.
7221 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007222 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007223 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7224 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007225 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007226 Result);
7227 }
Eric Christopherfd179292009-08-27 18:07:15 +00007228
Eli Friedman586272d2011-08-11 01:48:05 +00007229 // For symbols that require a load from a stub to get the address, emit the
7230 // load.
7231 if (isGlobalStubReference(OpFlag))
7232 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7233 MachinePointerInfo::getGOT(), false, false, 0);
7234
Chris Lattner18c59872009-06-27 04:16:01 +00007235 return Result;
7236}
7237
Dan Gohman475871a2008-07-27 21:46:04 +00007238SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007239X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007240 // Create the TargetBlockAddressAddress node.
7241 unsigned char OpFlags =
7242 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007243 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007244 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007245 DebugLoc dl = Op.getDebugLoc();
7246 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7247 /*isTarget=*/true, OpFlags);
7248
Dan Gohmanf705adb2009-10-30 01:28:02 +00007249 if (Subtarget->isPICStyleRIPRel() &&
7250 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007251 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7252 else
7253 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007254
Dan Gohman29cbade2009-11-20 23:18:13 +00007255 // With PIC, the address is actually $g + Offset.
7256 if (isGlobalRelativeToPICBase(OpFlags)) {
7257 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7258 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7259 Result);
7260 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007261
7262 return Result;
7263}
7264
7265SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007266X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007267 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007268 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007269 // Create the TargetGlobalAddress node, folding in the constant
7270 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007271 unsigned char OpFlags =
7272 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007273 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007274 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007275 if (OpFlags == X86II::MO_NO_FLAG &&
7276 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007277 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007278 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007279 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007280 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007281 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007282 }
Eric Christopherfd179292009-08-27 18:07:15 +00007283
Chris Lattner4f066492009-07-11 20:29:19 +00007284 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007285 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007286 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7287 else
7288 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007289
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007290 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007291 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007292 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7293 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007294 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007295 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007296
Chris Lattner36c25012009-07-10 07:34:39 +00007297 // For globals that require a load from a stub to get the address, emit the
7298 // load.
7299 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007300 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007301 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007302
Dan Gohman6520e202008-10-18 02:06:02 +00007303 // If there was a non-zero offset that we didn't fold, create an explicit
7304 // addition for it.
7305 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007306 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007307 DAG.getConstant(Offset, getPointerTy()));
7308
Evan Cheng0db9fe62006-04-25 20:13:52 +00007309 return Result;
7310}
7311
Evan Chengda43bcf2008-09-24 00:05:32 +00007312SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007313X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007314 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007315 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007316 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007317}
7318
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007319static SDValue
7320GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007321 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007322 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007323 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007324 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007325 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007326 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007327 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007328 GA->getOffset(),
7329 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007330 if (InFlag) {
7331 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007332 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007333 } else {
7334 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007335 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007336 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007337
7338 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007339 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007340
Rafael Espindola15f1b662009-04-24 12:59:40 +00007341 SDValue Flag = Chain.getValue(1);
7342 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007343}
7344
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007345// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007346static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007347LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007348 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007349 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007350 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7351 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007352 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007353 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007354 InFlag = Chain.getValue(1);
7355
Chris Lattnerb903bed2009-06-26 21:20:29 +00007356 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007357}
7358
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007359// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007360static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007361LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007362 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007363 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7364 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007365}
7366
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007367// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7368// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007369static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007370 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007371 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007372 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007373
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007374 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7375 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7376 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007377
Michael J. Spencerec38de22010-10-10 22:04:20 +00007378 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007379 DAG.getIntPtrConstant(0),
7380 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007381
Chris Lattnerb903bed2009-06-26 21:20:29 +00007382 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007383 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7384 // initialexec.
7385 unsigned WrapperKind = X86ISD::Wrapper;
7386 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007387 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007388 } else if (is64Bit) {
7389 assert(model == TLSModel::InitialExec);
7390 OperandFlags = X86II::MO_GOTTPOFF;
7391 WrapperKind = X86ISD::WrapperRIP;
7392 } else {
7393 assert(model == TLSModel::InitialExec);
7394 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007395 }
Eric Christopherfd179292009-08-27 18:07:15 +00007396
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007397 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7398 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007399 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007400 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007401 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007402 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007403
Rafael Espindola9a580232009-02-27 13:37:18 +00007404 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007405 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007406 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007407
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007408 // The address of the thread local variable is the add of the thread
7409 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007410 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007411}
7412
Dan Gohman475871a2008-07-27 21:46:04 +00007413SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007414X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007415
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007416 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007417 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007418
Eric Christopher30ef0e52010-06-03 04:07:48 +00007419 if (Subtarget->isTargetELF()) {
7420 // TODO: implement the "local dynamic" model
7421 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007422
Eric Christopher30ef0e52010-06-03 04:07:48 +00007423 // If GV is an alias then use the aliasee for determining
7424 // thread-localness.
7425 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7426 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007427
7428 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007429 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007430
Eric Christopher30ef0e52010-06-03 04:07:48 +00007431 switch (model) {
7432 case TLSModel::GeneralDynamic:
7433 case TLSModel::LocalDynamic: // not implemented
7434 if (Subtarget->is64Bit())
7435 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7436 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007437
Eric Christopher30ef0e52010-06-03 04:07:48 +00007438 case TLSModel::InitialExec:
7439 case TLSModel::LocalExec:
7440 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7441 Subtarget->is64Bit());
7442 }
7443 } else if (Subtarget->isTargetDarwin()) {
7444 // Darwin only has one model of TLS. Lower to that.
7445 unsigned char OpFlag = 0;
7446 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7447 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007448
Eric Christopher30ef0e52010-06-03 04:07:48 +00007449 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7450 // global base reg.
7451 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7452 !Subtarget->is64Bit();
7453 if (PIC32)
7454 OpFlag = X86II::MO_TLVP_PIC_BASE;
7455 else
7456 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007457 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007458 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007459 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007460 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007461 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007462
Eric Christopher30ef0e52010-06-03 04:07:48 +00007463 // With PIC32, the address is actually $g + Offset.
7464 if (PIC32)
7465 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7466 DAG.getNode(X86ISD::GlobalBaseReg,
7467 DebugLoc(), getPointerTy()),
7468 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007469
Eric Christopher30ef0e52010-06-03 04:07:48 +00007470 // Lowering the machine isd will make sure everything is in the right
7471 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007472 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007473 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007474 SDValue Args[] = { Chain, Offset };
7475 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007476
Eric Christopher30ef0e52010-06-03 04:07:48 +00007477 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7478 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7479 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007480
Eric Christopher30ef0e52010-06-03 04:07:48 +00007481 // And our return value (tls address) is in the standard call return value
7482 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007483 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7484 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007485 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007486
Eric Christopher30ef0e52010-06-03 04:07:48 +00007487 assert(false &&
7488 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007489
Torok Edwinc23197a2009-07-14 16:55:14 +00007490 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007491 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007492}
7493
Evan Cheng0db9fe62006-04-25 20:13:52 +00007494
Nadav Rotem43012222011-05-11 08:12:09 +00007495/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007496/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007497SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007498 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007499 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007500 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007501 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007502 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007503 SDValue ShOpLo = Op.getOperand(0);
7504 SDValue ShOpHi = Op.getOperand(1);
7505 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007506 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007507 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007508 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007509
Dan Gohman475871a2008-07-27 21:46:04 +00007510 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007511 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007512 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7513 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007514 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007515 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7516 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007517 }
Evan Chenge3413162006-01-09 18:33:28 +00007518
Owen Anderson825b72b2009-08-11 20:47:22 +00007519 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7520 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007521 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007522 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007523
Dan Gohman475871a2008-07-27 21:46:04 +00007524 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007525 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007526 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7527 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007528
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007529 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007530 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7531 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007532 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007533 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7534 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007535 }
7536
Dan Gohman475871a2008-07-27 21:46:04 +00007537 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007538 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007539}
Evan Chenga3195e82006-01-12 22:54:21 +00007540
Dan Gohmand858e902010-04-17 15:26:15 +00007541SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7542 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007543 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007544
Dale Johannesen0488fb62010-09-30 23:57:10 +00007545 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007546 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007547
Owen Anderson825b72b2009-08-11 20:47:22 +00007548 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007549 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007550
Eli Friedman36df4992009-05-27 00:47:34 +00007551 // These are really Legal; return the operand so the caller accepts it as
7552 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007553 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007554 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007555 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007556 Subtarget->is64Bit()) {
7557 return Op;
7558 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007559
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007560 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007561 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007562 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007563 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007564 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007565 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007566 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007567 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007568 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007569 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7570}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007571
Owen Andersone50ed302009-08-10 22:56:29 +00007572SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007573 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007574 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007575 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007576 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007577 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007578 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007579 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007580 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007581 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007582 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007583
Chris Lattner492a43e2010-09-22 01:28:21 +00007584 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007585
Stuart Hastings84be9582011-06-02 15:57:11 +00007586 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7587 MachineMemOperand *MMO;
7588 if (FI) {
7589 int SSFI = FI->getIndex();
7590 MMO =
7591 DAG.getMachineFunction()
7592 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7593 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7594 } else {
7595 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7596 StackSlot = StackSlot.getOperand(1);
7597 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007598 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007599 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7600 X86ISD::FILD, DL,
7601 Tys, Ops, array_lengthof(Ops),
7602 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007603
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007604 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007605 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007606 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007607
7608 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7609 // shouldn't be necessary except that RFP cannot be live across
7610 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007611 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007612 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7613 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007614 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007615 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007616 SDValue Ops[] = {
7617 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7618 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007619 MachineMemOperand *MMO =
7620 DAG.getMachineFunction()
7621 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007622 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007623
Chris Lattner492a43e2010-09-22 01:28:21 +00007624 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7625 Ops, array_lengthof(Ops),
7626 Op.getValueType(), MMO);
7627 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007628 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007629 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007630 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007631
Evan Cheng0db9fe62006-04-25 20:13:52 +00007632 return Result;
7633}
7634
Bill Wendling8b8a6362009-01-17 03:56:04 +00007635// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007636SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7637 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007638 // This algorithm is not obvious. Here it is in C code, more or less:
7639 /*
7640 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7641 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7642 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007643
Bill Wendling8b8a6362009-01-17 03:56:04 +00007644 // Copy ints to xmm registers.
7645 __m128i xh = _mm_cvtsi32_si128( hi );
7646 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007647
Bill Wendling8b8a6362009-01-17 03:56:04 +00007648 // Combine into low half of a single xmm register.
7649 __m128i x = _mm_unpacklo_epi32( xh, xl );
7650 __m128d d;
7651 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007652
Bill Wendling8b8a6362009-01-17 03:56:04 +00007653 // Merge in appropriate exponents to give the integer bits the right
7654 // magnitude.
7655 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007656
Bill Wendling8b8a6362009-01-17 03:56:04 +00007657 // Subtract away the biases to deal with the IEEE-754 double precision
7658 // implicit 1.
7659 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007660
Bill Wendling8b8a6362009-01-17 03:56:04 +00007661 // All conversions up to here are exact. The correctly rounded result is
7662 // calculated using the current rounding mode using the following
7663 // horizontal add.
7664 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7665 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7666 // store doesn't really need to be here (except
7667 // maybe to zero the other double)
7668 return sd;
7669 }
7670 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007671
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007672 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007673 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007674
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007675 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007676 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007677 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7678 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7679 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7680 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007681 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007682 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007683
Bill Wendling8b8a6362009-01-17 03:56:04 +00007684 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007685 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007686 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007687 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007688 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007689 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007690 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007691
Owen Anderson825b72b2009-08-11 20:47:22 +00007692 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7693 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007694 Op.getOperand(0),
7695 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007696 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7697 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007698 Op.getOperand(0),
7699 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007700 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7701 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
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 Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007705 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007706 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007707 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007708 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007709 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007710
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007711 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007712 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007713 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7714 DAG.getUNDEF(MVT::v2f64), ShufMask);
7715 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7716 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007717 DAG.getIntPtrConstant(0));
7718}
7719
Bill Wendling8b8a6362009-01-17 03:56:04 +00007720// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007721SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7722 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007723 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007724 // FP constant to bias correct the final result.
7725 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007726 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007727
7728 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007729 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007730 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007731
Eli Friedmanf3704762011-08-29 21:15:46 +00007732 // Zero out the upper parts of the register.
7733 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasSSE2(), DAG);
7734
Owen Anderson825b72b2009-08-11 20:47:22 +00007735 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007736 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007737 DAG.getIntPtrConstant(0));
7738
7739 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007741 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007742 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007743 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007744 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007745 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007746 MVT::v2f64, Bias)));
7747 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007748 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007749 DAG.getIntPtrConstant(0));
7750
7751 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007752 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007753
7754 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007755 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007756
Owen Anderson825b72b2009-08-11 20:47:22 +00007757 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007758 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007759 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007760 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007761 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007762 }
7763
7764 // Handle final rounding.
7765 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007766}
7767
Dan Gohmand858e902010-04-17 15:26:15 +00007768SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7769 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007770 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007771 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007772
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007773 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007774 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7775 // the optimization here.
7776 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007777 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007778
Owen Andersone50ed302009-08-10 22:56:29 +00007779 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007780 EVT DstVT = Op.getValueType();
7781 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007782 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007783 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007784 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007785
7786 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007787 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007788 if (SrcVT == MVT::i32) {
7789 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7790 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7791 getPointerTy(), StackSlot, WordOff);
7792 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007793 StackSlot, MachinePointerInfo(),
7794 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007795 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007796 OffsetSlot, MachinePointerInfo(),
7797 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007798 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7799 return Fild;
7800 }
7801
7802 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7803 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007804 StackSlot, MachinePointerInfo(),
7805 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007806 // For i64 source, we need to add the appropriate power of 2 if the input
7807 // was negative. This is the same as the optimization in
7808 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7809 // we must be careful to do the computation in x87 extended precision, not
7810 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007811 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7812 MachineMemOperand *MMO =
7813 DAG.getMachineFunction()
7814 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7815 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007816
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007817 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7818 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007819 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7820 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007821
7822 APInt FF(32, 0x5F800000ULL);
7823
7824 // Check whether the sign bit is set.
7825 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7826 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7827 ISD::SETLT);
7828
7829 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7830 SDValue FudgePtr = DAG.getConstantPool(
7831 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7832 getPointerTy());
7833
7834 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7835 SDValue Zero = DAG.getIntPtrConstant(0);
7836 SDValue Four = DAG.getIntPtrConstant(4);
7837 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7838 Zero, Four);
7839 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7840
7841 // Load the value out, extending it from f32 to f80.
7842 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007843 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007844 FudgePtr, MachinePointerInfo::getConstantPool(),
7845 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007846 // Extend everything to 80 bits to force it to be done on x87.
7847 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7848 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007849}
7850
Dan Gohman475871a2008-07-27 21:46:04 +00007851std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007852FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007853 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007854
Owen Andersone50ed302009-08-10 22:56:29 +00007855 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007856
7857 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007858 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7859 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007860 }
7861
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7863 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007864 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007865
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007866 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007867 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007868 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007869 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007870 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007871 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007872 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007873 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007874
Evan Cheng87c89352007-10-15 20:11:21 +00007875 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7876 // stack slot.
7877 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007878 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007879 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007880 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007881
Michael J. Spencerec38de22010-10-10 22:04:20 +00007882
7883
Evan Cheng0db9fe62006-04-25 20:13:52 +00007884 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007885 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007886 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007887 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7888 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7889 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007890 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007891
Dan Gohman475871a2008-07-27 21:46:04 +00007892 SDValue Chain = DAG.getEntryNode();
7893 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007894 EVT TheVT = Op.getOperand(0).getValueType();
7895 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007896 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007897 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007898 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007899 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007900 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007901 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007902 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007903 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007904
Chris Lattner492a43e2010-09-22 01:28:21 +00007905 MachineMemOperand *MMO =
7906 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7907 MachineMemOperand::MOLoad, MemSize, MemSize);
7908 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7909 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007910 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007911 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007912 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7913 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007914
Chris Lattner07290932010-09-22 01:05:16 +00007915 MachineMemOperand *MMO =
7916 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7917 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007918
Evan Cheng0db9fe62006-04-25 20:13:52 +00007919 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007920 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007921 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7922 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007923
Chris Lattner27a6c732007-11-24 07:07:01 +00007924 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007925}
7926
Dan Gohmand858e902010-04-17 15:26:15 +00007927SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7928 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007929 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007930 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007931
Eli Friedman948e95a2009-05-23 09:59:16 +00007932 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007933 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007934 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7935 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007936
Chris Lattner27a6c732007-11-24 07:07:01 +00007937 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007938 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007939 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007940}
7941
Dan Gohmand858e902010-04-17 15:26:15 +00007942SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7943 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007944 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7945 SDValue FIST = Vals.first, StackSlot = Vals.second;
7946 assert(FIST.getNode() && "Unexpected failure");
7947
7948 // Load the result.
7949 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007950 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007951}
7952
Dan Gohmand858e902010-04-17 15:26:15 +00007953SDValue X86TargetLowering::LowerFABS(SDValue Op,
7954 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007955 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007956 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007957 EVT VT = Op.getValueType();
7958 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007959 if (VT.isVector())
7960 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007961 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007962 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007963 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007964 CV.push_back(C);
7965 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007966 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007967 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007968 CV.push_back(C);
7969 CV.push_back(C);
7970 CV.push_back(C);
7971 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007972 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007973 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007974 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007975 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007976 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007977 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007978 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007979}
7980
Dan Gohmand858e902010-04-17 15:26:15 +00007981SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007982 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007983 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007984 EVT VT = Op.getValueType();
7985 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007986 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007987 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007988 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007989 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007990 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007991 CV.push_back(C);
7992 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007993 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007994 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007995 CV.push_back(C);
7996 CV.push_back(C);
7997 CV.push_back(C);
7998 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007999 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008000 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008001 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008002 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008003 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008004 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008005 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008006 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008007 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008008 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008009 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008010 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008011 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008012 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008013 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008014}
8015
Dan Gohmand858e902010-04-17 15:26:15 +00008016SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008017 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008018 SDValue Op0 = Op.getOperand(0);
8019 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008020 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008021 EVT VT = Op.getValueType();
8022 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008023
8024 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008025 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008026 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008027 SrcVT = VT;
8028 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008029 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008030 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008031 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008032 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008033 }
8034
8035 // At this point the operands and the result should have the same
8036 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008037
Evan Cheng68c47cb2007-01-05 07:55:56 +00008038 // First get the sign bit of second operand.
8039 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008040 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008041 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8042 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008043 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008044 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8045 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8046 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8047 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008048 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008049 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008050 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008051 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008052 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008053 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008054 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008055
8056 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008057 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008058 // Op0 is MVT::f32, Op1 is MVT::f64.
8059 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8060 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8061 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008062 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008064 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008065 }
8066
Evan Cheng73d6cf12007-01-05 21:37:56 +00008067 // Clear first operand sign bit.
8068 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008069 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008070 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8071 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008072 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008073 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8074 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8075 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8076 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008077 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008078 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008079 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008080 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008081 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008082 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008083 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008084
8085 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008086 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008087}
8088
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008089SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8090 SDValue N0 = Op.getOperand(0);
8091 DebugLoc dl = Op.getDebugLoc();
8092 EVT VT = Op.getValueType();
8093
8094 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8095 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8096 DAG.getConstant(1, VT));
8097 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8098}
8099
Dan Gohman076aee32009-03-04 19:44:21 +00008100/// Emit nodes that will be selected as "test Op0,Op0", or something
8101/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008102SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008103 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008104 DebugLoc dl = Op.getDebugLoc();
8105
Dan Gohman31125812009-03-07 01:58:32 +00008106 // CF and OF aren't always set the way we want. Determine which
8107 // of these we need.
8108 bool NeedCF = false;
8109 bool NeedOF = false;
8110 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008111 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008112 case X86::COND_A: case X86::COND_AE:
8113 case X86::COND_B: case X86::COND_BE:
8114 NeedCF = true;
8115 break;
8116 case X86::COND_G: case X86::COND_GE:
8117 case X86::COND_L: case X86::COND_LE:
8118 case X86::COND_O: case X86::COND_NO:
8119 NeedOF = true;
8120 break;
Dan Gohman31125812009-03-07 01:58:32 +00008121 }
8122
Dan Gohman076aee32009-03-04 19:44:21 +00008123 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008124 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8125 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008126 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8127 // Emit a CMP with 0, which is the TEST pattern.
8128 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8129 DAG.getConstant(0, Op.getValueType()));
8130
8131 unsigned Opcode = 0;
8132 unsigned NumOperands = 0;
8133 switch (Op.getNode()->getOpcode()) {
8134 case ISD::ADD:
8135 // Due to an isel shortcoming, be conservative if this add is likely to be
8136 // selected as part of a load-modify-store instruction. When the root node
8137 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8138 // uses of other nodes in the match, such as the ADD in this case. This
8139 // leads to the ADD being left around and reselected, with the result being
8140 // two adds in the output. Alas, even if none our users are stores, that
8141 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8142 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8143 // climbing the DAG back to the root, and it doesn't seem to be worth the
8144 // effort.
8145 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008146 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008147 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8148 goto default_case;
8149
8150 if (ConstantSDNode *C =
8151 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8152 // An add of one will be selected as an INC.
8153 if (C->getAPIntValue() == 1) {
8154 Opcode = X86ISD::INC;
8155 NumOperands = 1;
8156 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008157 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008158
8159 // An add of negative one (subtract of one) will be selected as a DEC.
8160 if (C->getAPIntValue().isAllOnesValue()) {
8161 Opcode = X86ISD::DEC;
8162 NumOperands = 1;
8163 break;
8164 }
Dan Gohman076aee32009-03-04 19:44:21 +00008165 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008166
8167 // Otherwise use a regular EFLAGS-setting add.
8168 Opcode = X86ISD::ADD;
8169 NumOperands = 2;
8170 break;
8171 case ISD::AND: {
8172 // If the primary and result isn't used, don't bother using X86ISD::AND,
8173 // because a TEST instruction will be better.
8174 bool NonFlagUse = false;
8175 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8176 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8177 SDNode *User = *UI;
8178 unsigned UOpNo = UI.getOperandNo();
8179 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8180 // Look pass truncate.
8181 UOpNo = User->use_begin().getOperandNo();
8182 User = *User->use_begin();
8183 }
8184
8185 if (User->getOpcode() != ISD::BRCOND &&
8186 User->getOpcode() != ISD::SETCC &&
8187 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8188 NonFlagUse = true;
8189 break;
8190 }
Dan Gohman076aee32009-03-04 19:44:21 +00008191 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008192
8193 if (!NonFlagUse)
8194 break;
8195 }
8196 // FALL THROUGH
8197 case ISD::SUB:
8198 case ISD::OR:
8199 case ISD::XOR:
8200 // Due to the ISEL shortcoming noted above, be conservative if this op is
8201 // likely to be selected as part of a load-modify-store instruction.
8202 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8203 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8204 if (UI->getOpcode() == ISD::STORE)
8205 goto default_case;
8206
8207 // Otherwise use a regular EFLAGS-setting instruction.
8208 switch (Op.getNode()->getOpcode()) {
8209 default: llvm_unreachable("unexpected operator!");
8210 case ISD::SUB: Opcode = X86ISD::SUB; break;
8211 case ISD::OR: Opcode = X86ISD::OR; break;
8212 case ISD::XOR: Opcode = X86ISD::XOR; break;
8213 case ISD::AND: Opcode = X86ISD::AND; break;
8214 }
8215
8216 NumOperands = 2;
8217 break;
8218 case X86ISD::ADD:
8219 case X86ISD::SUB:
8220 case X86ISD::INC:
8221 case X86ISD::DEC:
8222 case X86ISD::OR:
8223 case X86ISD::XOR:
8224 case X86ISD::AND:
8225 return SDValue(Op.getNode(), 1);
8226 default:
8227 default_case:
8228 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008229 }
8230
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008231 if (Opcode == 0)
8232 // Emit a CMP with 0, which is the TEST pattern.
8233 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8234 DAG.getConstant(0, Op.getValueType()));
8235
8236 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8237 SmallVector<SDValue, 4> Ops;
8238 for (unsigned i = 0; i != NumOperands; ++i)
8239 Ops.push_back(Op.getOperand(i));
8240
8241 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8242 DAG.ReplaceAllUsesWith(Op, New);
8243 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008244}
8245
8246/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8247/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008248SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008249 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008250 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8251 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008252 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008253
8254 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008255 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008256}
8257
Evan Chengd40d03e2010-01-06 19:38:29 +00008258/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8259/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008260SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8261 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008262 SDValue Op0 = And.getOperand(0);
8263 SDValue Op1 = And.getOperand(1);
8264 if (Op0.getOpcode() == ISD::TRUNCATE)
8265 Op0 = Op0.getOperand(0);
8266 if (Op1.getOpcode() == ISD::TRUNCATE)
8267 Op1 = Op1.getOperand(0);
8268
Evan Chengd40d03e2010-01-06 19:38:29 +00008269 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008270 if (Op1.getOpcode() == ISD::SHL)
8271 std::swap(Op0, Op1);
8272 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008273 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8274 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008275 // If we looked past a truncate, check that it's only truncating away
8276 // known zeros.
8277 unsigned BitWidth = Op0.getValueSizeInBits();
8278 unsigned AndBitWidth = And.getValueSizeInBits();
8279 if (BitWidth > AndBitWidth) {
8280 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8281 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8282 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8283 return SDValue();
8284 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008285 LHS = Op1;
8286 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008287 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008288 } else if (Op1.getOpcode() == ISD::Constant) {
8289 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8290 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008291 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8292 LHS = AndLHS.getOperand(0);
8293 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008294 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008295 }
Evan Cheng0488db92007-09-25 01:57:46 +00008296
Evan Chengd40d03e2010-01-06 19:38:29 +00008297 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008298 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008299 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008300 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008301 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008302 // Also promote i16 to i32 for performance / code size reason.
8303 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008304 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008305 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008306
Evan Chengd40d03e2010-01-06 19:38:29 +00008307 // If the operand types disagree, extend the shift amount to match. Since
8308 // BT ignores high bits (like shifts) we can use anyextend.
8309 if (LHS.getValueType() != RHS.getValueType())
8310 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008311
Evan Chengd40d03e2010-01-06 19:38:29 +00008312 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8313 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8314 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8315 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008316 }
8317
Evan Cheng54de3ea2010-01-05 06:52:31 +00008318 return SDValue();
8319}
8320
Dan Gohmand858e902010-04-17 15:26:15 +00008321SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00008322 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8323 SDValue Op0 = Op.getOperand(0);
8324 SDValue Op1 = Op.getOperand(1);
8325 DebugLoc dl = Op.getDebugLoc();
8326 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8327
8328 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008329 // Lower (X & (1 << N)) == 0 to BT(X, N).
8330 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8331 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008332 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008333 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008334 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008335 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8336 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8337 if (NewSetCC.getNode())
8338 return NewSetCC;
8339 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008340
Chris Lattner481eebc2010-12-19 21:23:48 +00008341 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8342 // these.
8343 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008344 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008345 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8346 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008347
Chris Lattner481eebc2010-12-19 21:23:48 +00008348 // If the input is a setcc, then reuse the input setcc or use a new one with
8349 // the inverted condition.
8350 if (Op0.getOpcode() == X86ISD::SETCC) {
8351 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8352 bool Invert = (CC == ISD::SETNE) ^
8353 cast<ConstantSDNode>(Op1)->isNullValue();
8354 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008355
Evan Cheng2c755ba2010-02-27 07:36:59 +00008356 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008357 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8358 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8359 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008360 }
8361
Evan Chenge5b51ac2010-04-17 06:13:15 +00008362 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008363 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008364 if (X86CC == X86::COND_INVALID)
8365 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008366
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008367 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008368 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008369 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008370}
8371
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008372// Lower256IntVETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
8373// ones, and then concatenate the result back.
8374static SDValue Lower256IntVETCC(SDValue Op, SelectionDAG &DAG) {
8375 EVT VT = Op.getValueType();
8376
8377 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::VSETCC &&
8378 "Unsupported value type for operation");
8379
8380 int NumElems = VT.getVectorNumElements();
8381 DebugLoc dl = Op.getDebugLoc();
8382 SDValue CC = Op.getOperand(2);
8383 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8384 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8385
8386 // Extract the LHS vectors
8387 SDValue LHS = Op.getOperand(0);
8388 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8389 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8390
8391 // Extract the RHS vectors
8392 SDValue RHS = Op.getOperand(1);
8393 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8394 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8395
8396 // Issue the operation on the smaller types and concatenate the result back
8397 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8398 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8399 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8400 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8401 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8402}
8403
8404
Dan Gohmand858e902010-04-17 15:26:15 +00008405SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008406 SDValue Cond;
8407 SDValue Op0 = Op.getOperand(0);
8408 SDValue Op1 = Op.getOperand(1);
8409 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008410 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008411 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8412 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008413 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008414
8415 if (isFP) {
8416 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008417 EVT EltVT = Op0.getValueType().getVectorElementType();
8418 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8419
8420 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008421 bool Swap = false;
8422
8423 switch (SetCCOpcode) {
8424 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008425 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008426 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00008427 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00008428 case ISD::SETGT: Swap = true; // Fallthrough
8429 case ISD::SETLT:
8430 case ISD::SETOLT: SSECC = 1; break;
8431 case ISD::SETOGE:
8432 case ISD::SETGE: Swap = true; // Fallthrough
8433 case ISD::SETLE:
8434 case ISD::SETOLE: SSECC = 2; break;
8435 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008436 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008437 case ISD::SETNE: SSECC = 4; break;
8438 case ISD::SETULE: Swap = true;
8439 case ISD::SETUGE: SSECC = 5; break;
8440 case ISD::SETULT: Swap = true;
8441 case ISD::SETUGT: SSECC = 6; break;
8442 case ISD::SETO: SSECC = 7; break;
8443 }
8444 if (Swap)
8445 std::swap(Op0, Op1);
8446
Nate Begemanfb8ead02008-07-25 19:05:58 +00008447 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008448 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008449 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008450 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008451 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8452 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008453 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008454 }
8455 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008456 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008457 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8458 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008459 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008460 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008461 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008462 }
8463 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00008464 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008465 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008466
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008467 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008468 if (!isFP && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008469 return Lower256IntVETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008470
Nate Begeman30a0de92008-07-17 16:51:19 +00008471 // We are handling one of the integer comparisons here. Since SSE only has
8472 // GT and EQ comparisons for integer, swapping operands and multiple
8473 // operations may be required for some comparisons.
8474 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8475 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008476
Owen Anderson825b72b2009-08-11 20:47:22 +00008477 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008478 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008479 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008480 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008481 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8482 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008483 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008484
Nate Begeman30a0de92008-07-17 16:51:19 +00008485 switch (SetCCOpcode) {
8486 default: break;
8487 case ISD::SETNE: Invert = true;
8488 case ISD::SETEQ: Opc = EQOpc; break;
8489 case ISD::SETLT: Swap = true;
8490 case ISD::SETGT: Opc = GTOpc; break;
8491 case ISD::SETGE: Swap = true;
8492 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8493 case ISD::SETULT: Swap = true;
8494 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8495 case ISD::SETUGE: Swap = true;
8496 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8497 }
8498 if (Swap)
8499 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008500
Nate Begeman30a0de92008-07-17 16:51:19 +00008501 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8502 // bits of the inputs before performing those operations.
8503 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008504 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008505 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8506 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008507 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008508 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8509 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008510 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8511 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008512 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008513
Dale Johannesenace16102009-02-03 19:33:06 +00008514 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008515
8516 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008517 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008518 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008519
Nate Begeman30a0de92008-07-17 16:51:19 +00008520 return Result;
8521}
Evan Cheng0488db92007-09-25 01:57:46 +00008522
Evan Cheng370e5342008-12-03 08:38:43 +00008523// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008524static bool isX86LogicalCmp(SDValue Op) {
8525 unsigned Opc = Op.getNode()->getOpcode();
8526 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8527 return true;
8528 if (Op.getResNo() == 1 &&
8529 (Opc == X86ISD::ADD ||
8530 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008531 Opc == X86ISD::ADC ||
8532 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008533 Opc == X86ISD::SMUL ||
8534 Opc == X86ISD::UMUL ||
8535 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008536 Opc == X86ISD::DEC ||
8537 Opc == X86ISD::OR ||
8538 Opc == X86ISD::XOR ||
8539 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008540 return true;
8541
Chris Lattner9637d5b2010-12-05 07:49:54 +00008542 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8543 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008544
Dan Gohman076aee32009-03-04 19:44:21 +00008545 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008546}
8547
Chris Lattnera2b56002010-12-05 01:23:24 +00008548static bool isZero(SDValue V) {
8549 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8550 return C && C->isNullValue();
8551}
8552
Chris Lattner96908b12010-12-05 02:00:51 +00008553static bool isAllOnes(SDValue V) {
8554 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8555 return C && C->isAllOnesValue();
8556}
8557
Dan Gohmand858e902010-04-17 15:26:15 +00008558SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008559 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008560 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008561 SDValue Op1 = Op.getOperand(1);
8562 SDValue Op2 = Op.getOperand(2);
8563 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008564 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008565
Dan Gohman1a492952009-10-20 16:22:37 +00008566 if (Cond.getOpcode() == ISD::SETCC) {
8567 SDValue NewCond = LowerSETCC(Cond, DAG);
8568 if (NewCond.getNode())
8569 Cond = NewCond;
8570 }
Evan Cheng734503b2006-09-11 02:19:56 +00008571
Chris Lattnera2b56002010-12-05 01:23:24 +00008572 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008573 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008574 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008575 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008576 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008577 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8578 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008579 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008580
Chris Lattnera2b56002010-12-05 01:23:24 +00008581 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008582
8583 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008584 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8585 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008586
8587 SDValue CmpOp0 = Cmp.getOperand(0);
8588 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8589 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008590
Chris Lattner96908b12010-12-05 02:00:51 +00008591 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008592 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8593 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008594
Chris Lattner96908b12010-12-05 02:00:51 +00008595 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8596 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008597
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008598 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008599 if (N2C == 0 || !N2C->isNullValue())
8600 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8601 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008602 }
8603 }
8604
Chris Lattnera2b56002010-12-05 01:23:24 +00008605 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008606 if (Cond.getOpcode() == ISD::AND &&
8607 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8608 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008609 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008610 Cond = Cond.getOperand(0);
8611 }
8612
Evan Cheng3f41d662007-10-08 22:16:29 +00008613 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8614 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008615 if (Cond.getOpcode() == X86ISD::SETCC ||
8616 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008617 CC = Cond.getOperand(0);
8618
Dan Gohman475871a2008-07-27 21:46:04 +00008619 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008620 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008621 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008622
Evan Cheng3f41d662007-10-08 22:16:29 +00008623 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008624 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008625 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008626 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008627
Chris Lattnerd1980a52009-03-12 06:52:53 +00008628 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8629 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008630 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008631 addTest = false;
8632 }
8633 }
8634
8635 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008636 // Look pass the truncate.
8637 if (Cond.getOpcode() == ISD::TRUNCATE)
8638 Cond = Cond.getOperand(0);
8639
8640 // We know the result of AND is compared against zero. Try to match
8641 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008642 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008643 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008644 if (NewSetCC.getNode()) {
8645 CC = NewSetCC.getOperand(0);
8646 Cond = NewSetCC.getOperand(1);
8647 addTest = false;
8648 }
8649 }
8650 }
8651
8652 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008653 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008654 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008655 }
8656
Benjamin Kramere915ff32010-12-22 23:09:28 +00008657 // a < b ? -1 : 0 -> RES = ~setcc_carry
8658 // a < b ? 0 : -1 -> RES = setcc_carry
8659 // a >= b ? -1 : 0 -> RES = setcc_carry
8660 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8661 if (Cond.getOpcode() == X86ISD::CMP) {
8662 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8663
8664 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8665 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8666 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8667 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8668 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8669 return DAG.getNOT(DL, Res, Res.getValueType());
8670 return Res;
8671 }
8672 }
8673
Evan Cheng0488db92007-09-25 01:57:46 +00008674 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8675 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008676 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008677 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008678 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008679}
8680
Evan Cheng370e5342008-12-03 08:38:43 +00008681// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8682// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8683// from the AND / OR.
8684static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8685 Opc = Op.getOpcode();
8686 if (Opc != ISD::OR && Opc != ISD::AND)
8687 return false;
8688 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8689 Op.getOperand(0).hasOneUse() &&
8690 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8691 Op.getOperand(1).hasOneUse());
8692}
8693
Evan Cheng961d6d42009-02-02 08:19:07 +00008694// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8695// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008696static bool isXor1OfSetCC(SDValue Op) {
8697 if (Op.getOpcode() != ISD::XOR)
8698 return false;
8699 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8700 if (N1C && N1C->getAPIntValue() == 1) {
8701 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8702 Op.getOperand(0).hasOneUse();
8703 }
8704 return false;
8705}
8706
Dan Gohmand858e902010-04-17 15:26:15 +00008707SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008708 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008709 SDValue Chain = Op.getOperand(0);
8710 SDValue Cond = Op.getOperand(1);
8711 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008712 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008713 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008714
Dan Gohman1a492952009-10-20 16:22:37 +00008715 if (Cond.getOpcode() == ISD::SETCC) {
8716 SDValue NewCond = LowerSETCC(Cond, DAG);
8717 if (NewCond.getNode())
8718 Cond = NewCond;
8719 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008720#if 0
8721 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008722 else if (Cond.getOpcode() == X86ISD::ADD ||
8723 Cond.getOpcode() == X86ISD::SUB ||
8724 Cond.getOpcode() == X86ISD::SMUL ||
8725 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008726 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008727#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008728
Evan Chengad9c0a32009-12-15 00:53:42 +00008729 // Look pass (and (setcc_carry (cmp ...)), 1).
8730 if (Cond.getOpcode() == ISD::AND &&
8731 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8732 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008733 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008734 Cond = Cond.getOperand(0);
8735 }
8736
Evan Cheng3f41d662007-10-08 22:16:29 +00008737 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8738 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008739 if (Cond.getOpcode() == X86ISD::SETCC ||
8740 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008741 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008742
Dan Gohman475871a2008-07-27 21:46:04 +00008743 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008744 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008745 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008746 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008747 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008748 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008749 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008750 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008751 default: break;
8752 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008753 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008754 // These can only come from an arithmetic instruction with overflow,
8755 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008756 Cond = Cond.getNode()->getOperand(1);
8757 addTest = false;
8758 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008759 }
Evan Cheng0488db92007-09-25 01:57:46 +00008760 }
Evan Cheng370e5342008-12-03 08:38:43 +00008761 } else {
8762 unsigned CondOpc;
8763 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8764 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008765 if (CondOpc == ISD::OR) {
8766 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8767 // two branches instead of an explicit OR instruction with a
8768 // separate test.
8769 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008770 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008771 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008772 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008773 Chain, Dest, CC, Cmp);
8774 CC = Cond.getOperand(1).getOperand(0);
8775 Cond = Cmp;
8776 addTest = false;
8777 }
8778 } else { // ISD::AND
8779 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8780 // two branches instead of an explicit AND instruction with a
8781 // separate test. However, we only do this if this block doesn't
8782 // have a fall-through edge, because this requires an explicit
8783 // jmp when the condition is false.
8784 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008785 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008786 Op.getNode()->hasOneUse()) {
8787 X86::CondCode CCode =
8788 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8789 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008790 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008791 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008792 // Look for an unconditional branch following this conditional branch.
8793 // We need this because we need to reverse the successors in order
8794 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008795 if (User->getOpcode() == ISD::BR) {
8796 SDValue FalseBB = User->getOperand(1);
8797 SDNode *NewBR =
8798 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008799 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008800 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008801 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008802
Dale Johannesene4d209d2009-02-03 20:21:25 +00008803 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008804 Chain, Dest, CC, Cmp);
8805 X86::CondCode CCode =
8806 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8807 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008808 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008809 Cond = Cmp;
8810 addTest = false;
8811 }
8812 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008813 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008814 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8815 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8816 // It should be transformed during dag combiner except when the condition
8817 // is set by a arithmetics with overflow node.
8818 X86::CondCode CCode =
8819 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8820 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008821 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008822 Cond = Cond.getOperand(0).getOperand(1);
8823 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008824 }
Evan Cheng0488db92007-09-25 01:57:46 +00008825 }
8826
8827 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008828 // Look pass the truncate.
8829 if (Cond.getOpcode() == ISD::TRUNCATE)
8830 Cond = Cond.getOperand(0);
8831
8832 // We know the result of AND is compared against zero. Try to match
8833 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008834 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008835 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8836 if (NewSetCC.getNode()) {
8837 CC = NewSetCC.getOperand(0);
8838 Cond = NewSetCC.getOperand(1);
8839 addTest = false;
8840 }
8841 }
8842 }
8843
8844 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008845 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008846 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008847 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008848 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008849 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008850}
8851
Anton Korobeynikove060b532007-04-17 19:34:00 +00008852
8853// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8854// Calls to _alloca is needed to probe the stack when allocating more than 4k
8855// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8856// that the guard pages used by the OS virtual memory manager are allocated in
8857// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008858SDValue
8859X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008860 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008861 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8862 EnableSegmentedStacks) &&
8863 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008864 "are being used");
8865 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008866 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008867
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008868 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008869 SDValue Chain = Op.getOperand(0);
8870 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008871 // FIXME: Ensure alignment here
8872
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008873 bool Is64Bit = Subtarget->is64Bit();
8874 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008875
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008876 if (EnableSegmentedStacks) {
8877 MachineFunction &MF = DAG.getMachineFunction();
8878 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008879
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008880 if (Is64Bit) {
8881 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008882 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008883 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008884
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008885 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8886 I != E; I++)
8887 if (I->hasNestAttr())
8888 report_fatal_error("Cannot use segmented stacks with functions that "
8889 "have nested arguments.");
8890 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008891
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008892 const TargetRegisterClass *AddrRegClass =
8893 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8894 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8895 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8896 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8897 DAG.getRegister(Vreg, SPTy));
8898 SDValue Ops1[2] = { Value, Chain };
8899 return DAG.getMergeValues(Ops1, 2, dl);
8900 } else {
8901 SDValue Flag;
8902 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008903
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008904 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8905 Flag = Chain.getValue(1);
8906 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008907
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008908 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8909 Flag = Chain.getValue(1);
8910
8911 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8912
8913 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8914 return DAG.getMergeValues(Ops1, 2, dl);
8915 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008916}
8917
Dan Gohmand858e902010-04-17 15:26:15 +00008918SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008919 MachineFunction &MF = DAG.getMachineFunction();
8920 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8921
Dan Gohman69de1932008-02-06 22:27:42 +00008922 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008923 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008924
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008925 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008926 // vastart just stores the address of the VarArgsFrameIndex slot into the
8927 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008928 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8929 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008930 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8931 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008932 }
8933
8934 // __va_list_tag:
8935 // gp_offset (0 - 6 * 8)
8936 // fp_offset (48 - 48 + 8 * 16)
8937 // overflow_arg_area (point to parameters coming in memory).
8938 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008939 SmallVector<SDValue, 8> MemOps;
8940 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008941 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008942 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008943 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8944 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008945 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008946 MemOps.push_back(Store);
8947
8948 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008949 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008950 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008951 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008952 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8953 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008954 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008955 MemOps.push_back(Store);
8956
8957 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008958 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008959 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008960 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8961 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008962 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8963 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008964 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008965 MemOps.push_back(Store);
8966
8967 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008968 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008969 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008970 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8971 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008972 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8973 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008974 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008975 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008976 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008977}
8978
Dan Gohmand858e902010-04-17 15:26:15 +00008979SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008980 assert(Subtarget->is64Bit() &&
8981 "LowerVAARG only handles 64-bit va_arg!");
8982 assert((Subtarget->isTargetLinux() ||
8983 Subtarget->isTargetDarwin()) &&
8984 "Unhandled target in LowerVAARG");
8985 assert(Op.getNode()->getNumOperands() == 4);
8986 SDValue Chain = Op.getOperand(0);
8987 SDValue SrcPtr = Op.getOperand(1);
8988 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8989 unsigned Align = Op.getConstantOperandVal(3);
8990 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008991
Dan Gohman320afb82010-10-12 18:00:49 +00008992 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008993 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00008994 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8995 uint8_t ArgMode;
8996
8997 // Decide which area this value should be read from.
8998 // TODO: Implement the AMD64 ABI in its entirety. This simple
8999 // selection mechanism works only for the basic types.
9000 if (ArgVT == MVT::f80) {
9001 llvm_unreachable("va_arg for f80 not yet implemented");
9002 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9003 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9004 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9005 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9006 } else {
9007 llvm_unreachable("Unhandled argument type in LowerVAARG");
9008 }
9009
9010 if (ArgMode == 2) {
9011 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009012 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009013 !(DAG.getMachineFunction()
9014 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009015 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009016 }
9017
9018 // Insert VAARG_64 node into the DAG
9019 // VAARG_64 returns two values: Variable Argument Address, Chain
9020 SmallVector<SDValue, 11> InstOps;
9021 InstOps.push_back(Chain);
9022 InstOps.push_back(SrcPtr);
9023 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9024 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9025 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9026 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9027 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9028 VTs, &InstOps[0], InstOps.size(),
9029 MVT::i64,
9030 MachinePointerInfo(SV),
9031 /*Align=*/0,
9032 /*Volatile=*/false,
9033 /*ReadMem=*/true,
9034 /*WriteMem=*/true);
9035 Chain = VAARG.getValue(1);
9036
9037 // Load the next argument and return it
9038 return DAG.getLoad(ArgVT, dl,
9039 Chain,
9040 VAARG,
9041 MachinePointerInfo(),
9042 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009043}
9044
Dan Gohmand858e902010-04-17 15:26:15 +00009045SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009046 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009047 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009048 SDValue Chain = Op.getOperand(0);
9049 SDValue DstPtr = Op.getOperand(1);
9050 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009051 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9052 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009053 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009054
Chris Lattnere72f2022010-09-21 05:40:29 +00009055 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009056 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009057 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009058 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009059}
9060
Dan Gohman475871a2008-07-27 21:46:04 +00009061SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009062X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009063 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009064 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009065 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009066 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009067 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009068 case Intrinsic::x86_sse_comieq_ss:
9069 case Intrinsic::x86_sse_comilt_ss:
9070 case Intrinsic::x86_sse_comile_ss:
9071 case Intrinsic::x86_sse_comigt_ss:
9072 case Intrinsic::x86_sse_comige_ss:
9073 case Intrinsic::x86_sse_comineq_ss:
9074 case Intrinsic::x86_sse_ucomieq_ss:
9075 case Intrinsic::x86_sse_ucomilt_ss:
9076 case Intrinsic::x86_sse_ucomile_ss:
9077 case Intrinsic::x86_sse_ucomigt_ss:
9078 case Intrinsic::x86_sse_ucomige_ss:
9079 case Intrinsic::x86_sse_ucomineq_ss:
9080 case Intrinsic::x86_sse2_comieq_sd:
9081 case Intrinsic::x86_sse2_comilt_sd:
9082 case Intrinsic::x86_sse2_comile_sd:
9083 case Intrinsic::x86_sse2_comigt_sd:
9084 case Intrinsic::x86_sse2_comige_sd:
9085 case Intrinsic::x86_sse2_comineq_sd:
9086 case Intrinsic::x86_sse2_ucomieq_sd:
9087 case Intrinsic::x86_sse2_ucomilt_sd:
9088 case Intrinsic::x86_sse2_ucomile_sd:
9089 case Intrinsic::x86_sse2_ucomigt_sd:
9090 case Intrinsic::x86_sse2_ucomige_sd:
9091 case Intrinsic::x86_sse2_ucomineq_sd: {
9092 unsigned Opc = 0;
9093 ISD::CondCode CC = ISD::SETCC_INVALID;
9094 switch (IntNo) {
9095 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009096 case Intrinsic::x86_sse_comieq_ss:
9097 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009098 Opc = X86ISD::COMI;
9099 CC = ISD::SETEQ;
9100 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009101 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009102 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009103 Opc = X86ISD::COMI;
9104 CC = ISD::SETLT;
9105 break;
9106 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009107 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009108 Opc = X86ISD::COMI;
9109 CC = ISD::SETLE;
9110 break;
9111 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009112 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009113 Opc = X86ISD::COMI;
9114 CC = ISD::SETGT;
9115 break;
9116 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009117 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009118 Opc = X86ISD::COMI;
9119 CC = ISD::SETGE;
9120 break;
9121 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009122 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009123 Opc = X86ISD::COMI;
9124 CC = ISD::SETNE;
9125 break;
9126 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009127 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009128 Opc = X86ISD::UCOMI;
9129 CC = ISD::SETEQ;
9130 break;
9131 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009132 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009133 Opc = X86ISD::UCOMI;
9134 CC = ISD::SETLT;
9135 break;
9136 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009137 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009138 Opc = X86ISD::UCOMI;
9139 CC = ISD::SETLE;
9140 break;
9141 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009142 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009143 Opc = X86ISD::UCOMI;
9144 CC = ISD::SETGT;
9145 break;
9146 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009147 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009148 Opc = X86ISD::UCOMI;
9149 CC = ISD::SETGE;
9150 break;
9151 case Intrinsic::x86_sse_ucomineq_ss:
9152 case Intrinsic::x86_sse2_ucomineq_sd:
9153 Opc = X86ISD::UCOMI;
9154 CC = ISD::SETNE;
9155 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009156 }
Evan Cheng734503b2006-09-11 02:19:56 +00009157
Dan Gohman475871a2008-07-27 21:46:04 +00009158 SDValue LHS = Op.getOperand(1);
9159 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009160 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009161 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009162 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9163 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9164 DAG.getConstant(X86CC, MVT::i8), Cond);
9165 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009166 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009167 // ptest and testp intrinsics. The intrinsic these come from are designed to
9168 // return an integer value, not just an instruction so lower it to the ptest
9169 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009170 case Intrinsic::x86_sse41_ptestz:
9171 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009172 case Intrinsic::x86_sse41_ptestnzc:
9173 case Intrinsic::x86_avx_ptestz_256:
9174 case Intrinsic::x86_avx_ptestc_256:
9175 case Intrinsic::x86_avx_ptestnzc_256:
9176 case Intrinsic::x86_avx_vtestz_ps:
9177 case Intrinsic::x86_avx_vtestc_ps:
9178 case Intrinsic::x86_avx_vtestnzc_ps:
9179 case Intrinsic::x86_avx_vtestz_pd:
9180 case Intrinsic::x86_avx_vtestc_pd:
9181 case Intrinsic::x86_avx_vtestnzc_pd:
9182 case Intrinsic::x86_avx_vtestz_ps_256:
9183 case Intrinsic::x86_avx_vtestc_ps_256:
9184 case Intrinsic::x86_avx_vtestnzc_ps_256:
9185 case Intrinsic::x86_avx_vtestz_pd_256:
9186 case Intrinsic::x86_avx_vtestc_pd_256:
9187 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9188 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009189 unsigned X86CC = 0;
9190 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009191 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009192 case Intrinsic::x86_avx_vtestz_ps:
9193 case Intrinsic::x86_avx_vtestz_pd:
9194 case Intrinsic::x86_avx_vtestz_ps_256:
9195 case Intrinsic::x86_avx_vtestz_pd_256:
9196 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009197 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009198 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009199 // ZF = 1
9200 X86CC = X86::COND_E;
9201 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009202 case Intrinsic::x86_avx_vtestc_ps:
9203 case Intrinsic::x86_avx_vtestc_pd:
9204 case Intrinsic::x86_avx_vtestc_ps_256:
9205 case Intrinsic::x86_avx_vtestc_pd_256:
9206 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009207 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009208 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009209 // CF = 1
9210 X86CC = X86::COND_B;
9211 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009212 case Intrinsic::x86_avx_vtestnzc_ps:
9213 case Intrinsic::x86_avx_vtestnzc_pd:
9214 case Intrinsic::x86_avx_vtestnzc_ps_256:
9215 case Intrinsic::x86_avx_vtestnzc_pd_256:
9216 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009217 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009218 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009219 // ZF and CF = 0
9220 X86CC = X86::COND_A;
9221 break;
9222 }
Eric Christopherfd179292009-08-27 18:07:15 +00009223
Eric Christopher71c67532009-07-29 00:28:05 +00009224 SDValue LHS = Op.getOperand(1);
9225 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009226 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9227 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009228 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9229 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9230 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009231 }
Evan Cheng5759f972008-05-04 09:15:50 +00009232
9233 // Fix vector shift instructions where the last operand is a non-immediate
9234 // i32 value.
9235 case Intrinsic::x86_sse2_pslli_w:
9236 case Intrinsic::x86_sse2_pslli_d:
9237 case Intrinsic::x86_sse2_pslli_q:
9238 case Intrinsic::x86_sse2_psrli_w:
9239 case Intrinsic::x86_sse2_psrli_d:
9240 case Intrinsic::x86_sse2_psrli_q:
9241 case Intrinsic::x86_sse2_psrai_w:
9242 case Intrinsic::x86_sse2_psrai_d:
9243 case Intrinsic::x86_mmx_pslli_w:
9244 case Intrinsic::x86_mmx_pslli_d:
9245 case Intrinsic::x86_mmx_pslli_q:
9246 case Intrinsic::x86_mmx_psrli_w:
9247 case Intrinsic::x86_mmx_psrli_d:
9248 case Intrinsic::x86_mmx_psrli_q:
9249 case Intrinsic::x86_mmx_psrai_w:
9250 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009251 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009252 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009253 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009254
9255 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009256 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009257 switch (IntNo) {
9258 case Intrinsic::x86_sse2_pslli_w:
9259 NewIntNo = Intrinsic::x86_sse2_psll_w;
9260 break;
9261 case Intrinsic::x86_sse2_pslli_d:
9262 NewIntNo = Intrinsic::x86_sse2_psll_d;
9263 break;
9264 case Intrinsic::x86_sse2_pslli_q:
9265 NewIntNo = Intrinsic::x86_sse2_psll_q;
9266 break;
9267 case Intrinsic::x86_sse2_psrli_w:
9268 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9269 break;
9270 case Intrinsic::x86_sse2_psrli_d:
9271 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9272 break;
9273 case Intrinsic::x86_sse2_psrli_q:
9274 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9275 break;
9276 case Intrinsic::x86_sse2_psrai_w:
9277 NewIntNo = Intrinsic::x86_sse2_psra_w;
9278 break;
9279 case Intrinsic::x86_sse2_psrai_d:
9280 NewIntNo = Intrinsic::x86_sse2_psra_d;
9281 break;
9282 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009283 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009284 switch (IntNo) {
9285 case Intrinsic::x86_mmx_pslli_w:
9286 NewIntNo = Intrinsic::x86_mmx_psll_w;
9287 break;
9288 case Intrinsic::x86_mmx_pslli_d:
9289 NewIntNo = Intrinsic::x86_mmx_psll_d;
9290 break;
9291 case Intrinsic::x86_mmx_pslli_q:
9292 NewIntNo = Intrinsic::x86_mmx_psll_q;
9293 break;
9294 case Intrinsic::x86_mmx_psrli_w:
9295 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9296 break;
9297 case Intrinsic::x86_mmx_psrli_d:
9298 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9299 break;
9300 case Intrinsic::x86_mmx_psrli_q:
9301 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9302 break;
9303 case Intrinsic::x86_mmx_psrai_w:
9304 NewIntNo = Intrinsic::x86_mmx_psra_w;
9305 break;
9306 case Intrinsic::x86_mmx_psrai_d:
9307 NewIntNo = Intrinsic::x86_mmx_psra_d;
9308 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009309 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009310 }
9311 break;
9312 }
9313 }
Mon P Wangefa42202009-09-03 19:56:25 +00009314
9315 // The vector shift intrinsics with scalars uses 32b shift amounts but
9316 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9317 // to be zero.
9318 SDValue ShOps[4];
9319 ShOps[0] = ShAmt;
9320 ShOps[1] = DAG.getConstant(0, MVT::i32);
9321 if (ShAmtVT == MVT::v4i32) {
9322 ShOps[2] = DAG.getUNDEF(MVT::i32);
9323 ShOps[3] = DAG.getUNDEF(MVT::i32);
9324 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9325 } else {
9326 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009327// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009328 }
9329
Owen Andersone50ed302009-08-10 22:56:29 +00009330 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009331 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009332 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009333 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009334 Op.getOperand(1), ShAmt);
9335 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009336 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009337}
Evan Cheng72261582005-12-20 06:22:03 +00009338
Dan Gohmand858e902010-04-17 15:26:15 +00009339SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9340 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009341 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9342 MFI->setReturnAddressIsTaken(true);
9343
Bill Wendling64e87322009-01-16 19:25:27 +00009344 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009345 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009346
9347 if (Depth > 0) {
9348 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9349 SDValue Offset =
9350 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009351 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009352 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009353 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009354 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009355 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009356 }
9357
9358 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009359 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009360 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009361 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009362}
9363
Dan Gohmand858e902010-04-17 15:26:15 +00009364SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009365 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9366 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009367
Owen Andersone50ed302009-08-10 22:56:29 +00009368 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009369 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009370 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9371 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009372 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009373 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009374 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9375 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009376 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009377 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009378}
9379
Dan Gohman475871a2008-07-27 21:46:04 +00009380SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009381 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009382 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009383}
9384
Dan Gohmand858e902010-04-17 15:26:15 +00009385SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009386 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009387 SDValue Chain = Op.getOperand(0);
9388 SDValue Offset = Op.getOperand(1);
9389 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009390 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009391
Dan Gohmand8816272010-08-11 18:14:00 +00009392 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9393 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9394 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009395 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009396
Dan Gohmand8816272010-08-11 18:14:00 +00009397 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9398 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009399 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009400 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9401 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009402 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009403 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009404
Dale Johannesene4d209d2009-02-03 20:21:25 +00009405 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009406 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009407 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009408}
9409
Duncan Sands4a544a72011-09-06 13:37:06 +00009410SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9411 SelectionDAG &DAG) const {
9412 return Op.getOperand(0);
9413}
9414
9415SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9416 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009417 SDValue Root = Op.getOperand(0);
9418 SDValue Trmp = Op.getOperand(1); // trampoline
9419 SDValue FPtr = Op.getOperand(2); // nested function
9420 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009421 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009422
Dan Gohman69de1932008-02-06 22:27:42 +00009423 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009424
9425 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009426 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009427
9428 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009429 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9430 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009431
Evan Cheng0e6a0522011-07-18 20:57:22 +00009432 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9433 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009434
9435 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9436
9437 // Load the pointer to the nested function into R11.
9438 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009439 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009440 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009441 Addr, MachinePointerInfo(TrmpAddr),
9442 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009443
Owen Anderson825b72b2009-08-11 20:47:22 +00009444 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9445 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009446 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9447 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009448 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009449
9450 // Load the 'nest' parameter value into R10.
9451 // R10 is specified in X86CallingConv.td
9452 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009453 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9454 DAG.getConstant(10, MVT::i64));
9455 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009456 Addr, MachinePointerInfo(TrmpAddr, 10),
9457 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009458
Owen Anderson825b72b2009-08-11 20:47:22 +00009459 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9460 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009461 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9462 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009463 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009464
9465 // Jump to the nested function.
9466 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009467 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9468 DAG.getConstant(20, MVT::i64));
9469 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009470 Addr, MachinePointerInfo(TrmpAddr, 20),
9471 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009472
9473 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009474 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9475 DAG.getConstant(22, MVT::i64));
9476 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009477 MachinePointerInfo(TrmpAddr, 22),
9478 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009479
Duncan Sands4a544a72011-09-06 13:37:06 +00009480 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009481 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009482 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009483 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009484 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009485 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009486
9487 switch (CC) {
9488 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009489 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009490 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009491 case CallingConv::X86_StdCall: {
9492 // Pass 'nest' parameter in ECX.
9493 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009494 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009495
9496 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009497 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009498 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009499
Chris Lattner58d74912008-03-12 17:45:29 +00009500 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009501 unsigned InRegCount = 0;
9502 unsigned Idx = 1;
9503
9504 for (FunctionType::param_iterator I = FTy->param_begin(),
9505 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009506 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009507 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009508 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009509
9510 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009511 report_fatal_error("Nest register in use - reduce number of inreg"
9512 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009513 }
9514 }
9515 break;
9516 }
9517 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009518 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009519 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009520 // Pass 'nest' parameter in EAX.
9521 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009522 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009523 break;
9524 }
9525
Dan Gohman475871a2008-07-27 21:46:04 +00009526 SDValue OutChains[4];
9527 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009528
Owen Anderson825b72b2009-08-11 20:47:22 +00009529 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9530 DAG.getConstant(10, MVT::i32));
9531 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009532
Chris Lattnera62fe662010-02-05 19:20:30 +00009533 // This is storing the opcode for MOV32ri.
9534 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009535 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009536 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009537 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009538 Trmp, MachinePointerInfo(TrmpAddr),
9539 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009540
Owen Anderson825b72b2009-08-11 20:47:22 +00009541 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9542 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009543 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9544 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009545 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009546
Chris Lattnera62fe662010-02-05 19:20:30 +00009547 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009548 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9549 DAG.getConstant(5, MVT::i32));
9550 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009551 MachinePointerInfo(TrmpAddr, 5),
9552 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009553
Owen Anderson825b72b2009-08-11 20:47:22 +00009554 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9555 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009556 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9557 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009558 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009559
Duncan Sands4a544a72011-09-06 13:37:06 +00009560 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009561 }
9562}
9563
Dan Gohmand858e902010-04-17 15:26:15 +00009564SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9565 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009566 /*
9567 The rounding mode is in bits 11:10 of FPSR, and has the following
9568 settings:
9569 00 Round to nearest
9570 01 Round to -inf
9571 10 Round to +inf
9572 11 Round to 0
9573
9574 FLT_ROUNDS, on the other hand, expects the following:
9575 -1 Undefined
9576 0 Round to 0
9577 1 Round to nearest
9578 2 Round to +inf
9579 3 Round to -inf
9580
9581 To perform the conversion, we do:
9582 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9583 */
9584
9585 MachineFunction &MF = DAG.getMachineFunction();
9586 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009587 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009588 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009589 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009590 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009591
9592 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009593 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009594 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009595
Michael J. Spencerec38de22010-10-10 22:04:20 +00009596
Chris Lattner2156b792010-09-22 01:11:26 +00009597 MachineMemOperand *MMO =
9598 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9599 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009600
Chris Lattner2156b792010-09-22 01:11:26 +00009601 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9602 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9603 DAG.getVTList(MVT::Other),
9604 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009605
9606 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009607 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009608 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009609
9610 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009611 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009612 DAG.getNode(ISD::SRL, DL, MVT::i16,
9613 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009614 CWD, DAG.getConstant(0x800, MVT::i16)),
9615 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009616 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009617 DAG.getNode(ISD::SRL, DL, MVT::i16,
9618 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009619 CWD, DAG.getConstant(0x400, MVT::i16)),
9620 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009621
Dan Gohman475871a2008-07-27 21:46:04 +00009622 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009623 DAG.getNode(ISD::AND, DL, MVT::i16,
9624 DAG.getNode(ISD::ADD, DL, MVT::i16,
9625 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009626 DAG.getConstant(1, MVT::i16)),
9627 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009628
9629
Duncan Sands83ec4b62008-06-06 12:08:01 +00009630 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009631 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009632}
9633
Dan Gohmand858e902010-04-17 15:26:15 +00009634SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009635 EVT VT = Op.getValueType();
9636 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009637 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009638 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009639
9640 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009641 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009642 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009643 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009644 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009645 }
Evan Cheng18efe262007-12-14 02:13:44 +00009646
Evan Cheng152804e2007-12-14 08:30:15 +00009647 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009648 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009649 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009650
9651 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009652 SDValue Ops[] = {
9653 Op,
9654 DAG.getConstant(NumBits+NumBits-1, OpVT),
9655 DAG.getConstant(X86::COND_E, MVT::i8),
9656 Op.getValue(1)
9657 };
9658 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009659
9660 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009661 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009662
Owen Anderson825b72b2009-08-11 20:47:22 +00009663 if (VT == MVT::i8)
9664 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009665 return Op;
9666}
9667
Dan Gohmand858e902010-04-17 15:26:15 +00009668SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009669 EVT VT = Op.getValueType();
9670 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009671 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009672 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009673
9674 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009675 if (VT == MVT::i8) {
9676 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009677 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009678 }
Evan Cheng152804e2007-12-14 08:30:15 +00009679
9680 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009681 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009682 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009683
9684 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009685 SDValue Ops[] = {
9686 Op,
9687 DAG.getConstant(NumBits, OpVT),
9688 DAG.getConstant(X86::COND_E, MVT::i8),
9689 Op.getValue(1)
9690 };
9691 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009692
Owen Anderson825b72b2009-08-11 20:47:22 +00009693 if (VT == MVT::i8)
9694 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009695 return Op;
9696}
9697
Craig Topper13894fa2011-08-24 06:14:18 +00009698// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9699// ones, and then concatenate the result back.
9700static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009701 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009702
9703 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9704 "Unsupported value type for operation");
9705
9706 int NumElems = VT.getVectorNumElements();
9707 DebugLoc dl = Op.getDebugLoc();
9708 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9709 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9710
9711 // Extract the LHS vectors
9712 SDValue LHS = Op.getOperand(0);
9713 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9714 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9715
9716 // Extract the RHS vectors
9717 SDValue RHS = Op.getOperand(1);
9718 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9719 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9720
9721 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9722 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9723
9724 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9725 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9726 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9727}
9728
9729SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9730 assert(Op.getValueType().getSizeInBits() == 256 &&
9731 Op.getValueType().isInteger() &&
9732 "Only handle AVX 256-bit vector integer operation");
9733 return Lower256IntArith(Op, DAG);
9734}
9735
9736SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9737 assert(Op.getValueType().getSizeInBits() == 256 &&
9738 Op.getValueType().isInteger() &&
9739 "Only handle AVX 256-bit vector integer operation");
9740 return Lower256IntArith(Op, DAG);
9741}
9742
9743SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9744 EVT VT = Op.getValueType();
9745
9746 // Decompose 256-bit ops into smaller 128-bit ops.
9747 if (VT.getSizeInBits() == 256)
9748 return Lower256IntArith(Op, DAG);
9749
Owen Anderson825b72b2009-08-11 20:47:22 +00009750 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009751 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009752
Mon P Wangaf9b9522008-12-18 21:42:19 +00009753 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9754 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9755 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9756 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9757 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9758 //
9759 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9760 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9761 // return AloBlo + AloBhi + AhiBlo;
9762
9763 SDValue A = Op.getOperand(0);
9764 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009765
Dale Johannesene4d209d2009-02-03 20:21:25 +00009766 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009767 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9768 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009769 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009770 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9771 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009772 SDValue AloBlo = 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 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009775 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009776 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009777 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009778 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009779 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009780 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009781 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009782 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9783 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009784 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009785 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9786 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009787 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9788 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009789 return Res;
9790}
9791
Nadav Rotem43012222011-05-11 08:12:09 +00009792SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9793
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009794 EVT VT = Op.getValueType();
9795 DebugLoc dl = Op.getDebugLoc();
9796 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009797 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009798 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009799
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009800 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
9801 return SDValue();
9802
9803 // Decompose 256-bit shifts into smaller 128-bit shifts.
9804 if (VT.getSizeInBits() == 256) {
9805 int NumElems = VT.getVectorNumElements();
9806 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9807 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9808
9809 // Extract the two vectors
9810 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9811 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9812 DAG, dl);
9813
9814 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009815 SDValue Amt1, Amt2;
9816 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9817 // Constant shift amount
9818 SmallVector<SDValue, 4> Amt1Csts;
9819 SmallVector<SDValue, 4> Amt2Csts;
9820 for (int i = 0; i < NumElems/2; ++i)
9821 Amt1Csts.push_back(Amt->getOperand(i));
9822 for (int i = NumElems/2; i < NumElems; ++i)
9823 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009824
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009825 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9826 &Amt1Csts[0], NumElems/2);
9827 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9828 &Amt2Csts[0], NumElems/2);
9829 } else {
9830 // Variable shift amount
9831 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9832 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9833 DAG, dl);
9834 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009835
9836 // Issue new vector shifts for the smaller types
9837 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9838 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9839
9840 // Concatenate the result back
9841 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9842 }
Nate Begeman51409212010-07-28 00:21:48 +00009843
Nadav Rotem43012222011-05-11 08:12:09 +00009844 // Optimize shl/srl/sra with constant shift amount.
9845 if (isSplatVector(Amt.getNode())) {
9846 SDValue SclrAmt = Amt->getOperand(0);
9847 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9848 uint64_t ShiftAmt = C->getZExtValue();
9849
9850 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9851 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9852 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9853 R, DAG.getConstant(ShiftAmt, MVT::i32));
9854
9855 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9856 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9857 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9858 R, DAG.getConstant(ShiftAmt, MVT::i32));
9859
9860 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9861 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9862 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9863 R, DAG.getConstant(ShiftAmt, MVT::i32));
9864
9865 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9866 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9867 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9868 R, DAG.getConstant(ShiftAmt, MVT::i32));
9869
9870 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9871 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9872 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9873 R, DAG.getConstant(ShiftAmt, MVT::i32));
9874
9875 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9876 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9877 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9878 R, DAG.getConstant(ShiftAmt, MVT::i32));
9879
9880 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9881 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9882 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9883 R, DAG.getConstant(ShiftAmt, MVT::i32));
9884
9885 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9886 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9887 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9888 R, DAG.getConstant(ShiftAmt, MVT::i32));
9889 }
9890 }
9891
9892 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009893 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009894 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9895 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9896 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9897
9898 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009899
Nate Begeman51409212010-07-28 00:21:48 +00009900 std::vector<Constant*> CV(4, CI);
9901 Constant *C = ConstantVector::get(CV);
9902 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9903 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009904 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009905 false, false, 16);
9906
9907 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009908 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009909 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9910 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9911 }
Nadav Rotem43012222011-05-11 08:12:09 +00009912 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009913 // a = a << 5;
9914 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9915 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9916 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9917
9918 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9919 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9920
9921 std::vector<Constant*> CVM1(16, CM1);
9922 std::vector<Constant*> CVM2(16, CM2);
9923 Constant *C = ConstantVector::get(CVM1);
9924 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9925 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009926 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009927 false, false, 16);
9928
9929 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9930 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9931 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9932 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9933 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009934 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009935 // a += a
9936 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009937
Nate Begeman51409212010-07-28 00:21:48 +00009938 C = ConstantVector::get(CVM2);
9939 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9940 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009941 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009942 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009943
Nate Begeman51409212010-07-28 00:21:48 +00009944 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9945 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9946 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9947 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9948 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009949 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009950 // a += a
9951 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009952
Nate Begeman51409212010-07-28 00:21:48 +00009953 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009954 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00009955 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
9956 return R;
9957 }
9958 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009959}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009960
Dan Gohmand858e902010-04-17 15:26:15 +00009961SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009962 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9963 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009964 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9965 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009966 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009967 SDValue LHS = N->getOperand(0);
9968 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009969 unsigned BaseOp = 0;
9970 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009971 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009972 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009973 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009974 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009975 // A subtract of one will be selected as a INC. Note that INC doesn't
9976 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009977 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9978 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009979 BaseOp = X86ISD::INC;
9980 Cond = X86::COND_O;
9981 break;
9982 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009983 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009984 Cond = X86::COND_O;
9985 break;
9986 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009987 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009988 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009989 break;
9990 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009991 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9992 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009993 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9994 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009995 BaseOp = X86ISD::DEC;
9996 Cond = X86::COND_O;
9997 break;
9998 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009999 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010000 Cond = X86::COND_O;
10001 break;
10002 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010003 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010004 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010005 break;
10006 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010007 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010008 Cond = X86::COND_O;
10009 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010010 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10011 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10012 MVT::i32);
10013 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010014
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010015 SDValue SetCC =
10016 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10017 DAG.getConstant(X86::COND_O, MVT::i32),
10018 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010019
Dan Gohman6e5fda22011-07-22 18:45:15 +000010020 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010021 }
Bill Wendling74c37652008-12-09 22:08:41 +000010022 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010023
Bill Wendling61edeb52008-12-02 01:06:39 +000010024 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010025 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010026 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010027
Bill Wendling61edeb52008-12-02 01:06:39 +000010028 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010029 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10030 DAG.getConstant(Cond, MVT::i32),
10031 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010032
Dan Gohman6e5fda22011-07-22 18:45:15 +000010033 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010034}
10035
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010036SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10037 DebugLoc dl = Op.getDebugLoc();
10038 SDNode* Node = Op.getNode();
10039 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10040 EVT VT = Node->getValueType(0);
10041
10042 if (Subtarget->hasSSE2() && VT.isVector()) {
10043 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10044 ExtraVT.getScalarType().getSizeInBits();
10045 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10046
10047 unsigned SHLIntrinsicsID = 0;
10048 unsigned SRAIntrinsicsID = 0;
10049 switch (VT.getSimpleVT().SimpleTy) {
10050 default:
10051 return SDValue();
10052 case MVT::v2i64: {
10053 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
10054 SRAIntrinsicsID = 0;
10055 break;
10056 }
10057 case MVT::v4i32: {
10058 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10059 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10060 break;
10061 }
10062 case MVT::v8i16: {
10063 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10064 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10065 break;
10066 }
10067 }
10068
10069 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10070 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10071 Node->getOperand(0), ShAmt);
10072
10073 // In case of 1 bit sext, no need to shr
10074 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10075
10076 if (SRAIntrinsicsID) {
10077 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10078 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10079 Tmp1, ShAmt);
10080 }
10081 return Tmp1;
10082 }
10083
10084 return SDValue();
10085}
10086
10087
Eric Christopher9a9d2752010-07-22 02:48:34 +000010088SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10089 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010090
Eric Christopher77ed1352011-07-08 00:04:56 +000010091 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10092 // There isn't any reason to disable it if the target processor supports it.
10093 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010094 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010095 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010096 SDValue Ops[] = {
10097 DAG.getRegister(X86::ESP, MVT::i32), // Base
10098 DAG.getTargetConstant(1, MVT::i8), // Scale
10099 DAG.getRegister(0, MVT::i32), // Index
10100 DAG.getTargetConstant(0, MVT::i32), // Disp
10101 DAG.getRegister(0, MVT::i32), // Segment.
10102 Zero,
10103 Chain
10104 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010105 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010106 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10107 array_lengthof(Ops));
10108 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010109 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010110
Eric Christopher9a9d2752010-07-22 02:48:34 +000010111 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010112 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010113 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010114
Chris Lattner132929a2010-08-14 17:26:09 +000010115 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10116 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10117 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10118 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010119
Chris Lattner132929a2010-08-14 17:26:09 +000010120 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10121 if (!Op1 && !Op2 && !Op3 && Op4)
10122 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010123
Chris Lattner132929a2010-08-14 17:26:09 +000010124 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10125 if (Op1 && !Op2 && !Op3 && !Op4)
10126 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010127
10128 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010129 // (MFENCE)>;
10130 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010131}
10132
Eli Friedman14648462011-07-27 22:21:52 +000010133SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10134 SelectionDAG &DAG) const {
10135 DebugLoc dl = Op.getDebugLoc();
10136 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10137 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10138 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10139 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10140
10141 // The only fence that needs an instruction is a sequentially-consistent
10142 // cross-thread fence.
10143 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10144 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10145 // no-sse2). There isn't any reason to disable it if the target processor
10146 // supports it.
10147 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
10148 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10149
10150 SDValue Chain = Op.getOperand(0);
10151 SDValue Zero = DAG.getConstant(0, MVT::i32);
10152 SDValue Ops[] = {
10153 DAG.getRegister(X86::ESP, MVT::i32), // Base
10154 DAG.getTargetConstant(1, MVT::i8), // Scale
10155 DAG.getRegister(0, MVT::i32), // Index
10156 DAG.getTargetConstant(0, MVT::i32), // Disp
10157 DAG.getRegister(0, MVT::i32), // Segment.
10158 Zero,
10159 Chain
10160 };
10161 SDNode *Res =
10162 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10163 array_lengthof(Ops));
10164 return SDValue(Res, 0);
10165 }
10166
10167 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10168 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10169}
10170
10171
Dan Gohmand858e902010-04-17 15:26:15 +000010172SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010173 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010174 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010175 unsigned Reg = 0;
10176 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010177 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010178 default:
10179 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010180 case MVT::i8: Reg = X86::AL; size = 1; break;
10181 case MVT::i16: Reg = X86::AX; size = 2; break;
10182 case MVT::i32: Reg = X86::EAX; size = 4; break;
10183 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010184 assert(Subtarget->is64Bit() && "Node not type legal!");
10185 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010186 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010187 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010188 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010189 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010190 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010191 Op.getOperand(1),
10192 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010193 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010194 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010195 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010196 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10197 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10198 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010199 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010200 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010201 return cpOut;
10202}
10203
Duncan Sands1607f052008-12-01 11:39:25 +000010204SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010205 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010206 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010207 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010208 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010209 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010210 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010211 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10212 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010213 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010214 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10215 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010216 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010217 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010218 rdx.getValue(1)
10219 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010220 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010221}
10222
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010223SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010224 SelectionDAG &DAG) const {
10225 EVT SrcVT = Op.getOperand(0).getValueType();
10226 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +000010227 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
10228 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010229 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010230 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010231 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010232 // i64 <=> MMX conversions are Legal.
10233 if (SrcVT==MVT::i64 && DstVT.isVector())
10234 return Op;
10235 if (DstVT==MVT::i64 && SrcVT.isVector())
10236 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010237 // MMX <=> MMX conversions are Legal.
10238 if (SrcVT.isVector() && DstVT.isVector())
10239 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010240 // All other conversions need to be expanded.
10241 return SDValue();
10242}
Chris Lattner5b856542010-12-20 00:59:46 +000010243
Dan Gohmand858e902010-04-17 15:26:15 +000010244SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010245 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010246 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010247 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010248 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010249 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010250 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010251 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010252 Node->getOperand(0),
10253 Node->getOperand(1), negOp,
10254 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010255 cast<AtomicSDNode>(Node)->getAlignment(),
10256 cast<AtomicSDNode>(Node)->getOrdering(),
10257 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010258}
10259
Eli Friedman327236c2011-08-24 20:50:09 +000010260static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10261 SDNode *Node = Op.getNode();
10262 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010263 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010264
10265 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010266 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10267 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10268 // (The only way to get a 16-byte store is cmpxchg16b)
10269 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10270 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10271 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010272 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10273 cast<AtomicSDNode>(Node)->getMemoryVT(),
10274 Node->getOperand(0),
10275 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010276 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010277 cast<AtomicSDNode>(Node)->getOrdering(),
10278 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010279 return Swap.getValue(1);
10280 }
10281 // Other atomic stores have a simple pattern.
10282 return Op;
10283}
10284
Chris Lattner5b856542010-12-20 00:59:46 +000010285static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10286 EVT VT = Op.getNode()->getValueType(0);
10287
10288 // Let legalize expand this if it isn't a legal type yet.
10289 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10290 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010291
Chris Lattner5b856542010-12-20 00:59:46 +000010292 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010293
Chris Lattner5b856542010-12-20 00:59:46 +000010294 unsigned Opc;
10295 bool ExtraOp = false;
10296 switch (Op.getOpcode()) {
10297 default: assert(0 && "Invalid code");
10298 case ISD::ADDC: Opc = X86ISD::ADD; break;
10299 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10300 case ISD::SUBC: Opc = X86ISD::SUB; break;
10301 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10302 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010303
Chris Lattner5b856542010-12-20 00:59:46 +000010304 if (!ExtraOp)
10305 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10306 Op.getOperand(1));
10307 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10308 Op.getOperand(1), Op.getOperand(2));
10309}
10310
Evan Cheng0db9fe62006-04-25 20:13:52 +000010311/// LowerOperation - Provide custom lowering hooks for some operations.
10312///
Dan Gohmand858e902010-04-17 15:26:15 +000010313SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010314 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010315 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010316 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010317 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010318 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010319 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10320 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010321 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010322 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010323 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010324 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10325 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10326 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010327 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010328 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010329 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10330 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10331 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010332 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010333 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010334 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010335 case ISD::SHL_PARTS:
10336 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010337 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010338 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010339 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010340 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010341 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010342 case ISD::FABS: return LowerFABS(Op, DAG);
10343 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010344 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010345 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010346 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +000010347 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010348 case ISD::SELECT: return LowerSELECT(Op, DAG);
10349 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010350 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010351 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010352 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010353 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010354 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010355 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10356 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010357 case ISD::FRAME_TO_ARGS_OFFSET:
10358 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010359 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010360 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010361 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10362 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010363 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010364 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10365 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010366 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010367 case ISD::SRA:
10368 case ISD::SRL:
10369 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010370 case ISD::SADDO:
10371 case ISD::UADDO:
10372 case ISD::SSUBO:
10373 case ISD::USUBO:
10374 case ISD::SMULO:
10375 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010376 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010377 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010378 case ISD::ADDC:
10379 case ISD::ADDE:
10380 case ISD::SUBC:
10381 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010382 case ISD::ADD: return LowerADD(Op, DAG);
10383 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010384 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010385}
10386
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010387static void ReplaceATOMIC_LOAD(SDNode *Node,
10388 SmallVectorImpl<SDValue> &Results,
10389 SelectionDAG &DAG) {
10390 DebugLoc dl = Node->getDebugLoc();
10391 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10392
10393 // Convert wide load -> cmpxchg8b/cmpxchg16b
10394 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10395 // (The only way to get a 16-byte load is cmpxchg16b)
10396 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010397 SDValue Zero = DAG.getConstant(0, VT);
10398 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010399 Node->getOperand(0),
10400 Node->getOperand(1), Zero, Zero,
10401 cast<AtomicSDNode>(Node)->getMemOperand(),
10402 cast<AtomicSDNode>(Node)->getOrdering(),
10403 cast<AtomicSDNode>(Node)->getSynchScope());
10404 Results.push_back(Swap.getValue(0));
10405 Results.push_back(Swap.getValue(1));
10406}
10407
Duncan Sands1607f052008-12-01 11:39:25 +000010408void X86TargetLowering::
10409ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010410 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010411 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010412 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010413 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010414
10415 SDValue Chain = Node->getOperand(0);
10416 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010417 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010418 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010419 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010420 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010421 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010422 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010423 SDValue Result =
10424 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10425 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010426 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010427 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010428 Results.push_back(Result.getValue(2));
10429}
10430
Duncan Sands126d9072008-07-04 11:47:58 +000010431/// ReplaceNodeResults - Replace a node with an illegal result type
10432/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010433void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10434 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010435 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010436 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010437 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010438 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010439 assert(false && "Do not know how to custom type legalize this operation!");
10440 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010441 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010442 case ISD::ADDC:
10443 case ISD::ADDE:
10444 case ISD::SUBC:
10445 case ISD::SUBE:
10446 // We don't want to expand or promote these.
10447 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010448 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010449 std::pair<SDValue,SDValue> Vals =
10450 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010451 SDValue FIST = Vals.first, StackSlot = Vals.second;
10452 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010453 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010454 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010455 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10456 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010457 }
10458 return;
10459 }
10460 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010461 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010462 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010463 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010464 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010465 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010466 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010467 eax.getValue(2));
10468 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10469 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010470 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010471 Results.push_back(edx.getValue(1));
10472 return;
10473 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010474 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010475 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010476 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010477 bool Regs64bit = T == MVT::i128;
10478 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010479 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010480 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10481 DAG.getConstant(0, HalfT));
10482 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10483 DAG.getConstant(1, HalfT));
10484 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10485 Regs64bit ? X86::RAX : X86::EAX,
10486 cpInL, SDValue());
10487 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10488 Regs64bit ? X86::RDX : X86::EDX,
10489 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010490 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010491 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10492 DAG.getConstant(0, HalfT));
10493 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10494 DAG.getConstant(1, HalfT));
10495 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10496 Regs64bit ? X86::RBX : X86::EBX,
10497 swapInL, cpInH.getValue(1));
10498 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10499 Regs64bit ? X86::RCX : X86::ECX,
10500 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010501 SDValue Ops[] = { swapInH.getValue(0),
10502 N->getOperand(1),
10503 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010504 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010505 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010506 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10507 X86ISD::LCMPXCHG8_DAG;
10508 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010509 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010510 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10511 Regs64bit ? X86::RAX : X86::EAX,
10512 HalfT, Result.getValue(1));
10513 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10514 Regs64bit ? X86::RDX : X86::EDX,
10515 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010516 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010517 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010518 Results.push_back(cpOutH.getValue(1));
10519 return;
10520 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010521 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010522 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10523 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010524 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010525 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10526 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010527 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010528 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10529 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010530 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010531 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10532 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010533 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010534 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10535 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010536 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010537 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10538 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010539 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010540 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10541 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010542 case ISD::ATOMIC_LOAD:
10543 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010544 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010545}
10546
Evan Cheng72261582005-12-20 06:22:03 +000010547const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10548 switch (Opcode) {
10549 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010550 case X86ISD::BSF: return "X86ISD::BSF";
10551 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010552 case X86ISD::SHLD: return "X86ISD::SHLD";
10553 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010554 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010555 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010556 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010557 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010558 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010559 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010560 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10561 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10562 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010563 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010564 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010565 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010566 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010567 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010568 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010569 case X86ISD::COMI: return "X86ISD::COMI";
10570 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010571 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010572 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010573 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10574 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010575 case X86ISD::CMOV: return "X86ISD::CMOV";
10576 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010577 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010578 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10579 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010580 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010581 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010582 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010583 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010584 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010585 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10586 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010587 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010588 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010589 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010590 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10591 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10592 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +000010593 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010594 case X86ISD::FMAX: return "X86ISD::FMAX";
10595 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010596 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10597 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010598 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010599 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010600 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010601 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010602 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010603 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10604 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010605 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10606 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10607 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10608 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10609 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10610 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010611 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10612 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010613 case X86ISD::VSHL: return "X86ISD::VSHL";
10614 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010615 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10616 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10617 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10618 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10619 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10620 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10621 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10622 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10623 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10624 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010625 case X86ISD::ADD: return "X86ISD::ADD";
10626 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010627 case X86ISD::ADC: return "X86ISD::ADC";
10628 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010629 case X86ISD::SMUL: return "X86ISD::SMUL";
10630 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010631 case X86ISD::INC: return "X86ISD::INC";
10632 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010633 case X86ISD::OR: return "X86ISD::OR";
10634 case X86ISD::XOR: return "X86ISD::XOR";
10635 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010636 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010637 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010638 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010639 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10640 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10641 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10642 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10643 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10644 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10645 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10646 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10647 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010648 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010649 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010650 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010651 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10652 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010653 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10654 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10655 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10656 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10657 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10658 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10659 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10660 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10661 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010662 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010663 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10664 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10665 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10666 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10667 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10668 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10669 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10670 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10671 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10672 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010673 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010674 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10675 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10676 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10677 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010678 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010679 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010680 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010681 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010682 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010683 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010684 }
10685}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010686
Chris Lattnerc9addb72007-03-30 23:15:24 +000010687// isLegalAddressingMode - Return true if the addressing mode represented
10688// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010689bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010690 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010691 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010692 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010693 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010694
Chris Lattnerc9addb72007-03-30 23:15:24 +000010695 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010696 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010697 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010698
Chris Lattnerc9addb72007-03-30 23:15:24 +000010699 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010700 unsigned GVFlags =
10701 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010702
Chris Lattnerdfed4132009-07-10 07:38:24 +000010703 // If a reference to this global requires an extra load, we can't fold it.
10704 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010705 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010706
Chris Lattnerdfed4132009-07-10 07:38:24 +000010707 // If BaseGV requires a register for the PIC base, we cannot also have a
10708 // BaseReg specified.
10709 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010710 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010711
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010712 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010713 if ((M != CodeModel::Small || R != Reloc::Static) &&
10714 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010715 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010716 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010717
Chris Lattnerc9addb72007-03-30 23:15:24 +000010718 switch (AM.Scale) {
10719 case 0:
10720 case 1:
10721 case 2:
10722 case 4:
10723 case 8:
10724 // These scales always work.
10725 break;
10726 case 3:
10727 case 5:
10728 case 9:
10729 // These scales are formed with basereg+scalereg. Only accept if there is
10730 // no basereg yet.
10731 if (AM.HasBaseReg)
10732 return false;
10733 break;
10734 default: // Other stuff never works.
10735 return false;
10736 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010737
Chris Lattnerc9addb72007-03-30 23:15:24 +000010738 return true;
10739}
10740
10741
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010742bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010743 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010744 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010745 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10746 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010747 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010748 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010749 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010750}
10751
Owen Andersone50ed302009-08-10 22:56:29 +000010752bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010753 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010754 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010755 unsigned NumBits1 = VT1.getSizeInBits();
10756 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010757 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010758 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010759 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010760}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010761
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010762bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010763 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010764 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010765}
10766
Owen Andersone50ed302009-08-10 22:56:29 +000010767bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010768 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010769 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010770}
10771
Owen Andersone50ed302009-08-10 22:56:29 +000010772bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010773 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010774 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010775}
10776
Evan Cheng60c07e12006-07-05 22:17:51 +000010777/// isShuffleMaskLegal - Targets can use this to indicate that they only
10778/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10779/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10780/// are assumed to be legal.
10781bool
Eric Christopherfd179292009-08-27 18:07:15 +000010782X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010783 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010784 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010785 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +000010786 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +000010787
Nate Begemana09008b2009-10-19 02:17:23 +000010788 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010789 return (VT.getVectorNumElements() == 2 ||
10790 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10791 isMOVLMask(M, VT) ||
10792 isSHUFPMask(M, VT) ||
10793 isPSHUFDMask(M, VT) ||
10794 isPSHUFHWMask(M, VT) ||
10795 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +000010796 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010797 isUNPCKLMask(M, VT) ||
10798 isUNPCKHMask(M, VT) ||
10799 isUNPCKL_v_undef_Mask(M, VT) ||
10800 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010801}
10802
Dan Gohman7d8143f2008-04-09 20:09:42 +000010803bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010804X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010805 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010806 unsigned NumElts = VT.getVectorNumElements();
10807 // FIXME: This collection of masks seems suspect.
10808 if (NumElts == 2)
10809 return true;
10810 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10811 return (isMOVLMask(Mask, VT) ||
10812 isCommutedMOVLMask(Mask, VT, true) ||
10813 isSHUFPMask(Mask, VT) ||
10814 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010815 }
10816 return false;
10817}
10818
10819//===----------------------------------------------------------------------===//
10820// X86 Scheduler Hooks
10821//===----------------------------------------------------------------------===//
10822
Mon P Wang63307c32008-05-05 19:05:59 +000010823// private utility function
10824MachineBasicBlock *
10825X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10826 MachineBasicBlock *MBB,
10827 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010828 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010829 unsigned LoadOpc,
10830 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010831 unsigned notOpc,
10832 unsigned EAXreg,
10833 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010834 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010835 // For the atomic bitwise operator, we generate
10836 // thisMBB:
10837 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010838 // ld t1 = [bitinstr.addr]
10839 // op t2 = t1, [bitinstr.val]
10840 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010841 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10842 // bz newMBB
10843 // fallthrough -->nextMBB
10844 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10845 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010846 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010847 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010848
Mon P Wang63307c32008-05-05 19:05:59 +000010849 /// First build the CFG
10850 MachineFunction *F = MBB->getParent();
10851 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010852 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10853 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10854 F->insert(MBBIter, newMBB);
10855 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010856
Dan Gohman14152b42010-07-06 20:24:04 +000010857 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10858 nextMBB->splice(nextMBB->begin(), thisMBB,
10859 llvm::next(MachineBasicBlock::iterator(bInstr)),
10860 thisMBB->end());
10861 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010862
Mon P Wang63307c32008-05-05 19:05:59 +000010863 // Update thisMBB to fall through to newMBB
10864 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010865
Mon P Wang63307c32008-05-05 19:05:59 +000010866 // newMBB jumps to itself and fall through to nextMBB
10867 newMBB->addSuccessor(nextMBB);
10868 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010869
Mon P Wang63307c32008-05-05 19:05:59 +000010870 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010871 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010872 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010873 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010874 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010875 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010876 int numArgs = bInstr->getNumOperands() - 1;
10877 for (int i=0; i < numArgs; ++i)
10878 argOpers[i] = &bInstr->getOperand(i+1);
10879
10880 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010881 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010882 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010883
Dale Johannesen140be2d2008-08-19 18:47:28 +000010884 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010885 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010886 for (int i=0; i <= lastAddrIndx; ++i)
10887 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010888
Dale Johannesen140be2d2008-08-19 18:47:28 +000010889 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010890 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010891 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010892 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010893 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010894 tt = t1;
10895
Dale Johannesen140be2d2008-08-19 18:47:28 +000010896 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010897 assert((argOpers[valArgIndx]->isReg() ||
10898 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010899 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010900 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010901 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010902 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010903 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010904 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010905 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010906
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010907 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010908 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010909
Dale Johannesene4d209d2009-02-03 20:21:25 +000010910 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010911 for (int i=0; i <= lastAddrIndx; ++i)
10912 (*MIB).addOperand(*argOpers[i]);
10913 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010914 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010915 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10916 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010917
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010918 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010919 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010920
Mon P Wang63307c32008-05-05 19:05:59 +000010921 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010922 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010923
Dan Gohman14152b42010-07-06 20:24:04 +000010924 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010925 return nextMBB;
10926}
10927
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010928// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010929MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010930X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10931 MachineBasicBlock *MBB,
10932 unsigned regOpcL,
10933 unsigned regOpcH,
10934 unsigned immOpcL,
10935 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010936 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010937 // For the atomic bitwise operator, we generate
10938 // thisMBB (instructions are in pairs, except cmpxchg8b)
10939 // ld t1,t2 = [bitinstr.addr]
10940 // newMBB:
10941 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
10942 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000010943 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010944 // mov ECX, EBX <- t5, t6
10945 // mov EAX, EDX <- t1, t2
10946 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
10947 // mov t3, t4 <- EAX, EDX
10948 // bz newMBB
10949 // result in out1, out2
10950 // fallthrough -->nextMBB
10951
10952 const TargetRegisterClass *RC = X86::GR32RegisterClass;
10953 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010954 const unsigned NotOpc = X86::NOT32r;
10955 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10956 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10957 MachineFunction::iterator MBBIter = MBB;
10958 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010959
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010960 /// First build the CFG
10961 MachineFunction *F = MBB->getParent();
10962 MachineBasicBlock *thisMBB = MBB;
10963 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10964 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10965 F->insert(MBBIter, newMBB);
10966 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010967
Dan Gohman14152b42010-07-06 20:24:04 +000010968 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10969 nextMBB->splice(nextMBB->begin(), thisMBB,
10970 llvm::next(MachineBasicBlock::iterator(bInstr)),
10971 thisMBB->end());
10972 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010973
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010974 // Update thisMBB to fall through to newMBB
10975 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010976
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010977 // newMBB jumps to itself and fall through to nextMBB
10978 newMBB->addSuccessor(nextMBB);
10979 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010980
Dale Johannesene4d209d2009-02-03 20:21:25 +000010981 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010982 // Insert instructions into newMBB based on incoming instruction
10983 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010984 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010985 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010986 MachineOperand& dest1Oper = bInstr->getOperand(0);
10987 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010988 MachineOperand* argOpers[2 + X86::AddrNumOperands];
10989 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010990 argOpers[i] = &bInstr->getOperand(i+2);
10991
Dan Gohman71ea4e52010-05-14 21:01:44 +000010992 // We use some of the operands multiple times, so conservatively just
10993 // clear any kill flags that might be present.
10994 if (argOpers[i]->isReg() && argOpers[i]->isUse())
10995 argOpers[i]->setIsKill(false);
10996 }
10997
Evan Chengad5b52f2010-01-08 19:14:57 +000010998 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010999 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011000
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011001 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011002 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011003 for (int i=0; i <= lastAddrIndx; ++i)
11004 (*MIB).addOperand(*argOpers[i]);
11005 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011006 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011007 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011008 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011009 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011010 MachineOperand newOp3 = *(argOpers[3]);
11011 if (newOp3.isImm())
11012 newOp3.setImm(newOp3.getImm()+4);
11013 else
11014 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011015 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011016 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011017
11018 // t3/4 are defined later, at the bottom of the loop
11019 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11020 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011021 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011022 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011023 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011024 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11025
Evan Cheng306b4ca2010-01-08 23:41:50 +000011026 // The subsequent operations should be using the destination registers of
11027 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011028 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011029 t1 = F->getRegInfo().createVirtualRegister(RC);
11030 t2 = F->getRegInfo().createVirtualRegister(RC);
11031 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11032 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011033 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011034 t1 = dest1Oper.getReg();
11035 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011036 }
11037
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011038 int valArgIndx = lastAddrIndx + 1;
11039 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011040 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011041 "invalid operand");
11042 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11043 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011044 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011045 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011046 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011047 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011048 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011049 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011050 (*MIB).addOperand(*argOpers[valArgIndx]);
11051 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011052 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011053 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011054 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011055 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011056 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011057 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011058 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011059 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011060 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011061 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011062
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011063 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011064 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011065 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011066 MIB.addReg(t2);
11067
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011068 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011069 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011070 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011071 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011072
Dale Johannesene4d209d2009-02-03 20:21:25 +000011073 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011074 for (int i=0; i <= lastAddrIndx; ++i)
11075 (*MIB).addOperand(*argOpers[i]);
11076
11077 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011078 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11079 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011080
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011081 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011082 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011083 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011084 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011085
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011086 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011087 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011088
Dan Gohman14152b42010-07-06 20:24:04 +000011089 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011090 return nextMBB;
11091}
11092
11093// private utility function
11094MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011095X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11096 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011097 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011098 // For the atomic min/max operator, we generate
11099 // thisMBB:
11100 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011101 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011102 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011103 // cmp t1, t2
11104 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011105 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011106 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11107 // bz newMBB
11108 // fallthrough -->nextMBB
11109 //
11110 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11111 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011112 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011113 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011114
Mon P Wang63307c32008-05-05 19:05:59 +000011115 /// First build the CFG
11116 MachineFunction *F = MBB->getParent();
11117 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011118 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11119 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11120 F->insert(MBBIter, newMBB);
11121 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011122
Dan Gohman14152b42010-07-06 20:24:04 +000011123 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11124 nextMBB->splice(nextMBB->begin(), thisMBB,
11125 llvm::next(MachineBasicBlock::iterator(mInstr)),
11126 thisMBB->end());
11127 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011128
Mon P Wang63307c32008-05-05 19:05:59 +000011129 // Update thisMBB to fall through to newMBB
11130 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011131
Mon P Wang63307c32008-05-05 19:05:59 +000011132 // newMBB jumps to newMBB and fall through to nextMBB
11133 newMBB->addSuccessor(nextMBB);
11134 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011135
Dale Johannesene4d209d2009-02-03 20:21:25 +000011136 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011137 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011138 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011139 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011140 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011141 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011142 int numArgs = mInstr->getNumOperands() - 1;
11143 for (int i=0; i < numArgs; ++i)
11144 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011145
Mon P Wang63307c32008-05-05 19:05:59 +000011146 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011147 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011148 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011149
Mon P Wangab3e7472008-05-05 22:56:23 +000011150 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011151 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011152 for (int i=0; i <= lastAddrIndx; ++i)
11153 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011154
Mon P Wang63307c32008-05-05 19:05:59 +000011155 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011156 assert((argOpers[valArgIndx]->isReg() ||
11157 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011158 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011159
11160 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011161 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011162 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011163 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011164 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011165 (*MIB).addOperand(*argOpers[valArgIndx]);
11166
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011167 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011168 MIB.addReg(t1);
11169
Dale Johannesene4d209d2009-02-03 20:21:25 +000011170 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011171 MIB.addReg(t1);
11172 MIB.addReg(t2);
11173
11174 // Generate movc
11175 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011176 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011177 MIB.addReg(t2);
11178 MIB.addReg(t1);
11179
11180 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011181 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011182 for (int i=0; i <= lastAddrIndx; ++i)
11183 (*MIB).addOperand(*argOpers[i]);
11184 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011185 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011186 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11187 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011188
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011189 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011190 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011191
Mon P Wang63307c32008-05-05 19:05:59 +000011192 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011193 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011194
Dan Gohman14152b42010-07-06 20:24:04 +000011195 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011196 return nextMBB;
11197}
11198
Eric Christopherf83a5de2009-08-27 18:08:16 +000011199// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011200// or XMM0_V32I8 in AVX all of this code can be replaced with that
11201// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011202MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011203X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011204 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011205 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11206 "Target must have SSE4.2 or AVX features enabled");
11207
Eric Christopherb120ab42009-08-18 22:50:32 +000011208 DebugLoc dl = MI->getDebugLoc();
11209 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011210 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011211 if (!Subtarget->hasAVX()) {
11212 if (memArg)
11213 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11214 else
11215 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11216 } else {
11217 if (memArg)
11218 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11219 else
11220 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11221 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011222
Eric Christopher41c902f2010-11-30 08:20:21 +000011223 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011224 for (unsigned i = 0; i < numArgs; ++i) {
11225 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011226 if (!(Op.isReg() && Op.isImplicit()))
11227 MIB.addOperand(Op);
11228 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011229 BuildMI(*BB, MI, dl,
11230 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11231 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011232 .addReg(X86::XMM0);
11233
Dan Gohman14152b42010-07-06 20:24:04 +000011234 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011235 return BB;
11236}
11237
11238MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011239X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011240 DebugLoc dl = MI->getDebugLoc();
11241 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011242
Eric Christopher228232b2010-11-30 07:20:12 +000011243 // Address into RAX/EAX, other two args into ECX, EDX.
11244 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11245 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11246 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11247 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011248 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011249
Eric Christopher228232b2010-11-30 07:20:12 +000011250 unsigned ValOps = X86::AddrNumOperands;
11251 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11252 .addReg(MI->getOperand(ValOps).getReg());
11253 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11254 .addReg(MI->getOperand(ValOps+1).getReg());
11255
11256 // The instruction doesn't actually take any operands though.
11257 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011258
Eric Christopher228232b2010-11-30 07:20:12 +000011259 MI->eraseFromParent(); // The pseudo is gone now.
11260 return BB;
11261}
11262
11263MachineBasicBlock *
11264X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011265 DebugLoc dl = MI->getDebugLoc();
11266 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011267
Eric Christopher228232b2010-11-30 07:20:12 +000011268 // First arg in ECX, the second in EAX.
11269 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11270 .addReg(MI->getOperand(0).getReg());
11271 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11272 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011273
Eric Christopher228232b2010-11-30 07:20:12 +000011274 // The instruction doesn't actually take any operands though.
11275 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011276
Eric Christopher228232b2010-11-30 07:20:12 +000011277 MI->eraseFromParent(); // The pseudo is gone now.
11278 return BB;
11279}
11280
11281MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011282X86TargetLowering::EmitVAARG64WithCustomInserter(
11283 MachineInstr *MI,
11284 MachineBasicBlock *MBB) const {
11285 // Emit va_arg instruction on X86-64.
11286
11287 // Operands to this pseudo-instruction:
11288 // 0 ) Output : destination address (reg)
11289 // 1-5) Input : va_list address (addr, i64mem)
11290 // 6 ) ArgSize : Size (in bytes) of vararg type
11291 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11292 // 8 ) Align : Alignment of type
11293 // 9 ) EFLAGS (implicit-def)
11294
11295 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11296 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11297
11298 unsigned DestReg = MI->getOperand(0).getReg();
11299 MachineOperand &Base = MI->getOperand(1);
11300 MachineOperand &Scale = MI->getOperand(2);
11301 MachineOperand &Index = MI->getOperand(3);
11302 MachineOperand &Disp = MI->getOperand(4);
11303 MachineOperand &Segment = MI->getOperand(5);
11304 unsigned ArgSize = MI->getOperand(6).getImm();
11305 unsigned ArgMode = MI->getOperand(7).getImm();
11306 unsigned Align = MI->getOperand(8).getImm();
11307
11308 // Memory Reference
11309 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11310 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11311 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11312
11313 // Machine Information
11314 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11315 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11316 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11317 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11318 DebugLoc DL = MI->getDebugLoc();
11319
11320 // struct va_list {
11321 // i32 gp_offset
11322 // i32 fp_offset
11323 // i64 overflow_area (address)
11324 // i64 reg_save_area (address)
11325 // }
11326 // sizeof(va_list) = 24
11327 // alignment(va_list) = 8
11328
11329 unsigned TotalNumIntRegs = 6;
11330 unsigned TotalNumXMMRegs = 8;
11331 bool UseGPOffset = (ArgMode == 1);
11332 bool UseFPOffset = (ArgMode == 2);
11333 unsigned MaxOffset = TotalNumIntRegs * 8 +
11334 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11335
11336 /* Align ArgSize to a multiple of 8 */
11337 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11338 bool NeedsAlign = (Align > 8);
11339
11340 MachineBasicBlock *thisMBB = MBB;
11341 MachineBasicBlock *overflowMBB;
11342 MachineBasicBlock *offsetMBB;
11343 MachineBasicBlock *endMBB;
11344
11345 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11346 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11347 unsigned OffsetReg = 0;
11348
11349 if (!UseGPOffset && !UseFPOffset) {
11350 // If we only pull from the overflow region, we don't create a branch.
11351 // We don't need to alter control flow.
11352 OffsetDestReg = 0; // unused
11353 OverflowDestReg = DestReg;
11354
11355 offsetMBB = NULL;
11356 overflowMBB = thisMBB;
11357 endMBB = thisMBB;
11358 } else {
11359 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11360 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11361 // If not, pull from overflow_area. (branch to overflowMBB)
11362 //
11363 // thisMBB
11364 // | .
11365 // | .
11366 // offsetMBB overflowMBB
11367 // | .
11368 // | .
11369 // endMBB
11370
11371 // Registers for the PHI in endMBB
11372 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11373 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11374
11375 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11376 MachineFunction *MF = MBB->getParent();
11377 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11378 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11379 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11380
11381 MachineFunction::iterator MBBIter = MBB;
11382 ++MBBIter;
11383
11384 // Insert the new basic blocks
11385 MF->insert(MBBIter, offsetMBB);
11386 MF->insert(MBBIter, overflowMBB);
11387 MF->insert(MBBIter, endMBB);
11388
11389 // Transfer the remainder of MBB and its successor edges to endMBB.
11390 endMBB->splice(endMBB->begin(), thisMBB,
11391 llvm::next(MachineBasicBlock::iterator(MI)),
11392 thisMBB->end());
11393 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11394
11395 // Make offsetMBB and overflowMBB successors of thisMBB
11396 thisMBB->addSuccessor(offsetMBB);
11397 thisMBB->addSuccessor(overflowMBB);
11398
11399 // endMBB is a successor of both offsetMBB and overflowMBB
11400 offsetMBB->addSuccessor(endMBB);
11401 overflowMBB->addSuccessor(endMBB);
11402
11403 // Load the offset value into a register
11404 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11405 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11406 .addOperand(Base)
11407 .addOperand(Scale)
11408 .addOperand(Index)
11409 .addDisp(Disp, UseFPOffset ? 4 : 0)
11410 .addOperand(Segment)
11411 .setMemRefs(MMOBegin, MMOEnd);
11412
11413 // Check if there is enough room left to pull this argument.
11414 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11415 .addReg(OffsetReg)
11416 .addImm(MaxOffset + 8 - ArgSizeA8);
11417
11418 // Branch to "overflowMBB" if offset >= max
11419 // Fall through to "offsetMBB" otherwise
11420 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11421 .addMBB(overflowMBB);
11422 }
11423
11424 // In offsetMBB, emit code to use the reg_save_area.
11425 if (offsetMBB) {
11426 assert(OffsetReg != 0);
11427
11428 // Read the reg_save_area address.
11429 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11430 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11431 .addOperand(Base)
11432 .addOperand(Scale)
11433 .addOperand(Index)
11434 .addDisp(Disp, 16)
11435 .addOperand(Segment)
11436 .setMemRefs(MMOBegin, MMOEnd);
11437
11438 // Zero-extend the offset
11439 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11440 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11441 .addImm(0)
11442 .addReg(OffsetReg)
11443 .addImm(X86::sub_32bit);
11444
11445 // Add the offset to the reg_save_area to get the final address.
11446 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11447 .addReg(OffsetReg64)
11448 .addReg(RegSaveReg);
11449
11450 // Compute the offset for the next argument
11451 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11452 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11453 .addReg(OffsetReg)
11454 .addImm(UseFPOffset ? 16 : 8);
11455
11456 // Store it back into the va_list.
11457 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11458 .addOperand(Base)
11459 .addOperand(Scale)
11460 .addOperand(Index)
11461 .addDisp(Disp, UseFPOffset ? 4 : 0)
11462 .addOperand(Segment)
11463 .addReg(NextOffsetReg)
11464 .setMemRefs(MMOBegin, MMOEnd);
11465
11466 // Jump to endMBB
11467 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11468 .addMBB(endMBB);
11469 }
11470
11471 //
11472 // Emit code to use overflow area
11473 //
11474
11475 // Load the overflow_area address into a register.
11476 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11477 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11478 .addOperand(Base)
11479 .addOperand(Scale)
11480 .addOperand(Index)
11481 .addDisp(Disp, 8)
11482 .addOperand(Segment)
11483 .setMemRefs(MMOBegin, MMOEnd);
11484
11485 // If we need to align it, do so. Otherwise, just copy the address
11486 // to OverflowDestReg.
11487 if (NeedsAlign) {
11488 // Align the overflow address
11489 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11490 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11491
11492 // aligned_addr = (addr + (align-1)) & ~(align-1)
11493 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11494 .addReg(OverflowAddrReg)
11495 .addImm(Align-1);
11496
11497 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11498 .addReg(TmpReg)
11499 .addImm(~(uint64_t)(Align-1));
11500 } else {
11501 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11502 .addReg(OverflowAddrReg);
11503 }
11504
11505 // Compute the next overflow address after this argument.
11506 // (the overflow address should be kept 8-byte aligned)
11507 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11508 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11509 .addReg(OverflowDestReg)
11510 .addImm(ArgSizeA8);
11511
11512 // Store the new overflow address.
11513 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11514 .addOperand(Base)
11515 .addOperand(Scale)
11516 .addOperand(Index)
11517 .addDisp(Disp, 8)
11518 .addOperand(Segment)
11519 .addReg(NextAddrReg)
11520 .setMemRefs(MMOBegin, MMOEnd);
11521
11522 // If we branched, emit the PHI to the front of endMBB.
11523 if (offsetMBB) {
11524 BuildMI(*endMBB, endMBB->begin(), DL,
11525 TII->get(X86::PHI), DestReg)
11526 .addReg(OffsetDestReg).addMBB(offsetMBB)
11527 .addReg(OverflowDestReg).addMBB(overflowMBB);
11528 }
11529
11530 // Erase the pseudo instruction
11531 MI->eraseFromParent();
11532
11533 return endMBB;
11534}
11535
11536MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011537X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11538 MachineInstr *MI,
11539 MachineBasicBlock *MBB) const {
11540 // Emit code to save XMM registers to the stack. The ABI says that the
11541 // number of registers to save is given in %al, so it's theoretically
11542 // possible to do an indirect jump trick to avoid saving all of them,
11543 // however this code takes a simpler approach and just executes all
11544 // of the stores if %al is non-zero. It's less code, and it's probably
11545 // easier on the hardware branch predictor, and stores aren't all that
11546 // expensive anyway.
11547
11548 // Create the new basic blocks. One block contains all the XMM stores,
11549 // and one block is the final destination regardless of whether any
11550 // stores were performed.
11551 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11552 MachineFunction *F = MBB->getParent();
11553 MachineFunction::iterator MBBIter = MBB;
11554 ++MBBIter;
11555 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11556 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11557 F->insert(MBBIter, XMMSaveMBB);
11558 F->insert(MBBIter, EndMBB);
11559
Dan Gohman14152b42010-07-06 20:24:04 +000011560 // Transfer the remainder of MBB and its successor edges to EndMBB.
11561 EndMBB->splice(EndMBB->begin(), MBB,
11562 llvm::next(MachineBasicBlock::iterator(MI)),
11563 MBB->end());
11564 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11565
Dan Gohmand6708ea2009-08-15 01:38:56 +000011566 // The original block will now fall through to the XMM save block.
11567 MBB->addSuccessor(XMMSaveMBB);
11568 // The XMMSaveMBB will fall through to the end block.
11569 XMMSaveMBB->addSuccessor(EndMBB);
11570
11571 // Now add the instructions.
11572 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11573 DebugLoc DL = MI->getDebugLoc();
11574
11575 unsigned CountReg = MI->getOperand(0).getReg();
11576 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11577 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11578
11579 if (!Subtarget->isTargetWin64()) {
11580 // If %al is 0, branch around the XMM save block.
11581 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011582 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011583 MBB->addSuccessor(EndMBB);
11584 }
11585
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011586 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011587 // In the XMM save block, save all the XMM argument registers.
11588 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11589 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011590 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011591 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011592 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011593 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011594 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011595 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011596 .addFrameIndex(RegSaveFrameIndex)
11597 .addImm(/*Scale=*/1)
11598 .addReg(/*IndexReg=*/0)
11599 .addImm(/*Disp=*/Offset)
11600 .addReg(/*Segment=*/0)
11601 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011602 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011603 }
11604
Dan Gohman14152b42010-07-06 20:24:04 +000011605 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011606
11607 return EndMBB;
11608}
Mon P Wang63307c32008-05-05 19:05:59 +000011609
Evan Cheng60c07e12006-07-05 22:17:51 +000011610MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011611X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011612 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011613 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11614 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011615
Chris Lattner52600972009-09-02 05:57:00 +000011616 // To "insert" a SELECT_CC instruction, we actually have to insert the
11617 // diamond control-flow pattern. The incoming instruction knows the
11618 // destination vreg to set, the condition code register to branch on, the
11619 // true/false values to select between, and a branch opcode to use.
11620 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11621 MachineFunction::iterator It = BB;
11622 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011623
Chris Lattner52600972009-09-02 05:57:00 +000011624 // thisMBB:
11625 // ...
11626 // TrueVal = ...
11627 // cmpTY ccX, r1, r2
11628 // bCC copy1MBB
11629 // fallthrough --> copy0MBB
11630 MachineBasicBlock *thisMBB = BB;
11631 MachineFunction *F = BB->getParent();
11632 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11633 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011634 F->insert(It, copy0MBB);
11635 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011636
Bill Wendling730c07e2010-06-25 20:48:10 +000011637 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11638 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011639 if (!MI->killsRegister(X86::EFLAGS)) {
11640 copy0MBB->addLiveIn(X86::EFLAGS);
11641 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011642 }
11643
Dan Gohman14152b42010-07-06 20:24:04 +000011644 // Transfer the remainder of BB and its successor edges to sinkMBB.
11645 sinkMBB->splice(sinkMBB->begin(), BB,
11646 llvm::next(MachineBasicBlock::iterator(MI)),
11647 BB->end());
11648 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11649
11650 // Add the true and fallthrough blocks as its successors.
11651 BB->addSuccessor(copy0MBB);
11652 BB->addSuccessor(sinkMBB);
11653
11654 // Create the conditional branch instruction.
11655 unsigned Opc =
11656 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11657 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11658
Chris Lattner52600972009-09-02 05:57:00 +000011659 // copy0MBB:
11660 // %FalseValue = ...
11661 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011662 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011663
Chris Lattner52600972009-09-02 05:57:00 +000011664 // sinkMBB:
11665 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11666 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011667 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11668 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011669 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11670 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11671
Dan Gohman14152b42010-07-06 20:24:04 +000011672 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011673 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011674}
11675
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011676MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011677X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11678 bool Is64Bit) const {
11679 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11680 DebugLoc DL = MI->getDebugLoc();
11681 MachineFunction *MF = BB->getParent();
11682 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11683
11684 assert(EnableSegmentedStacks);
11685
11686 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11687 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11688
11689 // BB:
11690 // ... [Till the alloca]
11691 // If stacklet is not large enough, jump to mallocMBB
11692 //
11693 // bumpMBB:
11694 // Allocate by subtracting from RSP
11695 // Jump to continueMBB
11696 //
11697 // mallocMBB:
11698 // Allocate by call to runtime
11699 //
11700 // continueMBB:
11701 // ...
11702 // [rest of original BB]
11703 //
11704
11705 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11706 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11707 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11708
11709 MachineRegisterInfo &MRI = MF->getRegInfo();
11710 const TargetRegisterClass *AddrRegClass =
11711 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11712
11713 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11714 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11715 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11716 sizeVReg = MI->getOperand(1).getReg(),
11717 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11718
11719 MachineFunction::iterator MBBIter = BB;
11720 ++MBBIter;
11721
11722 MF->insert(MBBIter, bumpMBB);
11723 MF->insert(MBBIter, mallocMBB);
11724 MF->insert(MBBIter, continueMBB);
11725
11726 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11727 (MachineBasicBlock::iterator(MI)), BB->end());
11728 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11729
11730 // Add code to the main basic block to check if the stack limit has been hit,
11731 // and if so, jump to mallocMBB otherwise to bumpMBB.
11732 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11733 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11734 .addReg(tmpSPVReg).addReg(sizeVReg);
11735 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11736 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11737 .addReg(tmpSPVReg);
11738 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11739
11740 // bumpMBB simply decreases the stack pointer, since we know the current
11741 // stacklet has enough space.
11742 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11743 .addReg(tmpSPVReg);
11744 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11745 .addReg(tmpSPVReg);
11746 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11747
11748 // Calls into a routine in libgcc to allocate more space from the heap.
11749 if (Is64Bit) {
11750 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11751 .addReg(sizeVReg);
11752 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11753 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11754 } else {
11755 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11756 .addImm(12);
11757 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11758 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11759 .addExternalSymbol("__morestack_allocate_stack_space");
11760 }
11761
11762 if (!Is64Bit)
11763 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11764 .addImm(16);
11765
11766 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11767 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11768 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11769
11770 // Set up the CFG correctly.
11771 BB->addSuccessor(bumpMBB);
11772 BB->addSuccessor(mallocMBB);
11773 mallocMBB->addSuccessor(continueMBB);
11774 bumpMBB->addSuccessor(continueMBB);
11775
11776 // Take care of the PHI nodes.
11777 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11778 MI->getOperand(0).getReg())
11779 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11780 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11781
11782 // Delete the original pseudo instruction.
11783 MI->eraseFromParent();
11784
11785 // And we're done.
11786 return continueMBB;
11787}
11788
11789MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011790X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011791 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011792 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11793 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011794
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011795 assert(!Subtarget->isTargetEnvMacho());
11796
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011797 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11798 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011799
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011800 if (Subtarget->isTargetWin64()) {
11801 if (Subtarget->isTargetCygMing()) {
11802 // ___chkstk(Mingw64):
11803 // Clobbers R10, R11, RAX and EFLAGS.
11804 // Updates RSP.
11805 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11806 .addExternalSymbol("___chkstk")
11807 .addReg(X86::RAX, RegState::Implicit)
11808 .addReg(X86::RSP, RegState::Implicit)
11809 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11810 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11811 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11812 } else {
11813 // __chkstk(MSVCRT): does not update stack pointer.
11814 // Clobbers R10, R11 and EFLAGS.
11815 // FIXME: RAX(allocated size) might be reused and not killed.
11816 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11817 .addExternalSymbol("__chkstk")
11818 .addReg(X86::RAX, RegState::Implicit)
11819 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11820 // RAX has the offset to subtracted from RSP.
11821 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11822 .addReg(X86::RSP)
11823 .addReg(X86::RAX);
11824 }
11825 } else {
11826 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011827 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11828
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011829 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11830 .addExternalSymbol(StackProbeSymbol)
11831 .addReg(X86::EAX, RegState::Implicit)
11832 .addReg(X86::ESP, RegState::Implicit)
11833 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11834 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11835 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11836 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011837
Dan Gohman14152b42010-07-06 20:24:04 +000011838 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011839 return BB;
11840}
Chris Lattner52600972009-09-02 05:57:00 +000011841
11842MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011843X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11844 MachineBasicBlock *BB) const {
11845 // This is pretty easy. We're taking the value that we received from
11846 // our load from the relocation, sticking it in either RDI (x86-64)
11847 // or EAX and doing an indirect call. The return value will then
11848 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011849 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011850 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011851 DebugLoc DL = MI->getDebugLoc();
11852 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011853
11854 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011855 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011856
Eric Christopher30ef0e52010-06-03 04:07:48 +000011857 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011858 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11859 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011860 .addReg(X86::RIP)
11861 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011862 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011863 MI->getOperand(3).getTargetFlags())
11864 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011865 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011866 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011867 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011868 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11869 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011870 .addReg(0)
11871 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011872 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011873 MI->getOperand(3).getTargetFlags())
11874 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011875 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011876 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011877 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011878 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11879 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011880 .addReg(TII->getGlobalBaseReg(F))
11881 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011882 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011883 MI->getOperand(3).getTargetFlags())
11884 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011885 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011886 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011887 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011888
Dan Gohman14152b42010-07-06 20:24:04 +000011889 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011890 return BB;
11891}
11892
11893MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011894X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011895 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011896 switch (MI->getOpcode()) {
11897 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011898 case X86::TAILJMPd64:
11899 case X86::TAILJMPr64:
11900 case X86::TAILJMPm64:
11901 assert(!"TAILJMP64 would not be touched here.");
11902 case X86::TCRETURNdi64:
11903 case X86::TCRETURNri64:
11904 case X86::TCRETURNmi64:
11905 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11906 // On AMD64, additional defs should be added before register allocation.
11907 if (!Subtarget->isTargetWin64()) {
11908 MI->addRegisterDefined(X86::RSI);
11909 MI->addRegisterDefined(X86::RDI);
11910 MI->addRegisterDefined(X86::XMM6);
11911 MI->addRegisterDefined(X86::XMM7);
11912 MI->addRegisterDefined(X86::XMM8);
11913 MI->addRegisterDefined(X86::XMM9);
11914 MI->addRegisterDefined(X86::XMM10);
11915 MI->addRegisterDefined(X86::XMM11);
11916 MI->addRegisterDefined(X86::XMM12);
11917 MI->addRegisterDefined(X86::XMM13);
11918 MI->addRegisterDefined(X86::XMM14);
11919 MI->addRegisterDefined(X86::XMM15);
11920 }
11921 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011922 case X86::WIN_ALLOCA:
11923 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011924 case X86::SEG_ALLOCA_32:
11925 return EmitLoweredSegAlloca(MI, BB, false);
11926 case X86::SEG_ALLOCA_64:
11927 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011928 case X86::TLSCall_32:
11929 case X86::TLSCall_64:
11930 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011931 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011932 case X86::CMOV_FR32:
11933 case X86::CMOV_FR64:
11934 case X86::CMOV_V4F32:
11935 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011936 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011937 case X86::CMOV_V8F32:
11938 case X86::CMOV_V4F64:
11939 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000011940 case X86::CMOV_GR16:
11941 case X86::CMOV_GR32:
11942 case X86::CMOV_RFP32:
11943 case X86::CMOV_RFP64:
11944 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011945 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011946
Dale Johannesen849f2142007-07-03 00:53:03 +000011947 case X86::FP32_TO_INT16_IN_MEM:
11948 case X86::FP32_TO_INT32_IN_MEM:
11949 case X86::FP32_TO_INT64_IN_MEM:
11950 case X86::FP64_TO_INT16_IN_MEM:
11951 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000011952 case X86::FP64_TO_INT64_IN_MEM:
11953 case X86::FP80_TO_INT16_IN_MEM:
11954 case X86::FP80_TO_INT32_IN_MEM:
11955 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000011956 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11957 DebugLoc DL = MI->getDebugLoc();
11958
Evan Cheng60c07e12006-07-05 22:17:51 +000011959 // Change the floating point control register to use "round towards zero"
11960 // mode when truncating to an integer value.
11961 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000011962 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000011963 addFrameReference(BuildMI(*BB, MI, DL,
11964 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011965
11966 // Load the old value of the high byte of the control word...
11967 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000011968 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000011969 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000011970 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011971
11972 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000011973 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000011974 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000011975
11976 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000011977 addFrameReference(BuildMI(*BB, MI, DL,
11978 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011979
11980 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000011981 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000011982 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000011983
11984 // Get the X86 opcode to use.
11985 unsigned Opc;
11986 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011987 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000011988 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
11989 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
11990 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
11991 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
11992 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
11993 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000011994 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
11995 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
11996 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000011997 }
11998
11999 X86AddressMode AM;
12000 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012001 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012002 AM.BaseType = X86AddressMode::RegBase;
12003 AM.Base.Reg = Op.getReg();
12004 } else {
12005 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012006 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012007 }
12008 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012009 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012010 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012011 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012012 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012013 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012014 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012015 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012016 AM.GV = Op.getGlobal();
12017 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012018 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012019 }
Dan Gohman14152b42010-07-06 20:24:04 +000012020 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012021 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012022
12023 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012024 addFrameReference(BuildMI(*BB, MI, DL,
12025 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012026
Dan Gohman14152b42010-07-06 20:24:04 +000012027 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012028 return BB;
12029 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012030 // String/text processing lowering.
12031 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012032 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012033 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12034 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012035 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012036 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12037 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012038 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012039 return EmitPCMP(MI, BB, 5, false /* in mem */);
12040 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012041 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012042 return EmitPCMP(MI, BB, 5, true /* in mem */);
12043
Eric Christopher228232b2010-11-30 07:20:12 +000012044 // Thread synchronization.
12045 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012046 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012047 case X86::MWAIT:
12048 return EmitMwait(MI, BB);
12049
Eric Christopherb120ab42009-08-18 22:50:32 +000012050 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012051 case X86::ATOMAND32:
12052 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012053 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012054 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012055 X86::NOT32r, X86::EAX,
12056 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012057 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012058 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12059 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012060 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012061 X86::NOT32r, X86::EAX,
12062 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012063 case X86::ATOMXOR32:
12064 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012065 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012066 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012067 X86::NOT32r, X86::EAX,
12068 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012069 case X86::ATOMNAND32:
12070 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012071 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012072 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012073 X86::NOT32r, X86::EAX,
12074 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012075 case X86::ATOMMIN32:
12076 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12077 case X86::ATOMMAX32:
12078 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12079 case X86::ATOMUMIN32:
12080 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12081 case X86::ATOMUMAX32:
12082 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012083
12084 case X86::ATOMAND16:
12085 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12086 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012087 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012088 X86::NOT16r, X86::AX,
12089 X86::GR16RegisterClass);
12090 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012091 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012092 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012093 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012094 X86::NOT16r, X86::AX,
12095 X86::GR16RegisterClass);
12096 case X86::ATOMXOR16:
12097 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12098 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012099 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012100 X86::NOT16r, X86::AX,
12101 X86::GR16RegisterClass);
12102 case X86::ATOMNAND16:
12103 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12104 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012105 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012106 X86::NOT16r, X86::AX,
12107 X86::GR16RegisterClass, true);
12108 case X86::ATOMMIN16:
12109 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12110 case X86::ATOMMAX16:
12111 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12112 case X86::ATOMUMIN16:
12113 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12114 case X86::ATOMUMAX16:
12115 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12116
12117 case X86::ATOMAND8:
12118 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12119 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012120 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012121 X86::NOT8r, X86::AL,
12122 X86::GR8RegisterClass);
12123 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012124 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012125 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012126 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012127 X86::NOT8r, X86::AL,
12128 X86::GR8RegisterClass);
12129 case X86::ATOMXOR8:
12130 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12131 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012132 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012133 X86::NOT8r, X86::AL,
12134 X86::GR8RegisterClass);
12135 case X86::ATOMNAND8:
12136 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12137 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012138 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012139 X86::NOT8r, X86::AL,
12140 X86::GR8RegisterClass, true);
12141 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012142 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012143 case X86::ATOMAND64:
12144 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012145 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012146 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012147 X86::NOT64r, X86::RAX,
12148 X86::GR64RegisterClass);
12149 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012150 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12151 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012152 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012153 X86::NOT64r, X86::RAX,
12154 X86::GR64RegisterClass);
12155 case X86::ATOMXOR64:
12156 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012157 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012158 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012159 X86::NOT64r, X86::RAX,
12160 X86::GR64RegisterClass);
12161 case X86::ATOMNAND64:
12162 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12163 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012164 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012165 X86::NOT64r, X86::RAX,
12166 X86::GR64RegisterClass, true);
12167 case X86::ATOMMIN64:
12168 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12169 case X86::ATOMMAX64:
12170 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12171 case X86::ATOMUMIN64:
12172 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12173 case X86::ATOMUMAX64:
12174 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012175
12176 // This group does 64-bit operations on a 32-bit host.
12177 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012178 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012179 X86::AND32rr, X86::AND32rr,
12180 X86::AND32ri, X86::AND32ri,
12181 false);
12182 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012183 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012184 X86::OR32rr, X86::OR32rr,
12185 X86::OR32ri, X86::OR32ri,
12186 false);
12187 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012188 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012189 X86::XOR32rr, X86::XOR32rr,
12190 X86::XOR32ri, X86::XOR32ri,
12191 false);
12192 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012193 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012194 X86::AND32rr, X86::AND32rr,
12195 X86::AND32ri, X86::AND32ri,
12196 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012197 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012198 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012199 X86::ADD32rr, X86::ADC32rr,
12200 X86::ADD32ri, X86::ADC32ri,
12201 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012202 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012203 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012204 X86::SUB32rr, X86::SBB32rr,
12205 X86::SUB32ri, X86::SBB32ri,
12206 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012207 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012208 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012209 X86::MOV32rr, X86::MOV32rr,
12210 X86::MOV32ri, X86::MOV32ri,
12211 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012212 case X86::VASTART_SAVE_XMM_REGS:
12213 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012214
12215 case X86::VAARG_64:
12216 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012217 }
12218}
12219
12220//===----------------------------------------------------------------------===//
12221// X86 Optimization Hooks
12222//===----------------------------------------------------------------------===//
12223
Dan Gohman475871a2008-07-27 21:46:04 +000012224void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012225 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012226 APInt &KnownZero,
12227 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012228 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012229 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012230 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012231 assert((Opc >= ISD::BUILTIN_OP_END ||
12232 Opc == ISD::INTRINSIC_WO_CHAIN ||
12233 Opc == ISD::INTRINSIC_W_CHAIN ||
12234 Opc == ISD::INTRINSIC_VOID) &&
12235 "Should use MaskedValueIsZero if you don't know whether Op"
12236 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012237
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012238 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012239 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012240 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012241 case X86ISD::ADD:
12242 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012243 case X86ISD::ADC:
12244 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012245 case X86ISD::SMUL:
12246 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012247 case X86ISD::INC:
12248 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012249 case X86ISD::OR:
12250 case X86ISD::XOR:
12251 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012252 // These nodes' second result is a boolean.
12253 if (Op.getResNo() == 0)
12254 break;
12255 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012256 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012257 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12258 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012259 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012260 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012261}
Chris Lattner259e97c2006-01-31 19:43:35 +000012262
Owen Andersonbc146b02010-09-21 20:42:50 +000012263unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12264 unsigned Depth) const {
12265 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12266 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12267 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012268
Owen Andersonbc146b02010-09-21 20:42:50 +000012269 // Fallback case.
12270 return 1;
12271}
12272
Evan Cheng206ee9d2006-07-07 08:33:52 +000012273/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012274/// node is a GlobalAddress + offset.
12275bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012276 const GlobalValue* &GA,
12277 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012278 if (N->getOpcode() == X86ISD::Wrapper) {
12279 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012280 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012281 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012282 return true;
12283 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012284 }
Evan Chengad4196b2008-05-12 19:56:52 +000012285 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012286}
12287
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012288/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12289/// same as extracting the high 128-bit part of 256-bit vector and then
12290/// inserting the result into the low part of a new 256-bit vector
12291static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12292 EVT VT = SVOp->getValueType(0);
12293 int NumElems = VT.getVectorNumElements();
12294
12295 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12296 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12297 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12298 SVOp->getMaskElt(j) >= 0)
12299 return false;
12300
12301 return true;
12302}
12303
12304/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12305/// same as extracting the low 128-bit part of 256-bit vector and then
12306/// inserting the result into the high part of a new 256-bit vector
12307static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12308 EVT VT = SVOp->getValueType(0);
12309 int NumElems = VT.getVectorNumElements();
12310
12311 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12312 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12313 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12314 SVOp->getMaskElt(j) >= 0)
12315 return false;
12316
12317 return true;
12318}
12319
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012320/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12321static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12322 TargetLowering::DAGCombinerInfo &DCI) {
12323 DebugLoc dl = N->getDebugLoc();
12324 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12325 SDValue V1 = SVOp->getOperand(0);
12326 SDValue V2 = SVOp->getOperand(1);
12327 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012328 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012329
12330 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12331 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12332 //
12333 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012334 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012335 // V UNDEF BUILD_VECTOR UNDEF
12336 // \ / \ /
12337 // CONCAT_VECTOR CONCAT_VECTOR
12338 // \ /
12339 // \ /
12340 // RESULT: V + zero extended
12341 //
12342 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12343 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12344 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12345 return SDValue();
12346
12347 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12348 return SDValue();
12349
12350 // To match the shuffle mask, the first half of the mask should
12351 // be exactly the first vector, and all the rest a splat with the
12352 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012353 for (int i = 0; i < NumElems/2; ++i)
12354 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12355 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12356 return SDValue();
12357
12358 // Emit a zeroed vector and insert the desired subvector on its
12359 // first half.
12360 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, DAG, dl);
12361 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12362 DAG.getConstant(0, MVT::i32), DAG, dl);
12363 return DCI.CombineTo(N, InsV);
12364 }
12365
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012366 //===--------------------------------------------------------------------===//
12367 // Combine some shuffles into subvector extracts and inserts:
12368 //
12369
12370 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12371 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12372 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12373 DAG, dl);
12374 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12375 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12376 return DCI.CombineTo(N, InsV);
12377 }
12378
12379 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12380 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12381 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12382 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12383 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12384 return DCI.CombineTo(N, InsV);
12385 }
12386
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012387 return SDValue();
12388}
12389
12390/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012391static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012392 TargetLowering::DAGCombinerInfo &DCI,
12393 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012394 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012395 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012396
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012397 // Don't create instructions with illegal types after legalize types has run.
12398 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12399 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12400 return SDValue();
12401
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012402 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12403 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12404 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012405 return PerformShuffleCombine256(N, DAG, DCI);
12406
12407 // Only handle 128 wide vector from here on.
12408 if (VT.getSizeInBits() != 128)
12409 return SDValue();
12410
12411 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12412 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12413 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012414 SmallVector<SDValue, 16> Elts;
12415 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012416 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012417
Nate Begemanfdea31a2010-03-24 20:49:50 +000012418 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012419}
Evan Chengd880b972008-05-09 21:53:03 +000012420
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012421/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12422/// generation and convert it from being a bunch of shuffles and extracts
12423/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012424static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12425 const TargetLowering &TLI) {
12426 SDValue InputVector = N->getOperand(0);
12427
12428 // Only operate on vectors of 4 elements, where the alternative shuffling
12429 // gets to be more expensive.
12430 if (InputVector.getValueType() != MVT::v4i32)
12431 return SDValue();
12432
12433 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12434 // single use which is a sign-extend or zero-extend, and all elements are
12435 // used.
12436 SmallVector<SDNode *, 4> Uses;
12437 unsigned ExtractedElements = 0;
12438 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12439 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12440 if (UI.getUse().getResNo() != InputVector.getResNo())
12441 return SDValue();
12442
12443 SDNode *Extract = *UI;
12444 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12445 return SDValue();
12446
12447 if (Extract->getValueType(0) != MVT::i32)
12448 return SDValue();
12449 if (!Extract->hasOneUse())
12450 return SDValue();
12451 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12452 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12453 return SDValue();
12454 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12455 return SDValue();
12456
12457 // Record which element was extracted.
12458 ExtractedElements |=
12459 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12460
12461 Uses.push_back(Extract);
12462 }
12463
12464 // If not all the elements were used, this may not be worthwhile.
12465 if (ExtractedElements != 15)
12466 return SDValue();
12467
12468 // Ok, we've now decided to do the transformation.
12469 DebugLoc dl = InputVector.getDebugLoc();
12470
12471 // Store the value to a temporary stack slot.
12472 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012473 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12474 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012475
12476 // Replace each use (extract) with a load of the appropriate element.
12477 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12478 UE = Uses.end(); UI != UE; ++UI) {
12479 SDNode *Extract = *UI;
12480
Nadav Rotem86694292011-05-17 08:31:57 +000012481 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012482 SDValue Idx = Extract->getOperand(1);
12483 unsigned EltSize =
12484 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12485 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12486 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12487
Nadav Rotem86694292011-05-17 08:31:57 +000012488 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012489 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012490
12491 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012492 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012493 ScalarAddr, MachinePointerInfo(),
12494 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012495
12496 // Replace the exact with the load.
12497 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12498 }
12499
12500 // The replacement was made in place; don't return anything.
12501 return SDValue();
12502}
12503
Chris Lattner83e6c992006-10-04 06:57:07 +000012504/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012505static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012506 const X86Subtarget *Subtarget) {
12507 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012508 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012509 // Get the LHS/RHS of the select.
12510 SDValue LHS = N->getOperand(1);
12511 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000012512
Dan Gohman670e5392009-09-21 18:03:22 +000012513 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012514 // instructions match the semantics of the common C idiom x<y?x:y but not
12515 // x<=y?x:y, because of how they handle negative zero (which can be
12516 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000012517 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000012518 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000012519 Cond.getOpcode() == ISD::SETCC) {
12520 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012521
Chris Lattner47b4ce82009-03-11 05:48:52 +000012522 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012523 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012524 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12525 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012526 switch (CC) {
12527 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012528 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012529 // Converting this to a min would handle NaNs incorrectly, and swapping
12530 // the operands would cause it to handle comparisons between positive
12531 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012532 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012533 if (!UnsafeFPMath &&
12534 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12535 break;
12536 std::swap(LHS, RHS);
12537 }
Dan Gohman670e5392009-09-21 18:03:22 +000012538 Opcode = X86ISD::FMIN;
12539 break;
12540 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012541 // Converting this to a min would handle comparisons between positive
12542 // and negative zero incorrectly.
12543 if (!UnsafeFPMath &&
12544 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12545 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012546 Opcode = X86ISD::FMIN;
12547 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012548 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012549 // Converting this to a min would handle both negative zeros and NaNs
12550 // incorrectly, but we can swap the operands to fix both.
12551 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012552 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012553 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012554 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012555 Opcode = X86ISD::FMIN;
12556 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012557
Dan Gohman670e5392009-09-21 18:03:22 +000012558 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012559 // Converting this to a max would handle comparisons between positive
12560 // and negative zero incorrectly.
12561 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012562 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012563 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012564 Opcode = X86ISD::FMAX;
12565 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012566 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012567 // Converting this to a max would handle NaNs incorrectly, and swapping
12568 // the operands would cause it to handle comparisons between positive
12569 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012570 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012571 if (!UnsafeFPMath &&
12572 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12573 break;
12574 std::swap(LHS, RHS);
12575 }
Dan Gohman670e5392009-09-21 18:03:22 +000012576 Opcode = X86ISD::FMAX;
12577 break;
12578 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012579 // Converting this to a max would handle both negative zeros and NaNs
12580 // incorrectly, but we can swap the operands to fix both.
12581 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012582 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012583 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012584 case ISD::SETGE:
12585 Opcode = X86ISD::FMAX;
12586 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012587 }
Dan Gohman670e5392009-09-21 18:03:22 +000012588 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012589 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12590 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012591 switch (CC) {
12592 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012593 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012594 // Converting this to a min would handle comparisons between positive
12595 // and negative zero incorrectly, and swapping the operands would
12596 // cause it to handle NaNs incorrectly.
12597 if (!UnsafeFPMath &&
12598 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012599 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012600 break;
12601 std::swap(LHS, RHS);
12602 }
Dan Gohman670e5392009-09-21 18:03:22 +000012603 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012604 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012605 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012606 // Converting this to a min would handle NaNs incorrectly.
12607 if (!UnsafeFPMath &&
12608 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12609 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012610 Opcode = X86ISD::FMIN;
12611 break;
12612 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012613 // Converting this to a min would handle both negative zeros and NaNs
12614 // incorrectly, but we can swap the operands to fix both.
12615 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012616 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012617 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012618 case ISD::SETGE:
12619 Opcode = X86ISD::FMIN;
12620 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012621
Dan Gohman670e5392009-09-21 18:03:22 +000012622 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012623 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012624 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012625 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012626 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012627 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012628 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012629 // Converting this to a max would handle comparisons between positive
12630 // and negative zero incorrectly, and swapping the operands would
12631 // cause it to handle NaNs incorrectly.
12632 if (!UnsafeFPMath &&
12633 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012634 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012635 break;
12636 std::swap(LHS, RHS);
12637 }
Dan Gohman670e5392009-09-21 18:03:22 +000012638 Opcode = X86ISD::FMAX;
12639 break;
12640 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012641 // Converting this to a max would handle both negative zeros and NaNs
12642 // incorrectly, but we can swap the operands to fix both.
12643 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012644 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012645 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012646 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012647 Opcode = X86ISD::FMAX;
12648 break;
12649 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012650 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012651
Chris Lattner47b4ce82009-03-11 05:48:52 +000012652 if (Opcode)
12653 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012654 }
Eric Christopherfd179292009-08-27 18:07:15 +000012655
Chris Lattnerd1980a52009-03-12 06:52:53 +000012656 // If this is a select between two integer constants, try to do some
12657 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012658 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12659 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012660 // Don't do this for crazy integer types.
12661 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12662 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012663 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012664 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012665
Chris Lattnercee56e72009-03-13 05:53:31 +000012666 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012667 // Efficiently invertible.
12668 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12669 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12670 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12671 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012672 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012673 }
Eric Christopherfd179292009-08-27 18:07:15 +000012674
Chris Lattnerd1980a52009-03-12 06:52:53 +000012675 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012676 if (FalseC->getAPIntValue() == 0 &&
12677 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012678 if (NeedsCondInvert) // Invert the condition if needed.
12679 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12680 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012681
Chris Lattnerd1980a52009-03-12 06:52:53 +000012682 // Zero extend the condition if needed.
12683 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012684
Chris Lattnercee56e72009-03-13 05:53:31 +000012685 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012686 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012687 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012688 }
Eric Christopherfd179292009-08-27 18:07:15 +000012689
Chris Lattner97a29a52009-03-13 05:22:11 +000012690 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012691 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012692 if (NeedsCondInvert) // Invert the condition if needed.
12693 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12694 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012695
Chris Lattner97a29a52009-03-13 05:22:11 +000012696 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012697 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12698 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012699 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012700 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012701 }
Eric Christopherfd179292009-08-27 18:07:15 +000012702
Chris Lattnercee56e72009-03-13 05:53:31 +000012703 // Optimize cases that will turn into an LEA instruction. This requires
12704 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012705 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012706 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012707 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012708
Chris Lattnercee56e72009-03-13 05:53:31 +000012709 bool isFastMultiplier = false;
12710 if (Diff < 10) {
12711 switch ((unsigned char)Diff) {
12712 default: break;
12713 case 1: // result = add base, cond
12714 case 2: // result = lea base( , cond*2)
12715 case 3: // result = lea base(cond, cond*2)
12716 case 4: // result = lea base( , cond*4)
12717 case 5: // result = lea base(cond, cond*4)
12718 case 8: // result = lea base( , cond*8)
12719 case 9: // result = lea base(cond, cond*8)
12720 isFastMultiplier = true;
12721 break;
12722 }
12723 }
Eric Christopherfd179292009-08-27 18:07:15 +000012724
Chris Lattnercee56e72009-03-13 05:53:31 +000012725 if (isFastMultiplier) {
12726 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12727 if (NeedsCondInvert) // Invert the condition if needed.
12728 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12729 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012730
Chris Lattnercee56e72009-03-13 05:53:31 +000012731 // Zero extend the condition if needed.
12732 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12733 Cond);
12734 // Scale the condition by the difference.
12735 if (Diff != 1)
12736 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12737 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012738
Chris Lattnercee56e72009-03-13 05:53:31 +000012739 // Add the base if non-zero.
12740 if (FalseC->getAPIntValue() != 0)
12741 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12742 SDValue(FalseC, 0));
12743 return Cond;
12744 }
Eric Christopherfd179292009-08-27 18:07:15 +000012745 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012746 }
12747 }
Eric Christopherfd179292009-08-27 18:07:15 +000012748
Dan Gohman475871a2008-07-27 21:46:04 +000012749 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012750}
12751
Chris Lattnerd1980a52009-03-12 06:52:53 +000012752/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12753static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12754 TargetLowering::DAGCombinerInfo &DCI) {
12755 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012756
Chris Lattnerd1980a52009-03-12 06:52:53 +000012757 // If the flag operand isn't dead, don't touch this CMOV.
12758 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12759 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012760
Evan Chengb5a55d92011-05-24 01:48:22 +000012761 SDValue FalseOp = N->getOperand(0);
12762 SDValue TrueOp = N->getOperand(1);
12763 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12764 SDValue Cond = N->getOperand(3);
12765 if (CC == X86::COND_E || CC == X86::COND_NE) {
12766 switch (Cond.getOpcode()) {
12767 default: break;
12768 case X86ISD::BSR:
12769 case X86ISD::BSF:
12770 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12771 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12772 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12773 }
12774 }
12775
Chris Lattnerd1980a52009-03-12 06:52:53 +000012776 // If this is a select between two integer constants, try to do some
12777 // optimizations. Note that the operands are ordered the opposite of SELECT
12778 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012779 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12780 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012781 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12782 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012783 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12784 CC = X86::GetOppositeBranchCondition(CC);
12785 std::swap(TrueC, FalseC);
12786 }
Eric Christopherfd179292009-08-27 18:07:15 +000012787
Chris Lattnerd1980a52009-03-12 06:52:53 +000012788 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012789 // This is efficient for any integer data type (including i8/i16) and
12790 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012791 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012792 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12793 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012794
Chris Lattnerd1980a52009-03-12 06:52:53 +000012795 // Zero extend the condition if needed.
12796 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012797
Chris Lattnerd1980a52009-03-12 06:52:53 +000012798 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12799 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012800 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012801 if (N->getNumValues() == 2) // Dead flag value?
12802 return DCI.CombineTo(N, Cond, SDValue());
12803 return Cond;
12804 }
Eric Christopherfd179292009-08-27 18:07:15 +000012805
Chris Lattnercee56e72009-03-13 05:53:31 +000012806 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12807 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012808 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012809 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12810 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012811
Chris Lattner97a29a52009-03-13 05:22:11 +000012812 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012813 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12814 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012815 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12816 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012817
Chris Lattner97a29a52009-03-13 05:22:11 +000012818 if (N->getNumValues() == 2) // Dead flag value?
12819 return DCI.CombineTo(N, Cond, SDValue());
12820 return Cond;
12821 }
Eric Christopherfd179292009-08-27 18:07:15 +000012822
Chris Lattnercee56e72009-03-13 05:53:31 +000012823 // Optimize cases that will turn into an LEA instruction. This requires
12824 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012825 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012826 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012827 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012828
Chris Lattnercee56e72009-03-13 05:53:31 +000012829 bool isFastMultiplier = false;
12830 if (Diff < 10) {
12831 switch ((unsigned char)Diff) {
12832 default: break;
12833 case 1: // result = add base, cond
12834 case 2: // result = lea base( , cond*2)
12835 case 3: // result = lea base(cond, cond*2)
12836 case 4: // result = lea base( , cond*4)
12837 case 5: // result = lea base(cond, cond*4)
12838 case 8: // result = lea base( , cond*8)
12839 case 9: // result = lea base(cond, cond*8)
12840 isFastMultiplier = true;
12841 break;
12842 }
12843 }
Eric Christopherfd179292009-08-27 18:07:15 +000012844
Chris Lattnercee56e72009-03-13 05:53:31 +000012845 if (isFastMultiplier) {
12846 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012847 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12848 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012849 // Zero extend the condition if needed.
12850 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12851 Cond);
12852 // Scale the condition by the difference.
12853 if (Diff != 1)
12854 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12855 DAG.getConstant(Diff, Cond.getValueType()));
12856
12857 // Add the base if non-zero.
12858 if (FalseC->getAPIntValue() != 0)
12859 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12860 SDValue(FalseC, 0));
12861 if (N->getNumValues() == 2) // Dead flag value?
12862 return DCI.CombineTo(N, Cond, SDValue());
12863 return Cond;
12864 }
Eric Christopherfd179292009-08-27 18:07:15 +000012865 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012866 }
12867 }
12868 return SDValue();
12869}
12870
12871
Evan Cheng0b0cd912009-03-28 05:57:29 +000012872/// PerformMulCombine - Optimize a single multiply with constant into two
12873/// in order to implement it with two cheaper instructions, e.g.
12874/// LEA + SHL, LEA + LEA.
12875static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12876 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012877 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12878 return SDValue();
12879
Owen Andersone50ed302009-08-10 22:56:29 +000012880 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012881 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012882 return SDValue();
12883
12884 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12885 if (!C)
12886 return SDValue();
12887 uint64_t MulAmt = C->getZExtValue();
12888 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12889 return SDValue();
12890
12891 uint64_t MulAmt1 = 0;
12892 uint64_t MulAmt2 = 0;
12893 if ((MulAmt % 9) == 0) {
12894 MulAmt1 = 9;
12895 MulAmt2 = MulAmt / 9;
12896 } else if ((MulAmt % 5) == 0) {
12897 MulAmt1 = 5;
12898 MulAmt2 = MulAmt / 5;
12899 } else if ((MulAmt % 3) == 0) {
12900 MulAmt1 = 3;
12901 MulAmt2 = MulAmt / 3;
12902 }
12903 if (MulAmt2 &&
12904 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12905 DebugLoc DL = N->getDebugLoc();
12906
12907 if (isPowerOf2_64(MulAmt2) &&
12908 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12909 // If second multiplifer is pow2, issue it first. We want the multiply by
12910 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12911 // is an add.
12912 std::swap(MulAmt1, MulAmt2);
12913
12914 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012915 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012916 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012917 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012918 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012919 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012920 DAG.getConstant(MulAmt1, VT));
12921
Eric Christopherfd179292009-08-27 18:07:15 +000012922 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012923 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000012924 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000012925 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012926 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000012927 DAG.getConstant(MulAmt2, VT));
12928
12929 // Do not add new nodes to DAG combiner worklist.
12930 DCI.CombineTo(N, NewMul, false);
12931 }
12932 return SDValue();
12933}
12934
Evan Chengad9c0a32009-12-15 00:53:42 +000012935static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
12936 SDValue N0 = N->getOperand(0);
12937 SDValue N1 = N->getOperand(1);
12938 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
12939 EVT VT = N0.getValueType();
12940
12941 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
12942 // since the result of setcc_c is all zero's or all ones.
12943 if (N1C && N0.getOpcode() == ISD::AND &&
12944 N0.getOperand(1).getOpcode() == ISD::Constant) {
12945 SDValue N00 = N0.getOperand(0);
12946 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
12947 ((N00.getOpcode() == ISD::ANY_EXTEND ||
12948 N00.getOpcode() == ISD::ZERO_EXTEND) &&
12949 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
12950 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
12951 APInt ShAmt = N1C->getAPIntValue();
12952 Mask = Mask.shl(ShAmt);
12953 if (Mask != 0)
12954 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
12955 N00, DAG.getConstant(Mask, VT));
12956 }
12957 }
12958
12959 return SDValue();
12960}
Evan Cheng0b0cd912009-03-28 05:57:29 +000012961
Nate Begeman740ab032009-01-26 00:52:55 +000012962/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
12963/// when possible.
12964static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
12965 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000012966 EVT VT = N->getValueType(0);
12967 if (!VT.isVector() && VT.isInteger() &&
12968 N->getOpcode() == ISD::SHL)
12969 return PerformSHLCombine(N, DAG);
12970
Nate Begeman740ab032009-01-26 00:52:55 +000012971 // On X86 with SSE2 support, we can transform this to a vector shift if
12972 // all elements are shifted by the same amount. We can't do this in legalize
12973 // because the a constant vector is typically transformed to a constant pool
12974 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000012975 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012976 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000012977
Owen Anderson825b72b2009-08-11 20:47:22 +000012978 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000012979 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000012980
Mon P Wang3becd092009-01-28 08:12:05 +000012981 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000012982 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000012983 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000012984 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000012985 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
12986 unsigned NumElts = VT.getVectorNumElements();
12987 unsigned i = 0;
12988 for (; i != NumElts; ++i) {
12989 SDValue Arg = ShAmtOp.getOperand(i);
12990 if (Arg.getOpcode() == ISD::UNDEF) continue;
12991 BaseShAmt = Arg;
12992 break;
12993 }
12994 for (; i != NumElts; ++i) {
12995 SDValue Arg = ShAmtOp.getOperand(i);
12996 if (Arg.getOpcode() == ISD::UNDEF) continue;
12997 if (Arg != BaseShAmt) {
12998 return SDValue();
12999 }
13000 }
13001 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013002 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013003 SDValue InVec = ShAmtOp.getOperand(0);
13004 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13005 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13006 unsigned i = 0;
13007 for (; i != NumElts; ++i) {
13008 SDValue Arg = InVec.getOperand(i);
13009 if (Arg.getOpcode() == ISD::UNDEF) continue;
13010 BaseShAmt = Arg;
13011 break;
13012 }
13013 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13014 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013015 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013016 if (C->getZExtValue() == SplatIdx)
13017 BaseShAmt = InVec.getOperand(1);
13018 }
13019 }
13020 if (BaseShAmt.getNode() == 0)
13021 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13022 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013023 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013024 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013025
Mon P Wangefa42202009-09-03 19:56:25 +000013026 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013027 if (EltVT.bitsGT(MVT::i32))
13028 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13029 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013030 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013031
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013032 // The shift amount is identical so we can do a vector shift.
13033 SDValue ValOp = N->getOperand(0);
13034 switch (N->getOpcode()) {
13035 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013036 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013037 break;
13038 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013039 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013040 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013041 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013042 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013043 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013044 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013045 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013046 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013047 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013048 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013049 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013050 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013051 break;
13052 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013053 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013054 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013055 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013056 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013057 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013058 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013059 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013060 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013061 break;
13062 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013063 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013064 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013065 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013066 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013067 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013068 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013069 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013070 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013071 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013072 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013073 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013074 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013075 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013076 }
13077 return SDValue();
13078}
13079
Nate Begemanb65c1752010-12-17 22:55:37 +000013080
Stuart Hastings865f0932011-06-03 23:53:54 +000013081// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13082// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13083// and friends. Likewise for OR -> CMPNEQSS.
13084static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13085 TargetLowering::DAGCombinerInfo &DCI,
13086 const X86Subtarget *Subtarget) {
13087 unsigned opcode;
13088
13089 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13090 // we're requiring SSE2 for both.
13091 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
13092 SDValue N0 = N->getOperand(0);
13093 SDValue N1 = N->getOperand(1);
13094 SDValue CMP0 = N0->getOperand(1);
13095 SDValue CMP1 = N1->getOperand(1);
13096 DebugLoc DL = N->getDebugLoc();
13097
13098 // The SETCCs should both refer to the same CMP.
13099 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13100 return SDValue();
13101
13102 SDValue CMP00 = CMP0->getOperand(0);
13103 SDValue CMP01 = CMP0->getOperand(1);
13104 EVT VT = CMP00.getValueType();
13105
13106 if (VT == MVT::f32 || VT == MVT::f64) {
13107 bool ExpectingFlags = false;
13108 // Check for any users that want flags:
13109 for (SDNode::use_iterator UI = N->use_begin(),
13110 UE = N->use_end();
13111 !ExpectingFlags && UI != UE; ++UI)
13112 switch (UI->getOpcode()) {
13113 default:
13114 case ISD::BR_CC:
13115 case ISD::BRCOND:
13116 case ISD::SELECT:
13117 ExpectingFlags = true;
13118 break;
13119 case ISD::CopyToReg:
13120 case ISD::SIGN_EXTEND:
13121 case ISD::ZERO_EXTEND:
13122 case ISD::ANY_EXTEND:
13123 break;
13124 }
13125
13126 if (!ExpectingFlags) {
13127 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13128 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13129
13130 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13131 X86::CondCode tmp = cc0;
13132 cc0 = cc1;
13133 cc1 = tmp;
13134 }
13135
13136 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13137 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13138 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13139 X86ISD::NodeType NTOperator = is64BitFP ?
13140 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13141 // FIXME: need symbolic constants for these magic numbers.
13142 // See X86ATTInstPrinter.cpp:printSSECC().
13143 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13144 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13145 DAG.getConstant(x86cc, MVT::i8));
13146 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13147 OnesOrZeroesF);
13148 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13149 DAG.getConstant(1, MVT::i32));
13150 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13151 return OneBitOfTruth;
13152 }
13153 }
13154 }
13155 }
13156 return SDValue();
13157}
13158
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013159/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13160/// so it can be folded inside ANDNP.
13161static bool CanFoldXORWithAllOnes(const SDNode *N) {
13162 EVT VT = N->getValueType(0);
13163
13164 // Match direct AllOnes for 128 and 256-bit vectors
13165 if (ISD::isBuildVectorAllOnes(N))
13166 return true;
13167
13168 // Look through a bit convert.
13169 if (N->getOpcode() == ISD::BITCAST)
13170 N = N->getOperand(0).getNode();
13171
13172 // Sometimes the operand may come from a insert_subvector building a 256-bit
13173 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013174 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013175 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13176 SDValue V1 = N->getOperand(0);
13177 SDValue V2 = N->getOperand(1);
13178
13179 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13180 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13181 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13182 ISD::isBuildVectorAllOnes(V2.getNode()))
13183 return true;
13184 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013185
13186 return false;
13187}
13188
Nate Begemanb65c1752010-12-17 22:55:37 +000013189static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13190 TargetLowering::DAGCombinerInfo &DCI,
13191 const X86Subtarget *Subtarget) {
13192 if (DCI.isBeforeLegalizeOps())
13193 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013194
Stuart Hastings865f0932011-06-03 23:53:54 +000013195 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13196 if (R.getNode())
13197 return R;
13198
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013199 // Want to form ANDNP nodes:
13200 // 1) In the hopes of then easily combining them with OR and AND nodes
13201 // to form PBLEND/PSIGN.
13202 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013203 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013204 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013205 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013206
Nate Begemanb65c1752010-12-17 22:55:37 +000013207 SDValue N0 = N->getOperand(0);
13208 SDValue N1 = N->getOperand(1);
13209 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013210
Nate Begemanb65c1752010-12-17 22:55:37 +000013211 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013212 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013213 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13214 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013215 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013216
13217 // Check RHS for vnot
13218 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013219 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13220 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013221 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013222
Nate Begemanb65c1752010-12-17 22:55:37 +000013223 return SDValue();
13224}
13225
Evan Cheng760d1942010-01-04 21:22:48 +000013226static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013227 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013228 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013229 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013230 return SDValue();
13231
Stuart Hastings865f0932011-06-03 23:53:54 +000013232 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13233 if (R.getNode())
13234 return R;
13235
Evan Cheng760d1942010-01-04 21:22:48 +000013236 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013237 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013238 return SDValue();
13239
Evan Cheng760d1942010-01-04 21:22:48 +000013240 SDValue N0 = N->getOperand(0);
13241 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013242
Nate Begemanb65c1752010-12-17 22:55:37 +000013243 // look for psign/blend
13244 if (Subtarget->hasSSSE3()) {
13245 if (VT == MVT::v2i64) {
13246 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013247 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013248 std::swap(N0, N1);
13249 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013250 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013251 SDValue Mask = N1.getOperand(0);
13252 SDValue X = N1.getOperand(1);
13253 SDValue Y;
13254 if (N0.getOperand(0) == Mask)
13255 Y = N0.getOperand(1);
13256 if (N0.getOperand(1) == Mask)
13257 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013258
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013259 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013260 if (!Y.getNode())
13261 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013262
Nate Begemanb65c1752010-12-17 22:55:37 +000013263 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13264 if (Mask.getOpcode() != ISD::BITCAST ||
13265 X.getOpcode() != ISD::BITCAST ||
13266 Y.getOpcode() != ISD::BITCAST)
13267 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013268
Nate Begemanb65c1752010-12-17 22:55:37 +000013269 // Look through mask bitcast.
13270 Mask = Mask.getOperand(0);
13271 EVT MaskVT = Mask.getValueType();
13272
13273 // Validate that the Mask operand is a vector sra node. The sra node
13274 // will be an intrinsic.
13275 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13276 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013277
Nate Begemanb65c1752010-12-17 22:55:37 +000013278 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13279 // there is no psrai.b
13280 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13281 case Intrinsic::x86_sse2_psrai_w:
13282 case Intrinsic::x86_sse2_psrai_d:
13283 break;
13284 default: return SDValue();
13285 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013286
Nate Begemanb65c1752010-12-17 22:55:37 +000013287 // Check that the SRA is all signbits.
13288 SDValue SraC = Mask.getOperand(2);
13289 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13290 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13291 if ((SraAmt + 1) != EltBits)
13292 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013293
Nate Begemanb65c1752010-12-17 22:55:37 +000013294 DebugLoc DL = N->getDebugLoc();
13295
13296 // Now we know we at least have a plendvb with the mask val. See if
13297 // we can form a psignb/w/d.
13298 // psign = x.type == y.type == mask.type && y = sub(0, x);
13299 X = X.getOperand(0);
13300 Y = Y.getOperand(0);
13301 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13302 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13303 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13304 unsigned Opc = 0;
13305 switch (EltBits) {
13306 case 8: Opc = X86ISD::PSIGNB; break;
13307 case 16: Opc = X86ISD::PSIGNW; break;
13308 case 32: Opc = X86ISD::PSIGND; break;
13309 default: break;
13310 }
13311 if (Opc) {
13312 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13313 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13314 }
13315 }
13316 // PBLENDVB only available on SSE 4.1
13317 if (!Subtarget->hasSSE41())
13318 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013319
Nate Begemanb65c1752010-12-17 22:55:37 +000013320 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13321 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13322 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000013323 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013324 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13325 }
13326 }
13327 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013328
Nate Begemanb65c1752010-12-17 22:55:37 +000013329 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013330 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13331 std::swap(N0, N1);
13332 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13333 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013334 if (!N0.hasOneUse() || !N1.hasOneUse())
13335 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013336
13337 SDValue ShAmt0 = N0.getOperand(1);
13338 if (ShAmt0.getValueType() != MVT::i8)
13339 return SDValue();
13340 SDValue ShAmt1 = N1.getOperand(1);
13341 if (ShAmt1.getValueType() != MVT::i8)
13342 return SDValue();
13343 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13344 ShAmt0 = ShAmt0.getOperand(0);
13345 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13346 ShAmt1 = ShAmt1.getOperand(0);
13347
13348 DebugLoc DL = N->getDebugLoc();
13349 unsigned Opc = X86ISD::SHLD;
13350 SDValue Op0 = N0.getOperand(0);
13351 SDValue Op1 = N1.getOperand(0);
13352 if (ShAmt0.getOpcode() == ISD::SUB) {
13353 Opc = X86ISD::SHRD;
13354 std::swap(Op0, Op1);
13355 std::swap(ShAmt0, ShAmt1);
13356 }
13357
Evan Cheng8b1190a2010-04-28 01:18:01 +000013358 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013359 if (ShAmt1.getOpcode() == ISD::SUB) {
13360 SDValue Sum = ShAmt1.getOperand(0);
13361 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013362 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13363 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13364 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13365 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013366 return DAG.getNode(Opc, DL, VT,
13367 Op0, Op1,
13368 DAG.getNode(ISD::TRUNCATE, DL,
13369 MVT::i8, ShAmt0));
13370 }
13371 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13372 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13373 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013374 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013375 return DAG.getNode(Opc, DL, VT,
13376 N0.getOperand(0), N1.getOperand(0),
13377 DAG.getNode(ISD::TRUNCATE, DL,
13378 MVT::i8, ShAmt0));
13379 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013380
Evan Cheng760d1942010-01-04 21:22:48 +000013381 return SDValue();
13382}
13383
Chris Lattner149a4e52008-02-22 02:09:43 +000013384/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013385static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013386 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013387 StoreSDNode *St = cast<StoreSDNode>(N);
13388 EVT VT = St->getValue().getValueType();
13389 EVT StVT = St->getMemoryVT();
13390 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013391 SDValue StoredVal = St->getOperand(1);
13392 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13393
13394 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013395 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13396 // 128-bit ones. If in the future the cost becomes only one memory access the
13397 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013398 if (VT.getSizeInBits() == 256 &&
13399 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13400 StoredVal.getNumOperands() == 2) {
13401
13402 SDValue Value0 = StoredVal.getOperand(0);
13403 SDValue Value1 = StoredVal.getOperand(1);
13404
13405 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13406 SDValue Ptr0 = St->getBasePtr();
13407 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13408
13409 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13410 St->getPointerInfo(), St->isVolatile(),
13411 St->isNonTemporal(), St->getAlignment());
13412 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13413 St->getPointerInfo(), St->isVolatile(),
13414 St->isNonTemporal(), St->getAlignment());
13415 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13416 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013417
13418 // Optimize trunc store (of multiple scalars) to shuffle and store.
13419 // First, pack all of the elements in one place. Next, store to memory
13420 // in fewer chunks.
13421 if (St->isTruncatingStore() && VT.isVector()) {
13422 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13423 unsigned NumElems = VT.getVectorNumElements();
13424 assert(StVT != VT && "Cannot truncate to the same type");
13425 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13426 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13427
13428 // From, To sizes and ElemCount must be pow of two
13429 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
13430 // We are going to use the original vector elt for storing.
13431 // accumulated smaller vector elements must be a multiple of bigger size.
13432 if (0 != (NumElems * ToSz) % FromSz) return SDValue();
13433 unsigned SizeRatio = FromSz / ToSz;
13434
13435 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13436
13437 // Create a type on which we perform the shuffle
13438 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13439 StVT.getScalarType(), NumElems*SizeRatio);
13440
13441 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13442
13443 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13444 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13445 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13446
13447 // Can't shuffle using an illegal type
13448 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13449
13450 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13451 DAG.getUNDEF(WideVec.getValueType()),
13452 ShuffleVec.data());
13453 // At this point all of the data is stored at the bottom of the
13454 // register. We now need to save it to mem.
13455
13456 // Find the largest store unit
13457 MVT StoreType = MVT::i8;
13458 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13459 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13460 MVT Tp = (MVT::SimpleValueType)tp;
13461 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13462 StoreType = Tp;
13463 }
13464
13465 // Bitcast the original vector into a vector of store-size units
13466 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13467 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13468 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13469 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13470 SmallVector<SDValue, 8> Chains;
13471 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13472 TLI.getPointerTy());
13473 SDValue Ptr = St->getBasePtr();
13474
13475 // Perform one or more big stores into memory.
13476 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13477 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13478 StoreType, ShuffWide,
13479 DAG.getIntPtrConstant(i));
13480 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13481 St->getPointerInfo(), St->isVolatile(),
13482 St->isNonTemporal(), St->getAlignment());
13483 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13484 Chains.push_back(Ch);
13485 }
13486
13487 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13488 Chains.size());
13489 }
13490
13491
Chris Lattner149a4e52008-02-22 02:09:43 +000013492 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13493 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013494 // A preferable solution to the general problem is to figure out the right
13495 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013496
13497 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013498 if (VT.getSizeInBits() != 64)
13499 return SDValue();
13500
Devang Patel578efa92009-06-05 21:57:13 +000013501 const Function *F = DAG.getMachineFunction().getFunction();
13502 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013503 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000013504 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000013505 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013506 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013507 isa<LoadSDNode>(St->getValue()) &&
13508 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13509 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013510 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013511 LoadSDNode *Ld = 0;
13512 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013513 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013514 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013515 // Must be a store of a load. We currently handle two cases: the load
13516 // is a direct child, and it's under an intervening TokenFactor. It is
13517 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013518 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013519 Ld = cast<LoadSDNode>(St->getChain());
13520 else if (St->getValue().hasOneUse() &&
13521 ChainVal->getOpcode() == ISD::TokenFactor) {
13522 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013523 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013524 TokenFactorIndex = i;
13525 Ld = cast<LoadSDNode>(St->getValue());
13526 } else
13527 Ops.push_back(ChainVal->getOperand(i));
13528 }
13529 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013530
Evan Cheng536e6672009-03-12 05:59:15 +000013531 if (!Ld || !ISD::isNormalLoad(Ld))
13532 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013533
Evan Cheng536e6672009-03-12 05:59:15 +000013534 // If this is not the MMX case, i.e. we are just turning i64 load/store
13535 // into f64 load/store, avoid the transformation if there are multiple
13536 // uses of the loaded value.
13537 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13538 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013539
Evan Cheng536e6672009-03-12 05:59:15 +000013540 DebugLoc LdDL = Ld->getDebugLoc();
13541 DebugLoc StDL = N->getDebugLoc();
13542 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13543 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13544 // pair instead.
13545 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013546 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013547 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13548 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013549 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013550 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013551 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013552 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013553 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013554 Ops.size());
13555 }
Evan Cheng536e6672009-03-12 05:59:15 +000013556 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013557 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013558 St->isVolatile(), St->isNonTemporal(),
13559 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013560 }
Evan Cheng536e6672009-03-12 05:59:15 +000013561
13562 // Otherwise, lower to two pairs of 32-bit loads / stores.
13563 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013564 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13565 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013566
Owen Anderson825b72b2009-08-11 20:47:22 +000013567 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013568 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013569 Ld->isVolatile(), Ld->isNonTemporal(),
13570 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013571 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013572 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013573 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013574 MinAlign(Ld->getAlignment(), 4));
13575
13576 SDValue NewChain = LoLd.getValue(1);
13577 if (TokenFactorIndex != -1) {
13578 Ops.push_back(LoLd);
13579 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013580 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013581 Ops.size());
13582 }
13583
13584 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013585 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13586 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013587
13588 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013589 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013590 St->isVolatile(), St->isNonTemporal(),
13591 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013592 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013593 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013594 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013595 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013596 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013597 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013598 }
Dan Gohman475871a2008-07-27 21:46:04 +000013599 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013600}
13601
Chris Lattner6cf73262008-01-25 06:14:17 +000013602/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13603/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013604static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013605 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13606 // F[X]OR(0.0, x) -> x
13607 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013608 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13609 if (C->getValueAPF().isPosZero())
13610 return N->getOperand(1);
13611 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13612 if (C->getValueAPF().isPosZero())
13613 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013614 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013615}
13616
13617/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013618static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013619 // FAND(0.0, x) -> 0.0
13620 // FAND(x, 0.0) -> 0.0
13621 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13622 if (C->getValueAPF().isPosZero())
13623 return N->getOperand(0);
13624 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13625 if (C->getValueAPF().isPosZero())
13626 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013627 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013628}
13629
Dan Gohmane5af2d32009-01-29 01:59:02 +000013630static SDValue PerformBTCombine(SDNode *N,
13631 SelectionDAG &DAG,
13632 TargetLowering::DAGCombinerInfo &DCI) {
13633 // BT ignores high bits in the bit index operand.
13634 SDValue Op1 = N->getOperand(1);
13635 if (Op1.hasOneUse()) {
13636 unsigned BitWidth = Op1.getValueSizeInBits();
13637 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13638 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013639 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13640 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013641 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013642 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13643 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13644 DCI.CommitTargetLoweringOpt(TLO);
13645 }
13646 return SDValue();
13647}
Chris Lattner83e6c992006-10-04 06:57:07 +000013648
Eli Friedman7a5e5552009-06-07 06:52:44 +000013649static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13650 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013651 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013652 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013653 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013654 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013655 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013656 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013657 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013658 }
13659 return SDValue();
13660}
13661
Evan Cheng2e489c42009-12-16 00:53:11 +000013662static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13663 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13664 // (and (i32 x86isd::setcc_carry), 1)
13665 // This eliminates the zext. This transformation is necessary because
13666 // ISD::SETCC is always legalized to i8.
13667 DebugLoc dl = N->getDebugLoc();
13668 SDValue N0 = N->getOperand(0);
13669 EVT VT = N->getValueType(0);
13670 if (N0.getOpcode() == ISD::AND &&
13671 N0.hasOneUse() &&
13672 N0.getOperand(0).hasOneUse()) {
13673 SDValue N00 = N0.getOperand(0);
13674 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13675 return SDValue();
13676 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13677 if (!C || C->getZExtValue() != 1)
13678 return SDValue();
13679 return DAG.getNode(ISD::AND, dl, VT,
13680 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13681 N00.getOperand(0), N00.getOperand(1)),
13682 DAG.getConstant(1, VT));
13683 }
13684
13685 return SDValue();
13686}
13687
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013688// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13689static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13690 unsigned X86CC = N->getConstantOperandVal(0);
13691 SDValue EFLAG = N->getOperand(1);
13692 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013693
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013694 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
13695 // a zext and produces an all-ones bit which is more useful than 0/1 in some
13696 // cases.
13697 if (X86CC == X86::COND_B)
13698 return DAG.getNode(ISD::AND, DL, MVT::i8,
13699 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
13700 DAG.getConstant(X86CC, MVT::i8), EFLAG),
13701 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013702
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013703 return SDValue();
13704}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013705
Benjamin Kramer1396c402011-06-18 11:09:41 +000013706static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
13707 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013708 SDValue Op0 = N->getOperand(0);
13709 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
13710 // a 32-bit target where SSE doesn't support i64->FP operations.
13711 if (Op0.getOpcode() == ISD::LOAD) {
13712 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
13713 EVT VT = Ld->getValueType(0);
13714 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
13715 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
13716 !XTLI->getSubtarget()->is64Bit() &&
13717 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000013718 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
13719 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013720 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
13721 return FILDChain;
13722 }
13723 }
13724 return SDValue();
13725}
13726
Chris Lattner23a01992010-12-20 01:37:09 +000013727// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
13728static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
13729 X86TargetLowering::DAGCombinerInfo &DCI) {
13730 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
13731 // the result is either zero or one (depending on the input carry bit).
13732 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
13733 if (X86::isZeroNode(N->getOperand(0)) &&
13734 X86::isZeroNode(N->getOperand(1)) &&
13735 // We don't have a good way to replace an EFLAGS use, so only do this when
13736 // dead right now.
13737 SDValue(N, 1).use_empty()) {
13738 DebugLoc DL = N->getDebugLoc();
13739 EVT VT = N->getValueType(0);
13740 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
13741 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
13742 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
13743 DAG.getConstant(X86::COND_B,MVT::i8),
13744 N->getOperand(2)),
13745 DAG.getConstant(1, VT));
13746 return DCI.CombineTo(N, Res1, CarryOut);
13747 }
13748
13749 return SDValue();
13750}
13751
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013752// fold (add Y, (sete X, 0)) -> adc 0, Y
13753// (add Y, (setne X, 0)) -> sbb -1, Y
13754// (sub (sete X, 0), Y) -> sbb 0, Y
13755// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013756static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013757 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013758
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013759 // Look through ZExts.
13760 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
13761 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
13762 return SDValue();
13763
13764 SDValue SetCC = Ext.getOperand(0);
13765 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
13766 return SDValue();
13767
13768 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
13769 if (CC != X86::COND_E && CC != X86::COND_NE)
13770 return SDValue();
13771
13772 SDValue Cmp = SetCC.getOperand(1);
13773 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000013774 !X86::isZeroNode(Cmp.getOperand(1)) ||
13775 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013776 return SDValue();
13777
13778 SDValue CmpOp0 = Cmp.getOperand(0);
13779 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
13780 DAG.getConstant(1, CmpOp0.getValueType()));
13781
13782 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
13783 if (CC == X86::COND_NE)
13784 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
13785 DL, OtherVal.getValueType(), OtherVal,
13786 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
13787 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
13788 DL, OtherVal.getValueType(), OtherVal,
13789 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
13790}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013791
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013792static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
13793 SDValue Op0 = N->getOperand(0);
13794 SDValue Op1 = N->getOperand(1);
13795
13796 // X86 can't encode an immediate LHS of a sub. See if we can push the
13797 // negation into a preceding instruction.
13798 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013799 // If the RHS of the sub is a XOR with one use and a constant, invert the
13800 // immediate. Then add one to the LHS of the sub so we can turn
13801 // X-Y -> X+~Y+1, saving one register.
13802 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
13803 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000013804 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013805 EVT VT = Op0.getValueType();
13806 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
13807 Op1.getOperand(0),
13808 DAG.getConstant(~XorC, VT));
13809 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000013810 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013811 }
13812 }
13813
13814 return OptimizeConditionalInDecrement(N, DAG);
13815}
13816
Dan Gohman475871a2008-07-27 21:46:04 +000013817SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000013818 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013819 SelectionDAG &DAG = DCI.DAG;
13820 switch (N->getOpcode()) {
13821 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013822 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013823 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000013824 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013825 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013826 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
13827 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000013828 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000013829 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000013830 case ISD::SHL:
13831 case ISD::SRA:
13832 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000013833 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000013834 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000013835 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013836 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000013837 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000013838 case X86ISD::FOR: return PerformFORCombine(N, DAG);
13839 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000013840 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013841 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000013842 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013843 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013844 case X86ISD::SHUFPS: // Handle all target specific shuffles
13845 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000013846 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013847 case X86ISD::PUNPCKHBW:
13848 case X86ISD::PUNPCKHWD:
13849 case X86ISD::PUNPCKHDQ:
13850 case X86ISD::PUNPCKHQDQ:
13851 case X86ISD::UNPCKHPS:
13852 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000013853 case X86ISD::VUNPCKHPSY:
13854 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013855 case X86ISD::PUNPCKLBW:
13856 case X86ISD::PUNPCKLWD:
13857 case X86ISD::PUNPCKLDQ:
13858 case X86ISD::PUNPCKLQDQ:
13859 case X86ISD::UNPCKLPS:
13860 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000013861 case X86ISD::VUNPCKLPSY:
13862 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013863 case X86ISD::MOVHLPS:
13864 case X86ISD::MOVLHPS:
13865 case X86ISD::PSHUFD:
13866 case X86ISD::PSHUFHW:
13867 case X86ISD::PSHUFLW:
13868 case X86ISD::MOVSS:
13869 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000013870 case X86ISD::VPERMILPS:
13871 case X86ISD::VPERMILPSY:
13872 case X86ISD::VPERMILPD:
13873 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000013874 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013875 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013876 }
13877
Dan Gohman475871a2008-07-27 21:46:04 +000013878 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013879}
13880
Evan Chenge5b51ac2010-04-17 06:13:15 +000013881/// isTypeDesirableForOp - Return true if the target has native support for
13882/// the specified value type and it is 'desirable' to use the type for the
13883/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
13884/// instruction encodings are longer and some i16 instructions are slow.
13885bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
13886 if (!isTypeLegal(VT))
13887 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013888 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000013889 return true;
13890
13891 switch (Opc) {
13892 default:
13893 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000013894 case ISD::LOAD:
13895 case ISD::SIGN_EXTEND:
13896 case ISD::ZERO_EXTEND:
13897 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013898 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013899 case ISD::SRL:
13900 case ISD::SUB:
13901 case ISD::ADD:
13902 case ISD::MUL:
13903 case ISD::AND:
13904 case ISD::OR:
13905 case ISD::XOR:
13906 return false;
13907 }
13908}
13909
13910/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000013911/// beneficial for dag combiner to promote the specified node. If true, it
13912/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000013913bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013914 EVT VT = Op.getValueType();
13915 if (VT != MVT::i16)
13916 return false;
13917
Evan Cheng4c26e932010-04-19 19:29:22 +000013918 bool Promote = false;
13919 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013920 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000013921 default: break;
13922 case ISD::LOAD: {
13923 LoadSDNode *LD = cast<LoadSDNode>(Op);
13924 // If the non-extending load has a single use and it's not live out, then it
13925 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013926 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
13927 Op.hasOneUse()*/) {
13928 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
13929 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
13930 // The only case where we'd want to promote LOAD (rather then it being
13931 // promoted as an operand is when it's only use is liveout.
13932 if (UI->getOpcode() != ISD::CopyToReg)
13933 return false;
13934 }
13935 }
Evan Cheng4c26e932010-04-19 19:29:22 +000013936 Promote = true;
13937 break;
13938 }
13939 case ISD::SIGN_EXTEND:
13940 case ISD::ZERO_EXTEND:
13941 case ISD::ANY_EXTEND:
13942 Promote = true;
13943 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013944 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013945 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000013946 SDValue N0 = Op.getOperand(0);
13947 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000013948 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000013949 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000013950 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013951 break;
13952 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000013953 case ISD::ADD:
13954 case ISD::MUL:
13955 case ISD::AND:
13956 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000013957 case ISD::XOR:
13958 Commute = true;
13959 // fallthrough
13960 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013961 SDValue N0 = Op.getOperand(0);
13962 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000013963 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013964 return false;
13965 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000013966 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013967 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000013968 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013969 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000013970 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013971 }
13972 }
13973
13974 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000013975 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013976}
13977
Evan Cheng60c07e12006-07-05 22:17:51 +000013978//===----------------------------------------------------------------------===//
13979// X86 Inline Assembly Support
13980//===----------------------------------------------------------------------===//
13981
Chris Lattnerb8105652009-07-20 17:51:36 +000013982bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
13983 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000013984
13985 std::string AsmStr = IA->getAsmString();
13986
13987 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000013988 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000013989 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000013990
13991 switch (AsmPieces.size()) {
13992 default: return false;
13993 case 1:
13994 AsmStr = AsmPieces[0];
13995 AsmPieces.clear();
13996 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
13997
Chris Lattner7a2bdde2011-04-15 05:18:47 +000013998 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000013999 // we will turn this bswap into something that will be lowered to logical ops
14000 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14001 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014002 // bswap $0
14003 if (AsmPieces.size() == 2 &&
14004 (AsmPieces[0] == "bswap" ||
14005 AsmPieces[0] == "bswapq" ||
14006 AsmPieces[0] == "bswapl") &&
14007 (AsmPieces[1] == "$0" ||
14008 AsmPieces[1] == "${0:q}")) {
14009 // No need to check constraints, nothing other than the equivalent of
14010 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014011 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014012 if (!Ty || Ty->getBitWidth() % 16 != 0)
14013 return false;
14014 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014015 }
14016 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014017 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014018 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014019 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014020 AsmPieces[1] == "$$8," &&
14021 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014022 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14023 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014024 const std::string &ConstraintsStr = IA->getConstraintString();
14025 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014026 std::sort(AsmPieces.begin(), AsmPieces.end());
14027 if (AsmPieces.size() == 4 &&
14028 AsmPieces[0] == "~{cc}" &&
14029 AsmPieces[1] == "~{dirflag}" &&
14030 AsmPieces[2] == "~{flags}" &&
14031 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014032 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014033 if (!Ty || Ty->getBitWidth() % 16 != 0)
14034 return false;
14035 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014036 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014037 }
14038 break;
14039 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014040 if (CI->getType()->isIntegerTy(32) &&
14041 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14042 SmallVector<StringRef, 4> Words;
14043 SplitString(AsmPieces[0], Words, " \t,");
14044 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14045 Words[2] == "${0:w}") {
14046 Words.clear();
14047 SplitString(AsmPieces[1], Words, " \t,");
14048 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14049 Words[2] == "$0") {
14050 Words.clear();
14051 SplitString(AsmPieces[2], Words, " \t,");
14052 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14053 Words[2] == "${0:w}") {
14054 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014055 const std::string &ConstraintsStr = IA->getConstraintString();
14056 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014057 std::sort(AsmPieces.begin(), AsmPieces.end());
14058 if (AsmPieces.size() == 4 &&
14059 AsmPieces[0] == "~{cc}" &&
14060 AsmPieces[1] == "~{dirflag}" &&
14061 AsmPieces[2] == "~{flags}" &&
14062 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014063 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014064 if (!Ty || Ty->getBitWidth() % 16 != 0)
14065 return false;
14066 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014067 }
14068 }
14069 }
14070 }
14071 }
Evan Cheng55d42002011-01-08 01:24:27 +000014072
14073 if (CI->getType()->isIntegerTy(64)) {
14074 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14075 if (Constraints.size() >= 2 &&
14076 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14077 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14078 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14079 SmallVector<StringRef, 4> Words;
14080 SplitString(AsmPieces[0], Words, " \t");
14081 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014082 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014083 SplitString(AsmPieces[1], Words, " \t");
14084 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14085 Words.clear();
14086 SplitString(AsmPieces[2], Words, " \t,");
14087 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14088 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014089 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014090 if (!Ty || Ty->getBitWidth() % 16 != 0)
14091 return false;
14092 return IntrinsicLowering::LowerToByteSwap(CI);
14093 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014094 }
14095 }
14096 }
14097 }
14098 break;
14099 }
14100 return false;
14101}
14102
14103
14104
Chris Lattnerf4dff842006-07-11 02:54:03 +000014105/// getConstraintType - Given a constraint letter, return the type of
14106/// constraint it is for this target.
14107X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014108X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14109 if (Constraint.size() == 1) {
14110 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014111 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014112 case 'q':
14113 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014114 case 'f':
14115 case 't':
14116 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014117 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014118 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014119 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014120 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014121 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014122 case 'a':
14123 case 'b':
14124 case 'c':
14125 case 'd':
14126 case 'S':
14127 case 'D':
14128 case 'A':
14129 return C_Register;
14130 case 'I':
14131 case 'J':
14132 case 'K':
14133 case 'L':
14134 case 'M':
14135 case 'N':
14136 case 'G':
14137 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014138 case 'e':
14139 case 'Z':
14140 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014141 default:
14142 break;
14143 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014144 }
Chris Lattner4234f572007-03-25 02:14:49 +000014145 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014146}
14147
John Thompson44ab89e2010-10-29 17:29:13 +000014148/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014149/// This object must already have been set up with the operand type
14150/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014151TargetLowering::ConstraintWeight
14152 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014153 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014154 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014155 Value *CallOperandVal = info.CallOperandVal;
14156 // If we don't have a value, we can't do a match,
14157 // but allow it at the lowest weight.
14158 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014159 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014160 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014161 // Look at the constraint type.
14162 switch (*constraint) {
14163 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014164 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14165 case 'R':
14166 case 'q':
14167 case 'Q':
14168 case 'a':
14169 case 'b':
14170 case 'c':
14171 case 'd':
14172 case 'S':
14173 case 'D':
14174 case 'A':
14175 if (CallOperandVal->getType()->isIntegerTy())
14176 weight = CW_SpecificReg;
14177 break;
14178 case 'f':
14179 case 't':
14180 case 'u':
14181 if (type->isFloatingPointTy())
14182 weight = CW_SpecificReg;
14183 break;
14184 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014185 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014186 weight = CW_SpecificReg;
14187 break;
14188 case 'x':
14189 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014190 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014191 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014192 break;
14193 case 'I':
14194 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14195 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014196 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014197 }
14198 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014199 case 'J':
14200 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14201 if (C->getZExtValue() <= 63)
14202 weight = CW_Constant;
14203 }
14204 break;
14205 case 'K':
14206 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14207 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14208 weight = CW_Constant;
14209 }
14210 break;
14211 case 'L':
14212 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14213 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14214 weight = CW_Constant;
14215 }
14216 break;
14217 case 'M':
14218 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14219 if (C->getZExtValue() <= 3)
14220 weight = CW_Constant;
14221 }
14222 break;
14223 case 'N':
14224 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14225 if (C->getZExtValue() <= 0xff)
14226 weight = CW_Constant;
14227 }
14228 break;
14229 case 'G':
14230 case 'C':
14231 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14232 weight = CW_Constant;
14233 }
14234 break;
14235 case 'e':
14236 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14237 if ((C->getSExtValue() >= -0x80000000LL) &&
14238 (C->getSExtValue() <= 0x7fffffffLL))
14239 weight = CW_Constant;
14240 }
14241 break;
14242 case 'Z':
14243 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14244 if (C->getZExtValue() <= 0xffffffff)
14245 weight = CW_Constant;
14246 }
14247 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014248 }
14249 return weight;
14250}
14251
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014252/// LowerXConstraint - try to replace an X constraint, which matches anything,
14253/// with another that has more specific requirements based on the type of the
14254/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014255const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014256LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014257 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14258 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014259 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014260 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014261 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014262 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014263 return "x";
14264 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014265
Chris Lattner5e764232008-04-26 23:02:14 +000014266 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014267}
14268
Chris Lattner48884cd2007-08-25 00:47:38 +000014269/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14270/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014271void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014272 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014273 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014274 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014275 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014276
Eric Christopher100c8332011-06-02 23:16:42 +000014277 // Only support length 1 constraints for now.
14278 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014279
Eric Christopher100c8332011-06-02 23:16:42 +000014280 char ConstraintLetter = Constraint[0];
14281 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014282 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014283 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014284 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014285 if (C->getZExtValue() <= 31) {
14286 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014287 break;
14288 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014289 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014290 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014291 case 'J':
14292 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014293 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014294 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14295 break;
14296 }
14297 }
14298 return;
14299 case 'K':
14300 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014301 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014302 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14303 break;
14304 }
14305 }
14306 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014307 case 'N':
14308 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014309 if (C->getZExtValue() <= 255) {
14310 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014311 break;
14312 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014313 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014314 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014315 case 'e': {
14316 // 32-bit signed value
14317 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014318 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14319 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014320 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014321 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014322 break;
14323 }
14324 // FIXME gcc accepts some relocatable values here too, but only in certain
14325 // memory models; it's complicated.
14326 }
14327 return;
14328 }
14329 case 'Z': {
14330 // 32-bit unsigned value
14331 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014332 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14333 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014334 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14335 break;
14336 }
14337 }
14338 // FIXME gcc accepts some relocatable values here too, but only in certain
14339 // memory models; it's complicated.
14340 return;
14341 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014342 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014343 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014344 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014345 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014346 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014347 break;
14348 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014349
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014350 // In any sort of PIC mode addresses need to be computed at runtime by
14351 // adding in a register or some sort of table lookup. These can't
14352 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014353 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014354 return;
14355
Chris Lattnerdc43a882007-05-03 16:52:29 +000014356 // If we are in non-pic codegen mode, we allow the address of a global (with
14357 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014358 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014359 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014360
Chris Lattner49921962009-05-08 18:23:14 +000014361 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14362 while (1) {
14363 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14364 Offset += GA->getOffset();
14365 break;
14366 } else if (Op.getOpcode() == ISD::ADD) {
14367 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14368 Offset += C->getZExtValue();
14369 Op = Op.getOperand(0);
14370 continue;
14371 }
14372 } else if (Op.getOpcode() == ISD::SUB) {
14373 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14374 Offset += -C->getZExtValue();
14375 Op = Op.getOperand(0);
14376 continue;
14377 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014378 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014379
Chris Lattner49921962009-05-08 18:23:14 +000014380 // Otherwise, this isn't something we can handle, reject it.
14381 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014382 }
Eric Christopherfd179292009-08-27 18:07:15 +000014383
Dan Gohman46510a72010-04-15 01:51:59 +000014384 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014385 // If we require an extra load to get this address, as in PIC mode, we
14386 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014387 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14388 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014389 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014390
Devang Patel0d881da2010-07-06 22:08:15 +000014391 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14392 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014393 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014394 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014395 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014396
Gabor Greifba36cb52008-08-28 21:40:38 +000014397 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014398 Ops.push_back(Result);
14399 return;
14400 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014401 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014402}
14403
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014404std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014405X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014406 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014407 // First, see if this is a constraint that directly corresponds to an LLVM
14408 // register class.
14409 if (Constraint.size() == 1) {
14410 // GCC Constraint Letters
14411 switch (Constraint[0]) {
14412 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014413 // TODO: Slight differences here in allocation order and leaving
14414 // RIP in the class. Do they matter any more here than they do
14415 // in the normal allocation?
14416 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14417 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014418 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014419 return std::make_pair(0U, X86::GR32RegisterClass);
14420 else if (VT == MVT::i16)
14421 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014422 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014423 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014424 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014425 return std::make_pair(0U, X86::GR64RegisterClass);
14426 break;
14427 }
14428 // 32-bit fallthrough
14429 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014430 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014431 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14432 else if (VT == MVT::i16)
14433 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014434 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014435 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14436 else if (VT == MVT::i64)
14437 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14438 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014439 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014440 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014441 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014442 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014443 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014444 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014445 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014446 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014447 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014448 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014449 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014450 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14451 if (VT == MVT::i16)
14452 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14453 if (VT == MVT::i32 || !Subtarget->is64Bit())
14454 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14455 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014456 case 'f': // FP Stack registers.
14457 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14458 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014459 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014460 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014461 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014462 return std::make_pair(0U, X86::RFP64RegisterClass);
14463 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014464 case 'y': // MMX_REGS if MMX allowed.
14465 if (!Subtarget->hasMMX()) break;
14466 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014467 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014468 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014469 // FALL THROUGH.
14470 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014471 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014472
Owen Anderson825b72b2009-08-11 20:47:22 +000014473 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014474 default: break;
14475 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014476 case MVT::f32:
14477 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014478 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014479 case MVT::f64:
14480 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014481 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014482 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014483 case MVT::v16i8:
14484 case MVT::v8i16:
14485 case MVT::v4i32:
14486 case MVT::v2i64:
14487 case MVT::v4f32:
14488 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014489 return std::make_pair(0U, X86::VR128RegisterClass);
14490 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014491 break;
14492 }
14493 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014494
Chris Lattnerf76d1802006-07-31 23:26:50 +000014495 // Use the default implementation in TargetLowering to convert the register
14496 // constraint into a member of a register class.
14497 std::pair<unsigned, const TargetRegisterClass*> Res;
14498 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014499
14500 // Not found as a standard register?
14501 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014502 // Map st(0) -> st(7) -> ST0
14503 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14504 tolower(Constraint[1]) == 's' &&
14505 tolower(Constraint[2]) == 't' &&
14506 Constraint[3] == '(' &&
14507 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14508 Constraint[5] == ')' &&
14509 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014510
Chris Lattner56d77c72009-09-13 22:41:48 +000014511 Res.first = X86::ST0+Constraint[4]-'0';
14512 Res.second = X86::RFP80RegisterClass;
14513 return Res;
14514 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014515
Chris Lattner56d77c72009-09-13 22:41:48 +000014516 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014517 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014518 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014519 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014520 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014521 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014522
14523 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014524 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014525 Res.first = X86::EFLAGS;
14526 Res.second = X86::CCRRegisterClass;
14527 return Res;
14528 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014529
Dale Johannesen330169f2008-11-13 21:52:36 +000014530 // 'A' means EAX + EDX.
14531 if (Constraint == "A") {
14532 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014533 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014534 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014535 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014536 return Res;
14537 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014538
Chris Lattnerf76d1802006-07-31 23:26:50 +000014539 // Otherwise, check to see if this is a register class of the wrong value
14540 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14541 // turn into {ax},{dx}.
14542 if (Res.second->hasType(VT))
14543 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014544
Chris Lattnerf76d1802006-07-31 23:26:50 +000014545 // All of the single-register GCC register classes map their values onto
14546 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14547 // really want an 8-bit or 32-bit register, map to the appropriate register
14548 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014549 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014550 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014551 unsigned DestReg = 0;
14552 switch (Res.first) {
14553 default: break;
14554 case X86::AX: DestReg = X86::AL; break;
14555 case X86::DX: DestReg = X86::DL; break;
14556 case X86::CX: DestReg = X86::CL; break;
14557 case X86::BX: DestReg = X86::BL; break;
14558 }
14559 if (DestReg) {
14560 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014561 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014562 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014563 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014564 unsigned DestReg = 0;
14565 switch (Res.first) {
14566 default: break;
14567 case X86::AX: DestReg = X86::EAX; break;
14568 case X86::DX: DestReg = X86::EDX; break;
14569 case X86::CX: DestReg = X86::ECX; break;
14570 case X86::BX: DestReg = X86::EBX; break;
14571 case X86::SI: DestReg = X86::ESI; break;
14572 case X86::DI: DestReg = X86::EDI; break;
14573 case X86::BP: DestReg = X86::EBP; break;
14574 case X86::SP: DestReg = X86::ESP; break;
14575 }
14576 if (DestReg) {
14577 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014578 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014579 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014580 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014581 unsigned DestReg = 0;
14582 switch (Res.first) {
14583 default: break;
14584 case X86::AX: DestReg = X86::RAX; break;
14585 case X86::DX: DestReg = X86::RDX; break;
14586 case X86::CX: DestReg = X86::RCX; break;
14587 case X86::BX: DestReg = X86::RBX; break;
14588 case X86::SI: DestReg = X86::RSI; break;
14589 case X86::DI: DestReg = X86::RDI; break;
14590 case X86::BP: DestReg = X86::RBP; break;
14591 case X86::SP: DestReg = X86::RSP; break;
14592 }
14593 if (DestReg) {
14594 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014595 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014596 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014597 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014598 } else if (Res.second == X86::FR32RegisterClass ||
14599 Res.second == X86::FR64RegisterClass ||
14600 Res.second == X86::VR128RegisterClass) {
14601 // Handle references to XMM physical registers that got mapped into the
14602 // wrong class. This can happen with constraints like {xmm0} where the
14603 // target independent register mapper will just pick the first match it can
14604 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014605 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014606 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014607 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014608 Res.second = X86::FR64RegisterClass;
14609 else if (X86::VR128RegisterClass->hasType(VT))
14610 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014611 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014612
Chris Lattnerf76d1802006-07-31 23:26:50 +000014613 return Res;
14614}