blob: 55d568092c0688cb32074f66fbdc9db43f94f797 [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
David Greenef125a292011-02-08 19:04:41 +000074static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
75
76
David Greenea5f26012011-02-07 19:36:54 +000077/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
78/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000079/// simple subregister reference. Idx is an index in the 128 bits we
80/// want. It need not be aligned to a 128-bit bounday. That makes
81/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000082static SDValue Extract128BitVector(SDValue Vec,
83 SDValue Idx,
84 SelectionDAG &DAG,
85 DebugLoc dl) {
86 EVT VT = Vec.getValueType();
87 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000088 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000089 int Factor = VT.getSizeInBits()/128;
90 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
91 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000092
93 // Extract from UNDEF is UNDEF.
94 if (Vec.getOpcode() == ISD::UNDEF)
95 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
96
97 if (isa<ConstantSDNode>(Idx)) {
98 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
99
100 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
101 // we can match to VEXTRACTF128.
102 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
103
104 // This is the index of the first element of the 128-bit chunk
105 // we want.
106 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
107 * ElemsPerChunk);
108
109 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000110 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
111 VecIdx);
112
113 return Result;
114 }
115
116 return SDValue();
117}
118
119/// Generate a DAG to put 128-bits into a vector > 128 bits. This
120/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000121/// simple superregister reference. Idx is an index in the 128 bits
122/// we want. It need not be aligned to a 128-bit bounday. That makes
123/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000124static SDValue Insert128BitVector(SDValue Result,
125 SDValue Vec,
126 SDValue Idx,
127 SelectionDAG &DAG,
128 DebugLoc dl) {
129 if (isa<ConstantSDNode>(Idx)) {
130 EVT VT = Vec.getValueType();
131 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
132
133 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000134 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000135 EVT ResultVT = Result.getValueType();
136
137 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000138 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000139
140 // This is the index of the first element of the 128-bit chunk
141 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000142 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000143 * ElemsPerChunk);
144
145 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000146 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
147 VecIdx);
148 return Result;
149 }
150
151 return SDValue();
152}
153
David Greenef125a292011-02-08 19:04:41 +0000154/// Given two vectors, concat them.
155static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
156 DebugLoc dl = Lower.getDebugLoc();
157
158 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
159
160 EVT VT = EVT::getVectorVT(*DAG.getContext(),
161 Lower.getValueType().getVectorElementType(),
162 Lower.getValueType().getVectorNumElements() * 2);
163
164 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
165 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
166
167 // Insert the upper subvector.
168 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
169 DAG.getConstant(
170 // This is half the length of the result
171 // vector. Start inserting the upper 128
172 // bits here.
173 Lower.getValueType().getVectorNumElements(),
174 MVT::i32),
175 DAG, dl);
176
177 // Insert the lower subvector.
178 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
179 return Vec;
180}
181
Chris Lattnerf0144122009-07-28 03:13:23 +0000182static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000183 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
184 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000185
Evan Cheng2bffee22011-02-01 01:14:13 +0000186 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000187 if (is64Bit)
188 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000189 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000190 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000191
Evan Cheng203576a2011-07-20 19:50:42 +0000192 if (Subtarget->isTargetELF())
193 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000194 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000195 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000196 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000197}
198
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000199X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000200 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000201 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000202 X86ScalarSSEf64 = Subtarget->hasXMMInt();
203 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000204 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000205
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000206 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000207 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000208
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000209 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000210 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000211
212 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000213 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000214
Eric Christopherde5e1012011-03-11 01:05:58 +0000215 // For 64-bit since we have so many registers use the ILP scheduler, for
216 // 32-bit code use the register pressure specific scheduling.
217 if (Subtarget->is64Bit())
218 setSchedulingPreference(Sched::ILP);
219 else
220 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000221 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000222
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000223 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000224 // Setup Windows compiler runtime calls.
225 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000226 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000227 setLibcallName(RTLIB::SREM_I64, "_allrem");
228 setLibcallName(RTLIB::UREM_I64, "_aullrem");
229 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000230 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000231 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000232 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000233 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000234 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
235 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
236 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000237 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
238 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000239 }
240
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000241 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000242 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000243 setUseUnderscoreSetJmp(false);
244 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000245 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000246 // MS runtime is weird: it exports _setjmp, but longjmp!
247 setUseUnderscoreSetJmp(true);
248 setUseUnderscoreLongJmp(false);
249 } else {
250 setUseUnderscoreSetJmp(true);
251 setUseUnderscoreLongJmp(true);
252 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000253
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000254 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000256 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000258 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000260
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000262
Scott Michelfdc40a02009-02-17 22:15:04 +0000263 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000265 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000267 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
269 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000270
271 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
273 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
274 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
275 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
276 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
277 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000278
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000279 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
280 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
282 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
283 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000284
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000288 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000289 // We have an algorithm for SSE2->double, and we turn this into a
290 // 64-bit FILD followed by conditional FADD for other targets.
291 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000292 // We have an algorithm for SSE2, and we turn this into a 64-bit
293 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000294 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000295 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000296
297 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
298 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
300 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000301
Devang Patel6a784892009-06-05 18:48:29 +0000302 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000303 // SSE has no i16 to fp conversion, only i32
304 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000306 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000308 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
310 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000311 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000312 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
314 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000315 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000316
Dale Johannesen73328d12007-09-19 23:55:34 +0000317 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
318 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
320 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000321
Evan Cheng02568ff2006-01-30 22:13:22 +0000322 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
323 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
325 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000326
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000327 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000329 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000331 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
333 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000334 }
335
336 // Handle FP_TO_UINT by promoting the destination to a larger signed
337 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
339 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
340 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000341
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000345 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000346 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 // Expand FP_TO_UINT into a select.
348 // FIXME: We would like to use a Custom expander here eventually to do
349 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000351 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000352 // With SSE3 we can use fisttpll to convert to a signed i64; without
353 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000355 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000356
Chris Lattner399610a2006-12-05 18:22:22 +0000357 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000358 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000359 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
360 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000361 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000362 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000363 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000365 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000366 }
Chris Lattner21f66852005-12-23 05:15:23 +0000367
Dan Gohmanb00ee212008-02-18 19:34:53 +0000368 // Scalar integer divide and remainder are lowered to use operations that
369 // produce two results, to match the available instructions. This exposes
370 // the two-result form to trivial CSE, which is able to combine x/y and x%y
371 // into a single instruction.
372 //
373 // Scalar integer multiply-high is also lowered to use two-result
374 // operations, to match the available instructions. However, plain multiply
375 // (low) operations are left as Legal, as there are single-result
376 // instructions for this in x86. Using the two-result multiply instructions
377 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000378 for (unsigned i = 0, e = 4; i != e; ++i) {
379 MVT VT = IntVTs[i];
380 setOperationAction(ISD::MULHS, VT, Expand);
381 setOperationAction(ISD::MULHU, VT, Expand);
382 setOperationAction(ISD::SDIV, VT, Expand);
383 setOperationAction(ISD::UDIV, VT, Expand);
384 setOperationAction(ISD::SREM, VT, Expand);
385 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000386
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000387 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000388 setOperationAction(ISD::ADDC, VT, Custom);
389 setOperationAction(ISD::ADDE, VT, Custom);
390 setOperationAction(ISD::SUBC, VT, Custom);
391 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000392 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000393
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
395 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
396 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
397 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000398 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
400 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
401 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
402 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
403 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
404 setOperationAction(ISD::FREM , MVT::f32 , Expand);
405 setOperationAction(ISD::FREM , MVT::f64 , Expand);
406 setOperationAction(ISD::FREM , MVT::f80 , Expand);
407 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000408
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
410 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000411 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
412 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
414 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000415 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000418 }
419
Benjamin Kramer1292c222010-12-04 20:32:23 +0000420 if (Subtarget->hasPOPCNT()) {
421 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
422 } else {
423 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
424 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
425 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
426 if (Subtarget->is64Bit())
427 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
428 }
429
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
431 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000432
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000434 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000435 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000436 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000437 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
439 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
440 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
441 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
442 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000443 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
445 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
446 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
447 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000448 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000450 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000451 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000453
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000454 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
456 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
457 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
458 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000459 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
461 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000462 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000463 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
465 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
466 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
467 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000468 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000469 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000470 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
472 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
473 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000474 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
476 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
477 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000478 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000479
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000480 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000482
Eric Christopher9a9d2752010-07-22 02:48:34 +0000483 // We may not have a libcall for MEMBARRIER so we should lower this.
484 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000485
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000486 // On X86 and X86-64, atomic operations are lowered to locked instructions.
487 // Locked instructions, in turn, have implicit fence semantics (all memory
488 // operations are flushed before issuing the locked instruction, and they
489 // are not buffered), so we can fold away the common pattern of
490 // fence-atomic-fence.
491 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000492
Mon P Wang63307c32008-05-05 19:05:59 +0000493 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000494 for (unsigned i = 0, e = 4; i != e; ++i) {
495 MVT VT = IntVTs[i];
496 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
497 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
498 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000499
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000500 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
503 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
505 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
506 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000508 }
509
Evan Cheng3c992d22006-03-07 02:02:57 +0000510 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000511 if (!Subtarget->isTargetDarwin() &&
512 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000513 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000515 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000516
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
518 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
519 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
520 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000521 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000522 setExceptionPointerRegister(X86::RAX);
523 setExceptionSelectorRegister(X86::RDX);
524 } else {
525 setExceptionPointerRegister(X86::EAX);
526 setExceptionSelectorRegister(X86::EDX);
527 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
529 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000530
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000532
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000534
Nate Begemanacc398c2006-01-25 18:21:52 +0000535 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::VASTART , MVT::Other, Custom);
537 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000538 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::VAARG , MVT::Other, Custom);
540 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000541 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::VAARG , MVT::Other, Expand);
543 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000544 }
Evan Chengae642192007-03-02 23:16:35 +0000545
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
547 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
NAKAMURA Takumia2e07622011-03-24 07:07:00 +0000548 setOperationAction(ISD::DYNAMIC_STACKALLOC,
549 (Subtarget->is64Bit() ? MVT::i64 : MVT::i32),
550 (Subtarget->isTargetCOFF()
551 && !Subtarget->isTargetEnvMacho()
552 ? Custom : Expand));
Chris Lattnerb99329e2006-01-13 02:42:53 +0000553
Evan Chengc7ce29b2009-02-13 22:36:38 +0000554 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000555 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000556 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
558 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000559
Evan Cheng223547a2006-01-31 22:28:30 +0000560 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::FABS , MVT::f64, Custom);
562 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000563
564 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::FNEG , MVT::f64, Custom);
566 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000567
Evan Cheng68c47cb2007-01-05 07:55:56 +0000568 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
570 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000571
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000572 // Lower this to FGETSIGNx86 plus an AND.
573 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
574 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
575
Evan Chengd25e9e82006-02-02 00:28:23 +0000576 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FSIN , MVT::f64, Expand);
578 setOperationAction(ISD::FCOS , MVT::f64, Expand);
579 setOperationAction(ISD::FSIN , MVT::f32, Expand);
580 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000581
Chris Lattnera54aa942006-01-29 06:26:08 +0000582 // Expand FP immediates into loads from the stack, except for the special
583 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584 addLegalFPImmediate(APFloat(+0.0)); // xorpd
585 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000586 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587 // Use SSE for f32, x87 for f64.
588 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
590 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
592 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000594
595 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000597
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599
600 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
602 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603
604 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000605 setOperationAction(ISD::FSIN , MVT::f32, Expand);
606 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000607
Nate Begemane1795842008-02-14 08:57:00 +0000608 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000609 addLegalFPImmediate(APFloat(+0.0f)); // xorps
610 addLegalFPImmediate(APFloat(+0.0)); // FLD0
611 addLegalFPImmediate(APFloat(+1.0)); // FLD1
612 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
613 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
614
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
617 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000619 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000621 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
623 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000624
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
626 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
627 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
628 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000629
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000630 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
632 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000633 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000634 addLegalFPImmediate(APFloat(+0.0)); // FLD0
635 addLegalFPImmediate(APFloat(+1.0)); // FLD1
636 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
637 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
639 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
640 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
641 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000642 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000643
Cameron Zwarich33390842011-07-08 21:39:21 +0000644 // We don't support FMA.
645 setOperationAction(ISD::FMA, MVT::f64, Expand);
646 setOperationAction(ISD::FMA, MVT::f32, Expand);
647
Dale Johannesen59a58732007-08-05 18:49:15 +0000648 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000649 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
651 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
652 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000654 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000655 addLegalFPImmediate(TmpFlt); // FLD0
656 TmpFlt.changeSign();
657 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000658
659 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000660 APFloat TmpFlt2(+1.0);
661 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
662 &ignored);
663 addLegalFPImmediate(TmpFlt2); // FLD1
664 TmpFlt2.changeSign();
665 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
666 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000667
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
670 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000671 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000672
673 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000674 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000675
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000676 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
678 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
679 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::FLOG, MVT::f80, Expand);
682 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
683 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
684 setOperationAction(ISD::FEXP, MVT::f80, Expand);
685 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000686
Mon P Wangf007a8b2008-11-06 05:31:54 +0000687 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000688 // (for widening) or expand (for scalarization). Then we will selectively
689 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
691 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
692 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000708 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
709 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000741 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000742 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
746 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
747 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
748 setTruncStoreAction((MVT::SimpleValueType)VT,
749 (MVT::SimpleValueType)InnerVT, Expand);
750 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
751 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
752 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000753 }
754
Evan Chengc7ce29b2009-02-13 22:36:38 +0000755 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
756 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000757 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000758 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000759 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000760 }
761
Dale Johannesen0488fb62010-09-30 23:57:10 +0000762 // MMX-sized vectors (other than x86mmx) are expected to be expanded
763 // into smaller operations.
764 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
765 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
766 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
767 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
768 setOperationAction(ISD::AND, MVT::v8i8, Expand);
769 setOperationAction(ISD::AND, MVT::v4i16, Expand);
770 setOperationAction(ISD::AND, MVT::v2i32, Expand);
771 setOperationAction(ISD::AND, MVT::v1i64, Expand);
772 setOperationAction(ISD::OR, MVT::v8i8, Expand);
773 setOperationAction(ISD::OR, MVT::v4i16, Expand);
774 setOperationAction(ISD::OR, MVT::v2i32, Expand);
775 setOperationAction(ISD::OR, MVT::v1i64, Expand);
776 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
777 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
778 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
779 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
780 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
781 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
782 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
783 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
784 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
785 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
786 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
787 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
788 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000789 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
790 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
791 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
792 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000793
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000794 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000796
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
798 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
799 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
800 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
801 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
802 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
803 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
804 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
805 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
806 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
807 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
808 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000809 }
810
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000811 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000812 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000813
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000814 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
815 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
817 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
818 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
819 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000820
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
822 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
823 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
824 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
826 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
827 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
828 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
829 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
830 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
831 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
832 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
833 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
834 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
835 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
836 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
839 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
840 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
841 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000842
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
844 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
847 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000848
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000849 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
850 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
851 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
852 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
853 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
854
Evan Cheng2c3ae372006-04-12 21:21:57 +0000855 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
857 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000858 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000859 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000860 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000861 // Do not attempt to custom lower non-128-bit vectors
862 if (!VT.is128BitVector())
863 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 setOperationAction(ISD::BUILD_VECTOR,
865 VT.getSimpleVT().SimpleTy, Custom);
866 setOperationAction(ISD::VECTOR_SHUFFLE,
867 VT.getSimpleVT().SimpleTy, Custom);
868 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
869 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000870 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000871
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
873 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
875 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
876 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
877 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000878
Nate Begemancdd1eec2008-02-12 22:51:28 +0000879 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
881 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000882 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000883
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000884 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
886 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000887 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000888
889 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000890 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000891 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000892
Owen Andersond6662ad2009-08-10 20:46:15 +0000893 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000895 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000897 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000898 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000899 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000901 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000903 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000904
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000906
Evan Cheng2c3ae372006-04-12 21:21:57 +0000907 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
909 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
910 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
911 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
914 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000915 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000916
Nate Begeman14d12ca2008-02-11 04:19:36 +0000917 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000918 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
921 setOperationAction(ISD::FRINT, MVT::f32, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
923 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
924 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
925 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
926 setOperationAction(ISD::FRINT, MVT::f64, Legal);
927 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
928
Nate Begeman14d12ca2008-02-11 04:19:36 +0000929 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000931
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000932 // Can turn SHL into an integer multiply.
933 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000934 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000935
Nate Begeman14d12ca2008-02-11 04:19:36 +0000936 // i8 and i16 vectors are custom , because the source register and source
937 // source memory operand types are not the same width. f32 vectors are
938 // custom since the immediate controlling the insert encodes additional
939 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
948 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000949
950 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
952 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000953 }
954 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000955
Nadav Rotem43012222011-05-11 08:12:09 +0000956 if (Subtarget->hasSSE2()) {
957 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
958 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
959 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
960
961 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
962 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
963 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
964
965 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
966 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
967 }
968
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000969 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000970 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000971
David Greene9b9838d2009-06-29 16:47:10 +0000972 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000973 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
974 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
975 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
976 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
977 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
978 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000979
Owen Anderson825b72b2009-08-11 20:47:22 +0000980 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
982 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000983
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
985 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
986 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
987 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
988 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
989 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000990
Owen Anderson825b72b2009-08-11 20:47:22 +0000991 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
992 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
993 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
994 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
995 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
996 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000997
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +0000998 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +0000999 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001000 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1001 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1002 EVT VT = SVT;
1003
1004 // Extract subvector is special because the value type
1005 // (result) is 128-bit but the source is 256-bit wide.
1006 if (VT.is128BitVector())
1007 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1008
1009 // Do not attempt to custom lower other non-256-bit vectors
1010 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001011 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001012
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001013 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1014 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1015 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1016 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
1017 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
1018 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001019 }
1020
David Greene54d8eba2011-01-27 22:38:56 +00001021 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001022 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1023 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1024 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001025
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001026 // Do not attempt to promote non-256-bit vectors
1027 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001028 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001029
1030 setOperationAction(ISD::AND, SVT, Promote);
1031 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1032 setOperationAction(ISD::OR, SVT, Promote);
1033 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1034 setOperationAction(ISD::XOR, SVT, Promote);
1035 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1036 setOperationAction(ISD::LOAD, SVT, Promote);
1037 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1038 setOperationAction(ISD::SELECT, SVT, Promote);
1039 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001040 }
David Greene9b9838d2009-06-29 16:47:10 +00001041 }
1042
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001043 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1044 // of this type with custom code.
1045 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1046 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1047 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1048 }
1049
Evan Cheng6be2c582006-04-05 23:38:46 +00001050 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001051 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001052
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001053
Eli Friedman962f5492010-06-02 19:35:46 +00001054 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1055 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001056 //
Eli Friedman962f5492010-06-02 19:35:46 +00001057 // FIXME: We really should do custom legalization for addition and
1058 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1059 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001060 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1061 // Add/Sub/Mul with overflow operations are custom lowered.
1062 MVT VT = IntVTs[i];
1063 setOperationAction(ISD::SADDO, VT, Custom);
1064 setOperationAction(ISD::UADDO, VT, Custom);
1065 setOperationAction(ISD::SSUBO, VT, Custom);
1066 setOperationAction(ISD::USUBO, VT, Custom);
1067 setOperationAction(ISD::SMULO, VT, Custom);
1068 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001069 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001070
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001071 // There are no 8-bit 3-address imul/mul instructions
1072 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1073 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001074
Evan Chengd54f2d52009-03-31 19:38:51 +00001075 if (!Subtarget->is64Bit()) {
1076 // These libcalls are not available in 32-bit.
1077 setLibcallName(RTLIB::SHL_I128, 0);
1078 setLibcallName(RTLIB::SRL_I128, 0);
1079 setLibcallName(RTLIB::SRA_I128, 0);
1080 }
1081
Evan Cheng206ee9d2006-07-07 08:33:52 +00001082 // We have target-specific dag combine patterns for the following nodes:
1083 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001084 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001085 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001086 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001087 setTargetDAGCombine(ISD::SHL);
1088 setTargetDAGCombine(ISD::SRA);
1089 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001090 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001091 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001092 setTargetDAGCombine(ISD::ADD);
1093 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001094 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001095 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001096 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001097 if (Subtarget->is64Bit())
1098 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001099
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001100 computeRegisterProperties();
1101
Evan Cheng05219282011-01-06 06:52:41 +00001102 // On Darwin, -Os means optimize for size without hurting performance,
1103 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001104 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001105 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001106 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001107 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1108 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1109 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001110 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001111 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001112
1113 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001114}
1115
Scott Michel5b8f82e2008-03-10 15:42:14 +00001116
Owen Anderson825b72b2009-08-11 20:47:22 +00001117MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1118 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001119}
1120
1121
Evan Cheng29286502008-01-23 23:17:41 +00001122/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1123/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001124static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001125 if (MaxAlign == 16)
1126 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001127 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001128 if (VTy->getBitWidth() == 128)
1129 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001130 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001131 unsigned EltAlign = 0;
1132 getMaxByValAlign(ATy->getElementType(), EltAlign);
1133 if (EltAlign > MaxAlign)
1134 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001135 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001136 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1137 unsigned EltAlign = 0;
1138 getMaxByValAlign(STy->getElementType(i), EltAlign);
1139 if (EltAlign > MaxAlign)
1140 MaxAlign = EltAlign;
1141 if (MaxAlign == 16)
1142 break;
1143 }
1144 }
1145 return;
1146}
1147
1148/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1149/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001150/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1151/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001152unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001153 if (Subtarget->is64Bit()) {
1154 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001155 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001156 if (TyAlign > 8)
1157 return TyAlign;
1158 return 8;
1159 }
1160
Evan Cheng29286502008-01-23 23:17:41 +00001161 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001162 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001163 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001164 return Align;
1165}
Chris Lattner2b02a442007-02-25 08:29:00 +00001166
Evan Chengf0df0312008-05-15 08:39:06 +00001167/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001168/// and store operations as a result of memset, memcpy, and memmove
1169/// lowering. If DstAlign is zero that means it's safe to destination
1170/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1171/// means there isn't a need to check it against alignment requirement,
1172/// probably because the source does not need to be loaded. If
1173/// 'NonScalarIntSafe' is true, that means it's safe to return a
1174/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1175/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1176/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001177/// It returns EVT::Other if the type should be determined using generic
1178/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001179EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001180X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1181 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001182 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001183 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001184 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001185 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1186 // linux. This is because the stack realignment code can't handle certain
1187 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001188 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001189 if (NonScalarIntSafe &&
1190 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001191 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001192 (Subtarget->isUnalignedMemAccessFast() ||
1193 ((DstAlign == 0 || DstAlign >= 16) &&
1194 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001195 Subtarget->getStackAlignment() >= 16) {
1196 if (Subtarget->hasSSE2())
1197 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001198 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001199 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001200 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001201 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001202 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001203 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001204 // Do not use f64 to lower memcpy if source is string constant. It's
1205 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001206 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001207 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001208 }
Evan Chengf0df0312008-05-15 08:39:06 +00001209 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001210 return MVT::i64;
1211 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001212}
1213
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001214/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1215/// current function. The returned value is a member of the
1216/// MachineJumpTableInfo::JTEntryKind enum.
1217unsigned X86TargetLowering::getJumpTableEncoding() const {
1218 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1219 // symbol.
1220 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1221 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001222 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001223
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001224 // Otherwise, use the normal jump table encoding heuristics.
1225 return TargetLowering::getJumpTableEncoding();
1226}
1227
Chris Lattnerc64daab2010-01-26 05:02:42 +00001228const MCExpr *
1229X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1230 const MachineBasicBlock *MBB,
1231 unsigned uid,MCContext &Ctx) const{
1232 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1233 Subtarget->isPICStyleGOT());
1234 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1235 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001236 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1237 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001238}
1239
Evan Chengcc415862007-11-09 01:32:10 +00001240/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1241/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001242SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001243 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001244 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001245 // This doesn't have DebugLoc associated with it, but is not really the
1246 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001247 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001248 return Table;
1249}
1250
Chris Lattner589c6f62010-01-26 06:28:43 +00001251/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1252/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1253/// MCExpr.
1254const MCExpr *X86TargetLowering::
1255getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1256 MCContext &Ctx) const {
1257 // X86-64 uses RIP relative addressing based on the jump table label.
1258 if (Subtarget->isPICStyleRIPRel())
1259 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1260
1261 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001262 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001263}
1264
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001265// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001266std::pair<const TargetRegisterClass*, uint8_t>
1267X86TargetLowering::findRepresentativeClass(EVT VT) const{
1268 const TargetRegisterClass *RRC = 0;
1269 uint8_t Cost = 1;
1270 switch (VT.getSimpleVT().SimpleTy) {
1271 default:
1272 return TargetLowering::findRepresentativeClass(VT);
1273 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1274 RRC = (Subtarget->is64Bit()
1275 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1276 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001277 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001278 RRC = X86::VR64RegisterClass;
1279 break;
1280 case MVT::f32: case MVT::f64:
1281 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1282 case MVT::v4f32: case MVT::v2f64:
1283 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1284 case MVT::v4f64:
1285 RRC = X86::VR128RegisterClass;
1286 break;
1287 }
1288 return std::make_pair(RRC, Cost);
1289}
1290
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001291bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1292 unsigned &Offset) const {
1293 if (!Subtarget->isTargetLinux())
1294 return false;
1295
1296 if (Subtarget->is64Bit()) {
1297 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1298 Offset = 0x28;
1299 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1300 AddressSpace = 256;
1301 else
1302 AddressSpace = 257;
1303 } else {
1304 // %gs:0x14 on i386
1305 Offset = 0x14;
1306 AddressSpace = 256;
1307 }
1308 return true;
1309}
1310
1311
Chris Lattner2b02a442007-02-25 08:29:00 +00001312//===----------------------------------------------------------------------===//
1313// Return Value Calling Convention Implementation
1314//===----------------------------------------------------------------------===//
1315
Chris Lattner59ed56b2007-02-28 04:55:35 +00001316#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001317
Michael J. Spencerec38de22010-10-10 22:04:20 +00001318bool
Eric Christopher471e4222011-06-08 23:55:35 +00001319X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1320 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001321 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001322 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001323 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001324 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001325 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001326 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001327}
1328
Dan Gohman98ca4f22009-08-05 01:29:28 +00001329SDValue
1330X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001331 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001332 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001333 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001334 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001335 MachineFunction &MF = DAG.getMachineFunction();
1336 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001337
Chris Lattner9774c912007-02-27 05:28:59 +00001338 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001339 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001340 RVLocs, *DAG.getContext());
1341 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001342
Evan Chengdcea1632010-02-04 02:40:39 +00001343 // Add the regs to the liveout set for the function.
1344 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1345 for (unsigned i = 0; i != RVLocs.size(); ++i)
1346 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1347 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001348
Dan Gohman475871a2008-07-27 21:46:04 +00001349 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001350
Dan Gohman475871a2008-07-27 21:46:04 +00001351 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001352 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1353 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001354 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1355 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001356
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001357 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001358 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1359 CCValAssign &VA = RVLocs[i];
1360 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001361 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001362 EVT ValVT = ValToCopy.getValueType();
1363
Dale Johannesenc4510512010-09-24 19:05:48 +00001364 // If this is x86-64, and we disabled SSE, we can't return FP values,
1365 // or SSE or MMX vectors.
1366 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1367 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001368 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001369 report_fatal_error("SSE register return with SSE disabled");
1370 }
1371 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1372 // llvm-gcc has never done it right and no one has noticed, so this
1373 // should be OK for now.
1374 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001375 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001376 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001377
Chris Lattner447ff682008-03-11 03:23:40 +00001378 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1379 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001380 if (VA.getLocReg() == X86::ST0 ||
1381 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001382 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1383 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001384 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001385 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001386 RetOps.push_back(ValToCopy);
1387 // Don't emit a copytoreg.
1388 continue;
1389 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001390
Evan Cheng242b38b2009-02-23 09:03:22 +00001391 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1392 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001393 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001394 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001395 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001396 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001397 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1398 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001399 // If we don't have SSE2 available, convert to v4f32 so the generated
1400 // register is legal.
1401 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001402 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001403 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001404 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001405 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001406
Dale Johannesendd64c412009-02-04 00:33:20 +00001407 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001408 Flag = Chain.getValue(1);
1409 }
Dan Gohman61a92132008-04-21 23:59:07 +00001410
1411 // The x86-64 ABI for returning structs by value requires that we copy
1412 // the sret argument into %rax for the return. We saved the argument into
1413 // a virtual register in the entry block, so now we copy the value out
1414 // and into %rax.
1415 if (Subtarget->is64Bit() &&
1416 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1417 MachineFunction &MF = DAG.getMachineFunction();
1418 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1419 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001420 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001421 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001422 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001423
Dale Johannesendd64c412009-02-04 00:33:20 +00001424 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001425 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001426
1427 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001428 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001429 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001430
Chris Lattner447ff682008-03-11 03:23:40 +00001431 RetOps[0] = Chain; // Update chain.
1432
1433 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001434 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001435 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001436
1437 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001438 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001439}
1440
Evan Cheng3d2125c2010-11-30 23:55:39 +00001441bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1442 if (N->getNumValues() != 1)
1443 return false;
1444 if (!N->hasNUsesOfValue(1, 0))
1445 return false;
1446
1447 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001448 if (Copy->getOpcode() != ISD::CopyToReg &&
1449 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001450 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001451
1452 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001453 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001454 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001455 if (UI->getOpcode() != X86ISD::RET_FLAG)
1456 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001457 HasRet = true;
1458 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001459
Evan Cheng1bf891a2010-12-01 22:59:46 +00001460 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001461}
1462
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001463EVT
1464X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001465 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001466 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001467 // TODO: Is this also valid on 32-bit?
1468 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001469 ReturnMVT = MVT::i8;
1470 else
1471 ReturnMVT = MVT::i32;
1472
1473 EVT MinVT = getRegisterType(Context, ReturnMVT);
1474 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001475}
1476
Dan Gohman98ca4f22009-08-05 01:29:28 +00001477/// LowerCallResult - Lower the result values of a call into the
1478/// appropriate copies out of appropriate physical registers.
1479///
1480SDValue
1481X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001482 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 const SmallVectorImpl<ISD::InputArg> &Ins,
1484 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001485 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001486
Chris Lattnere32bbf62007-02-28 07:09:55 +00001487 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001488 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001489 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001490 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1491 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001492 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001493
Chris Lattner3085e152007-02-25 08:59:22 +00001494 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001495 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001496 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001497 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001498
Torok Edwin3f142c32009-02-01 18:15:56 +00001499 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001500 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001501 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001502 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001503 }
1504
Evan Cheng79fb3b42009-02-20 20:43:02 +00001505 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001506
1507 // If this is a call to a function that returns an fp value on the floating
1508 // point stack, we must guarantee the the value is popped from the stack, so
1509 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001510 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001511 // instead.
1512 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1513 // If we prefer to use the value in xmm registers, copy it out as f80 and
1514 // use a truncate to move it from fp stack reg to xmm reg.
1515 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001516 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001517 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1518 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001519 Val = Chain.getValue(0);
1520
1521 // Round the f80 to the right size, which also moves it to the appropriate
1522 // xmm register.
1523 if (CopyVT != VA.getValVT())
1524 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1525 // This truncation won't change the value.
1526 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001527 } else {
1528 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1529 CopyVT, InFlag).getValue(1);
1530 Val = Chain.getValue(0);
1531 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001532 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001533 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001534 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001535
Dan Gohman98ca4f22009-08-05 01:29:28 +00001536 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001537}
1538
1539
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001540//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001541// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001542//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001543// StdCall calling convention seems to be standard for many Windows' API
1544// routines and around. It differs from C calling convention just a little:
1545// callee should clean up the stack, not caller. Symbols should be also
1546// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001547// For info on fast calling convention see Fast Calling Convention (tail call)
1548// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001549
Dan Gohman98ca4f22009-08-05 01:29:28 +00001550/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001551/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001552static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1553 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001554 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001555
Dan Gohman98ca4f22009-08-05 01:29:28 +00001556 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001557}
1558
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001559/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001560/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561static bool
1562ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1563 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001564 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001565
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001567}
1568
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001569/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1570/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001571/// the specific parameter attribute. The copy will be passed as a byval
1572/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001573static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001574CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001575 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1576 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001577 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001578
Dale Johannesendd64c412009-02-04 00:33:20 +00001579 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001580 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001581 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001582}
1583
Chris Lattner29689432010-03-11 00:22:57 +00001584/// IsTailCallConvention - Return true if the calling convention is one that
1585/// supports tail call optimization.
1586static bool IsTailCallConvention(CallingConv::ID CC) {
1587 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1588}
1589
Evan Cheng485fafc2011-03-21 01:19:09 +00001590bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1591 if (!CI->isTailCall())
1592 return false;
1593
1594 CallSite CS(CI);
1595 CallingConv::ID CalleeCC = CS.getCallingConv();
1596 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1597 return false;
1598
1599 return true;
1600}
1601
Evan Cheng0c439eb2010-01-27 00:07:07 +00001602/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1603/// a tailcall target by changing its ABI.
1604static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001605 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001606}
1607
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608SDValue
1609X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001610 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611 const SmallVectorImpl<ISD::InputArg> &Ins,
1612 DebugLoc dl, SelectionDAG &DAG,
1613 const CCValAssign &VA,
1614 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001615 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001616 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001617 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001618 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001619 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001620 EVT ValVT;
1621
1622 // If value is passed by pointer we have address passed instead of the value
1623 // itself.
1624 if (VA.getLocInfo() == CCValAssign::Indirect)
1625 ValVT = VA.getLocVT();
1626 else
1627 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001628
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001629 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001630 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001631 // In case of tail call optimization mark all arguments mutable. Since they
1632 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001633 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001634 unsigned Bytes = Flags.getByValSize();
1635 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1636 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001637 return DAG.getFrameIndex(FI, getPointerTy());
1638 } else {
1639 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001640 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001641 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1642 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001643 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001644 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001645 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001646}
1647
Dan Gohman475871a2008-07-27 21:46:04 +00001648SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001650 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001651 bool isVarArg,
1652 const SmallVectorImpl<ISD::InputArg> &Ins,
1653 DebugLoc dl,
1654 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001655 SmallVectorImpl<SDValue> &InVals)
1656 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001657 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001658 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001659
Gordon Henriksen86737662008-01-05 16:56:59 +00001660 const Function* Fn = MF.getFunction();
1661 if (Fn->hasExternalLinkage() &&
1662 Subtarget->isTargetCygMing() &&
1663 Fn->getName() == "main")
1664 FuncInfo->setForceFramePointer(true);
1665
Evan Cheng1bc78042006-04-26 01:20:17 +00001666 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001668 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001669
Chris Lattner29689432010-03-11 00:22:57 +00001670 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1671 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001672
Chris Lattner638402b2007-02-28 07:00:42 +00001673 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001674 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001675 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001676 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001677
1678 // Allocate shadow area for Win64
1679 if (IsWin64) {
1680 CCInfo.AllocateStack(32, 8);
1681 }
1682
Duncan Sands45907662010-10-31 13:21:44 +00001683 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001684
Chris Lattnerf39f7712007-02-28 05:46:49 +00001685 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001686 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001687 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1688 CCValAssign &VA = ArgLocs[i];
1689 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1690 // places.
1691 assert(VA.getValNo() != LastVal &&
1692 "Don't support value assigned to multiple locs yet");
1693 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001694
Chris Lattnerf39f7712007-02-28 05:46:49 +00001695 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001696 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001697 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001699 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001700 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001701 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001702 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001703 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001704 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001705 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001706 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1707 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001708 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001709 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001710 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001711 RC = X86::VR64RegisterClass;
1712 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001713 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001714
Devang Patel68e6bee2011-02-21 23:21:26 +00001715 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001717
Chris Lattnerf39f7712007-02-28 05:46:49 +00001718 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1719 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1720 // right size.
1721 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001722 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001723 DAG.getValueType(VA.getValVT()));
1724 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001725 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001726 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001727 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001728 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001729
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001730 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001731 // Handle MMX values passed in XMM regs.
1732 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001733 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1734 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001735 } else
1736 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001737 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001738 } else {
1739 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001741 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001742
1743 // If value is passed via pointer - do a load.
1744 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001745 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1746 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001747
Dan Gohman98ca4f22009-08-05 01:29:28 +00001748 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001749 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001750
Dan Gohman61a92132008-04-21 23:59:07 +00001751 // The x86-64 ABI for returning structs by value requires that we copy
1752 // the sret argument into %rax for the return. Save the argument into
1753 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001754 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001755 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1756 unsigned Reg = FuncInfo->getSRetReturnReg();
1757 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001759 FuncInfo->setSRetReturnReg(Reg);
1760 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001763 }
1764
Chris Lattnerf39f7712007-02-28 05:46:49 +00001765 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001766 // Align stack specially for tail calls.
1767 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001768 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001769
Evan Cheng1bc78042006-04-26 01:20:17 +00001770 // If the function takes variable number of arguments, make a frame index for
1771 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001772 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001773 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1774 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001775 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001776 }
1777 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001778 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1779
1780 // FIXME: We should really autogenerate these arrays
1781 static const unsigned GPR64ArgRegsWin64[] = {
1782 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001784 static const unsigned GPR64ArgRegs64Bit[] = {
1785 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1786 };
1787 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001788 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1789 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1790 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001791 const unsigned *GPR64ArgRegs;
1792 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001793
1794 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001795 // The XMM registers which might contain var arg parameters are shadowed
1796 // in their paired GPR. So we only need to save the GPR to their home
1797 // slots.
1798 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001799 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001800 } else {
1801 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1802 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001803
1804 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001805 }
1806 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1807 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001808
Devang Patel578efa92009-06-05 21:57:13 +00001809 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001810 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001811 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001812 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001813 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001814 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001815 // Kernel mode asks for SSE to be disabled, so don't push them
1816 // on the stack.
1817 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001818
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001819 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001820 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001821 // Get to the caller-allocated home save location. Add 8 to account
1822 // for the return address.
1823 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001824 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001825 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001826 // Fixup to set vararg frame on shadow area (4 x i64).
1827 if (NumIntRegs < 4)
1828 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001829 } else {
1830 // For X86-64, if there are vararg parameters that are passed via
1831 // registers, then we must store them to their spots on the stack so they
1832 // may be loaded by deferencing the result of va_next.
1833 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1834 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1835 FuncInfo->setRegSaveFrameIndex(
1836 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001837 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001838 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001839
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001841 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001842 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1843 getPointerTy());
1844 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001845 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001846 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1847 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001848 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001849 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001851 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001852 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001853 MachinePointerInfo::getFixedStack(
1854 FuncInfo->getRegSaveFrameIndex(), Offset),
1855 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001856 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001857 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001858 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001859
Dan Gohmanface41a2009-08-16 21:24:25 +00001860 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1861 // Now store the XMM (fp + vector) parameter registers.
1862 SmallVector<SDValue, 11> SaveXMMOps;
1863 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001864
Devang Patel68e6bee2011-02-21 23:21:26 +00001865 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001866 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1867 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001868
Dan Gohman1e93df62010-04-17 14:41:14 +00001869 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1870 FuncInfo->getRegSaveFrameIndex()));
1871 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1872 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001873
Dan Gohmanface41a2009-08-16 21:24:25 +00001874 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001875 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001876 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001877 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1878 SaveXMMOps.push_back(Val);
1879 }
1880 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1881 MVT::Other,
1882 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001883 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001884
1885 if (!MemOps.empty())
1886 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1887 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001889 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001890
Gordon Henriksen86737662008-01-05 16:56:59 +00001891 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001892 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001893 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001894 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001895 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001896 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001897 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001898 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001899 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001900
Gordon Henriksen86737662008-01-05 16:56:59 +00001901 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001902 // RegSaveFrameIndex is X86-64 only.
1903 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001904 if (CallConv == CallingConv::X86_FastCall ||
1905 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001906 // fastcc functions can't have varargs.
1907 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001908 }
Evan Cheng25caf632006-05-23 21:06:34 +00001909
Dan Gohman98ca4f22009-08-05 01:29:28 +00001910 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001911}
1912
Dan Gohman475871a2008-07-27 21:46:04 +00001913SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1915 SDValue StackPtr, SDValue Arg,
1916 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001917 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001918 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001919 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001920 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001921 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001922 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001923 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001924
1925 return DAG.getStore(Chain, dl, Arg, PtrOff,
1926 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001927 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001928}
1929
Bill Wendling64e87322009-01-16 19:25:27 +00001930/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001931/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001932SDValue
1933X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001934 SDValue &OutRetAddr, SDValue Chain,
1935 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001936 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001937 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001938 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001939 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001940
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001941 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001942 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1943 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001944 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001945}
1946
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001947/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001948/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001949static SDValue
1950EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001951 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001952 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001953 // Store the return address to the appropriate stack slot.
1954 if (!FPDiff) return Chain;
1955 // Calculate the new stack slot for the return address.
1956 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001957 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001958 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001961 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001962 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001963 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001964 return Chain;
1965}
1966
Dan Gohman98ca4f22009-08-05 01:29:28 +00001967SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001968X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001969 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001970 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001971 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001972 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001973 const SmallVectorImpl<ISD::InputArg> &Ins,
1974 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001975 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976 MachineFunction &MF = DAG.getMachineFunction();
1977 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001978 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001979 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001980 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001981
Evan Cheng5f941932010-02-05 02:21:12 +00001982 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001983 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001984 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1985 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001986 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001987
1988 // Sibcalls are automatically detected tailcalls which do not require
1989 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001990 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001991 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001992
1993 if (isTailCall)
1994 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001995 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001996
Chris Lattner29689432010-03-11 00:22:57 +00001997 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1998 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001999
Chris Lattner638402b2007-02-28 07:00:42 +00002000 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002001 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002002 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002003 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002004
2005 // Allocate shadow area for Win64
2006 if (IsWin64) {
2007 CCInfo.AllocateStack(32, 8);
2008 }
2009
Duncan Sands45907662010-10-31 13:21:44 +00002010 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002011
Chris Lattner423c5f42007-02-28 05:31:48 +00002012 // Get a count of how many bytes are to be pushed on the stack.
2013 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002014 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002015 // This is a sibcall. The memory operands are available in caller's
2016 // own caller's stack.
2017 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002018 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002019 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002020
Gordon Henriksen86737662008-01-05 16:56:59 +00002021 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002022 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002023 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002024 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002025 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2026 FPDiff = NumBytesCallerPushed - NumBytes;
2027
2028 // Set the delta of movement of the returnaddr stackslot.
2029 // But only set if delta is greater than previous delta.
2030 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2031 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2032 }
2033
Evan Chengf22f9b32010-02-06 03:28:46 +00002034 if (!IsSibcall)
2035 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002036
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002038 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002039 if (isTailCall && FPDiff)
2040 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2041 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002042
Dan Gohman475871a2008-07-27 21:46:04 +00002043 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2044 SmallVector<SDValue, 8> MemOpChains;
2045 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002046
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002047 // Walk the register/memloc assignments, inserting copies/loads. In the case
2048 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002049 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2050 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002051 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002052 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002053 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002054 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002055
Chris Lattner423c5f42007-02-28 05:31:48 +00002056 // Promote the value if needed.
2057 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002058 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002059 case CCValAssign::Full: break;
2060 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002061 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002062 break;
2063 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002064 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002065 break;
2066 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002067 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2068 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002069 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2071 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002072 } else
2073 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2074 break;
2075 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002076 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002077 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002078 case CCValAssign::Indirect: {
2079 // Store the argument.
2080 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002081 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002082 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002083 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002084 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002085 Arg = SpillSlot;
2086 break;
2087 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002088 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002089
Chris Lattner423c5f42007-02-28 05:31:48 +00002090 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002091 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2092 if (isVarArg && IsWin64) {
2093 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2094 // shadow reg if callee is a varargs function.
2095 unsigned ShadowReg = 0;
2096 switch (VA.getLocReg()) {
2097 case X86::XMM0: ShadowReg = X86::RCX; break;
2098 case X86::XMM1: ShadowReg = X86::RDX; break;
2099 case X86::XMM2: ShadowReg = X86::R8; break;
2100 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002101 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002102 if (ShadowReg)
2103 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002104 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002105 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002106 assert(VA.isMemLoc());
2107 if (StackPtr.getNode() == 0)
2108 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2109 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2110 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002111 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002112 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002113
Evan Cheng32fe1032006-05-25 00:59:30 +00002114 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002116 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002117
Evan Cheng347d5f72006-04-28 21:29:37 +00002118 // Build a sequence of copy-to-reg nodes chained together with token chain
2119 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002120 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002121 // Tail call byval lowering might overwrite argument registers so in case of
2122 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002123 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002124 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002125 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002126 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002127 InFlag = Chain.getValue(1);
2128 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002129
Chris Lattner88e1fd52009-07-09 04:24:46 +00002130 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002131 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2132 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002133 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002134 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2135 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002136 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002137 InFlag);
2138 InFlag = Chain.getValue(1);
2139 } else {
2140 // If we are tail calling and generating PIC/GOT style code load the
2141 // address of the callee into ECX. The value in ecx is used as target of
2142 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2143 // for tail calls on PIC/GOT architectures. Normally we would just put the
2144 // address of GOT into ebx and then call target@PLT. But for tail calls
2145 // ebx would be restored (since ebx is callee saved) before jumping to the
2146 // target@PLT.
2147
2148 // Note: The actual moving to ECX is done further down.
2149 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2150 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2151 !G->getGlobal()->hasProtectedVisibility())
2152 Callee = LowerGlobalAddress(Callee, DAG);
2153 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002154 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002155 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002156 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002157
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002158 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002159 // From AMD64 ABI document:
2160 // For calls that may call functions that use varargs or stdargs
2161 // (prototype-less calls or calls to functions containing ellipsis (...) in
2162 // the declaration) %al is used as hidden argument to specify the number
2163 // of SSE registers used. The contents of %al do not need to match exactly
2164 // the number of registers, but must be an ubound on the number of SSE
2165 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002166
Gordon Henriksen86737662008-01-05 16:56:59 +00002167 // Count the number of XMM registers allocated.
2168 static const unsigned XMMArgRegs[] = {
2169 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2170 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2171 };
2172 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002173 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002174 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002175
Dale Johannesendd64c412009-02-04 00:33:20 +00002176 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002177 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002178 InFlag = Chain.getValue(1);
2179 }
2180
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002181
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002182 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002183 if (isTailCall) {
2184 // Force all the incoming stack arguments to be loaded from the stack
2185 // before any new outgoing arguments are stored to the stack, because the
2186 // outgoing stack slots may alias the incoming argument stack slots, and
2187 // the alias isn't otherwise explicit. This is slightly more conservative
2188 // than necessary, because it means that each store effectively depends
2189 // on every argument instead of just those arguments it would clobber.
2190 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2191
Dan Gohman475871a2008-07-27 21:46:04 +00002192 SmallVector<SDValue, 8> MemOpChains2;
2193 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002194 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002195 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002196 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002197 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002198 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2199 CCValAssign &VA = ArgLocs[i];
2200 if (VA.isRegLoc())
2201 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002202 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002203 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002204 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002205 // Create frame index.
2206 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002207 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002208 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002209 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002210
Duncan Sands276dcbd2008-03-21 09:14:45 +00002211 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002212 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002213 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002214 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002215 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002216 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002217 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002218
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2220 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002221 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002222 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002223 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002224 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002225 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002226 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002227 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002228 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002229 }
2230 }
2231
2232 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002233 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002234 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002235
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002236 // Copy arguments to their registers.
2237 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002238 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002239 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002240 InFlag = Chain.getValue(1);
2241 }
Dan Gohman475871a2008-07-27 21:46:04 +00002242 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002243
Gordon Henriksen86737662008-01-05 16:56:59 +00002244 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002245 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002246 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002247 }
2248
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002249 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2250 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2251 // In the 64-bit large code model, we have to make all calls
2252 // through a register, since the call instruction's 32-bit
2253 // pc-relative offset may not be large enough to hold the whole
2254 // address.
2255 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002256 // If the callee is a GlobalAddress node (quite common, every direct call
2257 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2258 // it.
2259
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002260 // We should use extra load for direct calls to dllimported functions in
2261 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002262 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002263 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002264 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002265 bool ExtraLoad = false;
2266 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002267
Chris Lattner48a7d022009-07-09 05:02:21 +00002268 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2269 // external symbols most go through the PLT in PIC mode. If the symbol
2270 // has hidden or protected visibility, or if it is static or local, then
2271 // we don't need to use the PLT - we can directly call it.
2272 if (Subtarget->isTargetELF() &&
2273 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002274 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002275 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002276 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002277 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002278 (!Subtarget->getTargetTriple().isMacOSX() ||
2279 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002280 // PC-relative references to external symbols should go through $stub,
2281 // unless we're building with the leopard linker or later, which
2282 // automatically synthesizes these stubs.
2283 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002284 } else if (Subtarget->isPICStyleRIPRel() &&
2285 isa<Function>(GV) &&
2286 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2287 // If the function is marked as non-lazy, generate an indirect call
2288 // which loads from the GOT directly. This avoids runtime overhead
2289 // at the cost of eager binding (and one extra byte of encoding).
2290 OpFlags = X86II::MO_GOTPCREL;
2291 WrapperKind = X86ISD::WrapperRIP;
2292 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002293 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002294
Devang Patel0d881da2010-07-06 22:08:15 +00002295 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002296 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002297
2298 // Add a wrapper if needed.
2299 if (WrapperKind != ISD::DELETED_NODE)
2300 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2301 // Add extra indirection if needed.
2302 if (ExtraLoad)
2303 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2304 MachinePointerInfo::getGOT(),
2305 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002306 }
Bill Wendling056292f2008-09-16 21:48:12 +00002307 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002308 unsigned char OpFlags = 0;
2309
Evan Cheng1bf891a2010-12-01 22:59:46 +00002310 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2311 // external symbols should go through the PLT.
2312 if (Subtarget->isTargetELF() &&
2313 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2314 OpFlags = X86II::MO_PLT;
2315 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002316 (!Subtarget->getTargetTriple().isMacOSX() ||
2317 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002318 // PC-relative references to external symbols should go through $stub,
2319 // unless we're building with the leopard linker or later, which
2320 // automatically synthesizes these stubs.
2321 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002322 }
Eric Christopherfd179292009-08-27 18:07:15 +00002323
Chris Lattner48a7d022009-07-09 05:02:21 +00002324 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2325 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002326 }
2327
Chris Lattnerd96d0722007-02-25 06:40:16 +00002328 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002329 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002330 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002331
Evan Chengf22f9b32010-02-06 03:28:46 +00002332 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002333 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2334 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002335 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002337
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002338 Ops.push_back(Chain);
2339 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002340
Dan Gohman98ca4f22009-08-05 01:29:28 +00002341 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002343
Gordon Henriksen86737662008-01-05 16:56:59 +00002344 // Add argument registers to the end of the list so that they are known live
2345 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002346 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2347 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2348 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002349
Evan Cheng586ccac2008-03-18 23:36:35 +00002350 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002351 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002352 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2353
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002354 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002355 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002357
Gabor Greifba36cb52008-08-28 21:40:38 +00002358 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002359 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002360
Dan Gohman98ca4f22009-08-05 01:29:28 +00002361 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002362 // We used to do:
2363 //// If this is the first return lowered for this function, add the regs
2364 //// to the liveout set for the function.
2365 // This isn't right, although it's probably harmless on x86; liveouts
2366 // should be computed from returns not tail calls. Consider a void
2367 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368 return DAG.getNode(X86ISD::TC_RETURN, dl,
2369 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002370 }
2371
Dale Johannesenace16102009-02-03 19:33:06 +00002372 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002373 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002374
Chris Lattner2d297092006-05-23 18:50:38 +00002375 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002376 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002377 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002378 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002379 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002380 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002381 // pops the hidden struct pointer, so we have to push it back.
2382 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002383 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002384 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002385 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002386
Gordon Henriksenae636f82008-01-03 16:47:34 +00002387 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002388 if (!IsSibcall) {
2389 Chain = DAG.getCALLSEQ_END(Chain,
2390 DAG.getIntPtrConstant(NumBytes, true),
2391 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2392 true),
2393 InFlag);
2394 InFlag = Chain.getValue(1);
2395 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002396
Chris Lattner3085e152007-02-25 08:59:22 +00002397 // Handle result values, copying them out of physregs into vregs that we
2398 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002399 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2400 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002401}
2402
Evan Cheng25ab6902006-09-08 06:48:29 +00002403
2404//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002405// Fast Calling Convention (tail call) implementation
2406//===----------------------------------------------------------------------===//
2407
2408// Like std call, callee cleans arguments, convention except that ECX is
2409// reserved for storing the tail called function address. Only 2 registers are
2410// free for argument passing (inreg). Tail call optimization is performed
2411// provided:
2412// * tailcallopt is enabled
2413// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002414// On X86_64 architecture with GOT-style position independent code only local
2415// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002416// To keep the stack aligned according to platform abi the function
2417// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2418// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002419// If a tail called function callee has more arguments than the caller the
2420// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002421// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002422// original REtADDR, but before the saved framepointer or the spilled registers
2423// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2424// stack layout:
2425// arg1
2426// arg2
2427// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002428// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002429// move area ]
2430// (possible EBP)
2431// ESI
2432// EDI
2433// local1 ..
2434
2435/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2436/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002437unsigned
2438X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2439 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002440 MachineFunction &MF = DAG.getMachineFunction();
2441 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002442 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002443 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002444 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002445 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002446 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002447 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2448 // Number smaller than 12 so just add the difference.
2449 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2450 } else {
2451 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002452 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002453 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002454 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002455 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002456}
2457
Evan Cheng5f941932010-02-05 02:21:12 +00002458/// MatchingStackOffset - Return true if the given stack call argument is
2459/// already available in the same position (relatively) of the caller's
2460/// incoming argument stack.
2461static
2462bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2463 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2464 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002465 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2466 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002467 if (Arg.getOpcode() == ISD::CopyFromReg) {
2468 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002469 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002470 return false;
2471 MachineInstr *Def = MRI->getVRegDef(VR);
2472 if (!Def)
2473 return false;
2474 if (!Flags.isByVal()) {
2475 if (!TII->isLoadFromStackSlot(Def, FI))
2476 return false;
2477 } else {
2478 unsigned Opcode = Def->getOpcode();
2479 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2480 Def->getOperand(1).isFI()) {
2481 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002482 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002483 } else
2484 return false;
2485 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002486 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2487 if (Flags.isByVal())
2488 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002489 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002490 // define @foo(%struct.X* %A) {
2491 // tail call @bar(%struct.X* byval %A)
2492 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002493 return false;
2494 SDValue Ptr = Ld->getBasePtr();
2495 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2496 if (!FINode)
2497 return false;
2498 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002499 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002500 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002501 FI = FINode->getIndex();
2502 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002503 } else
2504 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002505
Evan Cheng4cae1332010-03-05 08:38:04 +00002506 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002507 if (!MFI->isFixedObjectIndex(FI))
2508 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002509 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002510}
2511
Dan Gohman98ca4f22009-08-05 01:29:28 +00002512/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2513/// for tail call optimization. Targets which want to do tail call
2514/// optimization should implement this function.
2515bool
2516X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002517 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002518 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002519 bool isCalleeStructRet,
2520 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002521 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002522 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002523 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002525 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002526 CalleeCC != CallingConv::C)
2527 return false;
2528
Evan Cheng7096ae42010-01-29 06:45:59 +00002529 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002530 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002531 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002532 CallingConv::ID CallerCC = CallerF->getCallingConv();
2533 bool CCMatch = CallerCC == CalleeCC;
2534
Dan Gohman1797ed52010-02-08 20:27:50 +00002535 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002536 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002537 return true;
2538 return false;
2539 }
2540
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002541 // Look for obvious safe cases to perform tail call optimization that do not
2542 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002543
Evan Cheng2c12cb42010-03-26 16:26:03 +00002544 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2545 // emit a special epilogue.
2546 if (RegInfo->needsStackRealignment(MF))
2547 return false;
2548
Evan Chenga375d472010-03-15 18:54:48 +00002549 // Also avoid sibcall optimization if either caller or callee uses struct
2550 // return semantics.
2551 if (isCalleeStructRet || isCallerStructRet)
2552 return false;
2553
Chad Rosier2416da32011-06-24 21:15:36 +00002554 // An stdcall caller is expected to clean up its arguments; the callee
2555 // isn't going to do that.
2556 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2557 return false;
2558
Chad Rosier871f6642011-05-18 19:59:50 +00002559 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002560 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002561 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002562
2563 // Optimizing for varargs on Win64 is unlikely to be safe without
2564 // additional testing.
2565 if (Subtarget->isTargetWin64())
2566 return false;
2567
Chad Rosier871f6642011-05-18 19:59:50 +00002568 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002569 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2570 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002571
Chad Rosier871f6642011-05-18 19:59:50 +00002572 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2573 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2574 if (!ArgLocs[i].isRegLoc())
2575 return false;
2576 }
2577
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002578 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2579 // Therefore if it's not used by the call it is not safe to optimize this into
2580 // a sibcall.
2581 bool Unused = false;
2582 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2583 if (!Ins[i].Used) {
2584 Unused = true;
2585 break;
2586 }
2587 }
2588 if (Unused) {
2589 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002590 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2591 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002592 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002593 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002594 CCValAssign &VA = RVLocs[i];
2595 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2596 return false;
2597 }
2598 }
2599
Evan Cheng13617962010-04-30 01:12:32 +00002600 // If the calling conventions do not match, then we'd better make sure the
2601 // results are returned in the same way as what the caller expects.
2602 if (!CCMatch) {
2603 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002604 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2605 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002606 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2607
2608 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002609 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2610 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002611 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2612
2613 if (RVLocs1.size() != RVLocs2.size())
2614 return false;
2615 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2616 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2617 return false;
2618 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2619 return false;
2620 if (RVLocs1[i].isRegLoc()) {
2621 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2622 return false;
2623 } else {
2624 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2625 return false;
2626 }
2627 }
2628 }
2629
Evan Chenga6bff982010-01-30 01:22:00 +00002630 // If the callee takes no arguments then go on to check the results of the
2631 // call.
2632 if (!Outs.empty()) {
2633 // Check if stack adjustment is needed. For now, do not do this if any
2634 // argument is passed on the stack.
2635 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002636 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2637 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002638
2639 // Allocate shadow area for Win64
2640 if (Subtarget->isTargetWin64()) {
2641 CCInfo.AllocateStack(32, 8);
2642 }
2643
Duncan Sands45907662010-10-31 13:21:44 +00002644 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002645 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002646 MachineFunction &MF = DAG.getMachineFunction();
2647 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2648 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002649
2650 // Check if the arguments are already laid out in the right way as
2651 // the caller's fixed stack objects.
2652 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002653 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2654 const X86InstrInfo *TII =
2655 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002656 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2657 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002658 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002659 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002660 if (VA.getLocInfo() == CCValAssign::Indirect)
2661 return false;
2662 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002663 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2664 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002665 return false;
2666 }
2667 }
2668 }
Evan Cheng9c044672010-05-29 01:35:22 +00002669
2670 // If the tailcall address may be in a register, then make sure it's
2671 // possible to register allocate for it. In 32-bit, the call address can
2672 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002673 // callee-saved registers are restored. These happen to be the same
2674 // registers used to pass 'inreg' arguments so watch out for those.
2675 if (!Subtarget->is64Bit() &&
2676 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002677 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002678 unsigned NumInRegs = 0;
2679 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2680 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002681 if (!VA.isRegLoc())
2682 continue;
2683 unsigned Reg = VA.getLocReg();
2684 switch (Reg) {
2685 default: break;
2686 case X86::EAX: case X86::EDX: case X86::ECX:
2687 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002688 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002689 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002690 }
2691 }
2692 }
Evan Chenga6bff982010-01-30 01:22:00 +00002693 }
Evan Chengb1712452010-01-27 06:25:16 +00002694
Evan Cheng86809cc2010-02-03 03:28:02 +00002695 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002696}
2697
Dan Gohman3df24e62008-09-03 23:12:08 +00002698FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002699X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2700 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002701}
2702
2703
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002704//===----------------------------------------------------------------------===//
2705// Other Lowering Hooks
2706//===----------------------------------------------------------------------===//
2707
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002708static bool MayFoldLoad(SDValue Op) {
2709 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2710}
2711
2712static bool MayFoldIntoStore(SDValue Op) {
2713 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2714}
2715
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002716static bool isTargetShuffle(unsigned Opcode) {
2717 switch(Opcode) {
2718 default: return false;
2719 case X86ISD::PSHUFD:
2720 case X86ISD::PSHUFHW:
2721 case X86ISD::PSHUFLW:
2722 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002723 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002724 case X86ISD::SHUFPS:
2725 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002726 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002727 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002728 case X86ISD::MOVLPS:
2729 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002730 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002731 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002732 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002733 case X86ISD::MOVSS:
2734 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002735 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002736 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002737 case X86ISD::VUNPCKLPS:
2738 case X86ISD::VUNPCKLPD:
2739 case X86ISD::VUNPCKLPSY:
2740 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002741 case X86ISD::PUNPCKLWD:
2742 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002743 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002744 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002745 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002746 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002747 case X86ISD::PUNPCKHWD:
2748 case X86ISD::PUNPCKHBW:
2749 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002750 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00002751 case X86ISD::VPERMIL:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002752 return true;
2753 }
2754 return false;
2755}
2756
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002757static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002758 SDValue V1, SelectionDAG &DAG) {
2759 switch(Opc) {
2760 default: llvm_unreachable("Unknown x86 shuffle node");
2761 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002762 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002763 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002764 return DAG.getNode(Opc, dl, VT, V1);
2765 }
2766
2767 return SDValue();
2768}
2769
2770static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002771 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002772 switch(Opc) {
2773 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002774 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002775 case X86ISD::PSHUFHW:
2776 case X86ISD::PSHUFLW:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00002777 case X86ISD::VPERMIL:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002778 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2779 }
2780
2781 return SDValue();
2782}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002783
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002784static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2785 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2786 switch(Opc) {
2787 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002788 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002789 case X86ISD::SHUFPD:
2790 case X86ISD::SHUFPS:
2791 return DAG.getNode(Opc, dl, VT, V1, V2,
2792 DAG.getConstant(TargetMask, MVT::i8));
2793 }
2794 return SDValue();
2795}
2796
2797static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2798 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2799 switch(Opc) {
2800 default: llvm_unreachable("Unknown x86 shuffle node");
2801 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002802 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002803 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002804 case X86ISD::MOVLPS:
2805 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002806 case X86ISD::MOVSS:
2807 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002808 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002809 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002810 case X86ISD::VUNPCKLPS:
2811 case X86ISD::VUNPCKLPD:
2812 case X86ISD::VUNPCKLPSY:
2813 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002814 case X86ISD::PUNPCKLWD:
2815 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002816 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002817 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002818 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002819 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002820 case X86ISD::PUNPCKHWD:
2821 case X86ISD::PUNPCKHBW:
2822 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002823 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002824 return DAG.getNode(Opc, dl, VT, V1, V2);
2825 }
2826 return SDValue();
2827}
2828
Dan Gohmand858e902010-04-17 15:26:15 +00002829SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002830 MachineFunction &MF = DAG.getMachineFunction();
2831 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2832 int ReturnAddrIndex = FuncInfo->getRAIndex();
2833
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002834 if (ReturnAddrIndex == 0) {
2835 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002836 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002837 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002838 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002839 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002840 }
2841
Evan Cheng25ab6902006-09-08 06:48:29 +00002842 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002843}
2844
2845
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002846bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2847 bool hasSymbolicDisplacement) {
2848 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002849 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002850 return false;
2851
2852 // If we don't have a symbolic displacement - we don't have any extra
2853 // restrictions.
2854 if (!hasSymbolicDisplacement)
2855 return true;
2856
2857 // FIXME: Some tweaks might be needed for medium code model.
2858 if (M != CodeModel::Small && M != CodeModel::Kernel)
2859 return false;
2860
2861 // For small code model we assume that latest object is 16MB before end of 31
2862 // bits boundary. We may also accept pretty large negative constants knowing
2863 // that all objects are in the positive half of address space.
2864 if (M == CodeModel::Small && Offset < 16*1024*1024)
2865 return true;
2866
2867 // For kernel code model we know that all object resist in the negative half
2868 // of 32bits address space. We may not accept negative offsets, since they may
2869 // be just off and we may accept pretty large positive ones.
2870 if (M == CodeModel::Kernel && Offset > 0)
2871 return true;
2872
2873 return false;
2874}
2875
Evan Chengef41ff62011-06-23 17:54:54 +00002876/// isCalleePop - Determines whether the callee is required to pop its
2877/// own arguments. Callee pop is necessary to support tail calls.
2878bool X86::isCalleePop(CallingConv::ID CallingConv,
2879 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2880 if (IsVarArg)
2881 return false;
2882
2883 switch (CallingConv) {
2884 default:
2885 return false;
2886 case CallingConv::X86_StdCall:
2887 return !is64Bit;
2888 case CallingConv::X86_FastCall:
2889 return !is64Bit;
2890 case CallingConv::X86_ThisCall:
2891 return !is64Bit;
2892 case CallingConv::Fast:
2893 return TailCallOpt;
2894 case CallingConv::GHC:
2895 return TailCallOpt;
2896 }
2897}
2898
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002899/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2900/// specific condition code, returning the condition code and the LHS/RHS of the
2901/// comparison to make.
2902static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2903 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002904 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002905 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2906 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2907 // X > -1 -> X == 0, jump !sign.
2908 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002909 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002910 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2911 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002912 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002913 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002914 // X < 1 -> X <= 0
2915 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002916 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002917 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002918 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002919
Evan Chengd9558e02006-01-06 00:43:03 +00002920 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002921 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002922 case ISD::SETEQ: return X86::COND_E;
2923 case ISD::SETGT: return X86::COND_G;
2924 case ISD::SETGE: return X86::COND_GE;
2925 case ISD::SETLT: return X86::COND_L;
2926 case ISD::SETLE: return X86::COND_LE;
2927 case ISD::SETNE: return X86::COND_NE;
2928 case ISD::SETULT: return X86::COND_B;
2929 case ISD::SETUGT: return X86::COND_A;
2930 case ISD::SETULE: return X86::COND_BE;
2931 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002932 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002933 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002934
Chris Lattner4c78e022008-12-23 23:42:27 +00002935 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002936
Chris Lattner4c78e022008-12-23 23:42:27 +00002937 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002938 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2939 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002940 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2941 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002942 }
2943
Chris Lattner4c78e022008-12-23 23:42:27 +00002944 switch (SetCCOpcode) {
2945 default: break;
2946 case ISD::SETOLT:
2947 case ISD::SETOLE:
2948 case ISD::SETUGT:
2949 case ISD::SETUGE:
2950 std::swap(LHS, RHS);
2951 break;
2952 }
2953
2954 // On a floating point condition, the flags are set as follows:
2955 // ZF PF CF op
2956 // 0 | 0 | 0 | X > Y
2957 // 0 | 0 | 1 | X < Y
2958 // 1 | 0 | 0 | X == Y
2959 // 1 | 1 | 1 | unordered
2960 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002961 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002962 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002963 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002964 case ISD::SETOLT: // flipped
2965 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002966 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002967 case ISD::SETOLE: // flipped
2968 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002969 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002970 case ISD::SETUGT: // flipped
2971 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002972 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002973 case ISD::SETUGE: // flipped
2974 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002975 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002976 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002977 case ISD::SETNE: return X86::COND_NE;
2978 case ISD::SETUO: return X86::COND_P;
2979 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002980 case ISD::SETOEQ:
2981 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002982 }
Evan Chengd9558e02006-01-06 00:43:03 +00002983}
2984
Evan Cheng4a460802006-01-11 00:33:36 +00002985/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2986/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002987/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002988static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002989 switch (X86CC) {
2990 default:
2991 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002992 case X86::COND_B:
2993 case X86::COND_BE:
2994 case X86::COND_E:
2995 case X86::COND_P:
2996 case X86::COND_A:
2997 case X86::COND_AE:
2998 case X86::COND_NE:
2999 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003000 return true;
3001 }
3002}
3003
Evan Chengeb2f9692009-10-27 19:56:55 +00003004/// isFPImmLegal - Returns true if the target can instruction select the
3005/// specified FP immediate natively. If false, the legalizer will
3006/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003007bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003008 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3009 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3010 return true;
3011 }
3012 return false;
3013}
3014
Nate Begeman9008ca62009-04-27 18:41:29 +00003015/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3016/// the specified range (L, H].
3017static bool isUndefOrInRange(int Val, int Low, int Hi) {
3018 return (Val < 0) || (Val >= Low && Val < Hi);
3019}
3020
3021/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3022/// specified value.
3023static bool isUndefOrEqual(int Val, int CmpVal) {
3024 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003026 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003027}
3028
Nate Begeman9008ca62009-04-27 18:41:29 +00003029/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3030/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3031/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003032static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003033 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003034 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003035 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003036 return (Mask[0] < 2 && Mask[1] < 2);
3037 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003038}
3039
Nate Begeman9008ca62009-04-27 18:41:29 +00003040bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003041 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003042 N->getMask(M);
3043 return ::isPSHUFDMask(M, N->getValueType(0));
3044}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003045
Nate Begeman9008ca62009-04-27 18:41:29 +00003046/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3047/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003048static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003050 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003051
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 // Lower quadword copied in order or undef.
3053 for (int i = 0; i != 4; ++i)
3054 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003055 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003056
Evan Cheng506d3df2006-03-29 23:07:14 +00003057 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 for (int i = 4; i != 8; ++i)
3059 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003060 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003061
Evan Cheng506d3df2006-03-29 23:07:14 +00003062 return true;
3063}
3064
Nate Begeman9008ca62009-04-27 18:41:29 +00003065bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003066 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003067 N->getMask(M);
3068 return ::isPSHUFHWMask(M, N->getValueType(0));
3069}
Evan Cheng506d3df2006-03-29 23:07:14 +00003070
Nate Begeman9008ca62009-04-27 18:41:29 +00003071/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3072/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003073static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003074 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003075 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003076
Rafael Espindola15684b22009-04-24 12:40:33 +00003077 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003078 for (int i = 4; i != 8; ++i)
3079 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003080 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003081
Rafael Espindola15684b22009-04-24 12:40:33 +00003082 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003083 for (int i = 0; i != 4; ++i)
3084 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003085 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003086
Rafael Espindola15684b22009-04-24 12:40:33 +00003087 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003088}
3089
Nate Begeman9008ca62009-04-27 18:41:29 +00003090bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003091 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003092 N->getMask(M);
3093 return ::isPSHUFLWMask(M, N->getValueType(0));
3094}
3095
Nate Begemana09008b2009-10-19 02:17:23 +00003096/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3097/// is suitable for input to PALIGNR.
3098static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3099 bool hasSSSE3) {
3100 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003101
Nate Begemana09008b2009-10-19 02:17:23 +00003102 // Do not handle v2i64 / v2f64 shuffles with palignr.
3103 if (e < 4 || !hasSSSE3)
3104 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003105
Nate Begemana09008b2009-10-19 02:17:23 +00003106 for (i = 0; i != e; ++i)
3107 if (Mask[i] >= 0)
3108 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003109
Nate Begemana09008b2009-10-19 02:17:23 +00003110 // All undef, not a palignr.
3111 if (i == e)
3112 return false;
3113
3114 // Determine if it's ok to perform a palignr with only the LHS, since we
3115 // don't have access to the actual shuffle elements to see if RHS is undef.
3116 bool Unary = Mask[i] < (int)e;
3117 bool NeedsUnary = false;
3118
3119 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003120
Nate Begemana09008b2009-10-19 02:17:23 +00003121 // Check the rest of the elements to see if they are consecutive.
3122 for (++i; i != e; ++i) {
3123 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003124 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003125 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003126
Nate Begemana09008b2009-10-19 02:17:23 +00003127 Unary = Unary && (m < (int)e);
3128 NeedsUnary = NeedsUnary || (m < s);
3129
3130 if (NeedsUnary && !Unary)
3131 return false;
3132 if (Unary && m != ((s+i) & (e-1)))
3133 return false;
3134 if (!Unary && m != (s+i))
3135 return false;
3136 }
3137 return true;
3138}
3139
3140bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3141 SmallVector<int, 8> M;
3142 N->getMask(M);
3143 return ::isPALIGNRMask(M, N->getValueType(0), true);
3144}
3145
Evan Cheng14aed5e2006-03-24 01:18:28 +00003146/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3147/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003148static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003149 int NumElems = VT.getVectorNumElements();
3150 if (NumElems != 2 && NumElems != 4)
3151 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003152
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 int Half = NumElems / 2;
3154 for (int i = 0; i < Half; ++i)
3155 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003156 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 for (int i = Half; i < NumElems; ++i)
3158 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Evan Cheng14aed5e2006-03-24 01:18:28 +00003161 return true;
3162}
3163
Nate Begeman9008ca62009-04-27 18:41:29 +00003164bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3165 SmallVector<int, 8> M;
3166 N->getMask(M);
3167 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003168}
3169
Evan Cheng213d2cf2007-05-17 18:45:50 +00003170/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003171/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3172/// half elements to come from vector 1 (which would equal the dest.) and
3173/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003174static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003176
3177 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003179
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 int Half = NumElems / 2;
3181 for (int i = 0; i < Half; ++i)
3182 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003183 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 for (int i = Half; i < NumElems; ++i)
3185 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003186 return false;
3187 return true;
3188}
3189
Nate Begeman9008ca62009-04-27 18:41:29 +00003190static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3191 SmallVector<int, 8> M;
3192 N->getMask(M);
3193 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003194}
3195
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003196/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3197/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003198bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3199 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003200 return false;
3201
Evan Cheng2064a2b2006-03-28 06:50:32 +00003202 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3204 isUndefOrEqual(N->getMaskElt(1), 7) &&
3205 isUndefOrEqual(N->getMaskElt(2), 2) &&
3206 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003207}
3208
Nate Begeman0b10b912009-11-07 23:17:15 +00003209/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3210/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3211/// <2, 3, 2, 3>
3212bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3213 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003214
Nate Begeman0b10b912009-11-07 23:17:15 +00003215 if (NumElems != 4)
3216 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003217
Nate Begeman0b10b912009-11-07 23:17:15 +00003218 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3219 isUndefOrEqual(N->getMaskElt(1), 3) &&
3220 isUndefOrEqual(N->getMaskElt(2), 2) &&
3221 isUndefOrEqual(N->getMaskElt(3), 3);
3222}
3223
Evan Cheng5ced1d82006-04-06 23:23:56 +00003224/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3225/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003226bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3227 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003228
Evan Cheng5ced1d82006-04-06 23:23:56 +00003229 if (NumElems != 2 && NumElems != 4)
3230 return false;
3231
Evan Chengc5cdff22006-04-07 21:53:05 +00003232 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003234 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003235
Evan Chengc5cdff22006-04-07 21:53:05 +00003236 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003238 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003239
3240 return true;
3241}
3242
Nate Begeman0b10b912009-11-07 23:17:15 +00003243/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3244/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3245bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003246 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003247
David Greenea20244d2011-03-02 17:23:43 +00003248 if ((NumElems != 2 && NumElems != 4)
3249 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003250 return false;
3251
Evan Chengc5cdff22006-04-07 21:53:05 +00003252 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003254 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003255
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 for (unsigned i = 0; i < NumElems/2; ++i)
3257 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003258 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003259
3260 return true;
3261}
3262
Evan Cheng0038e592006-03-28 00:39:58 +00003263/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3264/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003265static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003266 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003268 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003269 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003270
David Greenea20244d2011-03-02 17:23:43 +00003271 // Handle vector lengths > 128 bits. Define a "section" as a set of
3272 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3273 // sections.
3274 unsigned NumSections = VT.getSizeInBits() / 128;
3275 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3276 unsigned NumSectionElts = NumElts / NumSections;
3277
3278 unsigned Start = 0;
3279 unsigned End = NumSectionElts;
3280 for (unsigned s = 0; s < NumSections; ++s) {
3281 for (unsigned i = Start, j = s * NumSectionElts;
3282 i != End;
3283 i += 2, ++j) {
3284 int BitI = Mask[i];
3285 int BitI1 = Mask[i+1];
3286 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003287 return false;
David Greenea20244d2011-03-02 17:23:43 +00003288 if (V2IsSplat) {
3289 if (!isUndefOrEqual(BitI1, NumElts))
3290 return false;
3291 } else {
3292 if (!isUndefOrEqual(BitI1, j + NumElts))
3293 return false;
3294 }
Evan Cheng39623da2006-04-20 08:58:49 +00003295 }
David Greenea20244d2011-03-02 17:23:43 +00003296 // Process the next 128 bits.
3297 Start += NumSectionElts;
3298 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003299 }
David Greenea20244d2011-03-02 17:23:43 +00003300
Evan Cheng0038e592006-03-28 00:39:58 +00003301 return true;
3302}
3303
Nate Begeman9008ca62009-04-27 18:41:29 +00003304bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3305 SmallVector<int, 8> M;
3306 N->getMask(M);
3307 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003308}
3309
Evan Cheng4fcb9222006-03-28 02:43:26 +00003310/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3311/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003312static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003313 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003314 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003315 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003316 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003317
Nate Begeman9008ca62009-04-27 18:41:29 +00003318 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3319 int BitI = Mask[i];
3320 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003321 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003322 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003323 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003324 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003325 return false;
3326 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003327 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003328 return false;
3329 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003330 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003331 return true;
3332}
3333
Nate Begeman9008ca62009-04-27 18:41:29 +00003334bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3335 SmallVector<int, 8> M;
3336 N->getMask(M);
3337 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003338}
3339
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003340/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3341/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3342/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003343static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003345 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003346 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003347
David Greenea20244d2011-03-02 17:23:43 +00003348 // Handle vector lengths > 128 bits. Define a "section" as a set of
3349 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3350 // sections.
3351 unsigned NumSections = VT.getSizeInBits() / 128;
3352 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3353 unsigned NumSectionElts = NumElems / NumSections;
3354
3355 for (unsigned s = 0; s < NumSections; ++s) {
3356 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3357 i != NumSectionElts * (s + 1);
3358 i += 2, ++j) {
3359 int BitI = Mask[i];
3360 int BitI1 = Mask[i+1];
3361
3362 if (!isUndefOrEqual(BitI, j))
3363 return false;
3364 if (!isUndefOrEqual(BitI1, j))
3365 return false;
3366 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003367 }
David Greenea20244d2011-03-02 17:23:43 +00003368
Rafael Espindola15684b22009-04-24 12:40:33 +00003369 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003370}
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3373 SmallVector<int, 8> M;
3374 N->getMask(M);
3375 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3376}
3377
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003378/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3379/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3380/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003381static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003383 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3384 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003385
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3387 int BitI = Mask[i];
3388 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003389 if (!isUndefOrEqual(BitI, j))
3390 return false;
3391 if (!isUndefOrEqual(BitI1, j))
3392 return false;
3393 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003394 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003395}
3396
Nate Begeman9008ca62009-04-27 18:41:29 +00003397bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3398 SmallVector<int, 8> M;
3399 N->getMask(M);
3400 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3401}
3402
Evan Cheng017dcc62006-04-21 01:05:10 +00003403/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3404/// specifies a shuffle of elements that is suitable for input to MOVSS,
3405/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003406static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003407 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003408 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003409
3410 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003411
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003413 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003414
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 for (int i = 1; i < NumElts; ++i)
3416 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003417 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003418
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003419 return true;
3420}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003421
Nate Begeman9008ca62009-04-27 18:41:29 +00003422bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3423 SmallVector<int, 8> M;
3424 N->getMask(M);
3425 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003426}
3427
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003428/// isVPERMILMask - Return true if the specified VECTOR_SHUFFLE operand
3429/// specifies a shuffle of elements that is suitable for input to VPERMIL*.
3430static bool isVPERMILMask(const SmallVectorImpl<int> &Mask, EVT VT) {
3431 unsigned NumElts = VT.getVectorNumElements();
3432 unsigned NumLanes = VT.getSizeInBits()/128;
3433
3434 // Match any permutation of 128-bit vector with 32/64-bit types
3435 if (NumLanes == 1) {
3436 if (NumElts == 4 || NumElts == 2)
3437 return true;
3438 return false;
3439 }
3440
3441 // Only match 256-bit with 32/64-bit types
3442 if (NumElts != 8 && NumElts != 4)
3443 return false;
3444
3445 // The mask on the high lane should be the same as the low. Actually,
3446 // they can differ if any of the corresponding index in a lane is undef.
3447 int LaneSize = NumElts/NumLanes;
3448 for (int i = 0; i < LaneSize; ++i) {
3449 int HighElt = i+LaneSize;
3450 if (Mask[i] < 0 || Mask[HighElt] < 0)
3451 continue;
3452
3453 if (Mask[HighElt]-Mask[i] != LaneSize)
3454 return false;
3455 }
3456
3457 return true;
3458}
3459
3460/// getShuffleVPERMILImmediateediate - Return the appropriate immediate to shuffle
3461/// the specified VECTOR_MASK mask with VPERMIL* instructions.
3462static unsigned getShuffleVPERMILImmediate(SDNode *N) {
3463 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3464 EVT VT = SVOp->getValueType(0);
3465
3466 int NumElts = VT.getVectorNumElements();
3467 int NumLanes = VT.getSizeInBits()/128;
3468
3469 unsigned Mask = 0;
3470 for (int i = 0; i < NumElts/NumLanes /* lane size */; ++i)
3471 Mask |= SVOp->getMaskElt(i) << (i*2);
3472
3473 return Mask;
3474}
3475
Evan Cheng017dcc62006-04-21 01:05:10 +00003476/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3477/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003478/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003479static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 bool V2IsSplat = false, bool V2IsUndef = false) {
3481 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003482 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003483 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003484
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003486 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003487
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 for (int i = 1; i < NumOps; ++i)
3489 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3490 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3491 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003492 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003493
Evan Cheng39623da2006-04-20 08:58:49 +00003494 return true;
3495}
3496
Nate Begeman9008ca62009-04-27 18:41:29 +00003497static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003498 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003499 SmallVector<int, 8> M;
3500 N->getMask(M);
3501 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003502}
3503
Evan Chengd9539472006-04-14 21:59:03 +00003504/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3505/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003506bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3507 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003508 return false;
3509
3510 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003511 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003512 int Elt = N->getMaskElt(i);
3513 if (Elt >= 0 && Elt != 1)
3514 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003515 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003516
3517 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003518 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003519 int Elt = N->getMaskElt(i);
3520 if (Elt >= 0 && Elt != 3)
3521 return false;
3522 if (Elt == 3)
3523 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003524 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003525 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003526 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003527 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003528}
3529
3530/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3531/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003532bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3533 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003534 return false;
3535
3536 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003537 for (unsigned i = 0; i < 2; ++i)
3538 if (N->getMaskElt(i) > 0)
3539 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003540
3541 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003542 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003543 int Elt = N->getMaskElt(i);
3544 if (Elt >= 0 && Elt != 2)
3545 return false;
3546 if (Elt == 2)
3547 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003548 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003549 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003550 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003551}
3552
Evan Cheng0b457f02008-09-25 20:50:48 +00003553/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3554/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003555bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3556 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003557
Nate Begeman9008ca62009-04-27 18:41:29 +00003558 for (int i = 0; i < e; ++i)
3559 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003560 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003561 for (int i = 0; i < e; ++i)
3562 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003563 return false;
3564 return true;
3565}
3566
David Greenec38a03e2011-02-03 15:50:00 +00003567/// isVEXTRACTF128Index - Return true if the specified
3568/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3569/// suitable for input to VEXTRACTF128.
3570bool X86::isVEXTRACTF128Index(SDNode *N) {
3571 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3572 return false;
3573
3574 // The index should be aligned on a 128-bit boundary.
3575 uint64_t Index =
3576 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3577
3578 unsigned VL = N->getValueType(0).getVectorNumElements();
3579 unsigned VBits = N->getValueType(0).getSizeInBits();
3580 unsigned ElSize = VBits / VL;
3581 bool Result = (Index * ElSize) % 128 == 0;
3582
3583 return Result;
3584}
3585
David Greeneccacdc12011-02-04 16:08:29 +00003586/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3587/// operand specifies a subvector insert that is suitable for input to
3588/// VINSERTF128.
3589bool X86::isVINSERTF128Index(SDNode *N) {
3590 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3591 return false;
3592
3593 // The index should be aligned on a 128-bit boundary.
3594 uint64_t Index =
3595 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3596
3597 unsigned VL = N->getValueType(0).getVectorNumElements();
3598 unsigned VBits = N->getValueType(0).getSizeInBits();
3599 unsigned ElSize = VBits / VL;
3600 bool Result = (Index * ElSize) % 128 == 0;
3601
3602 return Result;
3603}
3604
Evan Cheng63d33002006-03-22 08:01:21 +00003605/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003606/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003607unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003608 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3609 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3610
Evan Chengb9df0ca2006-03-22 02:53:00 +00003611 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3612 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003613 for (int i = 0; i < NumOperands; ++i) {
3614 int Val = SVOp->getMaskElt(NumOperands-i-1);
3615 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003616 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003617 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003618 if (i != NumOperands - 1)
3619 Mask <<= Shift;
3620 }
Evan Cheng63d33002006-03-22 08:01:21 +00003621 return Mask;
3622}
3623
Evan Cheng506d3df2006-03-29 23:07:14 +00003624/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003625/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003626unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003627 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003628 unsigned Mask = 0;
3629 // 8 nodes, but we only care about the last 4.
3630 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003631 int Val = SVOp->getMaskElt(i);
3632 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003633 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003634 if (i != 4)
3635 Mask <<= 2;
3636 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003637 return Mask;
3638}
3639
3640/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003641/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003642unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003643 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003644 unsigned Mask = 0;
3645 // 8 nodes, but we only care about the first 4.
3646 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003647 int Val = SVOp->getMaskElt(i);
3648 if (Val >= 0)
3649 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003650 if (i != 0)
3651 Mask <<= 2;
3652 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003653 return Mask;
3654}
3655
Nate Begemana09008b2009-10-19 02:17:23 +00003656/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3657/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3658unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3659 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3660 EVT VVT = N->getValueType(0);
3661 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3662 int Val = 0;
3663
3664 unsigned i, e;
3665 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3666 Val = SVOp->getMaskElt(i);
3667 if (Val >= 0)
3668 break;
3669 }
3670 return (Val - i) * EltSize;
3671}
3672
David Greenec38a03e2011-02-03 15:50:00 +00003673/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3674/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3675/// instructions.
3676unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3677 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3678 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3679
3680 uint64_t Index =
3681 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3682
3683 EVT VecVT = N->getOperand(0).getValueType();
3684 EVT ElVT = VecVT.getVectorElementType();
3685
3686 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00003687 return Index / NumElemsPerChunk;
3688}
3689
David Greeneccacdc12011-02-04 16:08:29 +00003690/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3691/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3692/// instructions.
3693unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3694 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3695 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3696
3697 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003698 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003699
3700 EVT VecVT = N->getValueType(0);
3701 EVT ElVT = VecVT.getVectorElementType();
3702
3703 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00003704 return Index / NumElemsPerChunk;
3705}
3706
Evan Cheng37b73872009-07-30 08:33:02 +00003707/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3708/// constant +0.0.
3709bool X86::isZeroNode(SDValue Elt) {
3710 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003711 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003712 (isa<ConstantFPSDNode>(Elt) &&
3713 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3714}
3715
Nate Begeman9008ca62009-04-27 18:41:29 +00003716/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3717/// their permute mask.
3718static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3719 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003720 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003721 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003722 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003723
Nate Begeman5a5ca152009-04-29 05:20:52 +00003724 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003725 int idx = SVOp->getMaskElt(i);
3726 if (idx < 0)
3727 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003728 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003729 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003730 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003731 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003732 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003733 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3734 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003735}
3736
Evan Cheng779ccea2007-12-07 21:30:01 +00003737/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3738/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003739static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003740 unsigned NumElems = VT.getVectorNumElements();
3741 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003742 int idx = Mask[i];
3743 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003744 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003745 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003746 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003747 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003748 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003749 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003750}
3751
Evan Cheng533a0aa2006-04-19 20:35:22 +00003752/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3753/// match movhlps. The lower half elements should come from upper half of
3754/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003755/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003756static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3757 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003758 return false;
3759 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003760 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003761 return false;
3762 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003763 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003764 return false;
3765 return true;
3766}
3767
Evan Cheng5ced1d82006-04-06 23:23:56 +00003768/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003769/// is promoted to a vector. It also returns the LoadSDNode by reference if
3770/// required.
3771static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003772 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3773 return false;
3774 N = N->getOperand(0).getNode();
3775 if (!ISD::isNON_EXTLoad(N))
3776 return false;
3777 if (LD)
3778 *LD = cast<LoadSDNode>(N);
3779 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003780}
3781
Evan Cheng533a0aa2006-04-19 20:35:22 +00003782/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3783/// match movlp{s|d}. The lower half elements should come from lower half of
3784/// V1 (and in order), and the upper half elements should come from the upper
3785/// half of V2 (and in order). And since V1 will become the source of the
3786/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003787static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3788 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003789 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003790 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003791 // Is V2 is a vector load, don't do this transformation. We will try to use
3792 // load folding shufps op.
3793 if (ISD::isNON_EXTLoad(V2))
3794 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003795
Nate Begeman5a5ca152009-04-29 05:20:52 +00003796 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003797
Evan Cheng533a0aa2006-04-19 20:35:22 +00003798 if (NumElems != 2 && NumElems != 4)
3799 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003800 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003801 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003802 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003803 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003804 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003805 return false;
3806 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003807}
3808
Evan Cheng39623da2006-04-20 08:58:49 +00003809/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3810/// all the same.
3811static bool isSplatVector(SDNode *N) {
3812 if (N->getOpcode() != ISD::BUILD_VECTOR)
3813 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003814
Dan Gohman475871a2008-07-27 21:46:04 +00003815 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003816 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3817 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003818 return false;
3819 return true;
3820}
3821
Evan Cheng213d2cf2007-05-17 18:45:50 +00003822/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003823/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003824/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003825static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003826 SDValue V1 = N->getOperand(0);
3827 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003828 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3829 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003830 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003831 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003832 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003833 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3834 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003835 if (Opc != ISD::BUILD_VECTOR ||
3836 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003837 return false;
3838 } else if (Idx >= 0) {
3839 unsigned Opc = V1.getOpcode();
3840 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3841 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003842 if (Opc != ISD::BUILD_VECTOR ||
3843 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003844 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003845 }
3846 }
3847 return true;
3848}
3849
3850/// getZeroVector - Returns a vector of specified type with all zero elements.
3851///
Owen Andersone50ed302009-08-10 22:56:29 +00003852static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003853 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003854 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003855
Dale Johannesen0488fb62010-09-30 23:57:10 +00003856 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003857 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003858 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003859 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003860 if (HasSSE2) { // SSE2
3861 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3862 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3863 } else { // SSE1
3864 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3865 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3866 }
3867 } else if (VT.getSizeInBits() == 256) { // AVX
3868 // 256-bit logic and arithmetic instructions in AVX are
3869 // all floating-point, no support for integer ops. Default
3870 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003872 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3873 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003874 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003875 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003876}
3877
Chris Lattner8a594482007-11-25 00:24:49 +00003878/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003879/// Always build ones vectors as <4 x i32> or <8 x i32> bitcasted to
3880/// their original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00003881static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003882 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003883 assert((VT.is128BitVector() || VT.is256BitVector())
3884 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003885
Owen Anderson825b72b2009-08-11 20:47:22 +00003886 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003887
Dan Gohman475871a2008-07-27 21:46:04 +00003888 SDValue Vec;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003889 if (VT.is256BitVector()) {
3890 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3891 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
3892 } else
3893 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003894 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003895}
3896
Evan Cheng39623da2006-04-20 08:58:49 +00003897/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3898/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003899static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003900 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003901 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003902
Evan Cheng39623da2006-04-20 08:58:49 +00003903 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 SmallVector<int, 8> MaskVec;
3905 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003906
Nate Begeman5a5ca152009-04-29 05:20:52 +00003907 for (unsigned i = 0; i != NumElems; ++i) {
3908 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 MaskVec[i] = NumElems;
3910 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003911 }
Evan Cheng39623da2006-04-20 08:58:49 +00003912 }
Evan Cheng39623da2006-04-20 08:58:49 +00003913 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003914 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3915 SVOp->getOperand(1), &MaskVec[0]);
3916 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003917}
3918
Evan Cheng017dcc62006-04-21 01:05:10 +00003919/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3920/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003921static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003922 SDValue V2) {
3923 unsigned NumElems = VT.getVectorNumElements();
3924 SmallVector<int, 8> Mask;
3925 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003926 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 Mask.push_back(i);
3928 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003929}
3930
Nate Begeman9008ca62009-04-27 18:41:29 +00003931/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003932static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003933 SDValue V2) {
3934 unsigned NumElems = VT.getVectorNumElements();
3935 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003936 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003937 Mask.push_back(i);
3938 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003939 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003940 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003941}
3942
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003943/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003944static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003945 SDValue V2) {
3946 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003947 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003949 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 Mask.push_back(i + Half);
3951 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003952 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003953 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003954}
3955
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003956// PromoteSplatv8v16 - All i16 and i8 vector types can't be used directly by
3957// a generic shuffle instruction because the target has no such instructions.
3958// Generate shuffles which repeat i16 and i8 several times until they can be
3959// represented by v4f32 and then be manipulated by target suported shuffles.
3960static SDValue PromoteSplatv8v16(SDValue V, SelectionDAG &DAG, int &EltNo) {
3961 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003963 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00003964
Nate Begeman9008ca62009-04-27 18:41:29 +00003965 while (NumElems > 4) {
3966 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003967 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003969 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00003970 EltNo -= NumElems/2;
3971 }
3972 NumElems >>= 1;
3973 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003974 return V;
3975}
Eric Christopherfd179292009-08-27 18:07:15 +00003976
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003977/// getLegalSplat - Generate a legal splat with supported x86 shuffles
3978static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
3979 EVT VT = V.getValueType();
3980 DebugLoc dl = V.getDebugLoc();
3981 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
3982 && "Vector size not supported");
3983
3984 bool Is128 = VT.getSizeInBits() == 128;
3985 EVT NVT = Is128 ? MVT::v4f32 : MVT::v8f32;
3986 V = DAG.getNode(ISD::BITCAST, dl, NVT, V);
3987
3988 if (Is128) {
3989 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
3990 V = DAG.getVectorShuffle(NVT, dl, V, DAG.getUNDEF(NVT), &SplatMask[0]);
3991 } else {
3992 // The second half of indicies refer to the higher part, which is a
3993 // duplication of the lower one. This makes this shuffle a perfect match
3994 // for the VPERM instruction.
3995 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
3996 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
3997 V = DAG.getVectorShuffle(NVT, dl, V, DAG.getUNDEF(NVT), &SplatMask[0]);
3998 }
3999
4000 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4001}
4002
4003/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32 and
4004/// v8i32, v16i16 or v32i8 to v8f32.
4005static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4006 EVT SrcVT = SV->getValueType(0);
4007 SDValue V1 = SV->getOperand(0);
4008 DebugLoc dl = SV->getDebugLoc();
4009
4010 int EltNo = SV->getSplatIndex();
4011 int NumElems = SrcVT.getVectorNumElements();
4012 unsigned Size = SrcVT.getSizeInBits();
4013
4014 // Extract the 128-bit part containing the splat element and update
4015 // the splat element index when it refers to the higher register.
4016 if (Size == 256) {
4017 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4018 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4019 if (Idx > 0)
4020 EltNo -= NumElems/2;
4021 }
4022
4023 // Make this 128-bit vector duplicate i8 and i16 elements
4024 if (NumElems > 4)
4025 V1 = PromoteSplatv8v16(V1, DAG, EltNo);
4026
4027 // Recreate the 256-bit vector and place the same 128-bit vector
4028 // into the low and high part. This is necessary because we want
4029 // to use VPERM to shuffle the v8f32 vector, and VPERM only shuffles
4030 // inside each separate v4f32 lane.
4031 if (Size == 256) {
4032 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4033 DAG.getConstant(0, MVT::i32), DAG, dl);
4034 V1 = Insert128BitVector(InsV, V1,
4035 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4036 }
4037
4038 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004039}
4040
Evan Chengba05f722006-04-21 23:03:30 +00004041/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004042/// vector of zero or undef vector. This produces a shuffle where the low
4043/// element of V2 is swizzled into the zero/undef vector, landing at element
4044/// 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 +00004045static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00004046 bool isZero, bool HasSSE2,
4047 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004048 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004049 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00004050 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4051 unsigned NumElems = VT.getVectorNumElements();
4052 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004053 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004054 // If this is the insertion idx, put the low elt of V2 here.
4055 MaskVec.push_back(i == Idx ? NumElems : i);
4056 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004057}
4058
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004059/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4060/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004061static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4062 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004063 if (Depth == 6)
4064 return SDValue(); // Limit search depth.
4065
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004066 SDValue V = SDValue(N, 0);
4067 EVT VT = V.getValueType();
4068 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004069
4070 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4071 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4072 Index = SV->getMaskElt(Index);
4073
4074 if (Index < 0)
4075 return DAG.getUNDEF(VT.getVectorElementType());
4076
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004077 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004078 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004079 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004080 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004081
4082 // Recurse into target specific vector shuffles to find scalars.
4083 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004084 int NumElems = VT.getVectorNumElements();
4085 SmallVector<unsigned, 16> ShuffleMask;
4086 SDValue ImmN;
4087
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004088 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004089 case X86ISD::SHUFPS:
4090 case X86ISD::SHUFPD:
4091 ImmN = N->getOperand(N->getNumOperands()-1);
4092 DecodeSHUFPSMask(NumElems,
4093 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4094 ShuffleMask);
4095 break;
4096 case X86ISD::PUNPCKHBW:
4097 case X86ISD::PUNPCKHWD:
4098 case X86ISD::PUNPCKHDQ:
4099 case X86ISD::PUNPCKHQDQ:
4100 DecodePUNPCKHMask(NumElems, ShuffleMask);
4101 break;
4102 case X86ISD::UNPCKHPS:
4103 case X86ISD::UNPCKHPD:
4104 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4105 break;
4106 case X86ISD::PUNPCKLBW:
4107 case X86ISD::PUNPCKLWD:
4108 case X86ISD::PUNPCKLDQ:
4109 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004110 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004111 break;
4112 case X86ISD::UNPCKLPS:
4113 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004114 case X86ISD::VUNPCKLPS:
4115 case X86ISD::VUNPCKLPD:
4116 case X86ISD::VUNPCKLPSY:
4117 case X86ISD::VUNPCKLPDY:
4118 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004119 break;
4120 case X86ISD::MOVHLPS:
4121 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4122 break;
4123 case X86ISD::MOVLHPS:
4124 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4125 break;
4126 case X86ISD::PSHUFD:
4127 ImmN = N->getOperand(N->getNumOperands()-1);
4128 DecodePSHUFMask(NumElems,
4129 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4130 ShuffleMask);
4131 break;
4132 case X86ISD::PSHUFHW:
4133 ImmN = N->getOperand(N->getNumOperands()-1);
4134 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4135 ShuffleMask);
4136 break;
4137 case X86ISD::PSHUFLW:
4138 ImmN = N->getOperand(N->getNumOperands()-1);
4139 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4140 ShuffleMask);
4141 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004142 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004143 case X86ISD::MOVSD: {
4144 // The index 0 always comes from the first element of the second source,
4145 // this is why MOVSS and MOVSD are used in the first place. The other
4146 // elements come from the other positions of the first source vector.
4147 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004148 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4149 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004150 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004151 case X86ISD::VPERMIL:
4152 ImmN = N->getOperand(N->getNumOperands()-1);
4153 DecodeVPERMILMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4154 ShuffleMask);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004155 default:
4156 assert("not implemented for target shuffle node");
4157 return SDValue();
4158 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004159
4160 Index = ShuffleMask[Index];
4161 if (Index < 0)
4162 return DAG.getUNDEF(VT.getVectorElementType());
4163
4164 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4165 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4166 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004167 }
4168
4169 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004170 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004171 V = V.getOperand(0);
4172 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004173 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004174
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004175 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004176 return SDValue();
4177 }
4178
4179 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4180 return (Index == 0) ? V.getOperand(0)
4181 : DAG.getUNDEF(VT.getVectorElementType());
4182
4183 if (V.getOpcode() == ISD::BUILD_VECTOR)
4184 return V.getOperand(Index);
4185
4186 return SDValue();
4187}
4188
4189/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4190/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004191/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004192static
4193unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4194 bool ZerosFromLeft, SelectionDAG &DAG) {
4195 int i = 0;
4196
4197 while (i < NumElems) {
4198 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004199 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004200 if (!(Elt.getNode() &&
4201 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4202 break;
4203 ++i;
4204 }
4205
4206 return i;
4207}
4208
4209/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4210/// MaskE correspond consecutively to elements from one of the vector operands,
4211/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4212static
4213bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4214 int OpIdx, int NumElems, unsigned &OpNum) {
4215 bool SeenV1 = false;
4216 bool SeenV2 = false;
4217
4218 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4219 int Idx = SVOp->getMaskElt(i);
4220 // Ignore undef indicies
4221 if (Idx < 0)
4222 continue;
4223
4224 if (Idx < NumElems)
4225 SeenV1 = true;
4226 else
4227 SeenV2 = true;
4228
4229 // Only accept consecutive elements from the same vector
4230 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4231 return false;
4232 }
4233
4234 OpNum = SeenV1 ? 0 : 1;
4235 return true;
4236}
4237
4238/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4239/// logical left shift of a vector.
4240static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4241 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4242 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4243 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4244 false /* check zeros from right */, DAG);
4245 unsigned OpSrc;
4246
4247 if (!NumZeros)
4248 return false;
4249
4250 // Considering the elements in the mask that are not consecutive zeros,
4251 // check if they consecutively come from only one of the source vectors.
4252 //
4253 // V1 = {X, A, B, C} 0
4254 // \ \ \ /
4255 // vector_shuffle V1, V2 <1, 2, 3, X>
4256 //
4257 if (!isShuffleMaskConsecutive(SVOp,
4258 0, // Mask Start Index
4259 NumElems-NumZeros-1, // Mask End Index
4260 NumZeros, // Where to start looking in the src vector
4261 NumElems, // Number of elements in vector
4262 OpSrc)) // Which source operand ?
4263 return false;
4264
4265 isLeft = false;
4266 ShAmt = NumZeros;
4267 ShVal = SVOp->getOperand(OpSrc);
4268 return true;
4269}
4270
4271/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4272/// logical left shift of a vector.
4273static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4274 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4275 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4276 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4277 true /* check zeros from left */, DAG);
4278 unsigned OpSrc;
4279
4280 if (!NumZeros)
4281 return false;
4282
4283 // Considering the elements in the mask that are not consecutive zeros,
4284 // check if they consecutively come from only one of the source vectors.
4285 //
4286 // 0 { A, B, X, X } = V2
4287 // / \ / /
4288 // vector_shuffle V1, V2 <X, X, 4, 5>
4289 //
4290 if (!isShuffleMaskConsecutive(SVOp,
4291 NumZeros, // Mask Start Index
4292 NumElems-1, // Mask End Index
4293 0, // Where to start looking in the src vector
4294 NumElems, // Number of elements in vector
4295 OpSrc)) // Which source operand ?
4296 return false;
4297
4298 isLeft = true;
4299 ShAmt = NumZeros;
4300 ShVal = SVOp->getOperand(OpSrc);
4301 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004302}
4303
4304/// isVectorShift - Returns true if the shuffle can be implemented as a
4305/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004306static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004307 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004308 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4309 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4310 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004311
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004312 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004313}
4314
Evan Chengc78d3b42006-04-24 18:01:45 +00004315/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4316///
Dan Gohman475871a2008-07-27 21:46:04 +00004317static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004318 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004319 SelectionDAG &DAG,
4320 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004321 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004322 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004323
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004324 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004325 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004326 bool First = true;
4327 for (unsigned i = 0; i < 16; ++i) {
4328 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4329 if (ThisIsNonZero && First) {
4330 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004331 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004332 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004333 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004334 First = false;
4335 }
4336
4337 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004338 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004339 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4340 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004341 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004343 }
4344 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004345 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4346 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4347 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004348 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004349 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004350 } else
4351 ThisElt = LastElt;
4352
Gabor Greifba36cb52008-08-28 21:40:38 +00004353 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004354 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004355 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004356 }
4357 }
4358
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004359 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004360}
4361
Bill Wendlinga348c562007-03-22 18:42:45 +00004362/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004363///
Dan Gohman475871a2008-07-27 21:46:04 +00004364static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004365 unsigned NumNonZero, unsigned NumZero,
4366 SelectionDAG &DAG,
4367 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004368 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004369 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004370
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004371 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004372 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004373 bool First = true;
4374 for (unsigned i = 0; i < 8; ++i) {
4375 bool isNonZero = (NonZeros & (1 << i)) != 0;
4376 if (isNonZero) {
4377 if (First) {
4378 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004379 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004380 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004381 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004382 First = false;
4383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004384 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004385 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004386 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004387 }
4388 }
4389
4390 return V;
4391}
4392
Evan Chengf26ffe92008-05-29 08:22:04 +00004393/// getVShift - Return a vector logical shift node.
4394///
Owen Andersone50ed302009-08-10 22:56:29 +00004395static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 unsigned NumBits, SelectionDAG &DAG,
4397 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004398 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004399 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004400 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4401 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004402 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004403 DAG.getConstant(NumBits,
4404 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004405}
4406
Dan Gohman475871a2008-07-27 21:46:04 +00004407SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004408X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004409 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004410
Evan Chengc3630942009-12-09 21:00:30 +00004411 // Check if the scalar load can be widened into a vector load. And if
4412 // the address is "base + cst" see if the cst can be "absorbed" into
4413 // the shuffle mask.
4414 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4415 SDValue Ptr = LD->getBasePtr();
4416 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4417 return SDValue();
4418 EVT PVT = LD->getValueType(0);
4419 if (PVT != MVT::i32 && PVT != MVT::f32)
4420 return SDValue();
4421
4422 int FI = -1;
4423 int64_t Offset = 0;
4424 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4425 FI = FINode->getIndex();
4426 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004427 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004428 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4429 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4430 Offset = Ptr.getConstantOperandVal(1);
4431 Ptr = Ptr.getOperand(0);
4432 } else {
4433 return SDValue();
4434 }
4435
4436 SDValue Chain = LD->getChain();
4437 // Make sure the stack object alignment is at least 16.
4438 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4439 if (DAG.InferPtrAlignment(Ptr) < 16) {
4440 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004441 // Can't change the alignment. FIXME: It's possible to compute
4442 // the exact stack offset and reference FI + adjust offset instead.
4443 // If someone *really* cares about this. That's the way to implement it.
4444 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004445 } else {
4446 MFI->setObjectAlignment(FI, 16);
4447 }
4448 }
4449
4450 // (Offset % 16) must be multiple of 4. Then address is then
4451 // Ptr + (Offset & ~15).
4452 if (Offset < 0)
4453 return SDValue();
4454 if ((Offset % 16) & 3)
4455 return SDValue();
4456 int64_t StartOffset = Offset & ~15;
4457 if (StartOffset)
4458 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4459 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4460
4461 int EltNo = (Offset - StartOffset) >> 2;
4462 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4463 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004464 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4465 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004466 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004467 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004468 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4469 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004470 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004471 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004472 }
4473
4474 return SDValue();
4475}
4476
Michael J. Spencerec38de22010-10-10 22:04:20 +00004477/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4478/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004479/// load which has the same value as a build_vector whose operands are 'elts'.
4480///
4481/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004482///
Nate Begeman1449f292010-03-24 22:19:06 +00004483/// FIXME: we'd also like to handle the case where the last elements are zero
4484/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4485/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004486static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004487 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004488 EVT EltVT = VT.getVectorElementType();
4489 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004490
Nate Begemanfdea31a2010-03-24 20:49:50 +00004491 LoadSDNode *LDBase = NULL;
4492 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004493
Nate Begeman1449f292010-03-24 22:19:06 +00004494 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004495 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004496 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004497 for (unsigned i = 0; i < NumElems; ++i) {
4498 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004499
Nate Begemanfdea31a2010-03-24 20:49:50 +00004500 if (!Elt.getNode() ||
4501 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4502 return SDValue();
4503 if (!LDBase) {
4504 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4505 return SDValue();
4506 LDBase = cast<LoadSDNode>(Elt.getNode());
4507 LastLoadedElt = i;
4508 continue;
4509 }
4510 if (Elt.getOpcode() == ISD::UNDEF)
4511 continue;
4512
4513 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4514 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4515 return SDValue();
4516 LastLoadedElt = i;
4517 }
Nate Begeman1449f292010-03-24 22:19:06 +00004518
4519 // If we have found an entire vector of loads and undefs, then return a large
4520 // load of the entire vector width starting at the base pointer. If we found
4521 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004522 if (LastLoadedElt == NumElems - 1) {
4523 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004524 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004525 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004526 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004527 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004528 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004529 LDBase->isVolatile(), LDBase->isNonTemporal(),
4530 LDBase->getAlignment());
4531 } else if (NumElems == 4 && LastLoadedElt == 1) {
4532 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4533 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004534 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4535 Ops, 2, MVT::i32,
4536 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004537 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004538 }
4539 return SDValue();
4540}
4541
Evan Chengc3630942009-12-09 21:00:30 +00004542SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004543X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004544 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004545
David Greenef125a292011-02-08 19:04:41 +00004546 EVT VT = Op.getValueType();
4547 EVT ExtVT = VT.getVectorElementType();
4548
4549 unsigned NumElems = Op.getNumOperands();
4550
4551 // For AVX-length vectors, build the individual 128-bit pieces and
4552 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004553 if (VT.getSizeInBits() > 256 &&
4554 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004555 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4556 SmallVector<SDValue, 8> V;
4557 V.resize(NumElems);
4558 for (unsigned i = 0; i < NumElems; ++i) {
4559 V[i] = Op.getOperand(i);
4560 }
Owen Anderson95771af2011-02-25 21:41:48 +00004561
David Greenef125a292011-02-08 19:04:41 +00004562 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4563
4564 // Build the lower subvector.
4565 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4566 // Build the upper subvector.
4567 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4568 NumElems/2);
4569
4570 return ConcatVectors(Lower, Upper, DAG);
4571 }
4572
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004573 // All zero's:
4574 // - pxor (SSE2), xorps (SSE1), vpxor (128 AVX), xorp[s|d] (256 AVX)
4575 // All one's:
4576 // - pcmpeqd (SSE2 and 128 AVX), fallback to constant pools (256 AVX)
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004577 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004578 ISD::isBuildVectorAllOnes(Op.getNode())) {
4579 // Canonicalize this to <4 x i32> or <8 x 32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004580 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4581 // eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004582 if (Op.getValueType() == MVT::v4i32 ||
4583 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004584 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004585
Gabor Greifba36cb52008-08-28 21:40:38 +00004586 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004587 return getOnesVector(Op.getValueType(), DAG, dl);
4588 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004589 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004590
Owen Andersone50ed302009-08-10 22:56:29 +00004591 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592
Evan Cheng0db9fe62006-04-25 20:13:52 +00004593 unsigned NumZero = 0;
4594 unsigned NumNonZero = 0;
4595 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004596 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004597 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004599 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004600 if (Elt.getOpcode() == ISD::UNDEF)
4601 continue;
4602 Values.insert(Elt);
4603 if (Elt.getOpcode() != ISD::Constant &&
4604 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004605 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004606 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004607 NumZero++;
4608 else {
4609 NonZeros |= (1 << i);
4610 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004611 }
4612 }
4613
Chris Lattner97a2a562010-08-26 05:24:29 +00004614 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4615 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004616 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004617
Chris Lattner67f453a2008-03-09 05:42:06 +00004618 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004619 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004620 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004621 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004622
Chris Lattner62098042008-03-09 01:05:04 +00004623 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4624 // the value are obviously zero, truncate the value to i32 and do the
4625 // insertion that way. Only do this if the value is non-constant or if the
4626 // value is a constant being inserted into element 0. It is cheaper to do
4627 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004628 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004629 (!IsAllConstants || Idx == 0)) {
4630 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004631 // Handle SSE only.
4632 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4633 EVT VecVT = MVT::v4i32;
4634 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004635
Chris Lattner62098042008-03-09 01:05:04 +00004636 // Truncate the value (which may itself be a constant) to i32, and
4637 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004638 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004639 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004640 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4641 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004642
Chris Lattner62098042008-03-09 01:05:04 +00004643 // Now we have our 32-bit value zero extended in the low element of
4644 // a vector. If Idx != 0, swizzle it into place.
4645 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 SmallVector<int, 4> Mask;
4647 Mask.push_back(Idx);
4648 for (unsigned i = 1; i != VecElts; ++i)
4649 Mask.push_back(i);
4650 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004651 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004652 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004653 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004654 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004655 }
4656 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004657
Chris Lattner19f79692008-03-08 22:59:52 +00004658 // If we have a constant or non-constant insertion into the low element of
4659 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4660 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004661 // depending on what the source datatype is.
4662 if (Idx == 0) {
4663 if (NumZero == 0) {
4664 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004665 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4666 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004667 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4668 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4669 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4670 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004671 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4672 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004673 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4674 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004675 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4676 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4677 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004678 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004679 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004680 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004681
4682 // Is it a vector logical left shift?
4683 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004684 X86::isZeroNode(Op.getOperand(0)) &&
4685 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004686 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004687 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004688 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004689 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004690 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004691 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004692
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004693 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004694 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004695
Chris Lattner19f79692008-03-08 22:59:52 +00004696 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4697 // is a non-constant being inserted into an element other than the low one,
4698 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4699 // movd/movss) to move this into the low element, then shuffle it into
4700 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004701 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004702 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004703
Evan Cheng0db9fe62006-04-25 20:13:52 +00004704 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004705 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4706 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004707 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004708 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004709 MaskVec.push_back(i == Idx ? 0 : 1);
4710 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004711 }
4712 }
4713
Chris Lattner67f453a2008-03-09 05:42:06 +00004714 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004715 if (Values.size() == 1) {
4716 if (EVTBits == 32) {
4717 // Instead of a shuffle like this:
4718 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4719 // Check if it's possible to issue this instead.
4720 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4721 unsigned Idx = CountTrailingZeros_32(NonZeros);
4722 SDValue Item = Op.getOperand(Idx);
4723 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4724 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4725 }
Dan Gohman475871a2008-07-27 21:46:04 +00004726 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004727 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004728
Dan Gohmana3941172007-07-24 22:55:08 +00004729 // A vector full of immediates; various special cases are already
4730 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004731 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004732 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004733
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004734 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004735 if (EVTBits == 64) {
4736 if (NumNonZero == 1) {
4737 // One half is zero or undef.
4738 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004739 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004740 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004741 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4742 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004743 }
Dan Gohman475871a2008-07-27 21:46:04 +00004744 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004745 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746
4747 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004748 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004749 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004750 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004751 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004752 }
4753
Bill Wendling826f36f2007-03-28 00:57:11 +00004754 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004755 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004756 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004757 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004758 }
4759
4760 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004761 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004762 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004763 if (NumElems == 4 && NumZero > 0) {
4764 for (unsigned i = 0; i < 4; ++i) {
4765 bool isZero = !(NonZeros & (1 << i));
4766 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004767 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004768 else
Dale Johannesenace16102009-02-03 19:33:06 +00004769 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004770 }
4771
4772 for (unsigned i = 0; i < 2; ++i) {
4773 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4774 default: break;
4775 case 0:
4776 V[i] = V[i*2]; // Must be a zero vector.
4777 break;
4778 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004779 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004780 break;
4781 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004782 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004783 break;
4784 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004785 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004786 break;
4787 }
4788 }
4789
Nate Begeman9008ca62009-04-27 18:41:29 +00004790 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004791 bool Reverse = (NonZeros & 0x3) == 2;
4792 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004793 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004794 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4795 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004796 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4797 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004798 }
4799
Nate Begemanfdea31a2010-03-24 20:49:50 +00004800 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4801 // Check for a build vector of consecutive loads.
4802 for (unsigned i = 0; i < NumElems; ++i)
4803 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004804
Nate Begemanfdea31a2010-03-24 20:49:50 +00004805 // Check for elements which are consecutive loads.
4806 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4807 if (LD.getNode())
4808 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004809
4810 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004811 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004812 SDValue Result;
4813 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4814 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4815 else
4816 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004817
Chris Lattner24faf612010-08-28 17:59:08 +00004818 for (unsigned i = 1; i < NumElems; ++i) {
4819 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4820 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004821 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004822 }
4823 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004824 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004825
Chris Lattner6e80e442010-08-28 17:15:43 +00004826 // Otherwise, expand into a number of unpckl*, start by extending each of
4827 // our (non-undef) elements to the full vector width with the element in the
4828 // bottom slot of the vector (which generates no code for SSE).
4829 for (unsigned i = 0; i < NumElems; ++i) {
4830 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4831 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4832 else
4833 V[i] = DAG.getUNDEF(VT);
4834 }
4835
4836 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004837 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4838 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4839 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004840 unsigned EltStride = NumElems >> 1;
4841 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004842 for (unsigned i = 0; i < EltStride; ++i) {
4843 // If V[i+EltStride] is undef and this is the first round of mixing,
4844 // then it is safe to just drop this shuffle: V[i] is already in the
4845 // right place, the one element (since it's the first round) being
4846 // inserted as undef can be dropped. This isn't safe for successive
4847 // rounds because they will permute elements within both vectors.
4848 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4849 EltStride == NumElems/2)
4850 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004851
Chris Lattner6e80e442010-08-28 17:15:43 +00004852 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004853 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004854 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004855 }
4856 return V[0];
4857 }
Dan Gohman475871a2008-07-27 21:46:04 +00004858 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004859}
4860
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004861SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004862X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004863 // We support concatenate two MMX registers and place them in a MMX
4864 // register. This is better than doing a stack convert.
4865 DebugLoc dl = Op.getDebugLoc();
4866 EVT ResVT = Op.getValueType();
4867 assert(Op.getNumOperands() == 2);
4868 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4869 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4870 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004871 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004872 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4873 InVec = Op.getOperand(1);
4874 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4875 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004876 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004877 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4878 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4879 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004880 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004881 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4882 Mask[0] = 0; Mask[1] = 2;
4883 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4884 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004885 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004886}
4887
Nate Begemanb9a47b82009-02-23 08:49:38 +00004888// v8i16 shuffles - Prefer shuffles in the following order:
4889// 1. [all] pshuflw, pshufhw, optional move
4890// 2. [ssse3] 1 x pshufb
4891// 3. [ssse3] 2 x pshufb + 1 x por
4892// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004893SDValue
4894X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4895 SelectionDAG &DAG) const {
4896 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004897 SDValue V1 = SVOp->getOperand(0);
4898 SDValue V2 = SVOp->getOperand(1);
4899 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004900 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004901
Nate Begemanb9a47b82009-02-23 08:49:38 +00004902 // Determine if more than 1 of the words in each of the low and high quadwords
4903 // of the result come from the same quadword of one of the two inputs. Undef
4904 // mask values count as coming from any quadword, for better codegen.
4905 SmallVector<unsigned, 4> LoQuad(4);
4906 SmallVector<unsigned, 4> HiQuad(4);
4907 BitVector InputQuads(4);
4908 for (unsigned i = 0; i < 8; ++i) {
4909 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004910 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004911 MaskVals.push_back(EltIdx);
4912 if (EltIdx < 0) {
4913 ++Quad[0];
4914 ++Quad[1];
4915 ++Quad[2];
4916 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004917 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004918 }
4919 ++Quad[EltIdx / 4];
4920 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004921 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004922
Nate Begemanb9a47b82009-02-23 08:49:38 +00004923 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004924 unsigned MaxQuad = 1;
4925 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004926 if (LoQuad[i] > MaxQuad) {
4927 BestLoQuad = i;
4928 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004929 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004930 }
4931
Nate Begemanb9a47b82009-02-23 08:49:38 +00004932 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004933 MaxQuad = 1;
4934 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004935 if (HiQuad[i] > MaxQuad) {
4936 BestHiQuad = i;
4937 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004938 }
4939 }
4940
Nate Begemanb9a47b82009-02-23 08:49:38 +00004941 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004942 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004943 // single pshufb instruction is necessary. If There are more than 2 input
4944 // quads, disable the next transformation since it does not help SSSE3.
4945 bool V1Used = InputQuads[0] || InputQuads[1];
4946 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004947 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004948 if (InputQuads.count() == 2 && V1Used && V2Used) {
4949 BestLoQuad = InputQuads.find_first();
4950 BestHiQuad = InputQuads.find_next(BestLoQuad);
4951 }
4952 if (InputQuads.count() > 2) {
4953 BestLoQuad = -1;
4954 BestHiQuad = -1;
4955 }
4956 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004957
Nate Begemanb9a47b82009-02-23 08:49:38 +00004958 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4959 // the shuffle mask. If a quad is scored as -1, that means that it contains
4960 // words from all 4 input quadwords.
4961 SDValue NewV;
4962 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004963 SmallVector<int, 8> MaskV;
4964 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4965 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004966 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004967 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4968 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4969 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004970
Nate Begemanb9a47b82009-02-23 08:49:38 +00004971 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4972 // source words for the shuffle, to aid later transformations.
4973 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004974 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004975 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004976 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004977 if (idx != (int)i)
4978 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004979 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004980 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004981 AllWordsInNewV = false;
4982 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004983 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004984
Nate Begemanb9a47b82009-02-23 08:49:38 +00004985 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4986 if (AllWordsInNewV) {
4987 for (int i = 0; i != 8; ++i) {
4988 int idx = MaskVals[i];
4989 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004990 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004991 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004992 if ((idx != i) && idx < 4)
4993 pshufhw = false;
4994 if ((idx != i) && idx > 3)
4995 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004996 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004997 V1 = NewV;
4998 V2Used = false;
4999 BestLoQuad = 0;
5000 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005001 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005002
Nate Begemanb9a47b82009-02-23 08:49:38 +00005003 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5004 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005005 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005006 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5007 unsigned TargetMask = 0;
5008 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005009 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005010 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5011 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5012 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005013 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005014 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005015 }
Eric Christopherfd179292009-08-27 18:07:15 +00005016
Nate Begemanb9a47b82009-02-23 08:49:38 +00005017 // If we have SSSE3, and all words of the result are from 1 input vector,
5018 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5019 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005020 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005021 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005022
Nate Begemanb9a47b82009-02-23 08:49:38 +00005023 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005024 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005025 // mask, and elements that come from V1 in the V2 mask, so that the two
5026 // results can be OR'd together.
5027 bool TwoInputs = V1Used && V2Used;
5028 for (unsigned i = 0; i != 8; ++i) {
5029 int EltIdx = MaskVals[i] * 2;
5030 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005031 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5032 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 continue;
5034 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005035 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5036 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005037 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005038 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005039 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005040 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005041 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005042 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005043 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005044
Nate Begemanb9a47b82009-02-23 08:49:38 +00005045 // Calculate the shuffle mask for the second input, shuffle it, and
5046 // OR it with the first shuffled input.
5047 pshufbMask.clear();
5048 for (unsigned i = 0; i != 8; ++i) {
5049 int EltIdx = MaskVals[i] * 2;
5050 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005051 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5052 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005053 continue;
5054 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005055 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5056 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005057 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005058 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005059 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005060 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005061 MVT::v16i8, &pshufbMask[0], 16));
5062 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005063 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005064 }
5065
5066 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5067 // and update MaskVals with new element order.
5068 BitVector InOrder(8);
5069 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005070 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005071 for (int i = 0; i != 4; ++i) {
5072 int idx = MaskVals[i];
5073 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005074 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005075 InOrder.set(i);
5076 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005077 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005078 InOrder.set(i);
5079 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005080 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005081 }
5082 }
5083 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005084 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005085 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005086 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005087
5088 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5089 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5090 NewV.getOperand(0),
5091 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5092 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005093 }
Eric Christopherfd179292009-08-27 18:07:15 +00005094
Nate Begemanb9a47b82009-02-23 08:49:38 +00005095 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5096 // and update MaskVals with the new element order.
5097 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005098 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005099 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005100 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005101 for (unsigned i = 4; i != 8; ++i) {
5102 int idx = MaskVals[i];
5103 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005104 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005105 InOrder.set(i);
5106 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005107 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005108 InOrder.set(i);
5109 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005110 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005111 }
5112 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005113 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005114 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005115
5116 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5117 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5118 NewV.getOperand(0),
5119 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5120 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005121 }
Eric Christopherfd179292009-08-27 18:07:15 +00005122
Nate Begemanb9a47b82009-02-23 08:49:38 +00005123 // In case BestHi & BestLo were both -1, which means each quadword has a word
5124 // from each of the four input quadwords, calculate the InOrder bitvector now
5125 // before falling through to the insert/extract cleanup.
5126 if (BestLoQuad == -1 && BestHiQuad == -1) {
5127 NewV = V1;
5128 for (int i = 0; i != 8; ++i)
5129 if (MaskVals[i] < 0 || MaskVals[i] == i)
5130 InOrder.set(i);
5131 }
Eric Christopherfd179292009-08-27 18:07:15 +00005132
Nate Begemanb9a47b82009-02-23 08:49:38 +00005133 // The other elements are put in the right place using pextrw and pinsrw.
5134 for (unsigned i = 0; i != 8; ++i) {
5135 if (InOrder[i])
5136 continue;
5137 int EltIdx = MaskVals[i];
5138 if (EltIdx < 0)
5139 continue;
5140 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005141 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005142 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005144 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005145 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005146 DAG.getIntPtrConstant(i));
5147 }
5148 return NewV;
5149}
5150
5151// v16i8 shuffles - Prefer shuffles in the following order:
5152// 1. [ssse3] 1 x pshufb
5153// 2. [ssse3] 2 x pshufb + 1 x por
5154// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5155static
Nate Begeman9008ca62009-04-27 18:41:29 +00005156SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005157 SelectionDAG &DAG,
5158 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005159 SDValue V1 = SVOp->getOperand(0);
5160 SDValue V2 = SVOp->getOperand(1);
5161 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005162 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005163 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005164
Nate Begemanb9a47b82009-02-23 08:49:38 +00005165 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005166 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005167 // present, fall back to case 3.
5168 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5169 bool V1Only = true;
5170 bool V2Only = true;
5171 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005172 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005173 if (EltIdx < 0)
5174 continue;
5175 if (EltIdx < 16)
5176 V2Only = false;
5177 else
5178 V1Only = false;
5179 }
Eric Christopherfd179292009-08-27 18:07:15 +00005180
Nate Begemanb9a47b82009-02-23 08:49:38 +00005181 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5182 if (TLI.getSubtarget()->hasSSSE3()) {
5183 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005184
Nate Begemanb9a47b82009-02-23 08:49:38 +00005185 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005186 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005187 //
5188 // Otherwise, we have elements from both input vectors, and must zero out
5189 // elements that come from V2 in the first mask, and V1 in the second mask
5190 // so that we can OR them together.
5191 bool TwoInputs = !(V1Only || V2Only);
5192 for (unsigned i = 0; i != 16; ++i) {
5193 int EltIdx = MaskVals[i];
5194 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005195 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005196 continue;
5197 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005198 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005199 }
5200 // If all the elements are from V2, assign it to V1 and return after
5201 // building the first pshufb.
5202 if (V2Only)
5203 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005204 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005205 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005206 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005207 if (!TwoInputs)
5208 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005209
Nate Begemanb9a47b82009-02-23 08:49:38 +00005210 // Calculate the shuffle mask for the second input, shuffle it, and
5211 // OR it with the first shuffled input.
5212 pshufbMask.clear();
5213 for (unsigned i = 0; i != 16; ++i) {
5214 int EltIdx = MaskVals[i];
5215 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005216 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005217 continue;
5218 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005219 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005220 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005221 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005222 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005223 MVT::v16i8, &pshufbMask[0], 16));
5224 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005225 }
Eric Christopherfd179292009-08-27 18:07:15 +00005226
Nate Begemanb9a47b82009-02-23 08:49:38 +00005227 // No SSSE3 - Calculate in place words and then fix all out of place words
5228 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5229 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005230 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5231 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005232 SDValue NewV = V2Only ? V2 : V1;
5233 for (int i = 0; i != 8; ++i) {
5234 int Elt0 = MaskVals[i*2];
5235 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005236
Nate Begemanb9a47b82009-02-23 08:49:38 +00005237 // This word of the result is all undef, skip it.
5238 if (Elt0 < 0 && Elt1 < 0)
5239 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005240
Nate Begemanb9a47b82009-02-23 08:49:38 +00005241 // This word of the result is already in the correct place, skip it.
5242 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5243 continue;
5244 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5245 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005246
Nate Begemanb9a47b82009-02-23 08:49:38 +00005247 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5248 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5249 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005250
5251 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5252 // using a single extract together, load it and store it.
5253 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005254 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005255 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005256 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005257 DAG.getIntPtrConstant(i));
5258 continue;
5259 }
5260
Nate Begemanb9a47b82009-02-23 08:49:38 +00005261 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005262 // source byte is not also odd, shift the extracted word left 8 bits
5263 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005264 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005265 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005266 DAG.getIntPtrConstant(Elt1 / 2));
5267 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005268 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005269 DAG.getConstant(8,
5270 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005271 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005272 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5273 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005274 }
5275 // If Elt0 is defined, extract it from the appropriate source. If the
5276 // source byte is not also even, shift the extracted word right 8 bits. If
5277 // Elt1 was also defined, OR the extracted values together before
5278 // inserting them in the result.
5279 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005280 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005281 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5282 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005283 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005284 DAG.getConstant(8,
5285 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005286 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005287 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5288 DAG.getConstant(0x00FF, MVT::i16));
5289 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005290 : InsElt0;
5291 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005292 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005293 DAG.getIntPtrConstant(i));
5294 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005295 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005296}
5297
Evan Cheng7a831ce2007-12-15 03:00:47 +00005298/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005299/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005300/// done when every pair / quad of shuffle mask elements point to elements in
5301/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005302/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005303static
Nate Begeman9008ca62009-04-27 18:41:29 +00005304SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005305 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005306 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005307 SDValue V1 = SVOp->getOperand(0);
5308 SDValue V2 = SVOp->getOperand(1);
5309 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005310 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005311 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005312 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005313 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005314 case MVT::v4f32: NewVT = MVT::v2f64; break;
5315 case MVT::v4i32: NewVT = MVT::v2i64; break;
5316 case MVT::v8i16: NewVT = MVT::v4i32; break;
5317 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005318 }
5319
Nate Begeman9008ca62009-04-27 18:41:29 +00005320 int Scale = NumElems / NewWidth;
5321 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005322 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005323 int StartIdx = -1;
5324 for (int j = 0; j < Scale; ++j) {
5325 int EltIdx = SVOp->getMaskElt(i+j);
5326 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005327 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005328 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005329 StartIdx = EltIdx - (EltIdx % Scale);
5330 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005331 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005332 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005333 if (StartIdx == -1)
5334 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005335 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005336 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005337 }
5338
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005339 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5340 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005341 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005342}
5343
Evan Chengd880b972008-05-09 21:53:03 +00005344/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005345///
Owen Andersone50ed302009-08-10 22:56:29 +00005346static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005347 SDValue SrcOp, SelectionDAG &DAG,
5348 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005349 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005350 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005351 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005352 LD = dyn_cast<LoadSDNode>(SrcOp);
5353 if (!LD) {
5354 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5355 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005356 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005357 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005358 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005359 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005360 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005361 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005362 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005363 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005364 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5365 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5366 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005367 SrcOp.getOperand(0)
5368 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005369 }
5370 }
5371 }
5372
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005373 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005374 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005375 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005376 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005377}
5378
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005379/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5380/// which could not be matched by any known target speficic shuffle
5381static SDValue
5382LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5383 return SDValue();
5384}
5385
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005386/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5387/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005388static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005389LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005390 SDValue V1 = SVOp->getOperand(0);
5391 SDValue V2 = SVOp->getOperand(1);
5392 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005393 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005394
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005395 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5396
Evan Chengace3c172008-07-22 21:13:36 +00005397 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005398 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005399 SmallVector<int, 8> Mask1(4U, -1);
5400 SmallVector<int, 8> PermMask;
5401 SVOp->getMask(PermMask);
5402
Evan Chengace3c172008-07-22 21:13:36 +00005403 unsigned NumHi = 0;
5404 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005405 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005406 int Idx = PermMask[i];
5407 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005408 Locs[i] = std::make_pair(-1, -1);
5409 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005410 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5411 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005412 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005413 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005414 NumLo++;
5415 } else {
5416 Locs[i] = std::make_pair(1, NumHi);
5417 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005418 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005419 NumHi++;
5420 }
5421 }
5422 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005423
Evan Chengace3c172008-07-22 21:13:36 +00005424 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005425 // If no more than two elements come from either vector. This can be
5426 // implemented with two shuffles. First shuffle gather the elements.
5427 // The second shuffle, which takes the first shuffle as both of its
5428 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005429 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005430
Nate Begeman9008ca62009-04-27 18:41:29 +00005431 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005432
Evan Chengace3c172008-07-22 21:13:36 +00005433 for (unsigned i = 0; i != 4; ++i) {
5434 if (Locs[i].first == -1)
5435 continue;
5436 else {
5437 unsigned Idx = (i < 2) ? 0 : 4;
5438 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005439 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005440 }
5441 }
5442
Nate Begeman9008ca62009-04-27 18:41:29 +00005443 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005444 } else if (NumLo == 3 || NumHi == 3) {
5445 // Otherwise, we must have three elements from one vector, call it X, and
5446 // one element from the other, call it Y. First, use a shufps to build an
5447 // intermediate vector with the one element from Y and the element from X
5448 // that will be in the same half in the final destination (the indexes don't
5449 // matter). Then, use a shufps to build the final vector, taking the half
5450 // containing the element from Y from the intermediate, and the other half
5451 // from X.
5452 if (NumHi == 3) {
5453 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005454 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005455 std::swap(V1, V2);
5456 }
5457
5458 // Find the element from V2.
5459 unsigned HiIndex;
5460 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005461 int Val = PermMask[HiIndex];
5462 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005463 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005464 if (Val >= 4)
5465 break;
5466 }
5467
Nate Begeman9008ca62009-04-27 18:41:29 +00005468 Mask1[0] = PermMask[HiIndex];
5469 Mask1[1] = -1;
5470 Mask1[2] = PermMask[HiIndex^1];
5471 Mask1[3] = -1;
5472 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005473
5474 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005475 Mask1[0] = PermMask[0];
5476 Mask1[1] = PermMask[1];
5477 Mask1[2] = HiIndex & 1 ? 6 : 4;
5478 Mask1[3] = HiIndex & 1 ? 4 : 6;
5479 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005480 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005481 Mask1[0] = HiIndex & 1 ? 2 : 0;
5482 Mask1[1] = HiIndex & 1 ? 0 : 2;
5483 Mask1[2] = PermMask[2];
5484 Mask1[3] = PermMask[3];
5485 if (Mask1[2] >= 0)
5486 Mask1[2] += 4;
5487 if (Mask1[3] >= 0)
5488 Mask1[3] += 4;
5489 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005490 }
Evan Chengace3c172008-07-22 21:13:36 +00005491 }
5492
5493 // Break it into (shuffle shuffle_hi, shuffle_lo).
5494 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005495 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005496 SmallVector<int,8> LoMask(4U, -1);
5497 SmallVector<int,8> HiMask(4U, -1);
5498
5499 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005500 unsigned MaskIdx = 0;
5501 unsigned LoIdx = 0;
5502 unsigned HiIdx = 2;
5503 for (unsigned i = 0; i != 4; ++i) {
5504 if (i == 2) {
5505 MaskPtr = &HiMask;
5506 MaskIdx = 1;
5507 LoIdx = 0;
5508 HiIdx = 2;
5509 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005510 int Idx = PermMask[i];
5511 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005512 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005513 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005514 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005516 LoIdx++;
5517 } else {
5518 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005519 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005520 HiIdx++;
5521 }
5522 }
5523
Nate Begeman9008ca62009-04-27 18:41:29 +00005524 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5525 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5526 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005527 for (unsigned i = 0; i != 4; ++i) {
5528 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005529 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005530 } else {
5531 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005532 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005533 }
5534 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005535 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005536}
5537
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005538static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005539 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005540 V = V.getOperand(0);
5541 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5542 V = V.getOperand(0);
5543 if (MayFoldLoad(V))
5544 return true;
5545 return false;
5546}
5547
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005548// FIXME: the version above should always be used. Since there's
5549// a bug where several vector shuffles can't be folded because the
5550// DAG is not updated during lowering and a node claims to have two
5551// uses while it only has one, use this version, and let isel match
5552// another instruction if the load really happens to have more than
5553// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005554// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005555static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005556 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005557 V = V.getOperand(0);
5558 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5559 V = V.getOperand(0);
5560 if (ISD::isNormalLoad(V.getNode()))
5561 return true;
5562 return false;
5563}
5564
5565/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5566/// a vector extract, and if both can be later optimized into a single load.
5567/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5568/// here because otherwise a target specific shuffle node is going to be
5569/// emitted for this shuffle, and the optimization not done.
5570/// FIXME: This is probably not the best approach, but fix the problem
5571/// until the right path is decided.
5572static
5573bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5574 const TargetLowering &TLI) {
5575 EVT VT = V.getValueType();
5576 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5577
5578 // Be sure that the vector shuffle is present in a pattern like this:
5579 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5580 if (!V.hasOneUse())
5581 return false;
5582
5583 SDNode *N = *V.getNode()->use_begin();
5584 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5585 return false;
5586
5587 SDValue EltNo = N->getOperand(1);
5588 if (!isa<ConstantSDNode>(EltNo))
5589 return false;
5590
5591 // If the bit convert changed the number of elements, it is unsafe
5592 // to examine the mask.
5593 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005594 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005595 EVT SrcVT = V.getOperand(0).getValueType();
5596 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5597 return false;
5598 V = V.getOperand(0);
5599 HasShuffleIntoBitcast = true;
5600 }
5601
5602 // Select the input vector, guarding against out of range extract vector.
5603 unsigned NumElems = VT.getVectorNumElements();
5604 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5605 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5606 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5607
5608 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005609 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005610 V = V.getOperand(0);
5611
5612 if (ISD::isNormalLoad(V.getNode())) {
5613 // Is the original load suitable?
5614 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5615
5616 // FIXME: avoid the multi-use bug that is preventing lots of
5617 // of foldings to be detected, this is still wrong of course, but
5618 // give the temporary desired behavior, and if it happens that
5619 // the load has real more uses, during isel it will not fold, and
5620 // will generate poor code.
5621 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5622 return false;
5623
5624 if (!HasShuffleIntoBitcast)
5625 return true;
5626
5627 // If there's a bitcast before the shuffle, check if the load type and
5628 // alignment is valid.
5629 unsigned Align = LN0->getAlignment();
5630 unsigned NewAlign =
5631 TLI.getTargetData()->getABITypeAlignment(
5632 VT.getTypeForEVT(*DAG.getContext()));
5633
5634 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5635 return false;
5636 }
5637
5638 return true;
5639}
5640
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005641static
Evan Cheng835580f2010-10-07 20:50:20 +00005642SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5643 EVT VT = Op.getValueType();
5644
5645 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005646 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5647 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005648 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5649 V1, DAG));
5650}
5651
5652static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005653SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5654 bool HasSSE2) {
5655 SDValue V1 = Op.getOperand(0);
5656 SDValue V2 = Op.getOperand(1);
5657 EVT VT = Op.getValueType();
5658
5659 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5660
5661 if (HasSSE2 && VT == MVT::v2f64)
5662 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5663
5664 // v4f32 or v4i32
5665 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5666}
5667
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005668static
5669SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5670 SDValue V1 = Op.getOperand(0);
5671 SDValue V2 = Op.getOperand(1);
5672 EVT VT = Op.getValueType();
5673
5674 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5675 "unsupported shuffle type");
5676
5677 if (V2.getOpcode() == ISD::UNDEF)
5678 V2 = V1;
5679
5680 // v4i32 or v4f32
5681 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5682}
5683
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005684static
5685SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5686 SDValue V1 = Op.getOperand(0);
5687 SDValue V2 = Op.getOperand(1);
5688 EVT VT = Op.getValueType();
5689 unsigned NumElems = VT.getVectorNumElements();
5690
5691 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5692 // operand of these instructions is only memory, so check if there's a
5693 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5694 // same masks.
5695 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005696
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005697 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005698 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005699 CanFoldLoad = true;
5700
5701 // When V1 is a load, it can be folded later into a store in isel, example:
5702 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5703 // turns into:
5704 // (MOVLPSmr addr:$src1, VR128:$src2)
5705 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005706 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005707 CanFoldLoad = true;
5708
Eric Christopher893a8822011-02-20 05:04:42 +00005709 // Both of them can't be memory operations though.
5710 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5711 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005712
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005713 if (CanFoldLoad) {
5714 if (HasSSE2 && NumElems == 2)
5715 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5716
5717 if (NumElems == 4)
5718 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5719 }
5720
5721 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5722 // movl and movlp will both match v2i64, but v2i64 is never matched by
5723 // movl earlier because we make it strict to avoid messing with the movlp load
5724 // folding logic (see the code above getMOVLP call). Match it here then,
5725 // this is horrible, but will stay like this until we move all shuffle
5726 // matching to x86 specific nodes. Note that for the 1st condition all
5727 // types are matched with movsd.
5728 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5729 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5730 else if (HasSSE2)
5731 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5732
5733
5734 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5735
5736 // Invert the operand order and use SHUFPS to match it.
5737 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5738 X86::getShuffleSHUFImmediate(SVOp), DAG);
5739}
5740
David Greenec4db4e52011-02-28 19:06:56 +00005741static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005742 switch(VT.getSimpleVT().SimpleTy) {
5743 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5744 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005745 case MVT::v4f32:
5746 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5747 case MVT::v2f64:
5748 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5749 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5750 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005751 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5752 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5753 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005754 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005755 }
5756 return 0;
5757}
5758
5759static inline unsigned getUNPCKHOpcode(EVT VT) {
5760 switch(VT.getSimpleVT().SimpleTy) {
5761 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5762 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5763 case MVT::v4f32: return X86ISD::UNPCKHPS;
5764 case MVT::v2f64: return X86ISD::UNPCKHPD;
5765 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5766 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5767 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005768 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005769 }
5770 return 0;
5771}
5772
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005773static
5774SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005775 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005776 const X86Subtarget *Subtarget) {
5777 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5778 EVT VT = Op.getValueType();
5779 DebugLoc dl = Op.getDebugLoc();
5780 SDValue V1 = Op.getOperand(0);
5781 SDValue V2 = Op.getOperand(1);
5782
5783 if (isZeroShuffle(SVOp))
5784 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5785
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005786 // Handle splat operations
5787 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00005788 unsigned NumElem = VT.getVectorNumElements();
5789 // Special case, this is the only place now where it's allowed to return
5790 // a vector_shuffle operation without using a target specific node, because
5791 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
5792 // this be moved to DAGCombine instead?
5793 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005794 return Op;
5795
5796 // Handle splats by matching through known masks
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00005797 if ((VT.is128BitVector() && NumElem <= 4) ||
5798 (VT.is256BitVector() && NumElem <= 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005799 return SDValue();
5800
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00005801 // All i16 and i8 vector types can't be used directly by a generic shuffle
5802 // instruction because the target has no such instruction. Generate shuffles
5803 // which repeat i16 and i8 several times until they fit in i32, and then can
5804 // be manipulated by target suported shuffles. After the insertion of the
5805 // necessary shuffles, the result is bitcasted back to v4f32 or v8f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005806 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005807 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005808
5809 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5810 // do it!
5811 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5812 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5813 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005814 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005815 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5816 // FIXME: Figure out a cleaner way to do this.
5817 // Try to make use of movq to zero out the top part.
5818 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5819 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5820 if (NewOp.getNode()) {
5821 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5822 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5823 DAG, Subtarget, dl);
5824 }
5825 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5826 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5827 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5828 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5829 DAG, Subtarget, dl);
5830 }
5831 }
5832 return SDValue();
5833}
5834
Dan Gohman475871a2008-07-27 21:46:04 +00005835SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005836X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005837 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005838 SDValue V1 = Op.getOperand(0);
5839 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005840 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005841 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005842 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005843 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005844 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5845 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005846 bool V1IsSplat = false;
5847 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005848 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005849 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005850 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005851 MachineFunction &MF = DAG.getMachineFunction();
5852 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005853
Dale Johannesen0488fb62010-09-30 23:57:10 +00005854 // Shuffle operations on MMX not supported.
5855 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005856 return Op;
5857
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005858 // Vector shuffle lowering takes 3 steps:
5859 //
5860 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5861 // narrowing and commutation of operands should be handled.
5862 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5863 // shuffle nodes.
5864 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5865 // so the shuffle can be broken into other shuffles and the legalizer can
5866 // try the lowering again.
5867 //
5868 // The general ideia is that no vector_shuffle operation should be left to
5869 // be matched during isel, all of them must be converted to a target specific
5870 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005871
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005872 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5873 // narrowing and commutation of operands should be handled. The actual code
5874 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005875 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005876 if (NewOp.getNode())
5877 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005878
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005879 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5880 // unpckh_undef). Only use pshufd if speed is more important than size.
5881 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5882 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005883 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005884 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5885 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5886 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005887
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005888 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005889 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005890 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005891
Dale Johannesen0488fb62010-09-30 23:57:10 +00005892 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005893 return getMOVHighToLow(Op, dl, DAG);
5894
5895 // Use to match splats
5896 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5897 (VT == MVT::v2f64 || VT == MVT::v2i64))
5898 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5899
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005900 if (X86::isPSHUFDMask(SVOp)) {
5901 // The actual implementation will match the mask in the if above and then
5902 // during isel it can match several different instructions, not only pshufd
5903 // as its name says, sad but true, emulate the behavior for now...
5904 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5905 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5906
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005907 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5908
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005909 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005910 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5911
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005912 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005913 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5914 TargetMask, DAG);
5915
5916 if (VT == MVT::v4f32)
5917 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5918 TargetMask, DAG);
5919 }
Eric Christopherfd179292009-08-27 18:07:15 +00005920
Evan Chengf26ffe92008-05-29 08:22:04 +00005921 // Check if this can be converted into a logical shift.
5922 bool isLeft = false;
5923 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005924 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005925 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005926 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005927 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005928 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005929 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005930 EVT EltVT = VT.getVectorElementType();
5931 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005932 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005933 }
Eric Christopherfd179292009-08-27 18:07:15 +00005934
Nate Begeman9008ca62009-04-27 18:41:29 +00005935 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005936 if (V1IsUndef)
5937 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005938 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005939 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005940 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005941 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005942 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5943
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005944 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005945 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5946 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005947 }
Eric Christopherfd179292009-08-27 18:07:15 +00005948
Nate Begeman9008ca62009-04-27 18:41:29 +00005949 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005950 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5951 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005952
Dale Johannesen0488fb62010-09-30 23:57:10 +00005953 if (X86::isMOVHLPSMask(SVOp))
5954 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005955
Dale Johannesen0488fb62010-09-30 23:57:10 +00005956 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5957 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005958
Dale Johannesen0488fb62010-09-30 23:57:10 +00005959 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5960 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005961
Dale Johannesen0488fb62010-09-30 23:57:10 +00005962 if (X86::isMOVLPMask(SVOp))
5963 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005964
Nate Begeman9008ca62009-04-27 18:41:29 +00005965 if (ShouldXformToMOVHLPS(SVOp) ||
5966 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5967 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005968
Evan Chengf26ffe92008-05-29 08:22:04 +00005969 if (isShift) {
5970 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005971 EVT EltVT = VT.getVectorElementType();
5972 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005973 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005974 }
Eric Christopherfd179292009-08-27 18:07:15 +00005975
Evan Cheng9eca5e82006-10-25 21:49:50 +00005976 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005977 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5978 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005979 V1IsSplat = isSplatVector(V1.getNode());
5980 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005981
Chris Lattner8a594482007-11-25 00:24:49 +00005982 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005983 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005984 Op = CommuteVectorShuffle(SVOp, DAG);
5985 SVOp = cast<ShuffleVectorSDNode>(Op);
5986 V1 = SVOp->getOperand(0);
5987 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005988 std::swap(V1IsSplat, V2IsSplat);
5989 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005990 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005991 }
5992
Nate Begeman9008ca62009-04-27 18:41:29 +00005993 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5994 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005995 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005996 return V1;
5997 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5998 // the instruction selector will not match, so get a canonical MOVL with
5999 // swapped operands to undo the commute.
6000 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006001 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006002
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006003 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00006004 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
6005 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006006
6007 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006008 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006009
Evan Cheng9bbbb982006-10-25 20:48:19 +00006010 if (V2IsSplat) {
6011 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006012 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006013 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006014 SDValue NewMask = NormalizeMask(SVOp, DAG);
6015 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6016 if (NSVOp != SVOp) {
6017 if (X86::isUNPCKLMask(NSVOp, true)) {
6018 return NewMask;
6019 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6020 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006021 }
6022 }
6023 }
6024
Evan Cheng9eca5e82006-10-25 21:49:50 +00006025 if (Commuted) {
6026 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006027 // FIXME: this seems wrong.
6028 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6029 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006030
6031 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00006032 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
6033 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006034
6035 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006036 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006037 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006038
Nate Begeman9008ca62009-04-27 18:41:29 +00006039 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006040 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006041 return CommuteVectorShuffle(SVOp, DAG);
6042
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006043 // The checks below are all present in isShuffleMaskLegal, but they are
6044 // inlined here right now to enable us to directly emit target specific
6045 // nodes, and remove one by one until they don't return Op anymore.
6046 SmallVector<int, 16> M;
6047 SVOp->getMask(M);
6048
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006049 if (isPALIGNRMask(M, VT, HasSSSE3))
6050 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6051 X86::getShufflePALIGNRImmediate(SVOp),
6052 DAG);
6053
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006054 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6055 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00006056 if (VT == MVT::v2f64) {
6057 X86ISD::NodeType Opcode =
6058 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
6059 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
6060 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006061 if (VT == MVT::v2i64)
6062 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6063 }
6064
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006065 if (isPSHUFHWMask(M, VT))
6066 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6067 X86::getShufflePSHUFHWImmediate(SVOp),
6068 DAG);
6069
6070 if (isPSHUFLWMask(M, VT))
6071 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6072 X86::getShufflePSHUFLWImmediate(SVOp),
6073 DAG);
6074
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006075 if (isSHUFPMask(M, VT)) {
6076 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6077 if (VT == MVT::v4f32 || VT == MVT::v4i32)
6078 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
6079 TargetMask, DAG);
6080 if (VT == MVT::v2f64 || VT == MVT::v2i64)
6081 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
6082 TargetMask, DAG);
6083 }
6084
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006085 if (X86::isUNPCKL_v_undef_Mask(SVOp))
6086 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00006087 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
6088 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006089 if (X86::isUNPCKH_v_undef_Mask(SVOp))
6090 if (VT != MVT::v2i64 && VT != MVT::v2f64)
6091 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6092
Evan Cheng14b32e12007-12-11 01:46:18 +00006093 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00006094 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00006095 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006096 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00006097 return NewOp;
6098 }
6099
Owen Anderson825b72b2009-08-11 20:47:22 +00006100 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006101 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006102 if (NewOp.getNode())
6103 return NewOp;
6104 }
Eric Christopherfd179292009-08-27 18:07:15 +00006105
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006106 // Handle all 128-bit wide vectors with 4 elements, and match them with
6107 // several different shuffle types.
6108 if (NumElems == 4 && VT.getSizeInBits() == 128)
6109 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006110
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006111 //===--------------------------------------------------------------------===//
6112 // Custom lower or generate target specific nodes for 256-bit shuffles.
6113
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006114 // Handle VPERMIL permutations
6115 if (isVPERMILMask(M, VT)) {
6116 unsigned TargetMask = getShuffleVPERMILImmediate(SVOp);
6117 if (VT == MVT::v8f32)
6118 return getTargetShuffleNode(X86ISD::VPERMIL, dl, VT, V1, TargetMask, DAG);
6119 }
6120
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006121 // Handle general 256-bit shuffles
6122 if (VT.is256BitVector())
6123 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6124
Dan Gohman475871a2008-07-27 21:46:04 +00006125 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006126}
6127
Dan Gohman475871a2008-07-27 21:46:04 +00006128SDValue
6129X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006130 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006131 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006132 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006133 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006134 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006135 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006136 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006137 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006138 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006139 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006140 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6141 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6142 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006143 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6144 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006145 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006146 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006147 Op.getOperand(0)),
6148 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006149 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006150 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006151 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006152 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006153 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006154 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006155 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6156 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006157 // result has a single use which is a store or a bitcast to i32. And in
6158 // the case of a store, it's not worth it if the index is a constant 0,
6159 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006160 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006161 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006162 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006163 if ((User->getOpcode() != ISD::STORE ||
6164 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6165 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006166 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006167 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006168 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006169 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006170 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006171 Op.getOperand(0)),
6172 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006173 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006174 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006175 // ExtractPS works with constant index.
6176 if (isa<ConstantSDNode>(Op.getOperand(1)))
6177 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006178 }
Dan Gohman475871a2008-07-27 21:46:04 +00006179 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006180}
6181
6182
Dan Gohman475871a2008-07-27 21:46:04 +00006183SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006184X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6185 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006186 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006187 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006188
David Greene74a579d2011-02-10 16:57:36 +00006189 SDValue Vec = Op.getOperand(0);
6190 EVT VecVT = Vec.getValueType();
6191
6192 // If this is a 256-bit vector result, first extract the 128-bit
6193 // vector and then extract from the 128-bit vector.
6194 if (VecVT.getSizeInBits() > 128) {
6195 DebugLoc dl = Op.getNode()->getDebugLoc();
6196 unsigned NumElems = VecVT.getVectorNumElements();
6197 SDValue Idx = Op.getOperand(1);
6198
6199 if (!isa<ConstantSDNode>(Idx))
6200 return SDValue();
6201
6202 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6203 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6204
6205 // Get the 128-bit vector.
6206 bool Upper = IdxVal >= ExtractNumElems;
6207 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6208
6209 // Extract from it.
6210 SDValue ScaledIdx = Idx;
6211 if (Upper)
6212 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6213 DAG.getConstant(ExtractNumElems,
6214 Idx.getValueType()));
6215 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6216 ScaledIdx);
6217 }
6218
6219 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6220
Evan Cheng62a3f152008-03-24 21:52:23 +00006221 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006222 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006223 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006224 return Res;
6225 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006226
Owen Andersone50ed302009-08-10 22:56:29 +00006227 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006228 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006229 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006230 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006231 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006232 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006233 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006234 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6235 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006236 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006237 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006238 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006239 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006240 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006241 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006242 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006243 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006244 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006245 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006246 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006247 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006248 if (Idx == 0)
6249 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006250
Evan Cheng0db9fe62006-04-25 20:13:52 +00006251 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006252 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006253 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006254 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006255 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006256 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006257 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006258 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006259 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6260 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6261 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006262 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006263 if (Idx == 0)
6264 return Op;
6265
6266 // UNPCKHPD the element to the lowest double word, then movsd.
6267 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6268 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006269 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006270 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006271 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006272 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006273 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006274 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006275 }
6276
Dan Gohman475871a2008-07-27 21:46:04 +00006277 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006278}
6279
Dan Gohman475871a2008-07-27 21:46:04 +00006280SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006281X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6282 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006283 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006284 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006285 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006286
Dan Gohman475871a2008-07-27 21:46:04 +00006287 SDValue N0 = Op.getOperand(0);
6288 SDValue N1 = Op.getOperand(1);
6289 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006290
Dan Gohman8a55ce42009-09-23 21:02:20 +00006291 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006292 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006293 unsigned Opc;
6294 if (VT == MVT::v8i16)
6295 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006296 else if (VT == MVT::v16i8)
6297 Opc = X86ISD::PINSRB;
6298 else
6299 Opc = X86ISD::PINSRB;
6300
Nate Begeman14d12ca2008-02-11 04:19:36 +00006301 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6302 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006303 if (N1.getValueType() != MVT::i32)
6304 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6305 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006306 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006307 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006308 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006309 // Bits [7:6] of the constant are the source select. This will always be
6310 // zero here. The DAG Combiner may combine an extract_elt index into these
6311 // bits. For example (insert (extract, 3), 2) could be matched by putting
6312 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006313 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006314 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006315 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006316 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006317 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006318 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006319 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006320 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006321 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006322 // PINSR* works with constant index.
6323 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006324 }
Dan Gohman475871a2008-07-27 21:46:04 +00006325 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006326}
6327
Dan Gohman475871a2008-07-27 21:46:04 +00006328SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006329X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006330 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006331 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006332
David Greene6b381262011-02-09 15:32:06 +00006333 DebugLoc dl = Op.getDebugLoc();
6334 SDValue N0 = Op.getOperand(0);
6335 SDValue N1 = Op.getOperand(1);
6336 SDValue N2 = Op.getOperand(2);
6337
6338 // If this is a 256-bit vector result, first insert into a 128-bit
6339 // vector and then insert into the 256-bit vector.
6340 if (VT.getSizeInBits() > 128) {
6341 if (!isa<ConstantSDNode>(N2))
6342 return SDValue();
6343
6344 // Get the 128-bit vector.
6345 unsigned NumElems = VT.getVectorNumElements();
6346 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6347 bool Upper = IdxVal >= NumElems / 2;
6348
6349 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6350
6351 // Insert into it.
6352 SDValue ScaledN2 = N2;
6353 if (Upper)
6354 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006355 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006356 (VT.getSizeInBits() / 128),
6357 N2.getValueType()));
6358 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6359 N1, ScaledN2);
6360
6361 // Insert the 128-bit vector
6362 // FIXME: Why UNDEF?
6363 return Insert128BitVector(N0, Op, N2, DAG, dl);
6364 }
6365
Nate Begeman14d12ca2008-02-11 04:19:36 +00006366 if (Subtarget->hasSSE41())
6367 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6368
Dan Gohman8a55ce42009-09-23 21:02:20 +00006369 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006370 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006371
Dan Gohman8a55ce42009-09-23 21:02:20 +00006372 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006373 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6374 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006375 if (N1.getValueType() != MVT::i32)
6376 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6377 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006378 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006379 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006380 }
Dan Gohman475871a2008-07-27 21:46:04 +00006381 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006382}
6383
Dan Gohman475871a2008-07-27 21:46:04 +00006384SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006385X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006386 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006387 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006388 EVT OpVT = Op.getValueType();
6389
6390 // If this is a 256-bit vector result, first insert into a 128-bit
6391 // vector and then insert into the 256-bit vector.
6392 if (OpVT.getSizeInBits() > 128) {
6393 // Insert into a 128-bit vector.
6394 EVT VT128 = EVT::getVectorVT(*Context,
6395 OpVT.getVectorElementType(),
6396 OpVT.getVectorNumElements() / 2);
6397
6398 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6399
6400 // Insert the 128-bit vector.
6401 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6402 DAG.getConstant(0, MVT::i32),
6403 DAG, dl);
6404 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006405
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006406 if (Op.getValueType() == MVT::v1i64 &&
6407 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006408 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006409
Owen Anderson825b72b2009-08-11 20:47:22 +00006410 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006411 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6412 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006413 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006414 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006415}
6416
David Greene91585092011-01-26 15:38:49 +00006417// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6418// a simple subregister reference or explicit instructions to grab
6419// upper bits of a vector.
6420SDValue
6421X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6422 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006423 DebugLoc dl = Op.getNode()->getDebugLoc();
6424 SDValue Vec = Op.getNode()->getOperand(0);
6425 SDValue Idx = Op.getNode()->getOperand(1);
6426
6427 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6428 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6429 return Extract128BitVector(Vec, Idx, DAG, dl);
6430 }
David Greene91585092011-01-26 15:38:49 +00006431 }
6432 return SDValue();
6433}
6434
David Greenecfe33c42011-01-26 19:13:22 +00006435// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6436// simple superregister reference or explicit instructions to insert
6437// the upper bits of a vector.
6438SDValue
6439X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6440 if (Subtarget->hasAVX()) {
6441 DebugLoc dl = Op.getNode()->getDebugLoc();
6442 SDValue Vec = Op.getNode()->getOperand(0);
6443 SDValue SubVec = Op.getNode()->getOperand(1);
6444 SDValue Idx = Op.getNode()->getOperand(2);
6445
6446 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6447 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006448 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006449 }
6450 }
6451 return SDValue();
6452}
6453
Bill Wendling056292f2008-09-16 21:48:12 +00006454// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6455// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6456// one of the above mentioned nodes. It has to be wrapped because otherwise
6457// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6458// be used to form addressing mode. These wrapped nodes will be selected
6459// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006460SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006461X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006462 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006463
Chris Lattner41621a22009-06-26 19:22:52 +00006464 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6465 // global base reg.
6466 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006467 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006468 CodeModel::Model M = getTargetMachine().getCodeModel();
6469
Chris Lattner4f066492009-07-11 20:29:19 +00006470 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006471 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006472 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006473 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006474 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006475 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006476 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006477
Evan Cheng1606e8e2009-03-13 07:51:59 +00006478 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006479 CP->getAlignment(),
6480 CP->getOffset(), OpFlag);
6481 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006482 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006483 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006484 if (OpFlag) {
6485 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006486 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006487 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006488 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006489 }
6490
6491 return Result;
6492}
6493
Dan Gohmand858e902010-04-17 15:26:15 +00006494SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006495 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006496
Chris Lattner18c59872009-06-27 04:16:01 +00006497 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6498 // global base reg.
6499 unsigned char OpFlag = 0;
6500 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006501 CodeModel::Model M = getTargetMachine().getCodeModel();
6502
Chris Lattner4f066492009-07-11 20:29:19 +00006503 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006504 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006505 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006506 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006507 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006508 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006509 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006510
Chris Lattner18c59872009-06-27 04:16:01 +00006511 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6512 OpFlag);
6513 DebugLoc DL = JT->getDebugLoc();
6514 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006515
Chris Lattner18c59872009-06-27 04:16:01 +00006516 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006517 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006518 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6519 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006520 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006521 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006522
Chris Lattner18c59872009-06-27 04:16:01 +00006523 return Result;
6524}
6525
6526SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006527X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006528 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006529
Chris Lattner18c59872009-06-27 04:16:01 +00006530 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6531 // global base reg.
6532 unsigned char OpFlag = 0;
6533 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006534 CodeModel::Model M = getTargetMachine().getCodeModel();
6535
Chris Lattner4f066492009-07-11 20:29:19 +00006536 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006537 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006538 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006539 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006540 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006541 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006542 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006543
Chris Lattner18c59872009-06-27 04:16:01 +00006544 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006545
Chris Lattner18c59872009-06-27 04:16:01 +00006546 DebugLoc DL = Op.getDebugLoc();
6547 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006548
6549
Chris Lattner18c59872009-06-27 04:16:01 +00006550 // With PIC, the address is actually $g + Offset.
6551 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006552 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006553 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6554 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006555 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006556 Result);
6557 }
Eric Christopherfd179292009-08-27 18:07:15 +00006558
Chris Lattner18c59872009-06-27 04:16:01 +00006559 return Result;
6560}
6561
Dan Gohman475871a2008-07-27 21:46:04 +00006562SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006563X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006564 // Create the TargetBlockAddressAddress node.
6565 unsigned char OpFlags =
6566 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006567 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006568 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006569 DebugLoc dl = Op.getDebugLoc();
6570 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6571 /*isTarget=*/true, OpFlags);
6572
Dan Gohmanf705adb2009-10-30 01:28:02 +00006573 if (Subtarget->isPICStyleRIPRel() &&
6574 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006575 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6576 else
6577 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006578
Dan Gohman29cbade2009-11-20 23:18:13 +00006579 // With PIC, the address is actually $g + Offset.
6580 if (isGlobalRelativeToPICBase(OpFlags)) {
6581 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6582 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6583 Result);
6584 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006585
6586 return Result;
6587}
6588
6589SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006590X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006591 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006592 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006593 // Create the TargetGlobalAddress node, folding in the constant
6594 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006595 unsigned char OpFlags =
6596 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006597 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006598 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006599 if (OpFlags == X86II::MO_NO_FLAG &&
6600 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006601 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006602 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006603 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006604 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006605 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006606 }
Eric Christopherfd179292009-08-27 18:07:15 +00006607
Chris Lattner4f066492009-07-11 20:29:19 +00006608 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006609 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006610 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6611 else
6612 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006613
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006614 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006615 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006616 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6617 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006618 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006619 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006620
Chris Lattner36c25012009-07-10 07:34:39 +00006621 // For globals that require a load from a stub to get the address, emit the
6622 // load.
6623 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006624 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006625 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006626
Dan Gohman6520e202008-10-18 02:06:02 +00006627 // If there was a non-zero offset that we didn't fold, create an explicit
6628 // addition for it.
6629 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006630 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006631 DAG.getConstant(Offset, getPointerTy()));
6632
Evan Cheng0db9fe62006-04-25 20:13:52 +00006633 return Result;
6634}
6635
Evan Chengda43bcf2008-09-24 00:05:32 +00006636SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006637X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006638 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006639 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006640 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006641}
6642
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006643static SDValue
6644GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006645 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006646 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006647 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006648 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006649 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006650 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006651 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006652 GA->getOffset(),
6653 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006654 if (InFlag) {
6655 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006656 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006657 } else {
6658 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006659 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006660 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006661
6662 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006663 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006664
Rafael Espindola15f1b662009-04-24 12:59:40 +00006665 SDValue Flag = Chain.getValue(1);
6666 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006667}
6668
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006669// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006670static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006671LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006672 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006673 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006674 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6675 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006676 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006677 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006678 InFlag = Chain.getValue(1);
6679
Chris Lattnerb903bed2009-06-26 21:20:29 +00006680 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006681}
6682
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006683// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006684static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006685LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006686 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006687 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6688 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006689}
6690
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006691// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6692// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006693static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006694 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006695 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006696 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006697
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006698 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6699 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6700 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006701
Michael J. Spencerec38de22010-10-10 22:04:20 +00006702 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006703 DAG.getIntPtrConstant(0),
6704 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006705
Chris Lattnerb903bed2009-06-26 21:20:29 +00006706 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006707 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6708 // initialexec.
6709 unsigned WrapperKind = X86ISD::Wrapper;
6710 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006711 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006712 } else if (is64Bit) {
6713 assert(model == TLSModel::InitialExec);
6714 OperandFlags = X86II::MO_GOTTPOFF;
6715 WrapperKind = X86ISD::WrapperRIP;
6716 } else {
6717 assert(model == TLSModel::InitialExec);
6718 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006719 }
Eric Christopherfd179292009-08-27 18:07:15 +00006720
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006721 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6722 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006723 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006724 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006725 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006726 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006727
Rafael Espindola9a580232009-02-27 13:37:18 +00006728 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006729 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006730 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006731
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006732 // The address of the thread local variable is the add of the thread
6733 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006734 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006735}
6736
Dan Gohman475871a2008-07-27 21:46:04 +00006737SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006738X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006739
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006740 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006741 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006742
Eric Christopher30ef0e52010-06-03 04:07:48 +00006743 if (Subtarget->isTargetELF()) {
6744 // TODO: implement the "local dynamic" model
6745 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006746
Eric Christopher30ef0e52010-06-03 04:07:48 +00006747 // If GV is an alias then use the aliasee for determining
6748 // thread-localness.
6749 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6750 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006751
6752 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006753 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006754
Eric Christopher30ef0e52010-06-03 04:07:48 +00006755 switch (model) {
6756 case TLSModel::GeneralDynamic:
6757 case TLSModel::LocalDynamic: // not implemented
6758 if (Subtarget->is64Bit())
6759 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6760 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006761
Eric Christopher30ef0e52010-06-03 04:07:48 +00006762 case TLSModel::InitialExec:
6763 case TLSModel::LocalExec:
6764 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6765 Subtarget->is64Bit());
6766 }
6767 } else if (Subtarget->isTargetDarwin()) {
6768 // Darwin only has one model of TLS. Lower to that.
6769 unsigned char OpFlag = 0;
6770 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6771 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006772
Eric Christopher30ef0e52010-06-03 04:07:48 +00006773 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6774 // global base reg.
6775 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6776 !Subtarget->is64Bit();
6777 if (PIC32)
6778 OpFlag = X86II::MO_TLVP_PIC_BASE;
6779 else
6780 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006781 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006782 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006783 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006784 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006785 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006786
Eric Christopher30ef0e52010-06-03 04:07:48 +00006787 // With PIC32, the address is actually $g + Offset.
6788 if (PIC32)
6789 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6790 DAG.getNode(X86ISD::GlobalBaseReg,
6791 DebugLoc(), getPointerTy()),
6792 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006793
Eric Christopher30ef0e52010-06-03 04:07:48 +00006794 // Lowering the machine isd will make sure everything is in the right
6795 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006796 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006797 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006798 SDValue Args[] = { Chain, Offset };
6799 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006800
Eric Christopher30ef0e52010-06-03 04:07:48 +00006801 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6802 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6803 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006804
Eric Christopher30ef0e52010-06-03 04:07:48 +00006805 // And our return value (tls address) is in the standard call return value
6806 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006807 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6808 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006809 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006810
Eric Christopher30ef0e52010-06-03 04:07:48 +00006811 assert(false &&
6812 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006813
Torok Edwinc23197a2009-07-14 16:55:14 +00006814 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006815 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006816}
6817
Evan Cheng0db9fe62006-04-25 20:13:52 +00006818
Nadav Rotem43012222011-05-11 08:12:09 +00006819/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006820/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006821SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006822 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006823 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006824 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006825 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006826 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006827 SDValue ShOpLo = Op.getOperand(0);
6828 SDValue ShOpHi = Op.getOperand(1);
6829 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006830 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006831 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006832 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006833
Dan Gohman475871a2008-07-27 21:46:04 +00006834 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006835 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006836 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6837 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006838 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006839 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6840 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006841 }
Evan Chenge3413162006-01-09 18:33:28 +00006842
Owen Anderson825b72b2009-08-11 20:47:22 +00006843 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6844 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006845 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006846 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006847
Dan Gohman475871a2008-07-27 21:46:04 +00006848 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006849 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006850 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6851 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006852
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006853 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006854 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6855 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006856 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006857 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6858 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006859 }
6860
Dan Gohman475871a2008-07-27 21:46:04 +00006861 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006862 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006863}
Evan Chenga3195e82006-01-12 22:54:21 +00006864
Dan Gohmand858e902010-04-17 15:26:15 +00006865SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6866 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006867 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006868
Dale Johannesen0488fb62010-09-30 23:57:10 +00006869 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006870 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006871
Owen Anderson825b72b2009-08-11 20:47:22 +00006872 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006873 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006874
Eli Friedman36df4992009-05-27 00:47:34 +00006875 // These are really Legal; return the operand so the caller accepts it as
6876 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006877 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006878 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006879 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006880 Subtarget->is64Bit()) {
6881 return Op;
6882 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006883
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006884 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006885 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006886 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006887 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006888 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006889 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006890 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006891 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006892 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006893 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6894}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006895
Owen Andersone50ed302009-08-10 22:56:29 +00006896SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006897 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006898 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006899 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006900 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006901 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006902 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006903 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006904 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006905 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006906 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006907
Chris Lattner492a43e2010-09-22 01:28:21 +00006908 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006909
Stuart Hastings84be9582011-06-02 15:57:11 +00006910 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6911 MachineMemOperand *MMO;
6912 if (FI) {
6913 int SSFI = FI->getIndex();
6914 MMO =
6915 DAG.getMachineFunction()
6916 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6917 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6918 } else {
6919 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6920 StackSlot = StackSlot.getOperand(1);
6921 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006922 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006923 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6924 X86ISD::FILD, DL,
6925 Tys, Ops, array_lengthof(Ops),
6926 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006927
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006928 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006929 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006930 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006931
6932 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6933 // shouldn't be necessary except that RFP cannot be live across
6934 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006935 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006936 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6937 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006938 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006939 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006940 SDValue Ops[] = {
6941 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6942 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006943 MachineMemOperand *MMO =
6944 DAG.getMachineFunction()
6945 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006946 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006947
Chris Lattner492a43e2010-09-22 01:28:21 +00006948 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6949 Ops, array_lengthof(Ops),
6950 Op.getValueType(), MMO);
6951 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006952 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006953 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006954 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006955
Evan Cheng0db9fe62006-04-25 20:13:52 +00006956 return Result;
6957}
6958
Bill Wendling8b8a6362009-01-17 03:56:04 +00006959// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006960SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6961 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006962 // This algorithm is not obvious. Here it is in C code, more or less:
6963 /*
6964 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6965 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6966 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006967
Bill Wendling8b8a6362009-01-17 03:56:04 +00006968 // Copy ints to xmm registers.
6969 __m128i xh = _mm_cvtsi32_si128( hi );
6970 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006971
Bill Wendling8b8a6362009-01-17 03:56:04 +00006972 // Combine into low half of a single xmm register.
6973 __m128i x = _mm_unpacklo_epi32( xh, xl );
6974 __m128d d;
6975 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006976
Bill Wendling8b8a6362009-01-17 03:56:04 +00006977 // Merge in appropriate exponents to give the integer bits the right
6978 // magnitude.
6979 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006980
Bill Wendling8b8a6362009-01-17 03:56:04 +00006981 // Subtract away the biases to deal with the IEEE-754 double precision
6982 // implicit 1.
6983 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006984
Bill Wendling8b8a6362009-01-17 03:56:04 +00006985 // All conversions up to here are exact. The correctly rounded result is
6986 // calculated using the current rounding mode using the following
6987 // horizontal add.
6988 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6989 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6990 // store doesn't really need to be here (except
6991 // maybe to zero the other double)
6992 return sd;
6993 }
6994 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006995
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006996 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006997 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006998
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006999 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007000 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007001 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7002 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7003 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7004 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007005 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007006 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007007
Bill Wendling8b8a6362009-01-17 03:56:04 +00007008 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007009 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007010 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007011 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007012 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007013 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007014 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007015
Owen Anderson825b72b2009-08-11 20:47:22 +00007016 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7017 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007018 Op.getOperand(0),
7019 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007020 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7021 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007022 Op.getOperand(0),
7023 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007024 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7025 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007026 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007027 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007028 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007029 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007030 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007031 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007032 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007034
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007035 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007036 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007037 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7038 DAG.getUNDEF(MVT::v2f64), ShufMask);
7039 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7040 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007041 DAG.getIntPtrConstant(0));
7042}
7043
Bill Wendling8b8a6362009-01-17 03:56:04 +00007044// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007045SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7046 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007047 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007048 // FP constant to bias correct the final result.
7049 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007051
7052 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007053 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7054 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00007055 Op.getOperand(0),
7056 DAG.getIntPtrConstant(0)));
7057
Owen Anderson825b72b2009-08-11 20:47:22 +00007058 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007059 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007060 DAG.getIntPtrConstant(0));
7061
7062 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007064 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007065 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007067 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007068 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007069 MVT::v2f64, Bias)));
7070 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007071 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007072 DAG.getIntPtrConstant(0));
7073
7074 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007075 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007076
7077 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007078 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007079
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007081 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007082 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007083 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007084 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007085 }
7086
7087 // Handle final rounding.
7088 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007089}
7090
Dan Gohmand858e902010-04-17 15:26:15 +00007091SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7092 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007093 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007094 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007095
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007096 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007097 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7098 // the optimization here.
7099 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007100 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007101
Owen Andersone50ed302009-08-10 22:56:29 +00007102 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007103 EVT DstVT = Op.getValueType();
7104 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007105 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007106 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007107 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007108
7109 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007110 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007111 if (SrcVT == MVT::i32) {
7112 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7113 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7114 getPointerTy(), StackSlot, WordOff);
7115 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007116 StackSlot, MachinePointerInfo(),
7117 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007118 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007119 OffsetSlot, MachinePointerInfo(),
7120 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007121 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7122 return Fild;
7123 }
7124
7125 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7126 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007127 StackSlot, MachinePointerInfo(),
7128 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007129 // For i64 source, we need to add the appropriate power of 2 if the input
7130 // was negative. This is the same as the optimization in
7131 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7132 // we must be careful to do the computation in x87 extended precision, not
7133 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007134 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7135 MachineMemOperand *MMO =
7136 DAG.getMachineFunction()
7137 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7138 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007139
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007140 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7141 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007142 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7143 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007144
7145 APInt FF(32, 0x5F800000ULL);
7146
7147 // Check whether the sign bit is set.
7148 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7149 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7150 ISD::SETLT);
7151
7152 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7153 SDValue FudgePtr = DAG.getConstantPool(
7154 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7155 getPointerTy());
7156
7157 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7158 SDValue Zero = DAG.getIntPtrConstant(0);
7159 SDValue Four = DAG.getIntPtrConstant(4);
7160 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7161 Zero, Four);
7162 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7163
7164 // Load the value out, extending it from f32 to f80.
7165 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007166 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007167 FudgePtr, MachinePointerInfo::getConstantPool(),
7168 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007169 // Extend everything to 80 bits to force it to be done on x87.
7170 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7171 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007172}
7173
Dan Gohman475871a2008-07-27 21:46:04 +00007174std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007175FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007176 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007177
Owen Andersone50ed302009-08-10 22:56:29 +00007178 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007179
7180 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007181 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7182 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007183 }
7184
Owen Anderson825b72b2009-08-11 20:47:22 +00007185 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7186 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007187 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007188
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007189 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007190 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007191 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007192 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007193 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007194 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007195 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007196 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007197
Evan Cheng87c89352007-10-15 20:11:21 +00007198 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7199 // stack slot.
7200 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007201 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007202 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007203 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007204
Michael J. Spencerec38de22010-10-10 22:04:20 +00007205
7206
Evan Cheng0db9fe62006-04-25 20:13:52 +00007207 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007208 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007209 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007210 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7211 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7212 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007213 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007214
Dan Gohman475871a2008-07-27 21:46:04 +00007215 SDValue Chain = DAG.getEntryNode();
7216 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007217 EVT TheVT = Op.getOperand(0).getValueType();
7218 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007219 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007220 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007221 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007222 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007223 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007224 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007225 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007226 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007227
Chris Lattner492a43e2010-09-22 01:28:21 +00007228 MachineMemOperand *MMO =
7229 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7230 MachineMemOperand::MOLoad, MemSize, MemSize);
7231 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7232 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007233 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007234 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007235 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7236 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007237
Chris Lattner07290932010-09-22 01:05:16 +00007238 MachineMemOperand *MMO =
7239 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7240 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007241
Evan Cheng0db9fe62006-04-25 20:13:52 +00007242 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007243 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007244 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7245 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007246
Chris Lattner27a6c732007-11-24 07:07:01 +00007247 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007248}
7249
Dan Gohmand858e902010-04-17 15:26:15 +00007250SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7251 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007252 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007253 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007254
Eli Friedman948e95a2009-05-23 09:59:16 +00007255 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007256 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007257 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7258 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007259
Chris Lattner27a6c732007-11-24 07:07:01 +00007260 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007261 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007262 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007263}
7264
Dan Gohmand858e902010-04-17 15:26:15 +00007265SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7266 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007267 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7268 SDValue FIST = Vals.first, StackSlot = Vals.second;
7269 assert(FIST.getNode() && "Unexpected failure");
7270
7271 // Load the result.
7272 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007273 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007274}
7275
Dan Gohmand858e902010-04-17 15:26:15 +00007276SDValue X86TargetLowering::LowerFABS(SDValue Op,
7277 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007278 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007279 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007280 EVT VT = Op.getValueType();
7281 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007282 if (VT.isVector())
7283 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007284 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007285 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007286 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007287 CV.push_back(C);
7288 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007289 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007290 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007291 CV.push_back(C);
7292 CV.push_back(C);
7293 CV.push_back(C);
7294 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007295 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007296 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007297 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007298 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007299 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007300 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007301 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007302}
7303
Dan Gohmand858e902010-04-17 15:26:15 +00007304SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007305 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007306 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007307 EVT VT = Op.getValueType();
7308 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007309 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007310 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007311 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007312 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007313 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007314 CV.push_back(C);
7315 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007316 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007317 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007318 CV.push_back(C);
7319 CV.push_back(C);
7320 CV.push_back(C);
7321 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007322 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007323 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007324 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007325 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007326 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007327 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007328 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007329 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007330 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007331 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007332 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007333 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007334 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007335 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007336 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007337}
7338
Dan Gohmand858e902010-04-17 15:26:15 +00007339SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007340 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007341 SDValue Op0 = Op.getOperand(0);
7342 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007343 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007344 EVT VT = Op.getValueType();
7345 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007346
7347 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007348 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007349 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007350 SrcVT = VT;
7351 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007352 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007353 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007354 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007355 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007356 }
7357
7358 // At this point the operands and the result should have the same
7359 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007360
Evan Cheng68c47cb2007-01-05 07:55:56 +00007361 // First get the sign bit of second operand.
7362 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007363 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007364 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7365 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007366 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007367 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7368 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7369 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7370 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007371 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007372 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007373 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007374 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007375 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007376 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007377 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007378
7379 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007380 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007381 // Op0 is MVT::f32, Op1 is MVT::f64.
7382 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7383 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7384 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007385 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007386 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007387 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007388 }
7389
Evan Cheng73d6cf12007-01-05 21:37:56 +00007390 // Clear first operand sign bit.
7391 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007392 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007393 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7394 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007395 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007396 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7397 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7398 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7399 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007400 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007401 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007402 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007403 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007404 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007405 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007406 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007407
7408 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007409 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007410}
7411
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007412SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7413 SDValue N0 = Op.getOperand(0);
7414 DebugLoc dl = Op.getDebugLoc();
7415 EVT VT = Op.getValueType();
7416
7417 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7418 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7419 DAG.getConstant(1, VT));
7420 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7421}
7422
Dan Gohman076aee32009-03-04 19:44:21 +00007423/// Emit nodes that will be selected as "test Op0,Op0", or something
7424/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007425SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007426 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007427 DebugLoc dl = Op.getDebugLoc();
7428
Dan Gohman31125812009-03-07 01:58:32 +00007429 // CF and OF aren't always set the way we want. Determine which
7430 // of these we need.
7431 bool NeedCF = false;
7432 bool NeedOF = false;
7433 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007434 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007435 case X86::COND_A: case X86::COND_AE:
7436 case X86::COND_B: case X86::COND_BE:
7437 NeedCF = true;
7438 break;
7439 case X86::COND_G: case X86::COND_GE:
7440 case X86::COND_L: case X86::COND_LE:
7441 case X86::COND_O: case X86::COND_NO:
7442 NeedOF = true;
7443 break;
Dan Gohman31125812009-03-07 01:58:32 +00007444 }
7445
Dan Gohman076aee32009-03-04 19:44:21 +00007446 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007447 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7448 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007449 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7450 // Emit a CMP with 0, which is the TEST pattern.
7451 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7452 DAG.getConstant(0, Op.getValueType()));
7453
7454 unsigned Opcode = 0;
7455 unsigned NumOperands = 0;
7456 switch (Op.getNode()->getOpcode()) {
7457 case ISD::ADD:
7458 // Due to an isel shortcoming, be conservative if this add is likely to be
7459 // selected as part of a load-modify-store instruction. When the root node
7460 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7461 // uses of other nodes in the match, such as the ADD in this case. This
7462 // leads to the ADD being left around and reselected, with the result being
7463 // two adds in the output. Alas, even if none our users are stores, that
7464 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7465 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7466 // climbing the DAG back to the root, and it doesn't seem to be worth the
7467 // effort.
7468 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007469 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007470 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7471 goto default_case;
7472
7473 if (ConstantSDNode *C =
7474 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7475 // An add of one will be selected as an INC.
7476 if (C->getAPIntValue() == 1) {
7477 Opcode = X86ISD::INC;
7478 NumOperands = 1;
7479 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007480 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007481
7482 // An add of negative one (subtract of one) will be selected as a DEC.
7483 if (C->getAPIntValue().isAllOnesValue()) {
7484 Opcode = X86ISD::DEC;
7485 NumOperands = 1;
7486 break;
7487 }
Dan Gohman076aee32009-03-04 19:44:21 +00007488 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007489
7490 // Otherwise use a regular EFLAGS-setting add.
7491 Opcode = X86ISD::ADD;
7492 NumOperands = 2;
7493 break;
7494 case ISD::AND: {
7495 // If the primary and result isn't used, don't bother using X86ISD::AND,
7496 // because a TEST instruction will be better.
7497 bool NonFlagUse = false;
7498 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7499 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7500 SDNode *User = *UI;
7501 unsigned UOpNo = UI.getOperandNo();
7502 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7503 // Look pass truncate.
7504 UOpNo = User->use_begin().getOperandNo();
7505 User = *User->use_begin();
7506 }
7507
7508 if (User->getOpcode() != ISD::BRCOND &&
7509 User->getOpcode() != ISD::SETCC &&
7510 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7511 NonFlagUse = true;
7512 break;
7513 }
Dan Gohman076aee32009-03-04 19:44:21 +00007514 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007515
7516 if (!NonFlagUse)
7517 break;
7518 }
7519 // FALL THROUGH
7520 case ISD::SUB:
7521 case ISD::OR:
7522 case ISD::XOR:
7523 // Due to the ISEL shortcoming noted above, be conservative if this op is
7524 // likely to be selected as part of a load-modify-store instruction.
7525 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7526 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7527 if (UI->getOpcode() == ISD::STORE)
7528 goto default_case;
7529
7530 // Otherwise use a regular EFLAGS-setting instruction.
7531 switch (Op.getNode()->getOpcode()) {
7532 default: llvm_unreachable("unexpected operator!");
7533 case ISD::SUB: Opcode = X86ISD::SUB; break;
7534 case ISD::OR: Opcode = X86ISD::OR; break;
7535 case ISD::XOR: Opcode = X86ISD::XOR; break;
7536 case ISD::AND: Opcode = X86ISD::AND; break;
7537 }
7538
7539 NumOperands = 2;
7540 break;
7541 case X86ISD::ADD:
7542 case X86ISD::SUB:
7543 case X86ISD::INC:
7544 case X86ISD::DEC:
7545 case X86ISD::OR:
7546 case X86ISD::XOR:
7547 case X86ISD::AND:
7548 return SDValue(Op.getNode(), 1);
7549 default:
7550 default_case:
7551 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007552 }
7553
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007554 if (Opcode == 0)
7555 // Emit a CMP with 0, which is the TEST pattern.
7556 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7557 DAG.getConstant(0, Op.getValueType()));
7558
7559 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7560 SmallVector<SDValue, 4> Ops;
7561 for (unsigned i = 0; i != NumOperands; ++i)
7562 Ops.push_back(Op.getOperand(i));
7563
7564 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7565 DAG.ReplaceAllUsesWith(Op, New);
7566 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007567}
7568
7569/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7570/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007571SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007572 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007573 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7574 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007575 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007576
7577 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007578 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007579}
7580
Evan Chengd40d03e2010-01-06 19:38:29 +00007581/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7582/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007583SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7584 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007585 SDValue Op0 = And.getOperand(0);
7586 SDValue Op1 = And.getOperand(1);
7587 if (Op0.getOpcode() == ISD::TRUNCATE)
7588 Op0 = Op0.getOperand(0);
7589 if (Op1.getOpcode() == ISD::TRUNCATE)
7590 Op1 = Op1.getOperand(0);
7591
Evan Chengd40d03e2010-01-06 19:38:29 +00007592 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007593 if (Op1.getOpcode() == ISD::SHL)
7594 std::swap(Op0, Op1);
7595 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007596 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7597 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007598 // If we looked past a truncate, check that it's only truncating away
7599 // known zeros.
7600 unsigned BitWidth = Op0.getValueSizeInBits();
7601 unsigned AndBitWidth = And.getValueSizeInBits();
7602 if (BitWidth > AndBitWidth) {
7603 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7604 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7605 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7606 return SDValue();
7607 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007608 LHS = Op1;
7609 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007610 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007611 } else if (Op1.getOpcode() == ISD::Constant) {
7612 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7613 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007614 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7615 LHS = AndLHS.getOperand(0);
7616 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007617 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007618 }
Evan Cheng0488db92007-09-25 01:57:46 +00007619
Evan Chengd40d03e2010-01-06 19:38:29 +00007620 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007621 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007622 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007623 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007624 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007625 // Also promote i16 to i32 for performance / code size reason.
7626 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007627 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007628 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007629
Evan Chengd40d03e2010-01-06 19:38:29 +00007630 // If the operand types disagree, extend the shift amount to match. Since
7631 // BT ignores high bits (like shifts) we can use anyextend.
7632 if (LHS.getValueType() != RHS.getValueType())
7633 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007634
Evan Chengd40d03e2010-01-06 19:38:29 +00007635 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7636 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7637 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7638 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007639 }
7640
Evan Cheng54de3ea2010-01-05 06:52:31 +00007641 return SDValue();
7642}
7643
Dan Gohmand858e902010-04-17 15:26:15 +00007644SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007645 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7646 SDValue Op0 = Op.getOperand(0);
7647 SDValue Op1 = Op.getOperand(1);
7648 DebugLoc dl = Op.getDebugLoc();
7649 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7650
7651 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007652 // Lower (X & (1 << N)) == 0 to BT(X, N).
7653 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7654 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007655 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007656 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007657 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007658 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7659 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7660 if (NewSetCC.getNode())
7661 return NewSetCC;
7662 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007663
Chris Lattner481eebc2010-12-19 21:23:48 +00007664 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7665 // these.
7666 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007667 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007668 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7669 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007670
Chris Lattner481eebc2010-12-19 21:23:48 +00007671 // If the input is a setcc, then reuse the input setcc or use a new one with
7672 // the inverted condition.
7673 if (Op0.getOpcode() == X86ISD::SETCC) {
7674 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7675 bool Invert = (CC == ISD::SETNE) ^
7676 cast<ConstantSDNode>(Op1)->isNullValue();
7677 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007678
Evan Cheng2c755ba2010-02-27 07:36:59 +00007679 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007680 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7681 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7682 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007683 }
7684
Evan Chenge5b51ac2010-04-17 06:13:15 +00007685 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007686 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007687 if (X86CC == X86::COND_INVALID)
7688 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007689
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007690 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007691 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007692 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007693}
7694
Dan Gohmand858e902010-04-17 15:26:15 +00007695SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007696 SDValue Cond;
7697 SDValue Op0 = Op.getOperand(0);
7698 SDValue Op1 = Op.getOperand(1);
7699 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007700 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007701 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7702 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007703 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007704
7705 if (isFP) {
7706 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007707 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007708 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7709 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007710 bool Swap = false;
7711
7712 switch (SetCCOpcode) {
7713 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007714 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007715 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007716 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007717 case ISD::SETGT: Swap = true; // Fallthrough
7718 case ISD::SETLT:
7719 case ISD::SETOLT: SSECC = 1; break;
7720 case ISD::SETOGE:
7721 case ISD::SETGE: Swap = true; // Fallthrough
7722 case ISD::SETLE:
7723 case ISD::SETOLE: SSECC = 2; break;
7724 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007725 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007726 case ISD::SETNE: SSECC = 4; break;
7727 case ISD::SETULE: Swap = true;
7728 case ISD::SETUGE: SSECC = 5; break;
7729 case ISD::SETULT: Swap = true;
7730 case ISD::SETUGT: SSECC = 6; break;
7731 case ISD::SETO: SSECC = 7; break;
7732 }
7733 if (Swap)
7734 std::swap(Op0, Op1);
7735
Nate Begemanfb8ead02008-07-25 19:05:58 +00007736 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007737 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007738 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007739 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7741 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007742 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007743 }
7744 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007745 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007746 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7747 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007748 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007749 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007750 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007751 }
7752 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007753 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007754 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007755
Nate Begeman30a0de92008-07-17 16:51:19 +00007756 // We are handling one of the integer comparisons here. Since SSE only has
7757 // GT and EQ comparisons for integer, swapping operands and multiple
7758 // operations may be required for some comparisons.
7759 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7760 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007761
Owen Anderson825b72b2009-08-11 20:47:22 +00007762 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007763 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007764 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007765 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007766 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7767 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007768 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007769
Nate Begeman30a0de92008-07-17 16:51:19 +00007770 switch (SetCCOpcode) {
7771 default: break;
7772 case ISD::SETNE: Invert = true;
7773 case ISD::SETEQ: Opc = EQOpc; break;
7774 case ISD::SETLT: Swap = true;
7775 case ISD::SETGT: Opc = GTOpc; break;
7776 case ISD::SETGE: Swap = true;
7777 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7778 case ISD::SETULT: Swap = true;
7779 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7780 case ISD::SETUGE: Swap = true;
7781 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7782 }
7783 if (Swap)
7784 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007785
Nate Begeman30a0de92008-07-17 16:51:19 +00007786 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7787 // bits of the inputs before performing those operations.
7788 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007789 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007790 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7791 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007792 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007793 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7794 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007795 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7796 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007797 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007798
Dale Johannesenace16102009-02-03 19:33:06 +00007799 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007800
7801 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007802 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007803 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007804
Nate Begeman30a0de92008-07-17 16:51:19 +00007805 return Result;
7806}
Evan Cheng0488db92007-09-25 01:57:46 +00007807
Evan Cheng370e5342008-12-03 08:38:43 +00007808// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007809static bool isX86LogicalCmp(SDValue Op) {
7810 unsigned Opc = Op.getNode()->getOpcode();
7811 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7812 return true;
7813 if (Op.getResNo() == 1 &&
7814 (Opc == X86ISD::ADD ||
7815 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007816 Opc == X86ISD::ADC ||
7817 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007818 Opc == X86ISD::SMUL ||
7819 Opc == X86ISD::UMUL ||
7820 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007821 Opc == X86ISD::DEC ||
7822 Opc == X86ISD::OR ||
7823 Opc == X86ISD::XOR ||
7824 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007825 return true;
7826
Chris Lattner9637d5b2010-12-05 07:49:54 +00007827 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7828 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007829
Dan Gohman076aee32009-03-04 19:44:21 +00007830 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007831}
7832
Chris Lattnera2b56002010-12-05 01:23:24 +00007833static bool isZero(SDValue V) {
7834 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7835 return C && C->isNullValue();
7836}
7837
Chris Lattner96908b12010-12-05 02:00:51 +00007838static bool isAllOnes(SDValue V) {
7839 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7840 return C && C->isAllOnesValue();
7841}
7842
Dan Gohmand858e902010-04-17 15:26:15 +00007843SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007844 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007845 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007846 SDValue Op1 = Op.getOperand(1);
7847 SDValue Op2 = Op.getOperand(2);
7848 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007849 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007850
Dan Gohman1a492952009-10-20 16:22:37 +00007851 if (Cond.getOpcode() == ISD::SETCC) {
7852 SDValue NewCond = LowerSETCC(Cond, DAG);
7853 if (NewCond.getNode())
7854 Cond = NewCond;
7855 }
Evan Cheng734503b2006-09-11 02:19:56 +00007856
Chris Lattnera2b56002010-12-05 01:23:24 +00007857 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007858 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007859 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007860 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007861 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007862 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7863 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007864 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007865
Chris Lattnera2b56002010-12-05 01:23:24 +00007866 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007867
7868 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007869 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7870 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007871
7872 SDValue CmpOp0 = Cmp.getOperand(0);
7873 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7874 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007875
Chris Lattner96908b12010-12-05 02:00:51 +00007876 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007877 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7878 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007879
Chris Lattner96908b12010-12-05 02:00:51 +00007880 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7881 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007882
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007883 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007884 if (N2C == 0 || !N2C->isNullValue())
7885 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7886 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007887 }
7888 }
7889
Chris Lattnera2b56002010-12-05 01:23:24 +00007890 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007891 if (Cond.getOpcode() == ISD::AND &&
7892 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7893 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007894 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007895 Cond = Cond.getOperand(0);
7896 }
7897
Evan Cheng3f41d662007-10-08 22:16:29 +00007898 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7899 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007900 if (Cond.getOpcode() == X86ISD::SETCC ||
7901 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007902 CC = Cond.getOperand(0);
7903
Dan Gohman475871a2008-07-27 21:46:04 +00007904 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007905 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007906 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007907
Evan Cheng3f41d662007-10-08 22:16:29 +00007908 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007909 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007910 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007911 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007912
Chris Lattnerd1980a52009-03-12 06:52:53 +00007913 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7914 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007915 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007916 addTest = false;
7917 }
7918 }
7919
7920 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007921 // Look pass the truncate.
7922 if (Cond.getOpcode() == ISD::TRUNCATE)
7923 Cond = Cond.getOperand(0);
7924
7925 // We know the result of AND is compared against zero. Try to match
7926 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007927 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007928 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007929 if (NewSetCC.getNode()) {
7930 CC = NewSetCC.getOperand(0);
7931 Cond = NewSetCC.getOperand(1);
7932 addTest = false;
7933 }
7934 }
7935 }
7936
7937 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007938 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007939 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007940 }
7941
Benjamin Kramere915ff32010-12-22 23:09:28 +00007942 // a < b ? -1 : 0 -> RES = ~setcc_carry
7943 // a < b ? 0 : -1 -> RES = setcc_carry
7944 // a >= b ? -1 : 0 -> RES = setcc_carry
7945 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7946 if (Cond.getOpcode() == X86ISD::CMP) {
7947 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7948
7949 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7950 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7951 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7952 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7953 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7954 return DAG.getNOT(DL, Res, Res.getValueType());
7955 return Res;
7956 }
7957 }
7958
Evan Cheng0488db92007-09-25 01:57:46 +00007959 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7960 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007961 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007962 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007963 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007964}
7965
Evan Cheng370e5342008-12-03 08:38:43 +00007966// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7967// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7968// from the AND / OR.
7969static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7970 Opc = Op.getOpcode();
7971 if (Opc != ISD::OR && Opc != ISD::AND)
7972 return false;
7973 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7974 Op.getOperand(0).hasOneUse() &&
7975 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7976 Op.getOperand(1).hasOneUse());
7977}
7978
Evan Cheng961d6d42009-02-02 08:19:07 +00007979// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7980// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007981static bool isXor1OfSetCC(SDValue Op) {
7982 if (Op.getOpcode() != ISD::XOR)
7983 return false;
7984 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7985 if (N1C && N1C->getAPIntValue() == 1) {
7986 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7987 Op.getOperand(0).hasOneUse();
7988 }
7989 return false;
7990}
7991
Dan Gohmand858e902010-04-17 15:26:15 +00007992SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007993 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007994 SDValue Chain = Op.getOperand(0);
7995 SDValue Cond = Op.getOperand(1);
7996 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007997 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007998 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007999
Dan Gohman1a492952009-10-20 16:22:37 +00008000 if (Cond.getOpcode() == ISD::SETCC) {
8001 SDValue NewCond = LowerSETCC(Cond, DAG);
8002 if (NewCond.getNode())
8003 Cond = NewCond;
8004 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008005#if 0
8006 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008007 else if (Cond.getOpcode() == X86ISD::ADD ||
8008 Cond.getOpcode() == X86ISD::SUB ||
8009 Cond.getOpcode() == X86ISD::SMUL ||
8010 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008011 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008012#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008013
Evan Chengad9c0a32009-12-15 00:53:42 +00008014 // Look pass (and (setcc_carry (cmp ...)), 1).
8015 if (Cond.getOpcode() == ISD::AND &&
8016 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8017 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008018 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008019 Cond = Cond.getOperand(0);
8020 }
8021
Evan Cheng3f41d662007-10-08 22:16:29 +00008022 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8023 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008024 if (Cond.getOpcode() == X86ISD::SETCC ||
8025 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008026 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008027
Dan Gohman475871a2008-07-27 21:46:04 +00008028 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008029 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008030 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008031 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008032 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008033 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008034 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008035 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008036 default: break;
8037 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008038 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008039 // These can only come from an arithmetic instruction with overflow,
8040 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008041 Cond = Cond.getNode()->getOperand(1);
8042 addTest = false;
8043 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008044 }
Evan Cheng0488db92007-09-25 01:57:46 +00008045 }
Evan Cheng370e5342008-12-03 08:38:43 +00008046 } else {
8047 unsigned CondOpc;
8048 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8049 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008050 if (CondOpc == ISD::OR) {
8051 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8052 // two branches instead of an explicit OR instruction with a
8053 // separate test.
8054 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008055 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008056 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008057 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008058 Chain, Dest, CC, Cmp);
8059 CC = Cond.getOperand(1).getOperand(0);
8060 Cond = Cmp;
8061 addTest = false;
8062 }
8063 } else { // ISD::AND
8064 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8065 // two branches instead of an explicit AND instruction with a
8066 // separate test. However, we only do this if this block doesn't
8067 // have a fall-through edge, because this requires an explicit
8068 // jmp when the condition is false.
8069 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008070 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008071 Op.getNode()->hasOneUse()) {
8072 X86::CondCode CCode =
8073 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8074 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008075 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008076 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008077 // Look for an unconditional branch following this conditional branch.
8078 // We need this because we need to reverse the successors in order
8079 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008080 if (User->getOpcode() == ISD::BR) {
8081 SDValue FalseBB = User->getOperand(1);
8082 SDNode *NewBR =
8083 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008084 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008085 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008086 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008087
Dale Johannesene4d209d2009-02-03 20:21:25 +00008088 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008089 Chain, Dest, CC, Cmp);
8090 X86::CondCode CCode =
8091 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8092 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008093 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008094 Cond = Cmp;
8095 addTest = false;
8096 }
8097 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008098 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008099 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8100 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8101 // It should be transformed during dag combiner except when the condition
8102 // is set by a arithmetics with overflow node.
8103 X86::CondCode CCode =
8104 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8105 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008107 Cond = Cond.getOperand(0).getOperand(1);
8108 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008109 }
Evan Cheng0488db92007-09-25 01:57:46 +00008110 }
8111
8112 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008113 // Look pass the truncate.
8114 if (Cond.getOpcode() == ISD::TRUNCATE)
8115 Cond = Cond.getOperand(0);
8116
8117 // We know the result of AND is compared against zero. Try to match
8118 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008119 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008120 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8121 if (NewSetCC.getNode()) {
8122 CC = NewSetCC.getOperand(0);
8123 Cond = NewSetCC.getOperand(1);
8124 addTest = false;
8125 }
8126 }
8127 }
8128
8129 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008130 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008131 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008132 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008133 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008134 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008135}
8136
Anton Korobeynikove060b532007-04-17 19:34:00 +00008137
8138// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8139// Calls to _alloca is needed to probe the stack when allocating more than 4k
8140// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8141// that the guard pages used by the OS virtual memory manager are allocated in
8142// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008143SDValue
8144X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008145 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008146 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008147 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008148 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008149 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008150
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008151 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008152 SDValue Chain = Op.getOperand(0);
8153 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008154 // FIXME: Ensure alignment here
8155
Dan Gohman475871a2008-07-27 21:46:04 +00008156 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008157
Owen Anderson825b72b2009-08-11 20:47:22 +00008158 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008159 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008160
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008161 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008162 Flag = Chain.getValue(1);
8163
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008164 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008165
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008166 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008167 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008168
Dale Johannesendd64c412009-02-04 00:33:20 +00008169 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008170
Dan Gohman475871a2008-07-27 21:46:04 +00008171 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008172 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008173}
8174
Dan Gohmand858e902010-04-17 15:26:15 +00008175SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008176 MachineFunction &MF = DAG.getMachineFunction();
8177 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8178
Dan Gohman69de1932008-02-06 22:27:42 +00008179 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008180 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008181
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008182 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008183 // vastart just stores the address of the VarArgsFrameIndex slot into the
8184 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008185 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8186 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008187 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8188 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008189 }
8190
8191 // __va_list_tag:
8192 // gp_offset (0 - 6 * 8)
8193 // fp_offset (48 - 48 + 8 * 16)
8194 // overflow_arg_area (point to parameters coming in memory).
8195 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008196 SmallVector<SDValue, 8> MemOps;
8197 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008198 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008199 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008200 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8201 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008202 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008203 MemOps.push_back(Store);
8204
8205 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008206 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008207 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008208 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008209 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8210 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008211 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008212 MemOps.push_back(Store);
8213
8214 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008215 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008216 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008217 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8218 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008219 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8220 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008221 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008222 MemOps.push_back(Store);
8223
8224 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008225 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008226 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008227 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8228 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008229 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8230 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008231 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008232 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008233 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008234}
8235
Dan Gohmand858e902010-04-17 15:26:15 +00008236SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008237 assert(Subtarget->is64Bit() &&
8238 "LowerVAARG only handles 64-bit va_arg!");
8239 assert((Subtarget->isTargetLinux() ||
8240 Subtarget->isTargetDarwin()) &&
8241 "Unhandled target in LowerVAARG");
8242 assert(Op.getNode()->getNumOperands() == 4);
8243 SDValue Chain = Op.getOperand(0);
8244 SDValue SrcPtr = Op.getOperand(1);
8245 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8246 unsigned Align = Op.getConstantOperandVal(3);
8247 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008248
Dan Gohman320afb82010-10-12 18:00:49 +00008249 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008250 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00008251 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8252 uint8_t ArgMode;
8253
8254 // Decide which area this value should be read from.
8255 // TODO: Implement the AMD64 ABI in its entirety. This simple
8256 // selection mechanism works only for the basic types.
8257 if (ArgVT == MVT::f80) {
8258 llvm_unreachable("va_arg for f80 not yet implemented");
8259 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8260 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8261 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8262 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8263 } else {
8264 llvm_unreachable("Unhandled argument type in LowerVAARG");
8265 }
8266
8267 if (ArgMode == 2) {
8268 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008269 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008270 !(DAG.getMachineFunction()
8271 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008272 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008273 }
8274
8275 // Insert VAARG_64 node into the DAG
8276 // VAARG_64 returns two values: Variable Argument Address, Chain
8277 SmallVector<SDValue, 11> InstOps;
8278 InstOps.push_back(Chain);
8279 InstOps.push_back(SrcPtr);
8280 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8281 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8282 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8283 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8284 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8285 VTs, &InstOps[0], InstOps.size(),
8286 MVT::i64,
8287 MachinePointerInfo(SV),
8288 /*Align=*/0,
8289 /*Volatile=*/false,
8290 /*ReadMem=*/true,
8291 /*WriteMem=*/true);
8292 Chain = VAARG.getValue(1);
8293
8294 // Load the next argument and return it
8295 return DAG.getLoad(ArgVT, dl,
8296 Chain,
8297 VAARG,
8298 MachinePointerInfo(),
8299 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008300}
8301
Dan Gohmand858e902010-04-17 15:26:15 +00008302SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008303 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008304 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008305 SDValue Chain = Op.getOperand(0);
8306 SDValue DstPtr = Op.getOperand(1);
8307 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008308 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8309 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008310 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008311
Chris Lattnere72f2022010-09-21 05:40:29 +00008312 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008313 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008314 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008315 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008316}
8317
Dan Gohman475871a2008-07-27 21:46:04 +00008318SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008319X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008320 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008321 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008322 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008323 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008324 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008325 case Intrinsic::x86_sse_comieq_ss:
8326 case Intrinsic::x86_sse_comilt_ss:
8327 case Intrinsic::x86_sse_comile_ss:
8328 case Intrinsic::x86_sse_comigt_ss:
8329 case Intrinsic::x86_sse_comige_ss:
8330 case Intrinsic::x86_sse_comineq_ss:
8331 case Intrinsic::x86_sse_ucomieq_ss:
8332 case Intrinsic::x86_sse_ucomilt_ss:
8333 case Intrinsic::x86_sse_ucomile_ss:
8334 case Intrinsic::x86_sse_ucomigt_ss:
8335 case Intrinsic::x86_sse_ucomige_ss:
8336 case Intrinsic::x86_sse_ucomineq_ss:
8337 case Intrinsic::x86_sse2_comieq_sd:
8338 case Intrinsic::x86_sse2_comilt_sd:
8339 case Intrinsic::x86_sse2_comile_sd:
8340 case Intrinsic::x86_sse2_comigt_sd:
8341 case Intrinsic::x86_sse2_comige_sd:
8342 case Intrinsic::x86_sse2_comineq_sd:
8343 case Intrinsic::x86_sse2_ucomieq_sd:
8344 case Intrinsic::x86_sse2_ucomilt_sd:
8345 case Intrinsic::x86_sse2_ucomile_sd:
8346 case Intrinsic::x86_sse2_ucomigt_sd:
8347 case Intrinsic::x86_sse2_ucomige_sd:
8348 case Intrinsic::x86_sse2_ucomineq_sd: {
8349 unsigned Opc = 0;
8350 ISD::CondCode CC = ISD::SETCC_INVALID;
8351 switch (IntNo) {
8352 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008353 case Intrinsic::x86_sse_comieq_ss:
8354 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008355 Opc = X86ISD::COMI;
8356 CC = ISD::SETEQ;
8357 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008358 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008359 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008360 Opc = X86ISD::COMI;
8361 CC = ISD::SETLT;
8362 break;
8363 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008364 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008365 Opc = X86ISD::COMI;
8366 CC = ISD::SETLE;
8367 break;
8368 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008369 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008370 Opc = X86ISD::COMI;
8371 CC = ISD::SETGT;
8372 break;
8373 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008374 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008375 Opc = X86ISD::COMI;
8376 CC = ISD::SETGE;
8377 break;
8378 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008379 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008380 Opc = X86ISD::COMI;
8381 CC = ISD::SETNE;
8382 break;
8383 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008384 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008385 Opc = X86ISD::UCOMI;
8386 CC = ISD::SETEQ;
8387 break;
8388 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008389 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008390 Opc = X86ISD::UCOMI;
8391 CC = ISD::SETLT;
8392 break;
8393 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008394 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008395 Opc = X86ISD::UCOMI;
8396 CC = ISD::SETLE;
8397 break;
8398 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008399 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008400 Opc = X86ISD::UCOMI;
8401 CC = ISD::SETGT;
8402 break;
8403 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008404 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008405 Opc = X86ISD::UCOMI;
8406 CC = ISD::SETGE;
8407 break;
8408 case Intrinsic::x86_sse_ucomineq_ss:
8409 case Intrinsic::x86_sse2_ucomineq_sd:
8410 Opc = X86ISD::UCOMI;
8411 CC = ISD::SETNE;
8412 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008413 }
Evan Cheng734503b2006-09-11 02:19:56 +00008414
Dan Gohman475871a2008-07-27 21:46:04 +00008415 SDValue LHS = Op.getOperand(1);
8416 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008417 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008418 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008419 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8420 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8421 DAG.getConstant(X86CC, MVT::i8), Cond);
8422 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008423 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008424 // ptest and testp intrinsics. The intrinsic these come from are designed to
8425 // return an integer value, not just an instruction so lower it to the ptest
8426 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008427 case Intrinsic::x86_sse41_ptestz:
8428 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008429 case Intrinsic::x86_sse41_ptestnzc:
8430 case Intrinsic::x86_avx_ptestz_256:
8431 case Intrinsic::x86_avx_ptestc_256:
8432 case Intrinsic::x86_avx_ptestnzc_256:
8433 case Intrinsic::x86_avx_vtestz_ps:
8434 case Intrinsic::x86_avx_vtestc_ps:
8435 case Intrinsic::x86_avx_vtestnzc_ps:
8436 case Intrinsic::x86_avx_vtestz_pd:
8437 case Intrinsic::x86_avx_vtestc_pd:
8438 case Intrinsic::x86_avx_vtestnzc_pd:
8439 case Intrinsic::x86_avx_vtestz_ps_256:
8440 case Intrinsic::x86_avx_vtestc_ps_256:
8441 case Intrinsic::x86_avx_vtestnzc_ps_256:
8442 case Intrinsic::x86_avx_vtestz_pd_256:
8443 case Intrinsic::x86_avx_vtestc_pd_256:
8444 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8445 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008446 unsigned X86CC = 0;
8447 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008448 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008449 case Intrinsic::x86_avx_vtestz_ps:
8450 case Intrinsic::x86_avx_vtestz_pd:
8451 case Intrinsic::x86_avx_vtestz_ps_256:
8452 case Intrinsic::x86_avx_vtestz_pd_256:
8453 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008454 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008455 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008456 // ZF = 1
8457 X86CC = X86::COND_E;
8458 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008459 case Intrinsic::x86_avx_vtestc_ps:
8460 case Intrinsic::x86_avx_vtestc_pd:
8461 case Intrinsic::x86_avx_vtestc_ps_256:
8462 case Intrinsic::x86_avx_vtestc_pd_256:
8463 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008464 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008465 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008466 // CF = 1
8467 X86CC = X86::COND_B;
8468 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008469 case Intrinsic::x86_avx_vtestnzc_ps:
8470 case Intrinsic::x86_avx_vtestnzc_pd:
8471 case Intrinsic::x86_avx_vtestnzc_ps_256:
8472 case Intrinsic::x86_avx_vtestnzc_pd_256:
8473 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008474 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008475 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008476 // ZF and CF = 0
8477 X86CC = X86::COND_A;
8478 break;
8479 }
Eric Christopherfd179292009-08-27 18:07:15 +00008480
Eric Christopher71c67532009-07-29 00:28:05 +00008481 SDValue LHS = Op.getOperand(1);
8482 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008483 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8484 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008485 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8486 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8487 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008488 }
Evan Cheng5759f972008-05-04 09:15:50 +00008489
8490 // Fix vector shift instructions where the last operand is a non-immediate
8491 // i32 value.
8492 case Intrinsic::x86_sse2_pslli_w:
8493 case Intrinsic::x86_sse2_pslli_d:
8494 case Intrinsic::x86_sse2_pslli_q:
8495 case Intrinsic::x86_sse2_psrli_w:
8496 case Intrinsic::x86_sse2_psrli_d:
8497 case Intrinsic::x86_sse2_psrli_q:
8498 case Intrinsic::x86_sse2_psrai_w:
8499 case Intrinsic::x86_sse2_psrai_d:
8500 case Intrinsic::x86_mmx_pslli_w:
8501 case Intrinsic::x86_mmx_pslli_d:
8502 case Intrinsic::x86_mmx_pslli_q:
8503 case Intrinsic::x86_mmx_psrli_w:
8504 case Intrinsic::x86_mmx_psrli_d:
8505 case Intrinsic::x86_mmx_psrli_q:
8506 case Intrinsic::x86_mmx_psrai_w:
8507 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008508 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008509 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008510 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008511
8512 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008513 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008514 switch (IntNo) {
8515 case Intrinsic::x86_sse2_pslli_w:
8516 NewIntNo = Intrinsic::x86_sse2_psll_w;
8517 break;
8518 case Intrinsic::x86_sse2_pslli_d:
8519 NewIntNo = Intrinsic::x86_sse2_psll_d;
8520 break;
8521 case Intrinsic::x86_sse2_pslli_q:
8522 NewIntNo = Intrinsic::x86_sse2_psll_q;
8523 break;
8524 case Intrinsic::x86_sse2_psrli_w:
8525 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8526 break;
8527 case Intrinsic::x86_sse2_psrli_d:
8528 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8529 break;
8530 case Intrinsic::x86_sse2_psrli_q:
8531 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8532 break;
8533 case Intrinsic::x86_sse2_psrai_w:
8534 NewIntNo = Intrinsic::x86_sse2_psra_w;
8535 break;
8536 case Intrinsic::x86_sse2_psrai_d:
8537 NewIntNo = Intrinsic::x86_sse2_psra_d;
8538 break;
8539 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008540 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008541 switch (IntNo) {
8542 case Intrinsic::x86_mmx_pslli_w:
8543 NewIntNo = Intrinsic::x86_mmx_psll_w;
8544 break;
8545 case Intrinsic::x86_mmx_pslli_d:
8546 NewIntNo = Intrinsic::x86_mmx_psll_d;
8547 break;
8548 case Intrinsic::x86_mmx_pslli_q:
8549 NewIntNo = Intrinsic::x86_mmx_psll_q;
8550 break;
8551 case Intrinsic::x86_mmx_psrli_w:
8552 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8553 break;
8554 case Intrinsic::x86_mmx_psrli_d:
8555 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8556 break;
8557 case Intrinsic::x86_mmx_psrli_q:
8558 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8559 break;
8560 case Intrinsic::x86_mmx_psrai_w:
8561 NewIntNo = Intrinsic::x86_mmx_psra_w;
8562 break;
8563 case Intrinsic::x86_mmx_psrai_d:
8564 NewIntNo = Intrinsic::x86_mmx_psra_d;
8565 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008566 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008567 }
8568 break;
8569 }
8570 }
Mon P Wangefa42202009-09-03 19:56:25 +00008571
8572 // The vector shift intrinsics with scalars uses 32b shift amounts but
8573 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8574 // to be zero.
8575 SDValue ShOps[4];
8576 ShOps[0] = ShAmt;
8577 ShOps[1] = DAG.getConstant(0, MVT::i32);
8578 if (ShAmtVT == MVT::v4i32) {
8579 ShOps[2] = DAG.getUNDEF(MVT::i32);
8580 ShOps[3] = DAG.getUNDEF(MVT::i32);
8581 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8582 } else {
8583 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008584// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008585 }
8586
Owen Andersone50ed302009-08-10 22:56:29 +00008587 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008588 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008589 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008590 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008591 Op.getOperand(1), ShAmt);
8592 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008593 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008594}
Evan Cheng72261582005-12-20 06:22:03 +00008595
Dan Gohmand858e902010-04-17 15:26:15 +00008596SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8597 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008598 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8599 MFI->setReturnAddressIsTaken(true);
8600
Bill Wendling64e87322009-01-16 19:25:27 +00008601 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008602 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008603
8604 if (Depth > 0) {
8605 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8606 SDValue Offset =
8607 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008608 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008609 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008610 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008611 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008612 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008613 }
8614
8615 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008616 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008617 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008618 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008619}
8620
Dan Gohmand858e902010-04-17 15:26:15 +00008621SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008622 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8623 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008624
Owen Andersone50ed302009-08-10 22:56:29 +00008625 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008626 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008627 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8628 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008629 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008630 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008631 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8632 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008633 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008634 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008635}
8636
Dan Gohman475871a2008-07-27 21:46:04 +00008637SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008638 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008639 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008640}
8641
Dan Gohmand858e902010-04-17 15:26:15 +00008642SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008643 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008644 SDValue Chain = Op.getOperand(0);
8645 SDValue Offset = Op.getOperand(1);
8646 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008647 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008648
Dan Gohmand8816272010-08-11 18:14:00 +00008649 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8650 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8651 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008652 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008653
Dan Gohmand8816272010-08-11 18:14:00 +00008654 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8655 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008656 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008657 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8658 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008659 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008660 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008661
Dale Johannesene4d209d2009-02-03 20:21:25 +00008662 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008663 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008664 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008665}
8666
Dan Gohman475871a2008-07-27 21:46:04 +00008667SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008668 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008669 SDValue Root = Op.getOperand(0);
8670 SDValue Trmp = Op.getOperand(1); // trampoline
8671 SDValue FPtr = Op.getOperand(2); // nested function
8672 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008673 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008674
Dan Gohman69de1932008-02-06 22:27:42 +00008675 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008676
8677 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008678 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008679
8680 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008681 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8682 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008683
Evan Cheng0e6a0522011-07-18 20:57:22 +00008684 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
8685 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008686
8687 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8688
8689 // Load the pointer to the nested function into R11.
8690 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008691 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008693 Addr, MachinePointerInfo(TrmpAddr),
8694 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008695
Owen Anderson825b72b2009-08-11 20:47:22 +00008696 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8697 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008698 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8699 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008700 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008701
8702 // Load the 'nest' parameter value into R10.
8703 // R10 is specified in X86CallingConv.td
8704 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008705 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8706 DAG.getConstant(10, MVT::i64));
8707 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008708 Addr, MachinePointerInfo(TrmpAddr, 10),
8709 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008710
Owen Anderson825b72b2009-08-11 20:47:22 +00008711 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8712 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008713 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8714 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008715 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008716
8717 // Jump to the nested function.
8718 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008719 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8720 DAG.getConstant(20, MVT::i64));
8721 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008722 Addr, MachinePointerInfo(TrmpAddr, 20),
8723 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008724
8725 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008726 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8727 DAG.getConstant(22, MVT::i64));
8728 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008729 MachinePointerInfo(TrmpAddr, 22),
8730 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008731
Dan Gohman475871a2008-07-27 21:46:04 +00008732 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008733 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008734 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008735 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008736 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008737 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008738 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008739 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008740
8741 switch (CC) {
8742 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008743 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008744 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008745 case CallingConv::X86_StdCall: {
8746 // Pass 'nest' parameter in ECX.
8747 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008748 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008749
8750 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008751 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008752 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008753
Chris Lattner58d74912008-03-12 17:45:29 +00008754 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008755 unsigned InRegCount = 0;
8756 unsigned Idx = 1;
8757
8758 for (FunctionType::param_iterator I = FTy->param_begin(),
8759 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008760 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008761 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008762 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008763
8764 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008765 report_fatal_error("Nest register in use - reduce number of inreg"
8766 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008767 }
8768 }
8769 break;
8770 }
8771 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008772 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008773 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008774 // Pass 'nest' parameter in EAX.
8775 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008776 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008777 break;
8778 }
8779
Dan Gohman475871a2008-07-27 21:46:04 +00008780 SDValue OutChains[4];
8781 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008782
Owen Anderson825b72b2009-08-11 20:47:22 +00008783 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8784 DAG.getConstant(10, MVT::i32));
8785 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008786
Chris Lattnera62fe662010-02-05 19:20:30 +00008787 // This is storing the opcode for MOV32ri.
8788 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00008789 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008790 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008791 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008792 Trmp, MachinePointerInfo(TrmpAddr),
8793 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008794
Owen Anderson825b72b2009-08-11 20:47:22 +00008795 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8796 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008797 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8798 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008799 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008800
Chris Lattnera62fe662010-02-05 19:20:30 +00008801 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008802 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8803 DAG.getConstant(5, MVT::i32));
8804 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008805 MachinePointerInfo(TrmpAddr, 5),
8806 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008807
Owen Anderson825b72b2009-08-11 20:47:22 +00008808 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8809 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008810 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8811 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008812 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008813
Dan Gohman475871a2008-07-27 21:46:04 +00008814 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008815 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008816 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008817 }
8818}
8819
Dan Gohmand858e902010-04-17 15:26:15 +00008820SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8821 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008822 /*
8823 The rounding mode is in bits 11:10 of FPSR, and has the following
8824 settings:
8825 00 Round to nearest
8826 01 Round to -inf
8827 10 Round to +inf
8828 11 Round to 0
8829
8830 FLT_ROUNDS, on the other hand, expects the following:
8831 -1 Undefined
8832 0 Round to 0
8833 1 Round to nearest
8834 2 Round to +inf
8835 3 Round to -inf
8836
8837 To perform the conversion, we do:
8838 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8839 */
8840
8841 MachineFunction &MF = DAG.getMachineFunction();
8842 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008843 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008844 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008845 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008846 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008847
8848 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008849 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008850 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008851
Michael J. Spencerec38de22010-10-10 22:04:20 +00008852
Chris Lattner2156b792010-09-22 01:11:26 +00008853 MachineMemOperand *MMO =
8854 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8855 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008856
Chris Lattner2156b792010-09-22 01:11:26 +00008857 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8858 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8859 DAG.getVTList(MVT::Other),
8860 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008861
8862 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008863 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008864 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008865
8866 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008867 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008868 DAG.getNode(ISD::SRL, DL, MVT::i16,
8869 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008870 CWD, DAG.getConstant(0x800, MVT::i16)),
8871 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008872 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008873 DAG.getNode(ISD::SRL, DL, MVT::i16,
8874 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008875 CWD, DAG.getConstant(0x400, MVT::i16)),
8876 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008877
Dan Gohman475871a2008-07-27 21:46:04 +00008878 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008879 DAG.getNode(ISD::AND, DL, MVT::i16,
8880 DAG.getNode(ISD::ADD, DL, MVT::i16,
8881 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008882 DAG.getConstant(1, MVT::i16)),
8883 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008884
8885
Duncan Sands83ec4b62008-06-06 12:08:01 +00008886 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008887 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008888}
8889
Dan Gohmand858e902010-04-17 15:26:15 +00008890SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008891 EVT VT = Op.getValueType();
8892 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008893 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008894 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008895
8896 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008897 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008898 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008899 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008900 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008901 }
Evan Cheng18efe262007-12-14 02:13:44 +00008902
Evan Cheng152804e2007-12-14 08:30:15 +00008903 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008904 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008905 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008906
8907 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008908 SDValue Ops[] = {
8909 Op,
8910 DAG.getConstant(NumBits+NumBits-1, OpVT),
8911 DAG.getConstant(X86::COND_E, MVT::i8),
8912 Op.getValue(1)
8913 };
8914 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008915
8916 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008917 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008918
Owen Anderson825b72b2009-08-11 20:47:22 +00008919 if (VT == MVT::i8)
8920 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008921 return Op;
8922}
8923
Dan Gohmand858e902010-04-17 15:26:15 +00008924SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008925 EVT VT = Op.getValueType();
8926 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008927 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008928 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008929
8930 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008931 if (VT == MVT::i8) {
8932 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008933 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008934 }
Evan Cheng152804e2007-12-14 08:30:15 +00008935
8936 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008937 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008938 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008939
8940 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008941 SDValue Ops[] = {
8942 Op,
8943 DAG.getConstant(NumBits, OpVT),
8944 DAG.getConstant(X86::COND_E, MVT::i8),
8945 Op.getValue(1)
8946 };
8947 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008948
Owen Anderson825b72b2009-08-11 20:47:22 +00008949 if (VT == MVT::i8)
8950 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008951 return Op;
8952}
8953
Dan Gohmand858e902010-04-17 15:26:15 +00008954SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008955 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008956 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008957 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008958
Mon P Wangaf9b9522008-12-18 21:42:19 +00008959 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8960 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8961 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8962 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8963 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8964 //
8965 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8966 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8967 // return AloBlo + AloBhi + AhiBlo;
8968
8969 SDValue A = Op.getOperand(0);
8970 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008971
Dale Johannesene4d209d2009-02-03 20:21:25 +00008972 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008973 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8974 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008975 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008976 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8977 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008978 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008979 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008980 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008981 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008982 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008983 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008984 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008985 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008986 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008987 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008988 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8989 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008990 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008991 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8992 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008993 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8994 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008995 return Res;
8996}
8997
Nadav Rotem43012222011-05-11 08:12:09 +00008998SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8999
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009000 EVT VT = Op.getValueType();
9001 DebugLoc dl = Op.getDebugLoc();
9002 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009003 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009004
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009005 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009006
Nadav Rotem43012222011-05-11 08:12:09 +00009007 // Must have SSE2.
9008 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00009009
Nadav Rotem43012222011-05-11 08:12:09 +00009010 // Optimize shl/srl/sra with constant shift amount.
9011 if (isSplatVector(Amt.getNode())) {
9012 SDValue SclrAmt = Amt->getOperand(0);
9013 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9014 uint64_t ShiftAmt = C->getZExtValue();
9015
9016 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9017 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9018 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9019 R, DAG.getConstant(ShiftAmt, MVT::i32));
9020
9021 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9022 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9023 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9024 R, DAG.getConstant(ShiftAmt, MVT::i32));
9025
9026 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9027 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9028 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9029 R, DAG.getConstant(ShiftAmt, MVT::i32));
9030
9031 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9032 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9033 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9034 R, DAG.getConstant(ShiftAmt, MVT::i32));
9035
9036 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9037 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9038 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9039 R, DAG.getConstant(ShiftAmt, MVT::i32));
9040
9041 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9042 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9043 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9044 R, DAG.getConstant(ShiftAmt, MVT::i32));
9045
9046 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9047 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9048 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9049 R, DAG.getConstant(ShiftAmt, MVT::i32));
9050
9051 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9052 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9053 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9054 R, DAG.getConstant(ShiftAmt, MVT::i32));
9055 }
9056 }
9057
9058 // Lower SHL with variable shift amount.
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009059 // Cannot lower SHL without SSE2 or later.
9060 if (!Subtarget->hasSSE2()) return SDValue();
Nadav Rotem43012222011-05-11 08:12:09 +00009061
9062 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009063 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9064 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9065 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9066
9067 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009068
Nate Begeman51409212010-07-28 00:21:48 +00009069 std::vector<Constant*> CV(4, CI);
9070 Constant *C = ConstantVector::get(CV);
9071 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9072 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009073 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009074 false, false, 16);
9075
9076 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009077 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009078 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9079 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9080 }
Nadav Rotem43012222011-05-11 08:12:09 +00009081 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009082 // a = a << 5;
9083 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9084 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9085 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9086
9087 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9088 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9089
9090 std::vector<Constant*> CVM1(16, CM1);
9091 std::vector<Constant*> CVM2(16, CM2);
9092 Constant *C = ConstantVector::get(CVM1);
9093 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9094 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009095 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009096 false, false, 16);
9097
9098 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9099 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9100 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9101 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9102 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009103 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009104 // a += a
9105 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009106
Nate Begeman51409212010-07-28 00:21:48 +00009107 C = ConstantVector::get(CVM2);
9108 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9109 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009110 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009111 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009112
Nate Begeman51409212010-07-28 00:21:48 +00009113 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9114 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9115 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9116 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9117 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009118 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009119 // a += a
9120 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009121
Nate Begeman51409212010-07-28 00:21:48 +00009122 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009123 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00009124 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
9125 return R;
9126 }
9127 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009128}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009129
Dan Gohmand858e902010-04-17 15:26:15 +00009130SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009131 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9132 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009133 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9134 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009135 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009136 SDValue LHS = N->getOperand(0);
9137 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009138 unsigned BaseOp = 0;
9139 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009140 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009141 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009142 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009143 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009144 // A subtract of one will be selected as a INC. Note that INC doesn't
9145 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009146 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9147 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009148 BaseOp = X86ISD::INC;
9149 Cond = X86::COND_O;
9150 break;
9151 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009152 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009153 Cond = X86::COND_O;
9154 break;
9155 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009156 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009157 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009158 break;
9159 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009160 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9161 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009162 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9163 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009164 BaseOp = X86ISD::DEC;
9165 Cond = X86::COND_O;
9166 break;
9167 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009168 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009169 Cond = X86::COND_O;
9170 break;
9171 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009172 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009173 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009174 break;
9175 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009176 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009177 Cond = X86::COND_O;
9178 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009179 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9180 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9181 MVT::i32);
9182 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009183
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009184 SDValue SetCC =
9185 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9186 DAG.getConstant(X86::COND_O, MVT::i32),
9187 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009188
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009189 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9190 return Sum;
9191 }
Bill Wendling74c37652008-12-09 22:08:41 +00009192 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009193
Bill Wendling61edeb52008-12-02 01:06:39 +00009194 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009195 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009196 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009197
Bill Wendling61edeb52008-12-02 01:06:39 +00009198 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009199 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9200 DAG.getConstant(Cond, MVT::i32),
9201 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009202
Bill Wendling61edeb52008-12-02 01:06:39 +00009203 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9204 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009205}
9206
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009207SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
9208 DebugLoc dl = Op.getDebugLoc();
9209 SDNode* Node = Op.getNode();
9210 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
9211 EVT VT = Node->getValueType(0);
9212
9213 if (Subtarget->hasSSE2() && VT.isVector()) {
9214 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
9215 ExtraVT.getScalarType().getSizeInBits();
9216 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
9217
9218 unsigned SHLIntrinsicsID = 0;
9219 unsigned SRAIntrinsicsID = 0;
9220 switch (VT.getSimpleVT().SimpleTy) {
9221 default:
9222 return SDValue();
9223 case MVT::v2i64: {
9224 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
9225 SRAIntrinsicsID = 0;
9226 break;
9227 }
9228 case MVT::v4i32: {
9229 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
9230 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
9231 break;
9232 }
9233 case MVT::v8i16: {
9234 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
9235 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
9236 break;
9237 }
9238 }
9239
9240 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9241 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
9242 Node->getOperand(0), ShAmt);
9243
9244 // In case of 1 bit sext, no need to shr
9245 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
9246
9247 if (SRAIntrinsicsID) {
9248 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9249 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
9250 Tmp1, ShAmt);
9251 }
9252 return Tmp1;
9253 }
9254
9255 return SDValue();
9256}
9257
9258
Eric Christopher9a9d2752010-07-22 02:48:34 +00009259SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9260 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009261
Eric Christopher77ed1352011-07-08 00:04:56 +00009262 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
9263 // There isn't any reason to disable it if the target processor supports it.
9264 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009265 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +00009266 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009267 SDValue Ops[] = {
9268 DAG.getRegister(X86::ESP, MVT::i32), // Base
9269 DAG.getTargetConstant(1, MVT::i8), // Scale
9270 DAG.getRegister(0, MVT::i32), // Index
9271 DAG.getTargetConstant(0, MVT::i32), // Disp
9272 DAG.getRegister(0, MVT::i32), // Segment.
9273 Zero,
9274 Chain
9275 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009276 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009277 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9278 array_lengthof(Ops));
9279 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009280 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009281
Eric Christopher9a9d2752010-07-22 02:48:34 +00009282 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009283 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009284 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009285
Chris Lattner132929a2010-08-14 17:26:09 +00009286 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9287 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9288 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9289 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009290
Chris Lattner132929a2010-08-14 17:26:09 +00009291 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9292 if (!Op1 && !Op2 && !Op3 && Op4)
9293 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009294
Chris Lattner132929a2010-08-14 17:26:09 +00009295 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9296 if (Op1 && !Op2 && !Op3 && !Op4)
9297 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009298
9299 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009300 // (MFENCE)>;
9301 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009302}
9303
Dan Gohmand858e902010-04-17 15:26:15 +00009304SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009305 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009306 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009307 unsigned Reg = 0;
9308 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009309 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009310 default:
9311 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009312 case MVT::i8: Reg = X86::AL; size = 1; break;
9313 case MVT::i16: Reg = X86::AX; size = 2; break;
9314 case MVT::i32: Reg = X86::EAX; size = 4; break;
9315 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009316 assert(Subtarget->is64Bit() && "Node not type legal!");
9317 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009318 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009319 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009320 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009321 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009322 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009323 Op.getOperand(1),
9324 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009325 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009326 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009327 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009328 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9329 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9330 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009331 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009332 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009333 return cpOut;
9334}
9335
Duncan Sands1607f052008-12-01 11:39:25 +00009336SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009337 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009338 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009339 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009340 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009341 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009342 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009343 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9344 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009345 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009346 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9347 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009348 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009349 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009350 rdx.getValue(1)
9351 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009352 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009353}
9354
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009355SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009356 SelectionDAG &DAG) const {
9357 EVT SrcVT = Op.getOperand(0).getValueType();
9358 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009359 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9360 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009361 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009362 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009363 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009364 // i64 <=> MMX conversions are Legal.
9365 if (SrcVT==MVT::i64 && DstVT.isVector())
9366 return Op;
9367 if (DstVT==MVT::i64 && SrcVT.isVector())
9368 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009369 // MMX <=> MMX conversions are Legal.
9370 if (SrcVT.isVector() && DstVT.isVector())
9371 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009372 // All other conversions need to be expanded.
9373 return SDValue();
9374}
Chris Lattner5b856542010-12-20 00:59:46 +00009375
Dan Gohmand858e902010-04-17 15:26:15 +00009376SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009377 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009378 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009379 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009380 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009381 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009382 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009383 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009384 Node->getOperand(0),
9385 Node->getOperand(1), negOp,
9386 cast<AtomicSDNode>(Node)->getSrcValue(),
9387 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009388}
9389
Chris Lattner5b856542010-12-20 00:59:46 +00009390static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9391 EVT VT = Op.getNode()->getValueType(0);
9392
9393 // Let legalize expand this if it isn't a legal type yet.
9394 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9395 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009396
Chris Lattner5b856542010-12-20 00:59:46 +00009397 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009398
Chris Lattner5b856542010-12-20 00:59:46 +00009399 unsigned Opc;
9400 bool ExtraOp = false;
9401 switch (Op.getOpcode()) {
9402 default: assert(0 && "Invalid code");
9403 case ISD::ADDC: Opc = X86ISD::ADD; break;
9404 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9405 case ISD::SUBC: Opc = X86ISD::SUB; break;
9406 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9407 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009408
Chris Lattner5b856542010-12-20 00:59:46 +00009409 if (!ExtraOp)
9410 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9411 Op.getOperand(1));
9412 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9413 Op.getOperand(1), Op.getOperand(2));
9414}
9415
Evan Cheng0db9fe62006-04-25 20:13:52 +00009416/// LowerOperation - Provide custom lowering hooks for some operations.
9417///
Dan Gohmand858e902010-04-17 15:26:15 +00009418SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009419 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009420 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009421 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +00009422 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009423 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9424 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009425 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009426 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009427 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9428 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9429 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009430 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009431 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009432 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9433 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9434 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009435 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009436 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009437 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009438 case ISD::SHL_PARTS:
9439 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009440 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009441 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009442 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009443 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009444 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009445 case ISD::FABS: return LowerFABS(Op, DAG);
9446 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009447 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009448 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009449 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009450 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009451 case ISD::SELECT: return LowerSELECT(Op, DAG);
9452 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009453 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009454 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009455 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009456 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009457 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009458 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9459 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009460 case ISD::FRAME_TO_ARGS_OFFSET:
9461 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009462 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009463 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009464 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009465 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009466 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9467 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009468 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009469 case ISD::SRA:
9470 case ISD::SRL:
9471 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009472 case ISD::SADDO:
9473 case ISD::UADDO:
9474 case ISD::SSUBO:
9475 case ISD::USUBO:
9476 case ISD::SMULO:
9477 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009478 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009479 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009480 case ISD::ADDC:
9481 case ISD::ADDE:
9482 case ISD::SUBC:
9483 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009484 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009485}
9486
Duncan Sands1607f052008-12-01 11:39:25 +00009487void X86TargetLowering::
9488ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009489 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009490 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009491 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009492 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009493
9494 SDValue Chain = Node->getOperand(0);
9495 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009496 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009497 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009498 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009499 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009500 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009501 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009502 SDValue Result =
9503 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9504 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009505 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009506 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009507 Results.push_back(Result.getValue(2));
9508}
9509
Duncan Sands126d9072008-07-04 11:47:58 +00009510/// ReplaceNodeResults - Replace a node with an illegal result type
9511/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009512void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9513 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009514 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009515 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009516 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009517 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009518 assert(false && "Do not know how to custom type legalize this operation!");
9519 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009520 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +00009521 case ISD::ADDC:
9522 case ISD::ADDE:
9523 case ISD::SUBC:
9524 case ISD::SUBE:
9525 // We don't want to expand or promote these.
9526 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009527 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009528 std::pair<SDValue,SDValue> Vals =
9529 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009530 SDValue FIST = Vals.first, StackSlot = Vals.second;
9531 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009532 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009533 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009534 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9535 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009536 }
9537 return;
9538 }
9539 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009540 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009541 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009542 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009543 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009544 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009545 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009546 eax.getValue(2));
9547 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9548 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009549 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009550 Results.push_back(edx.getValue(1));
9551 return;
9552 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009553 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009554 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009555 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009556 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009557 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9558 DAG.getConstant(0, MVT::i32));
9559 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9560 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009561 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9562 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009563 cpInL.getValue(1));
9564 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009565 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9566 DAG.getConstant(0, MVT::i32));
9567 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9568 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009569 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009570 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009571 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009572 swapInL.getValue(1));
9573 SDValue Ops[] = { swapInH.getValue(0),
9574 N->getOperand(1),
9575 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009576 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009577 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9578 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9579 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009580 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009581 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009582 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009583 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009584 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009585 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009586 Results.push_back(cpOutH.getValue(1));
9587 return;
9588 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009589 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009590 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9591 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009592 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009593 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9594 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009595 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009596 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9597 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009598 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009599 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9600 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009601 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009602 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9603 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009604 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009605 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9606 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009607 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009608 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9609 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009610 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009611}
9612
Evan Cheng72261582005-12-20 06:22:03 +00009613const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9614 switch (Opcode) {
9615 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009616 case X86ISD::BSF: return "X86ISD::BSF";
9617 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009618 case X86ISD::SHLD: return "X86ISD::SHLD";
9619 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009620 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009621 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009622 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009623 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009624 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009625 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009626 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9627 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9628 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009629 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009630 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009631 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009632 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009633 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009634 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009635 case X86ISD::COMI: return "X86ISD::COMI";
9636 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009637 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009638 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009639 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9640 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009641 case X86ISD::CMOV: return "X86ISD::CMOV";
9642 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009643 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009644 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9645 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009646 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009647 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009648 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009649 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009650 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009651 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9652 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009653 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009654 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +00009655 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +00009656 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9657 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9658 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009659 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009660 case X86ISD::FMAX: return "X86ISD::FMAX";
9661 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009662 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9663 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009664 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009665 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009666 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009667 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009668 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009669 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9670 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009671 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9672 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9673 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9674 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9675 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9676 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009677 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9678 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009679 case X86ISD::VSHL: return "X86ISD::VSHL";
9680 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009681 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9682 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9683 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9684 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9685 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9686 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9687 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9688 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9689 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9690 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009691 case X86ISD::ADD: return "X86ISD::ADD";
9692 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009693 case X86ISD::ADC: return "X86ISD::ADC";
9694 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009695 case X86ISD::SMUL: return "X86ISD::SMUL";
9696 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009697 case X86ISD::INC: return "X86ISD::INC";
9698 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009699 case X86ISD::OR: return "X86ISD::OR";
9700 case X86ISD::XOR: return "X86ISD::XOR";
9701 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009702 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009703 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009704 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009705 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9706 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9707 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9708 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9709 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9710 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9711 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9712 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9713 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009714 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009715 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009716 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009717 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9718 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009719 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9720 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9721 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9722 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9723 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9724 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9725 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9726 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9727 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009728 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9729 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9730 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9731 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009732 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9733 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9734 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9735 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9736 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9737 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9738 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9739 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9740 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9741 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00009742 case X86ISD::VPERMIL: return "X86ISD::VPERMIL";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009743 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009744 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009745 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009746 }
9747}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009748
Chris Lattnerc9addb72007-03-30 23:15:24 +00009749// isLegalAddressingMode - Return true if the addressing mode represented
9750// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009751bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009752 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00009753 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009754 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009755 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009756
Chris Lattnerc9addb72007-03-30 23:15:24 +00009757 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009758 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009759 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009760
Chris Lattnerc9addb72007-03-30 23:15:24 +00009761 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009762 unsigned GVFlags =
9763 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009764
Chris Lattnerdfed4132009-07-10 07:38:24 +00009765 // If a reference to this global requires an extra load, we can't fold it.
9766 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009767 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009768
Chris Lattnerdfed4132009-07-10 07:38:24 +00009769 // If BaseGV requires a register for the PIC base, we cannot also have a
9770 // BaseReg specified.
9771 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009772 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009773
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009774 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009775 if ((M != CodeModel::Small || R != Reloc::Static) &&
9776 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009777 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009778 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009779
Chris Lattnerc9addb72007-03-30 23:15:24 +00009780 switch (AM.Scale) {
9781 case 0:
9782 case 1:
9783 case 2:
9784 case 4:
9785 case 8:
9786 // These scales always work.
9787 break;
9788 case 3:
9789 case 5:
9790 case 9:
9791 // These scales are formed with basereg+scalereg. Only accept if there is
9792 // no basereg yet.
9793 if (AM.HasBaseReg)
9794 return false;
9795 break;
9796 default: // Other stuff never works.
9797 return false;
9798 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009799
Chris Lattnerc9addb72007-03-30 23:15:24 +00009800 return true;
9801}
9802
9803
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009804bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009805 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009806 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009807 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9808 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009809 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009810 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009811 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009812}
9813
Owen Andersone50ed302009-08-10 22:56:29 +00009814bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009815 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009816 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009817 unsigned NumBits1 = VT1.getSizeInBits();
9818 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009819 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009820 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009821 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009822}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009823
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009824bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009825 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009826 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009827}
9828
Owen Andersone50ed302009-08-10 22:56:29 +00009829bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009830 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009831 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009832}
9833
Owen Andersone50ed302009-08-10 22:56:29 +00009834bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009835 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009836 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009837}
9838
Evan Cheng60c07e12006-07-05 22:17:51 +00009839/// isShuffleMaskLegal - Targets can use this to indicate that they only
9840/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9841/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9842/// are assumed to be legal.
9843bool
Eric Christopherfd179292009-08-27 18:07:15 +00009844X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009845 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009846 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009847 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009848 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009849
Nate Begemana09008b2009-10-19 02:17:23 +00009850 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009851 return (VT.getVectorNumElements() == 2 ||
9852 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9853 isMOVLMask(M, VT) ||
9854 isSHUFPMask(M, VT) ||
9855 isPSHUFDMask(M, VT) ||
9856 isPSHUFHWMask(M, VT) ||
9857 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009858 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009859 isUNPCKLMask(M, VT) ||
9860 isUNPCKHMask(M, VT) ||
9861 isUNPCKL_v_undef_Mask(M, VT) ||
9862 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009863}
9864
Dan Gohman7d8143f2008-04-09 20:09:42 +00009865bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009866X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009867 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009868 unsigned NumElts = VT.getVectorNumElements();
9869 // FIXME: This collection of masks seems suspect.
9870 if (NumElts == 2)
9871 return true;
9872 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9873 return (isMOVLMask(Mask, VT) ||
9874 isCommutedMOVLMask(Mask, VT, true) ||
9875 isSHUFPMask(Mask, VT) ||
9876 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009877 }
9878 return false;
9879}
9880
9881//===----------------------------------------------------------------------===//
9882// X86 Scheduler Hooks
9883//===----------------------------------------------------------------------===//
9884
Mon P Wang63307c32008-05-05 19:05:59 +00009885// private utility function
9886MachineBasicBlock *
9887X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9888 MachineBasicBlock *MBB,
9889 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009890 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009891 unsigned LoadOpc,
9892 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009893 unsigned notOpc,
9894 unsigned EAXreg,
9895 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009896 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009897 // For the atomic bitwise operator, we generate
9898 // thisMBB:
9899 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009900 // ld t1 = [bitinstr.addr]
9901 // op t2 = t1, [bitinstr.val]
9902 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009903 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9904 // bz newMBB
9905 // fallthrough -->nextMBB
9906 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9907 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009908 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009909 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009910
Mon P Wang63307c32008-05-05 19:05:59 +00009911 /// First build the CFG
9912 MachineFunction *F = MBB->getParent();
9913 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009914 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9915 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9916 F->insert(MBBIter, newMBB);
9917 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009918
Dan Gohman14152b42010-07-06 20:24:04 +00009919 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9920 nextMBB->splice(nextMBB->begin(), thisMBB,
9921 llvm::next(MachineBasicBlock::iterator(bInstr)),
9922 thisMBB->end());
9923 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009924
Mon P Wang63307c32008-05-05 19:05:59 +00009925 // Update thisMBB to fall through to newMBB
9926 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009927
Mon P Wang63307c32008-05-05 19:05:59 +00009928 // newMBB jumps to itself and fall through to nextMBB
9929 newMBB->addSuccessor(nextMBB);
9930 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009931
Mon P Wang63307c32008-05-05 19:05:59 +00009932 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009933 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009934 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009935 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009936 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009937 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009938 int numArgs = bInstr->getNumOperands() - 1;
9939 for (int i=0; i < numArgs; ++i)
9940 argOpers[i] = &bInstr->getOperand(i+1);
9941
9942 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009943 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009944 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009945
Dale Johannesen140be2d2008-08-19 18:47:28 +00009946 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009947 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009948 for (int i=0; i <= lastAddrIndx; ++i)
9949 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009950
Dale Johannesen140be2d2008-08-19 18:47:28 +00009951 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009952 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009953 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009954 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009955 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009956 tt = t1;
9957
Dale Johannesen140be2d2008-08-19 18:47:28 +00009958 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009959 assert((argOpers[valArgIndx]->isReg() ||
9960 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009961 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009962 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009963 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009964 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009965 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009966 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009967 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009968
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009969 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009970 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009971
Dale Johannesene4d209d2009-02-03 20:21:25 +00009972 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009973 for (int i=0; i <= lastAddrIndx; ++i)
9974 (*MIB).addOperand(*argOpers[i]);
9975 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009976 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009977 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9978 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009979
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009980 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009981 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009982
Mon P Wang63307c32008-05-05 19:05:59 +00009983 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009984 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009985
Dan Gohman14152b42010-07-06 20:24:04 +00009986 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009987 return nextMBB;
9988}
9989
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009990// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009991MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009992X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9993 MachineBasicBlock *MBB,
9994 unsigned regOpcL,
9995 unsigned regOpcH,
9996 unsigned immOpcL,
9997 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009998 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009999 // For the atomic bitwise operator, we generate
10000 // thisMBB (instructions are in pairs, except cmpxchg8b)
10001 // ld t1,t2 = [bitinstr.addr]
10002 // newMBB:
10003 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
10004 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000010005 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010006 // mov ECX, EBX <- t5, t6
10007 // mov EAX, EDX <- t1, t2
10008 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
10009 // mov t3, t4 <- EAX, EDX
10010 // bz newMBB
10011 // result in out1, out2
10012 // fallthrough -->nextMBB
10013
10014 const TargetRegisterClass *RC = X86::GR32RegisterClass;
10015 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010016 const unsigned NotOpc = X86::NOT32r;
10017 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10018 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10019 MachineFunction::iterator MBBIter = MBB;
10020 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010021
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010022 /// First build the CFG
10023 MachineFunction *F = MBB->getParent();
10024 MachineBasicBlock *thisMBB = MBB;
10025 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10026 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10027 F->insert(MBBIter, newMBB);
10028 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010029
Dan Gohman14152b42010-07-06 20:24:04 +000010030 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10031 nextMBB->splice(nextMBB->begin(), thisMBB,
10032 llvm::next(MachineBasicBlock::iterator(bInstr)),
10033 thisMBB->end());
10034 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010035
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010036 // Update thisMBB to fall through to newMBB
10037 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010038
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010039 // newMBB jumps to itself and fall through to nextMBB
10040 newMBB->addSuccessor(nextMBB);
10041 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010042
Dale Johannesene4d209d2009-02-03 20:21:25 +000010043 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010044 // Insert instructions into newMBB based on incoming instruction
10045 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010046 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010047 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010048 MachineOperand& dest1Oper = bInstr->getOperand(0);
10049 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010050 MachineOperand* argOpers[2 + X86::AddrNumOperands];
10051 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010052 argOpers[i] = &bInstr->getOperand(i+2);
10053
Dan Gohman71ea4e52010-05-14 21:01:44 +000010054 // We use some of the operands multiple times, so conservatively just
10055 // clear any kill flags that might be present.
10056 if (argOpers[i]->isReg() && argOpers[i]->isUse())
10057 argOpers[i]->setIsKill(false);
10058 }
10059
Evan Chengad5b52f2010-01-08 19:14:57 +000010060 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010061 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000010062
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010063 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010064 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010065 for (int i=0; i <= lastAddrIndx; ++i)
10066 (*MIB).addOperand(*argOpers[i]);
10067 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010068 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000010069 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000010070 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010071 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000010072 MachineOperand newOp3 = *(argOpers[3]);
10073 if (newOp3.isImm())
10074 newOp3.setImm(newOp3.getImm()+4);
10075 else
10076 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010077 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000010078 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010079
10080 // t3/4 are defined later, at the bottom of the loop
10081 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
10082 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010083 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010084 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010085 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010086 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
10087
Evan Cheng306b4ca2010-01-08 23:41:50 +000010088 // The subsequent operations should be using the destination registers of
10089 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000010090 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000010091 t1 = F->getRegInfo().createVirtualRegister(RC);
10092 t2 = F->getRegInfo().createVirtualRegister(RC);
10093 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
10094 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010095 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000010096 t1 = dest1Oper.getReg();
10097 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010098 }
10099
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010100 int valArgIndx = lastAddrIndx + 1;
10101 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000010102 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010103 "invalid operand");
10104 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
10105 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010106 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010107 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010108 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010109 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000010110 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000010111 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010112 (*MIB).addOperand(*argOpers[valArgIndx]);
10113 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000010114 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010115 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000010116 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010117 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010118 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010119 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010120 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000010121 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000010122 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010123 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010124
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010125 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010126 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010127 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010128 MIB.addReg(t2);
10129
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010130 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010131 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010132 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010133 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000010134
Dale Johannesene4d209d2009-02-03 20:21:25 +000010135 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010136 for (int i=0; i <= lastAddrIndx; ++i)
10137 (*MIB).addOperand(*argOpers[i]);
10138
10139 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010140 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10141 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010142
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010143 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010144 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010145 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010146 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010147
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010148 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010149 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010150
Dan Gohman14152b42010-07-06 20:24:04 +000010151 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010152 return nextMBB;
10153}
10154
10155// private utility function
10156MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000010157X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
10158 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010159 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010160 // For the atomic min/max operator, we generate
10161 // thisMBB:
10162 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010163 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000010164 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000010165 // cmp t1, t2
10166 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000010167 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010168 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10169 // bz newMBB
10170 // fallthrough -->nextMBB
10171 //
10172 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10173 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010174 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010175 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010176
Mon P Wang63307c32008-05-05 19:05:59 +000010177 /// First build the CFG
10178 MachineFunction *F = MBB->getParent();
10179 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010180 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10181 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10182 F->insert(MBBIter, newMBB);
10183 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010184
Dan Gohman14152b42010-07-06 20:24:04 +000010185 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10186 nextMBB->splice(nextMBB->begin(), thisMBB,
10187 llvm::next(MachineBasicBlock::iterator(mInstr)),
10188 thisMBB->end());
10189 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010190
Mon P Wang63307c32008-05-05 19:05:59 +000010191 // Update thisMBB to fall through to newMBB
10192 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010193
Mon P Wang63307c32008-05-05 19:05:59 +000010194 // newMBB jumps to newMBB and fall through to nextMBB
10195 newMBB->addSuccessor(nextMBB);
10196 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010197
Dale Johannesene4d209d2009-02-03 20:21:25 +000010198 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010199 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010200 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010201 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010202 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010203 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010204 int numArgs = mInstr->getNumOperands() - 1;
10205 for (int i=0; i < numArgs; ++i)
10206 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010207
Mon P Wang63307c32008-05-05 19:05:59 +000010208 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010209 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010210 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010211
Mon P Wangab3e7472008-05-05 22:56:23 +000010212 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010213 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010214 for (int i=0; i <= lastAddrIndx; ++i)
10215 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010216
Mon P Wang63307c32008-05-05 19:05:59 +000010217 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010218 assert((argOpers[valArgIndx]->isReg() ||
10219 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010220 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010221
10222 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010223 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010224 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010225 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010226 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010227 (*MIB).addOperand(*argOpers[valArgIndx]);
10228
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010229 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010230 MIB.addReg(t1);
10231
Dale Johannesene4d209d2009-02-03 20:21:25 +000010232 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010233 MIB.addReg(t1);
10234 MIB.addReg(t2);
10235
10236 // Generate movc
10237 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010238 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010239 MIB.addReg(t2);
10240 MIB.addReg(t1);
10241
10242 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010243 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010244 for (int i=0; i <= lastAddrIndx; ++i)
10245 (*MIB).addOperand(*argOpers[i]);
10246 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010247 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010248 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10249 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010250
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010251 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010252 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010253
Mon P Wang63307c32008-05-05 19:05:59 +000010254 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010255 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010256
Dan Gohman14152b42010-07-06 20:24:04 +000010257 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010258 return nextMBB;
10259}
10260
Eric Christopherf83a5de2009-08-27 18:08:16 +000010261// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010262// or XMM0_V32I8 in AVX all of this code can be replaced with that
10263// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010264MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010265X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010266 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010267 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10268 "Target must have SSE4.2 or AVX features enabled");
10269
Eric Christopherb120ab42009-08-18 22:50:32 +000010270 DebugLoc dl = MI->getDebugLoc();
10271 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010272 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010273 if (!Subtarget->hasAVX()) {
10274 if (memArg)
10275 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10276 else
10277 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10278 } else {
10279 if (memArg)
10280 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10281 else
10282 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10283 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010284
Eric Christopher41c902f2010-11-30 08:20:21 +000010285 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010286 for (unsigned i = 0; i < numArgs; ++i) {
10287 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010288 if (!(Op.isReg() && Op.isImplicit()))
10289 MIB.addOperand(Op);
10290 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010291 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010292 .addReg(X86::XMM0);
10293
Dan Gohman14152b42010-07-06 20:24:04 +000010294 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010295 return BB;
10296}
10297
10298MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010299X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010300 DebugLoc dl = MI->getDebugLoc();
10301 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010302
Eric Christopher228232b2010-11-30 07:20:12 +000010303 // Address into RAX/EAX, other two args into ECX, EDX.
10304 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10305 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10306 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10307 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010308 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010309
Eric Christopher228232b2010-11-30 07:20:12 +000010310 unsigned ValOps = X86::AddrNumOperands;
10311 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10312 .addReg(MI->getOperand(ValOps).getReg());
10313 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10314 .addReg(MI->getOperand(ValOps+1).getReg());
10315
10316 // The instruction doesn't actually take any operands though.
10317 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010318
Eric Christopher228232b2010-11-30 07:20:12 +000010319 MI->eraseFromParent(); // The pseudo is gone now.
10320 return BB;
10321}
10322
10323MachineBasicBlock *
10324X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010325 DebugLoc dl = MI->getDebugLoc();
10326 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010327
Eric Christopher228232b2010-11-30 07:20:12 +000010328 // First arg in ECX, the second in EAX.
10329 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10330 .addReg(MI->getOperand(0).getReg());
10331 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10332 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010333
Eric Christopher228232b2010-11-30 07:20:12 +000010334 // The instruction doesn't actually take any operands though.
10335 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010336
Eric Christopher228232b2010-11-30 07:20:12 +000010337 MI->eraseFromParent(); // The pseudo is gone now.
10338 return BB;
10339}
10340
10341MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010342X86TargetLowering::EmitVAARG64WithCustomInserter(
10343 MachineInstr *MI,
10344 MachineBasicBlock *MBB) const {
10345 // Emit va_arg instruction on X86-64.
10346
10347 // Operands to this pseudo-instruction:
10348 // 0 ) Output : destination address (reg)
10349 // 1-5) Input : va_list address (addr, i64mem)
10350 // 6 ) ArgSize : Size (in bytes) of vararg type
10351 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10352 // 8 ) Align : Alignment of type
10353 // 9 ) EFLAGS (implicit-def)
10354
10355 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10356 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10357
10358 unsigned DestReg = MI->getOperand(0).getReg();
10359 MachineOperand &Base = MI->getOperand(1);
10360 MachineOperand &Scale = MI->getOperand(2);
10361 MachineOperand &Index = MI->getOperand(3);
10362 MachineOperand &Disp = MI->getOperand(4);
10363 MachineOperand &Segment = MI->getOperand(5);
10364 unsigned ArgSize = MI->getOperand(6).getImm();
10365 unsigned ArgMode = MI->getOperand(7).getImm();
10366 unsigned Align = MI->getOperand(8).getImm();
10367
10368 // Memory Reference
10369 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10370 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10371 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10372
10373 // Machine Information
10374 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10375 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10376 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10377 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10378 DebugLoc DL = MI->getDebugLoc();
10379
10380 // struct va_list {
10381 // i32 gp_offset
10382 // i32 fp_offset
10383 // i64 overflow_area (address)
10384 // i64 reg_save_area (address)
10385 // }
10386 // sizeof(va_list) = 24
10387 // alignment(va_list) = 8
10388
10389 unsigned TotalNumIntRegs = 6;
10390 unsigned TotalNumXMMRegs = 8;
10391 bool UseGPOffset = (ArgMode == 1);
10392 bool UseFPOffset = (ArgMode == 2);
10393 unsigned MaxOffset = TotalNumIntRegs * 8 +
10394 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10395
10396 /* Align ArgSize to a multiple of 8 */
10397 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10398 bool NeedsAlign = (Align > 8);
10399
10400 MachineBasicBlock *thisMBB = MBB;
10401 MachineBasicBlock *overflowMBB;
10402 MachineBasicBlock *offsetMBB;
10403 MachineBasicBlock *endMBB;
10404
10405 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10406 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10407 unsigned OffsetReg = 0;
10408
10409 if (!UseGPOffset && !UseFPOffset) {
10410 // If we only pull from the overflow region, we don't create a branch.
10411 // We don't need to alter control flow.
10412 OffsetDestReg = 0; // unused
10413 OverflowDestReg = DestReg;
10414
10415 offsetMBB = NULL;
10416 overflowMBB = thisMBB;
10417 endMBB = thisMBB;
10418 } else {
10419 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10420 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10421 // If not, pull from overflow_area. (branch to overflowMBB)
10422 //
10423 // thisMBB
10424 // | .
10425 // | .
10426 // offsetMBB overflowMBB
10427 // | .
10428 // | .
10429 // endMBB
10430
10431 // Registers for the PHI in endMBB
10432 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10433 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10434
10435 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10436 MachineFunction *MF = MBB->getParent();
10437 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10438 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10439 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10440
10441 MachineFunction::iterator MBBIter = MBB;
10442 ++MBBIter;
10443
10444 // Insert the new basic blocks
10445 MF->insert(MBBIter, offsetMBB);
10446 MF->insert(MBBIter, overflowMBB);
10447 MF->insert(MBBIter, endMBB);
10448
10449 // Transfer the remainder of MBB and its successor edges to endMBB.
10450 endMBB->splice(endMBB->begin(), thisMBB,
10451 llvm::next(MachineBasicBlock::iterator(MI)),
10452 thisMBB->end());
10453 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10454
10455 // Make offsetMBB and overflowMBB successors of thisMBB
10456 thisMBB->addSuccessor(offsetMBB);
10457 thisMBB->addSuccessor(overflowMBB);
10458
10459 // endMBB is a successor of both offsetMBB and overflowMBB
10460 offsetMBB->addSuccessor(endMBB);
10461 overflowMBB->addSuccessor(endMBB);
10462
10463 // Load the offset value into a register
10464 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10465 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10466 .addOperand(Base)
10467 .addOperand(Scale)
10468 .addOperand(Index)
10469 .addDisp(Disp, UseFPOffset ? 4 : 0)
10470 .addOperand(Segment)
10471 .setMemRefs(MMOBegin, MMOEnd);
10472
10473 // Check if there is enough room left to pull this argument.
10474 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10475 .addReg(OffsetReg)
10476 .addImm(MaxOffset + 8 - ArgSizeA8);
10477
10478 // Branch to "overflowMBB" if offset >= max
10479 // Fall through to "offsetMBB" otherwise
10480 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10481 .addMBB(overflowMBB);
10482 }
10483
10484 // In offsetMBB, emit code to use the reg_save_area.
10485 if (offsetMBB) {
10486 assert(OffsetReg != 0);
10487
10488 // Read the reg_save_area address.
10489 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10490 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10491 .addOperand(Base)
10492 .addOperand(Scale)
10493 .addOperand(Index)
10494 .addDisp(Disp, 16)
10495 .addOperand(Segment)
10496 .setMemRefs(MMOBegin, MMOEnd);
10497
10498 // Zero-extend the offset
10499 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10500 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10501 .addImm(0)
10502 .addReg(OffsetReg)
10503 .addImm(X86::sub_32bit);
10504
10505 // Add the offset to the reg_save_area to get the final address.
10506 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10507 .addReg(OffsetReg64)
10508 .addReg(RegSaveReg);
10509
10510 // Compute the offset for the next argument
10511 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10512 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10513 .addReg(OffsetReg)
10514 .addImm(UseFPOffset ? 16 : 8);
10515
10516 // Store it back into the va_list.
10517 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10518 .addOperand(Base)
10519 .addOperand(Scale)
10520 .addOperand(Index)
10521 .addDisp(Disp, UseFPOffset ? 4 : 0)
10522 .addOperand(Segment)
10523 .addReg(NextOffsetReg)
10524 .setMemRefs(MMOBegin, MMOEnd);
10525
10526 // Jump to endMBB
10527 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10528 .addMBB(endMBB);
10529 }
10530
10531 //
10532 // Emit code to use overflow area
10533 //
10534
10535 // Load the overflow_area address into a register.
10536 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10537 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10538 .addOperand(Base)
10539 .addOperand(Scale)
10540 .addOperand(Index)
10541 .addDisp(Disp, 8)
10542 .addOperand(Segment)
10543 .setMemRefs(MMOBegin, MMOEnd);
10544
10545 // If we need to align it, do so. Otherwise, just copy the address
10546 // to OverflowDestReg.
10547 if (NeedsAlign) {
10548 // Align the overflow address
10549 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10550 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10551
10552 // aligned_addr = (addr + (align-1)) & ~(align-1)
10553 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10554 .addReg(OverflowAddrReg)
10555 .addImm(Align-1);
10556
10557 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10558 .addReg(TmpReg)
10559 .addImm(~(uint64_t)(Align-1));
10560 } else {
10561 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10562 .addReg(OverflowAddrReg);
10563 }
10564
10565 // Compute the next overflow address after this argument.
10566 // (the overflow address should be kept 8-byte aligned)
10567 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10568 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10569 .addReg(OverflowDestReg)
10570 .addImm(ArgSizeA8);
10571
10572 // Store the new overflow address.
10573 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10574 .addOperand(Base)
10575 .addOperand(Scale)
10576 .addOperand(Index)
10577 .addDisp(Disp, 8)
10578 .addOperand(Segment)
10579 .addReg(NextAddrReg)
10580 .setMemRefs(MMOBegin, MMOEnd);
10581
10582 // If we branched, emit the PHI to the front of endMBB.
10583 if (offsetMBB) {
10584 BuildMI(*endMBB, endMBB->begin(), DL,
10585 TII->get(X86::PHI), DestReg)
10586 .addReg(OffsetDestReg).addMBB(offsetMBB)
10587 .addReg(OverflowDestReg).addMBB(overflowMBB);
10588 }
10589
10590 // Erase the pseudo instruction
10591 MI->eraseFromParent();
10592
10593 return endMBB;
10594}
10595
10596MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010597X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10598 MachineInstr *MI,
10599 MachineBasicBlock *MBB) const {
10600 // Emit code to save XMM registers to the stack. The ABI says that the
10601 // number of registers to save is given in %al, so it's theoretically
10602 // possible to do an indirect jump trick to avoid saving all of them,
10603 // however this code takes a simpler approach and just executes all
10604 // of the stores if %al is non-zero. It's less code, and it's probably
10605 // easier on the hardware branch predictor, and stores aren't all that
10606 // expensive anyway.
10607
10608 // Create the new basic blocks. One block contains all the XMM stores,
10609 // and one block is the final destination regardless of whether any
10610 // stores were performed.
10611 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10612 MachineFunction *F = MBB->getParent();
10613 MachineFunction::iterator MBBIter = MBB;
10614 ++MBBIter;
10615 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10616 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10617 F->insert(MBBIter, XMMSaveMBB);
10618 F->insert(MBBIter, EndMBB);
10619
Dan Gohman14152b42010-07-06 20:24:04 +000010620 // Transfer the remainder of MBB and its successor edges to EndMBB.
10621 EndMBB->splice(EndMBB->begin(), MBB,
10622 llvm::next(MachineBasicBlock::iterator(MI)),
10623 MBB->end());
10624 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10625
Dan Gohmand6708ea2009-08-15 01:38:56 +000010626 // The original block will now fall through to the XMM save block.
10627 MBB->addSuccessor(XMMSaveMBB);
10628 // The XMMSaveMBB will fall through to the end block.
10629 XMMSaveMBB->addSuccessor(EndMBB);
10630
10631 // Now add the instructions.
10632 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10633 DebugLoc DL = MI->getDebugLoc();
10634
10635 unsigned CountReg = MI->getOperand(0).getReg();
10636 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10637 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10638
10639 if (!Subtarget->isTargetWin64()) {
10640 // If %al is 0, branch around the XMM save block.
10641 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010642 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010643 MBB->addSuccessor(EndMBB);
10644 }
10645
10646 // In the XMM save block, save all the XMM argument registers.
10647 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10648 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010649 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010650 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010651 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010652 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010653 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010654 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10655 .addFrameIndex(RegSaveFrameIndex)
10656 .addImm(/*Scale=*/1)
10657 .addReg(/*IndexReg=*/0)
10658 .addImm(/*Disp=*/Offset)
10659 .addReg(/*Segment=*/0)
10660 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010661 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010662 }
10663
Dan Gohman14152b42010-07-06 20:24:04 +000010664 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010665
10666 return EndMBB;
10667}
Mon P Wang63307c32008-05-05 19:05:59 +000010668
Evan Cheng60c07e12006-07-05 22:17:51 +000010669MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010670X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010671 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010672 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10673 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010674
Chris Lattner52600972009-09-02 05:57:00 +000010675 // To "insert" a SELECT_CC instruction, we actually have to insert the
10676 // diamond control-flow pattern. The incoming instruction knows the
10677 // destination vreg to set, the condition code register to branch on, the
10678 // true/false values to select between, and a branch opcode to use.
10679 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10680 MachineFunction::iterator It = BB;
10681 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010682
Chris Lattner52600972009-09-02 05:57:00 +000010683 // thisMBB:
10684 // ...
10685 // TrueVal = ...
10686 // cmpTY ccX, r1, r2
10687 // bCC copy1MBB
10688 // fallthrough --> copy0MBB
10689 MachineBasicBlock *thisMBB = BB;
10690 MachineFunction *F = BB->getParent();
10691 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10692 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010693 F->insert(It, copy0MBB);
10694 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010695
Bill Wendling730c07e2010-06-25 20:48:10 +000010696 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10697 // live into the sink and copy blocks.
10698 const MachineFunction *MF = BB->getParent();
10699 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10700 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010701
Dan Gohman14152b42010-07-06 20:24:04 +000010702 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10703 const MachineOperand &MO = MI->getOperand(I);
10704 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010705 unsigned Reg = MO.getReg();
10706 if (Reg != X86::EFLAGS) continue;
10707 copy0MBB->addLiveIn(Reg);
10708 sinkMBB->addLiveIn(Reg);
10709 }
10710
Dan Gohman14152b42010-07-06 20:24:04 +000010711 // Transfer the remainder of BB and its successor edges to sinkMBB.
10712 sinkMBB->splice(sinkMBB->begin(), BB,
10713 llvm::next(MachineBasicBlock::iterator(MI)),
10714 BB->end());
10715 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10716
10717 // Add the true and fallthrough blocks as its successors.
10718 BB->addSuccessor(copy0MBB);
10719 BB->addSuccessor(sinkMBB);
10720
10721 // Create the conditional branch instruction.
10722 unsigned Opc =
10723 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10724 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10725
Chris Lattner52600972009-09-02 05:57:00 +000010726 // copy0MBB:
10727 // %FalseValue = ...
10728 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010729 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010730
Chris Lattner52600972009-09-02 05:57:00 +000010731 // sinkMBB:
10732 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10733 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010734 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10735 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010736 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10737 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10738
Dan Gohman14152b42010-07-06 20:24:04 +000010739 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010740 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010741}
10742
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010743MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010744X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010745 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010746 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10747 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010748
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010749 assert(!Subtarget->isTargetEnvMacho());
10750
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010751 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10752 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010753
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010754 if (Subtarget->isTargetWin64()) {
10755 if (Subtarget->isTargetCygMing()) {
10756 // ___chkstk(Mingw64):
10757 // Clobbers R10, R11, RAX and EFLAGS.
10758 // Updates RSP.
10759 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10760 .addExternalSymbol("___chkstk")
10761 .addReg(X86::RAX, RegState::Implicit)
10762 .addReg(X86::RSP, RegState::Implicit)
10763 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10764 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10765 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10766 } else {
10767 // __chkstk(MSVCRT): does not update stack pointer.
10768 // Clobbers R10, R11 and EFLAGS.
10769 // FIXME: RAX(allocated size) might be reused and not killed.
10770 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10771 .addExternalSymbol("__chkstk")
10772 .addReg(X86::RAX, RegState::Implicit)
10773 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10774 // RAX has the offset to subtracted from RSP.
10775 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10776 .addReg(X86::RSP)
10777 .addReg(X86::RAX);
10778 }
10779 } else {
10780 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010781 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10782
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010783 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10784 .addExternalSymbol(StackProbeSymbol)
10785 .addReg(X86::EAX, RegState::Implicit)
10786 .addReg(X86::ESP, RegState::Implicit)
10787 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10788 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10789 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10790 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010791
Dan Gohman14152b42010-07-06 20:24:04 +000010792 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010793 return BB;
10794}
Chris Lattner52600972009-09-02 05:57:00 +000010795
10796MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010797X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10798 MachineBasicBlock *BB) const {
10799 // This is pretty easy. We're taking the value that we received from
10800 // our load from the relocation, sticking it in either RDI (x86-64)
10801 // or EAX and doing an indirect call. The return value will then
10802 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010803 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010804 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010805 DebugLoc DL = MI->getDebugLoc();
10806 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010807
10808 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010809 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010810
Eric Christopher30ef0e52010-06-03 04:07:48 +000010811 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010812 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10813 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010814 .addReg(X86::RIP)
10815 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010816 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010817 MI->getOperand(3).getTargetFlags())
10818 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010819 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010820 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010821 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010822 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10823 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010824 .addReg(0)
10825 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010826 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010827 MI->getOperand(3).getTargetFlags())
10828 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010829 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010830 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010831 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010832 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10833 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010834 .addReg(TII->getGlobalBaseReg(F))
10835 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010836 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010837 MI->getOperand(3).getTargetFlags())
10838 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010839 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010840 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010841 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010842
Dan Gohman14152b42010-07-06 20:24:04 +000010843 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010844 return BB;
10845}
10846
10847MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010848X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010849 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010850 switch (MI->getOpcode()) {
10851 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010852 case X86::TAILJMPd64:
10853 case X86::TAILJMPr64:
10854 case X86::TAILJMPm64:
10855 assert(!"TAILJMP64 would not be touched here.");
10856 case X86::TCRETURNdi64:
10857 case X86::TCRETURNri64:
10858 case X86::TCRETURNmi64:
10859 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10860 // On AMD64, additional defs should be added before register allocation.
10861 if (!Subtarget->isTargetWin64()) {
10862 MI->addRegisterDefined(X86::RSI);
10863 MI->addRegisterDefined(X86::RDI);
10864 MI->addRegisterDefined(X86::XMM6);
10865 MI->addRegisterDefined(X86::XMM7);
10866 MI->addRegisterDefined(X86::XMM8);
10867 MI->addRegisterDefined(X86::XMM9);
10868 MI->addRegisterDefined(X86::XMM10);
10869 MI->addRegisterDefined(X86::XMM11);
10870 MI->addRegisterDefined(X86::XMM12);
10871 MI->addRegisterDefined(X86::XMM13);
10872 MI->addRegisterDefined(X86::XMM14);
10873 MI->addRegisterDefined(X86::XMM15);
10874 }
10875 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010876 case X86::WIN_ALLOCA:
10877 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010878 case X86::TLSCall_32:
10879 case X86::TLSCall_64:
10880 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010881 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010882 case X86::CMOV_FR32:
10883 case X86::CMOV_FR64:
10884 case X86::CMOV_V4F32:
10885 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010886 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010887 case X86::CMOV_GR16:
10888 case X86::CMOV_GR32:
10889 case X86::CMOV_RFP32:
10890 case X86::CMOV_RFP64:
10891 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010892 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010893
Dale Johannesen849f2142007-07-03 00:53:03 +000010894 case X86::FP32_TO_INT16_IN_MEM:
10895 case X86::FP32_TO_INT32_IN_MEM:
10896 case X86::FP32_TO_INT64_IN_MEM:
10897 case X86::FP64_TO_INT16_IN_MEM:
10898 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010899 case X86::FP64_TO_INT64_IN_MEM:
10900 case X86::FP80_TO_INT16_IN_MEM:
10901 case X86::FP80_TO_INT32_IN_MEM:
10902 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010903 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10904 DebugLoc DL = MI->getDebugLoc();
10905
Evan Cheng60c07e12006-07-05 22:17:51 +000010906 // Change the floating point control register to use "round towards zero"
10907 // mode when truncating to an integer value.
10908 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010909 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010910 addFrameReference(BuildMI(*BB, MI, DL,
10911 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010912
10913 // Load the old value of the high byte of the control word...
10914 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010915 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010916 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010917 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010918
10919 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010920 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010921 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010922
10923 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010924 addFrameReference(BuildMI(*BB, MI, DL,
10925 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010926
10927 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010928 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010929 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010930
10931 // Get the X86 opcode to use.
10932 unsigned Opc;
10933 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010934 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010935 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10936 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10937 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10938 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10939 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10940 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010941 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10942 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10943 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010944 }
10945
10946 X86AddressMode AM;
10947 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010948 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010949 AM.BaseType = X86AddressMode::RegBase;
10950 AM.Base.Reg = Op.getReg();
10951 } else {
10952 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010953 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010954 }
10955 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010956 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010957 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010958 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010959 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010960 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010961 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010962 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010963 AM.GV = Op.getGlobal();
10964 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010965 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010966 }
Dan Gohman14152b42010-07-06 20:24:04 +000010967 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010968 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010969
10970 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010971 addFrameReference(BuildMI(*BB, MI, DL,
10972 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010973
Dan Gohman14152b42010-07-06 20:24:04 +000010974 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010975 return BB;
10976 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010977 // String/text processing lowering.
10978 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010979 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010980 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10981 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010982 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010983 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10984 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010985 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010986 return EmitPCMP(MI, BB, 5, false /* in mem */);
10987 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010988 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010989 return EmitPCMP(MI, BB, 5, true /* in mem */);
10990
Eric Christopher228232b2010-11-30 07:20:12 +000010991 // Thread synchronization.
10992 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010993 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010994 case X86::MWAIT:
10995 return EmitMwait(MI, BB);
10996
Eric Christopherb120ab42009-08-18 22:50:32 +000010997 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010998 case X86::ATOMAND32:
10999 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011000 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011001 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011002 X86::NOT32r, X86::EAX,
11003 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000011004 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000011005 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
11006 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011007 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011008 X86::NOT32r, X86::EAX,
11009 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000011010 case X86::ATOMXOR32:
11011 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011012 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011013 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011014 X86::NOT32r, X86::EAX,
11015 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011016 case X86::ATOMNAND32:
11017 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011018 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011019 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011020 X86::NOT32r, X86::EAX,
11021 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000011022 case X86::ATOMMIN32:
11023 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
11024 case X86::ATOMMAX32:
11025 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
11026 case X86::ATOMUMIN32:
11027 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
11028 case X86::ATOMUMAX32:
11029 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000011030
11031 case X86::ATOMAND16:
11032 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
11033 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011034 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011035 X86::NOT16r, X86::AX,
11036 X86::GR16RegisterClass);
11037 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000011038 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011039 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011040 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011041 X86::NOT16r, X86::AX,
11042 X86::GR16RegisterClass);
11043 case X86::ATOMXOR16:
11044 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
11045 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011046 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011047 X86::NOT16r, X86::AX,
11048 X86::GR16RegisterClass);
11049 case X86::ATOMNAND16:
11050 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
11051 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011052 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011053 X86::NOT16r, X86::AX,
11054 X86::GR16RegisterClass, true);
11055 case X86::ATOMMIN16:
11056 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
11057 case X86::ATOMMAX16:
11058 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
11059 case X86::ATOMUMIN16:
11060 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
11061 case X86::ATOMUMAX16:
11062 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
11063
11064 case X86::ATOMAND8:
11065 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
11066 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011067 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011068 X86::NOT8r, X86::AL,
11069 X86::GR8RegisterClass);
11070 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000011071 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011072 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011073 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011074 X86::NOT8r, X86::AL,
11075 X86::GR8RegisterClass);
11076 case X86::ATOMXOR8:
11077 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
11078 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011079 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011080 X86::NOT8r, X86::AL,
11081 X86::GR8RegisterClass);
11082 case X86::ATOMNAND8:
11083 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
11084 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011085 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011086 X86::NOT8r, X86::AL,
11087 X86::GR8RegisterClass, true);
11088 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011089 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000011090 case X86::ATOMAND64:
11091 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011092 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011093 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011094 X86::NOT64r, X86::RAX,
11095 X86::GR64RegisterClass);
11096 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000011097 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
11098 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011099 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011100 X86::NOT64r, X86::RAX,
11101 X86::GR64RegisterClass);
11102 case X86::ATOMXOR64:
11103 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011104 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011105 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011106 X86::NOT64r, X86::RAX,
11107 X86::GR64RegisterClass);
11108 case X86::ATOMNAND64:
11109 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
11110 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011111 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011112 X86::NOT64r, X86::RAX,
11113 X86::GR64RegisterClass, true);
11114 case X86::ATOMMIN64:
11115 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
11116 case X86::ATOMMAX64:
11117 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
11118 case X86::ATOMUMIN64:
11119 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
11120 case X86::ATOMUMAX64:
11121 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011122
11123 // This group does 64-bit operations on a 32-bit host.
11124 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011125 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011126 X86::AND32rr, X86::AND32rr,
11127 X86::AND32ri, X86::AND32ri,
11128 false);
11129 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011130 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011131 X86::OR32rr, X86::OR32rr,
11132 X86::OR32ri, X86::OR32ri,
11133 false);
11134 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011135 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011136 X86::XOR32rr, X86::XOR32rr,
11137 X86::XOR32ri, X86::XOR32ri,
11138 false);
11139 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011140 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011141 X86::AND32rr, X86::AND32rr,
11142 X86::AND32ri, X86::AND32ri,
11143 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011144 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011145 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011146 X86::ADD32rr, X86::ADC32rr,
11147 X86::ADD32ri, X86::ADC32ri,
11148 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011149 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011150 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011151 X86::SUB32rr, X86::SBB32rr,
11152 X86::SUB32ri, X86::SBB32ri,
11153 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000011154 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011155 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000011156 X86::MOV32rr, X86::MOV32rr,
11157 X86::MOV32ri, X86::MOV32ri,
11158 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011159 case X86::VASTART_SAVE_XMM_REGS:
11160 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000011161
11162 case X86::VAARG_64:
11163 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011164 }
11165}
11166
11167//===----------------------------------------------------------------------===//
11168// X86 Optimization Hooks
11169//===----------------------------------------------------------------------===//
11170
Dan Gohman475871a2008-07-27 21:46:04 +000011171void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000011172 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011173 APInt &KnownZero,
11174 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000011175 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000011176 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011177 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000011178 assert((Opc >= ISD::BUILTIN_OP_END ||
11179 Opc == ISD::INTRINSIC_WO_CHAIN ||
11180 Opc == ISD::INTRINSIC_W_CHAIN ||
11181 Opc == ISD::INTRINSIC_VOID) &&
11182 "Should use MaskedValueIsZero if you don't know whether Op"
11183 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011184
Dan Gohmanf4f92f52008-02-13 23:07:24 +000011185 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011186 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000011187 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011188 case X86ISD::ADD:
11189 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000011190 case X86ISD::ADC:
11191 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011192 case X86ISD::SMUL:
11193 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011194 case X86ISD::INC:
11195 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011196 case X86ISD::OR:
11197 case X86ISD::XOR:
11198 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011199 // These nodes' second result is a boolean.
11200 if (Op.getResNo() == 0)
11201 break;
11202 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011203 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011204 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11205 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011206 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011207 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011208}
Chris Lattner259e97c2006-01-31 19:43:35 +000011209
Owen Andersonbc146b02010-09-21 20:42:50 +000011210unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11211 unsigned Depth) const {
11212 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11213 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11214 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011215
Owen Andersonbc146b02010-09-21 20:42:50 +000011216 // Fallback case.
11217 return 1;
11218}
11219
Evan Cheng206ee9d2006-07-07 08:33:52 +000011220/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011221/// node is a GlobalAddress + offset.
11222bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011223 const GlobalValue* &GA,
11224 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011225 if (N->getOpcode() == X86ISD::Wrapper) {
11226 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011227 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011228 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011229 return true;
11230 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011231 }
Evan Chengad4196b2008-05-12 19:56:52 +000011232 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011233}
11234
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000011235/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
11236static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
11237 TargetLowering::DAGCombinerInfo &DCI) {
11238 DebugLoc dl = N->getDebugLoc();
11239 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
11240 SDValue V1 = SVOp->getOperand(0);
11241 SDValue V2 = SVOp->getOperand(1);
11242 EVT VT = SVOp->getValueType(0);
11243
11244 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
11245 V2.getOpcode() == ISD::CONCAT_VECTORS) {
11246 //
11247 // 0,0,0,...
11248 // \
11249 // V UNDEF BUILD_VECTOR UNDEF
11250 // \ / \ /
11251 // CONCAT_VECTOR CONCAT_VECTOR
11252 // \ /
11253 // \ /
11254 // RESULT: V + zero extended
11255 //
11256 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
11257 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
11258 V1.getOperand(1).getOpcode() != ISD::UNDEF)
11259 return SDValue();
11260
11261 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
11262 return SDValue();
11263
11264 // To match the shuffle mask, the first half of the mask should
11265 // be exactly the first vector, and all the rest a splat with the
11266 // first element of the second one.
11267 int NumElems = VT.getVectorNumElements();
11268 for (int i = 0; i < NumElems/2; ++i)
11269 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
11270 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
11271 return SDValue();
11272
11273 // Emit a zeroed vector and insert the desired subvector on its
11274 // first half.
11275 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, DAG, dl);
11276 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
11277 DAG.getConstant(0, MVT::i32), DAG, dl);
11278 return DCI.CombineTo(N, InsV);
11279 }
11280
11281 return SDValue();
11282}
11283
11284/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000011285static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011286 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011287 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011288 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011289
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011290 // Don't create instructions with illegal types after legalize types has run.
11291 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11292 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11293 return SDValue();
11294
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000011295 // Only handle pure VECTOR_SHUFFLE nodes.
11296 if (VT.getSizeInBits() == 256 && N->getOpcode() == ISD::VECTOR_SHUFFLE)
11297 return PerformShuffleCombine256(N, DAG, DCI);
11298
11299 // Only handle 128 wide vector from here on.
11300 if (VT.getSizeInBits() != 128)
11301 return SDValue();
11302
11303 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
11304 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
11305 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000011306 SmallVector<SDValue, 16> Elts;
11307 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011308 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011309
Nate Begemanfdea31a2010-03-24 20:49:50 +000011310 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011311}
Evan Chengd880b972008-05-09 21:53:03 +000011312
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011313/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11314/// generation and convert it from being a bunch of shuffles and extracts
11315/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011316static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11317 const TargetLowering &TLI) {
11318 SDValue InputVector = N->getOperand(0);
11319
11320 // Only operate on vectors of 4 elements, where the alternative shuffling
11321 // gets to be more expensive.
11322 if (InputVector.getValueType() != MVT::v4i32)
11323 return SDValue();
11324
11325 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11326 // single use which is a sign-extend or zero-extend, and all elements are
11327 // used.
11328 SmallVector<SDNode *, 4> Uses;
11329 unsigned ExtractedElements = 0;
11330 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11331 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11332 if (UI.getUse().getResNo() != InputVector.getResNo())
11333 return SDValue();
11334
11335 SDNode *Extract = *UI;
11336 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11337 return SDValue();
11338
11339 if (Extract->getValueType(0) != MVT::i32)
11340 return SDValue();
11341 if (!Extract->hasOneUse())
11342 return SDValue();
11343 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11344 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11345 return SDValue();
11346 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11347 return SDValue();
11348
11349 // Record which element was extracted.
11350 ExtractedElements |=
11351 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11352
11353 Uses.push_back(Extract);
11354 }
11355
11356 // If not all the elements were used, this may not be worthwhile.
11357 if (ExtractedElements != 15)
11358 return SDValue();
11359
11360 // Ok, we've now decided to do the transformation.
11361 DebugLoc dl = InputVector.getDebugLoc();
11362
11363 // Store the value to a temporary stack slot.
11364 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011365 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11366 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011367
11368 // Replace each use (extract) with a load of the appropriate element.
11369 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11370 UE = Uses.end(); UI != UE; ++UI) {
11371 SDNode *Extract = *UI;
11372
Nadav Rotem86694292011-05-17 08:31:57 +000011373 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011374 SDValue Idx = Extract->getOperand(1);
11375 unsigned EltSize =
11376 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11377 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11378 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11379
Nadav Rotem86694292011-05-17 08:31:57 +000011380 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011381 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011382
11383 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011384 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011385 ScalarAddr, MachinePointerInfo(),
11386 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011387
11388 // Replace the exact with the load.
11389 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11390 }
11391
11392 // The replacement was made in place; don't return anything.
11393 return SDValue();
11394}
11395
Chris Lattner83e6c992006-10-04 06:57:07 +000011396/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011397static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011398 const X86Subtarget *Subtarget) {
11399 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011400 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011401 // Get the LHS/RHS of the select.
11402 SDValue LHS = N->getOperand(1);
11403 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011404
Dan Gohman670e5392009-09-21 18:03:22 +000011405 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011406 // instructions match the semantics of the common C idiom x<y?x:y but not
11407 // x<=y?x:y, because of how they handle negative zero (which can be
11408 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011409 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011410 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011411 Cond.getOpcode() == ISD::SETCC) {
11412 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011413
Chris Lattner47b4ce82009-03-11 05:48:52 +000011414 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011415 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011416 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11417 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011418 switch (CC) {
11419 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011420 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011421 // Converting this to a min would handle NaNs incorrectly, and swapping
11422 // the operands would cause it to handle comparisons between positive
11423 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011424 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011425 if (!UnsafeFPMath &&
11426 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11427 break;
11428 std::swap(LHS, RHS);
11429 }
Dan Gohman670e5392009-09-21 18:03:22 +000011430 Opcode = X86ISD::FMIN;
11431 break;
11432 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011433 // Converting this to a min would handle comparisons between positive
11434 // and negative zero incorrectly.
11435 if (!UnsafeFPMath &&
11436 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11437 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011438 Opcode = X86ISD::FMIN;
11439 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011440 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011441 // Converting this to a min would handle both negative zeros and NaNs
11442 // incorrectly, but we can swap the operands to fix both.
11443 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011444 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011445 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011446 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011447 Opcode = X86ISD::FMIN;
11448 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011449
Dan Gohman670e5392009-09-21 18:03:22 +000011450 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011451 // Converting this to a max would handle comparisons between positive
11452 // and negative zero incorrectly.
11453 if (!UnsafeFPMath &&
11454 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11455 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011456 Opcode = X86ISD::FMAX;
11457 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011458 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011459 // Converting this to a max would handle NaNs incorrectly, and swapping
11460 // the operands would cause it to handle comparisons between positive
11461 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011462 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011463 if (!UnsafeFPMath &&
11464 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11465 break;
11466 std::swap(LHS, RHS);
11467 }
Dan Gohman670e5392009-09-21 18:03:22 +000011468 Opcode = X86ISD::FMAX;
11469 break;
11470 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011471 // Converting this to a max would handle both negative zeros and NaNs
11472 // incorrectly, but we can swap the operands to fix both.
11473 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011474 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011475 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011476 case ISD::SETGE:
11477 Opcode = X86ISD::FMAX;
11478 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011479 }
Dan Gohman670e5392009-09-21 18:03:22 +000011480 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011481 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11482 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011483 switch (CC) {
11484 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011485 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011486 // Converting this to a min would handle comparisons between positive
11487 // and negative zero incorrectly, and swapping the operands would
11488 // cause it to handle NaNs incorrectly.
11489 if (!UnsafeFPMath &&
11490 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011491 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011492 break;
11493 std::swap(LHS, RHS);
11494 }
Dan Gohman670e5392009-09-21 18:03:22 +000011495 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011496 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011497 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011498 // Converting this to a min would handle NaNs incorrectly.
11499 if (!UnsafeFPMath &&
11500 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11501 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011502 Opcode = X86ISD::FMIN;
11503 break;
11504 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011505 // Converting this to a min would handle both negative zeros and NaNs
11506 // incorrectly, but we can swap the operands to fix both.
11507 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011508 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011509 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011510 case ISD::SETGE:
11511 Opcode = X86ISD::FMIN;
11512 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011513
Dan Gohman670e5392009-09-21 18:03:22 +000011514 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011515 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011516 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011517 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011518 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011519 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011520 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011521 // Converting this to a max would handle comparisons between positive
11522 // and negative zero incorrectly, and swapping the operands would
11523 // cause it to handle NaNs incorrectly.
11524 if (!UnsafeFPMath &&
11525 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011526 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011527 break;
11528 std::swap(LHS, RHS);
11529 }
Dan Gohman670e5392009-09-21 18:03:22 +000011530 Opcode = X86ISD::FMAX;
11531 break;
11532 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011533 // Converting this to a max would handle both negative zeros and NaNs
11534 // incorrectly, but we can swap the operands to fix both.
11535 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011536 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011537 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011538 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011539 Opcode = X86ISD::FMAX;
11540 break;
11541 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011542 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011543
Chris Lattner47b4ce82009-03-11 05:48:52 +000011544 if (Opcode)
11545 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011546 }
Eric Christopherfd179292009-08-27 18:07:15 +000011547
Chris Lattnerd1980a52009-03-12 06:52:53 +000011548 // If this is a select between two integer constants, try to do some
11549 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011550 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11551 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011552 // Don't do this for crazy integer types.
11553 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11554 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011555 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011556 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011557
Chris Lattnercee56e72009-03-13 05:53:31 +000011558 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011559 // Efficiently invertible.
11560 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11561 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11562 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11563 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011564 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011565 }
Eric Christopherfd179292009-08-27 18:07:15 +000011566
Chris Lattnerd1980a52009-03-12 06:52:53 +000011567 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011568 if (FalseC->getAPIntValue() == 0 &&
11569 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011570 if (NeedsCondInvert) // Invert the condition if needed.
11571 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11572 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011573
Chris Lattnerd1980a52009-03-12 06:52:53 +000011574 // Zero extend the condition if needed.
11575 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011576
Chris Lattnercee56e72009-03-13 05:53:31 +000011577 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011578 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011579 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011580 }
Eric Christopherfd179292009-08-27 18:07:15 +000011581
Chris Lattner97a29a52009-03-13 05:22:11 +000011582 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011583 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011584 if (NeedsCondInvert) // Invert the condition if needed.
11585 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11586 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011587
Chris Lattner97a29a52009-03-13 05:22:11 +000011588 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011589 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11590 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011591 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011592 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011593 }
Eric Christopherfd179292009-08-27 18:07:15 +000011594
Chris Lattnercee56e72009-03-13 05:53:31 +000011595 // Optimize cases that will turn into an LEA instruction. This requires
11596 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011597 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011598 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011599 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011600
Chris Lattnercee56e72009-03-13 05:53:31 +000011601 bool isFastMultiplier = false;
11602 if (Diff < 10) {
11603 switch ((unsigned char)Diff) {
11604 default: break;
11605 case 1: // result = add base, cond
11606 case 2: // result = lea base( , cond*2)
11607 case 3: // result = lea base(cond, cond*2)
11608 case 4: // result = lea base( , cond*4)
11609 case 5: // result = lea base(cond, cond*4)
11610 case 8: // result = lea base( , cond*8)
11611 case 9: // result = lea base(cond, cond*8)
11612 isFastMultiplier = true;
11613 break;
11614 }
11615 }
Eric Christopherfd179292009-08-27 18:07:15 +000011616
Chris Lattnercee56e72009-03-13 05:53:31 +000011617 if (isFastMultiplier) {
11618 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11619 if (NeedsCondInvert) // Invert the condition if needed.
11620 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11621 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011622
Chris Lattnercee56e72009-03-13 05:53:31 +000011623 // Zero extend the condition if needed.
11624 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11625 Cond);
11626 // Scale the condition by the difference.
11627 if (Diff != 1)
11628 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11629 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011630
Chris Lattnercee56e72009-03-13 05:53:31 +000011631 // Add the base if non-zero.
11632 if (FalseC->getAPIntValue() != 0)
11633 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11634 SDValue(FalseC, 0));
11635 return Cond;
11636 }
Eric Christopherfd179292009-08-27 18:07:15 +000011637 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011638 }
11639 }
Eric Christopherfd179292009-08-27 18:07:15 +000011640
Dan Gohman475871a2008-07-27 21:46:04 +000011641 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011642}
11643
Chris Lattnerd1980a52009-03-12 06:52:53 +000011644/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11645static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11646 TargetLowering::DAGCombinerInfo &DCI) {
11647 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011648
Chris Lattnerd1980a52009-03-12 06:52:53 +000011649 // If the flag operand isn't dead, don't touch this CMOV.
11650 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11651 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011652
Evan Chengb5a55d92011-05-24 01:48:22 +000011653 SDValue FalseOp = N->getOperand(0);
11654 SDValue TrueOp = N->getOperand(1);
11655 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11656 SDValue Cond = N->getOperand(3);
11657 if (CC == X86::COND_E || CC == X86::COND_NE) {
11658 switch (Cond.getOpcode()) {
11659 default: break;
11660 case X86ISD::BSR:
11661 case X86ISD::BSF:
11662 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11663 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11664 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11665 }
11666 }
11667
Chris Lattnerd1980a52009-03-12 06:52:53 +000011668 // If this is a select between two integer constants, try to do some
11669 // optimizations. Note that the operands are ordered the opposite of SELECT
11670 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011671 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11672 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011673 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11674 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011675 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11676 CC = X86::GetOppositeBranchCondition(CC);
11677 std::swap(TrueC, FalseC);
11678 }
Eric Christopherfd179292009-08-27 18:07:15 +000011679
Chris Lattnerd1980a52009-03-12 06:52:53 +000011680 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011681 // This is efficient for any integer data type (including i8/i16) and
11682 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011683 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011684 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11685 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011686
Chris Lattnerd1980a52009-03-12 06:52:53 +000011687 // Zero extend the condition if needed.
11688 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011689
Chris Lattnerd1980a52009-03-12 06:52:53 +000011690 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11691 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011692 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011693 if (N->getNumValues() == 2) // Dead flag value?
11694 return DCI.CombineTo(N, Cond, SDValue());
11695 return Cond;
11696 }
Eric Christopherfd179292009-08-27 18:07:15 +000011697
Chris Lattnercee56e72009-03-13 05:53:31 +000011698 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11699 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011700 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011701 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11702 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011703
Chris Lattner97a29a52009-03-13 05:22:11 +000011704 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011705 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11706 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011707 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11708 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011709
Chris Lattner97a29a52009-03-13 05:22:11 +000011710 if (N->getNumValues() == 2) // Dead flag value?
11711 return DCI.CombineTo(N, Cond, SDValue());
11712 return Cond;
11713 }
Eric Christopherfd179292009-08-27 18:07:15 +000011714
Chris Lattnercee56e72009-03-13 05:53:31 +000011715 // Optimize cases that will turn into an LEA instruction. This requires
11716 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011717 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011718 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011719 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011720
Chris Lattnercee56e72009-03-13 05:53:31 +000011721 bool isFastMultiplier = false;
11722 if (Diff < 10) {
11723 switch ((unsigned char)Diff) {
11724 default: break;
11725 case 1: // result = add base, cond
11726 case 2: // result = lea base( , cond*2)
11727 case 3: // result = lea base(cond, cond*2)
11728 case 4: // result = lea base( , cond*4)
11729 case 5: // result = lea base(cond, cond*4)
11730 case 8: // result = lea base( , cond*8)
11731 case 9: // result = lea base(cond, cond*8)
11732 isFastMultiplier = true;
11733 break;
11734 }
11735 }
Eric Christopherfd179292009-08-27 18:07:15 +000011736
Chris Lattnercee56e72009-03-13 05:53:31 +000011737 if (isFastMultiplier) {
11738 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011739 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11740 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011741 // Zero extend the condition if needed.
11742 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11743 Cond);
11744 // Scale the condition by the difference.
11745 if (Diff != 1)
11746 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11747 DAG.getConstant(Diff, Cond.getValueType()));
11748
11749 // Add the base if non-zero.
11750 if (FalseC->getAPIntValue() != 0)
11751 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11752 SDValue(FalseC, 0));
11753 if (N->getNumValues() == 2) // Dead flag value?
11754 return DCI.CombineTo(N, Cond, SDValue());
11755 return Cond;
11756 }
Eric Christopherfd179292009-08-27 18:07:15 +000011757 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011758 }
11759 }
11760 return SDValue();
11761}
11762
11763
Evan Cheng0b0cd912009-03-28 05:57:29 +000011764/// PerformMulCombine - Optimize a single multiply with constant into two
11765/// in order to implement it with two cheaper instructions, e.g.
11766/// LEA + SHL, LEA + LEA.
11767static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11768 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011769 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11770 return SDValue();
11771
Owen Andersone50ed302009-08-10 22:56:29 +000011772 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011773 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011774 return SDValue();
11775
11776 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11777 if (!C)
11778 return SDValue();
11779 uint64_t MulAmt = C->getZExtValue();
11780 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11781 return SDValue();
11782
11783 uint64_t MulAmt1 = 0;
11784 uint64_t MulAmt2 = 0;
11785 if ((MulAmt % 9) == 0) {
11786 MulAmt1 = 9;
11787 MulAmt2 = MulAmt / 9;
11788 } else if ((MulAmt % 5) == 0) {
11789 MulAmt1 = 5;
11790 MulAmt2 = MulAmt / 5;
11791 } else if ((MulAmt % 3) == 0) {
11792 MulAmt1 = 3;
11793 MulAmt2 = MulAmt / 3;
11794 }
11795 if (MulAmt2 &&
11796 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11797 DebugLoc DL = N->getDebugLoc();
11798
11799 if (isPowerOf2_64(MulAmt2) &&
11800 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11801 // If second multiplifer is pow2, issue it first. We want the multiply by
11802 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11803 // is an add.
11804 std::swap(MulAmt1, MulAmt2);
11805
11806 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011807 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011808 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011809 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011810 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011811 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011812 DAG.getConstant(MulAmt1, VT));
11813
Eric Christopherfd179292009-08-27 18:07:15 +000011814 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011815 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011816 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011817 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011818 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011819 DAG.getConstant(MulAmt2, VT));
11820
11821 // Do not add new nodes to DAG combiner worklist.
11822 DCI.CombineTo(N, NewMul, false);
11823 }
11824 return SDValue();
11825}
11826
Evan Chengad9c0a32009-12-15 00:53:42 +000011827static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11828 SDValue N0 = N->getOperand(0);
11829 SDValue N1 = N->getOperand(1);
11830 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11831 EVT VT = N0.getValueType();
11832
11833 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11834 // since the result of setcc_c is all zero's or all ones.
11835 if (N1C && N0.getOpcode() == ISD::AND &&
11836 N0.getOperand(1).getOpcode() == ISD::Constant) {
11837 SDValue N00 = N0.getOperand(0);
11838 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11839 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11840 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11841 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11842 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11843 APInt ShAmt = N1C->getAPIntValue();
11844 Mask = Mask.shl(ShAmt);
11845 if (Mask != 0)
11846 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11847 N00, DAG.getConstant(Mask, VT));
11848 }
11849 }
11850
11851 return SDValue();
11852}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011853
Nate Begeman740ab032009-01-26 00:52:55 +000011854/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11855/// when possible.
11856static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11857 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011858 EVT VT = N->getValueType(0);
11859 if (!VT.isVector() && VT.isInteger() &&
11860 N->getOpcode() == ISD::SHL)
11861 return PerformSHLCombine(N, DAG);
11862
Nate Begeman740ab032009-01-26 00:52:55 +000011863 // On X86 with SSE2 support, we can transform this to a vector shift if
11864 // all elements are shifted by the same amount. We can't do this in legalize
11865 // because the a constant vector is typically transformed to a constant pool
11866 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011867 if (!Subtarget->hasSSE2())
11868 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011869
Owen Anderson825b72b2009-08-11 20:47:22 +000011870 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011871 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011872
Mon P Wang3becd092009-01-28 08:12:05 +000011873 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011874 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011875 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011876 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011877 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11878 unsigned NumElts = VT.getVectorNumElements();
11879 unsigned i = 0;
11880 for (; i != NumElts; ++i) {
11881 SDValue Arg = ShAmtOp.getOperand(i);
11882 if (Arg.getOpcode() == ISD::UNDEF) continue;
11883 BaseShAmt = Arg;
11884 break;
11885 }
11886 for (; i != NumElts; ++i) {
11887 SDValue Arg = ShAmtOp.getOperand(i);
11888 if (Arg.getOpcode() == ISD::UNDEF) continue;
11889 if (Arg != BaseShAmt) {
11890 return SDValue();
11891 }
11892 }
11893 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011894 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011895 SDValue InVec = ShAmtOp.getOperand(0);
11896 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11897 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11898 unsigned i = 0;
11899 for (; i != NumElts; ++i) {
11900 SDValue Arg = InVec.getOperand(i);
11901 if (Arg.getOpcode() == ISD::UNDEF) continue;
11902 BaseShAmt = Arg;
11903 break;
11904 }
11905 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11906 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011907 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011908 if (C->getZExtValue() == SplatIdx)
11909 BaseShAmt = InVec.getOperand(1);
11910 }
11911 }
11912 if (BaseShAmt.getNode() == 0)
11913 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11914 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011915 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011916 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011917
Mon P Wangefa42202009-09-03 19:56:25 +000011918 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011919 if (EltVT.bitsGT(MVT::i32))
11920 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11921 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011922 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011923
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011924 // The shift amount is identical so we can do a vector shift.
11925 SDValue ValOp = N->getOperand(0);
11926 switch (N->getOpcode()) {
11927 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011928 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011929 break;
11930 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011931 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011932 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011933 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011934 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011935 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011936 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011937 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011938 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011939 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011940 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011941 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011942 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011943 break;
11944 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011945 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011946 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011947 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011948 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011949 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011950 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011951 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011952 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011953 break;
11954 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011955 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011956 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011957 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011958 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011959 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011960 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011961 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011962 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011963 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011964 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011965 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011966 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011967 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011968 }
11969 return SDValue();
11970}
11971
Nate Begemanb65c1752010-12-17 22:55:37 +000011972
Stuart Hastings865f0932011-06-03 23:53:54 +000011973// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11974// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11975// and friends. Likewise for OR -> CMPNEQSS.
11976static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11977 TargetLowering::DAGCombinerInfo &DCI,
11978 const X86Subtarget *Subtarget) {
11979 unsigned opcode;
11980
11981 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11982 // we're requiring SSE2 for both.
11983 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11984 SDValue N0 = N->getOperand(0);
11985 SDValue N1 = N->getOperand(1);
11986 SDValue CMP0 = N0->getOperand(1);
11987 SDValue CMP1 = N1->getOperand(1);
11988 DebugLoc DL = N->getDebugLoc();
11989
11990 // The SETCCs should both refer to the same CMP.
11991 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11992 return SDValue();
11993
11994 SDValue CMP00 = CMP0->getOperand(0);
11995 SDValue CMP01 = CMP0->getOperand(1);
11996 EVT VT = CMP00.getValueType();
11997
11998 if (VT == MVT::f32 || VT == MVT::f64) {
11999 bool ExpectingFlags = false;
12000 // Check for any users that want flags:
12001 for (SDNode::use_iterator UI = N->use_begin(),
12002 UE = N->use_end();
12003 !ExpectingFlags && UI != UE; ++UI)
12004 switch (UI->getOpcode()) {
12005 default:
12006 case ISD::BR_CC:
12007 case ISD::BRCOND:
12008 case ISD::SELECT:
12009 ExpectingFlags = true;
12010 break;
12011 case ISD::CopyToReg:
12012 case ISD::SIGN_EXTEND:
12013 case ISD::ZERO_EXTEND:
12014 case ISD::ANY_EXTEND:
12015 break;
12016 }
12017
12018 if (!ExpectingFlags) {
12019 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
12020 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
12021
12022 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
12023 X86::CondCode tmp = cc0;
12024 cc0 = cc1;
12025 cc1 = tmp;
12026 }
12027
12028 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
12029 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
12030 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
12031 X86ISD::NodeType NTOperator = is64BitFP ?
12032 X86ISD::FSETCCsd : X86ISD::FSETCCss;
12033 // FIXME: need symbolic constants for these magic numbers.
12034 // See X86ATTInstPrinter.cpp:printSSECC().
12035 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
12036 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
12037 DAG.getConstant(x86cc, MVT::i8));
12038 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
12039 OnesOrZeroesF);
12040 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
12041 DAG.getConstant(1, MVT::i32));
12042 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
12043 return OneBitOfTruth;
12044 }
12045 }
12046 }
12047 }
12048 return SDValue();
12049}
12050
Nate Begemanb65c1752010-12-17 22:55:37 +000012051static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
12052 TargetLowering::DAGCombinerInfo &DCI,
12053 const X86Subtarget *Subtarget) {
12054 if (DCI.isBeforeLegalizeOps())
12055 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012056
Stuart Hastings865f0932011-06-03 23:53:54 +000012057 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
12058 if (R.getNode())
12059 return R;
12060
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000012061 // Want to form ANDNP nodes:
12062 // 1) In the hopes of then easily combining them with OR and AND nodes
12063 // to form PBLEND/PSIGN.
12064 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000012065 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000012066 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000012067 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012068
Nate Begemanb65c1752010-12-17 22:55:37 +000012069 SDValue N0 = N->getOperand(0);
12070 SDValue N1 = N->getOperand(1);
12071 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012072
Nate Begemanb65c1752010-12-17 22:55:37 +000012073 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012074 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000012075 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012076 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000012077
12078 // Check RHS for vnot
12079 if (N1.getOpcode() == ISD::XOR &&
12080 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012081 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012082
Nate Begemanb65c1752010-12-17 22:55:37 +000012083 return SDValue();
12084}
12085
Evan Cheng760d1942010-01-04 21:22:48 +000012086static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000012087 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000012088 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000012089 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000012090 return SDValue();
12091
Stuart Hastings865f0932011-06-03 23:53:54 +000012092 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
12093 if (R.getNode())
12094 return R;
12095
Evan Cheng760d1942010-01-04 21:22:48 +000012096 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000012097 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000012098 return SDValue();
12099
Evan Cheng760d1942010-01-04 21:22:48 +000012100 SDValue N0 = N->getOperand(0);
12101 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012102
Nate Begemanb65c1752010-12-17 22:55:37 +000012103 // look for psign/blend
12104 if (Subtarget->hasSSSE3()) {
12105 if (VT == MVT::v2i64) {
12106 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012107 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000012108 std::swap(N0, N1);
12109 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012110 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000012111 SDValue Mask = N1.getOperand(0);
12112 SDValue X = N1.getOperand(1);
12113 SDValue Y;
12114 if (N0.getOperand(0) == Mask)
12115 Y = N0.getOperand(1);
12116 if (N0.getOperand(1) == Mask)
12117 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012118
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012119 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000012120 if (!Y.getNode())
12121 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012122
Nate Begemanb65c1752010-12-17 22:55:37 +000012123 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
12124 if (Mask.getOpcode() != ISD::BITCAST ||
12125 X.getOpcode() != ISD::BITCAST ||
12126 Y.getOpcode() != ISD::BITCAST)
12127 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012128
Nate Begemanb65c1752010-12-17 22:55:37 +000012129 // Look through mask bitcast.
12130 Mask = Mask.getOperand(0);
12131 EVT MaskVT = Mask.getValueType();
12132
12133 // Validate that the Mask operand is a vector sra node. The sra node
12134 // will be an intrinsic.
12135 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
12136 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012137
Nate Begemanb65c1752010-12-17 22:55:37 +000012138 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
12139 // there is no psrai.b
12140 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
12141 case Intrinsic::x86_sse2_psrai_w:
12142 case Intrinsic::x86_sse2_psrai_d:
12143 break;
12144 default: return SDValue();
12145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012146
Nate Begemanb65c1752010-12-17 22:55:37 +000012147 // Check that the SRA is all signbits.
12148 SDValue SraC = Mask.getOperand(2);
12149 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
12150 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
12151 if ((SraAmt + 1) != EltBits)
12152 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012153
Nate Begemanb65c1752010-12-17 22:55:37 +000012154 DebugLoc DL = N->getDebugLoc();
12155
12156 // Now we know we at least have a plendvb with the mask val. See if
12157 // we can form a psignb/w/d.
12158 // psign = x.type == y.type == mask.type && y = sub(0, x);
12159 X = X.getOperand(0);
12160 Y = Y.getOperand(0);
12161 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
12162 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
12163 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
12164 unsigned Opc = 0;
12165 switch (EltBits) {
12166 case 8: Opc = X86ISD::PSIGNB; break;
12167 case 16: Opc = X86ISD::PSIGNW; break;
12168 case 32: Opc = X86ISD::PSIGND; break;
12169 default: break;
12170 }
12171 if (Opc) {
12172 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
12173 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
12174 }
12175 }
12176 // PBLENDVB only available on SSE 4.1
12177 if (!Subtarget->hasSSE41())
12178 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012179
Nate Begemanb65c1752010-12-17 22:55:37 +000012180 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
12181 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
12182 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000012183 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000012184 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
12185 }
12186 }
12187 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012188
Nate Begemanb65c1752010-12-17 22:55:37 +000012189 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000012190 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
12191 std::swap(N0, N1);
12192 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
12193 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000012194 if (!N0.hasOneUse() || !N1.hasOneUse())
12195 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000012196
12197 SDValue ShAmt0 = N0.getOperand(1);
12198 if (ShAmt0.getValueType() != MVT::i8)
12199 return SDValue();
12200 SDValue ShAmt1 = N1.getOperand(1);
12201 if (ShAmt1.getValueType() != MVT::i8)
12202 return SDValue();
12203 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
12204 ShAmt0 = ShAmt0.getOperand(0);
12205 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
12206 ShAmt1 = ShAmt1.getOperand(0);
12207
12208 DebugLoc DL = N->getDebugLoc();
12209 unsigned Opc = X86ISD::SHLD;
12210 SDValue Op0 = N0.getOperand(0);
12211 SDValue Op1 = N1.getOperand(0);
12212 if (ShAmt0.getOpcode() == ISD::SUB) {
12213 Opc = X86ISD::SHRD;
12214 std::swap(Op0, Op1);
12215 std::swap(ShAmt0, ShAmt1);
12216 }
12217
Evan Cheng8b1190a2010-04-28 01:18:01 +000012218 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000012219 if (ShAmt1.getOpcode() == ISD::SUB) {
12220 SDValue Sum = ShAmt1.getOperand(0);
12221 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000012222 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
12223 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
12224 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
12225 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000012226 return DAG.getNode(Opc, DL, VT,
12227 Op0, Op1,
12228 DAG.getNode(ISD::TRUNCATE, DL,
12229 MVT::i8, ShAmt0));
12230 }
12231 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
12232 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
12233 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000012234 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000012235 return DAG.getNode(Opc, DL, VT,
12236 N0.getOperand(0), N1.getOperand(0),
12237 DAG.getNode(ISD::TRUNCATE, DL,
12238 MVT::i8, ShAmt0));
12239 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012240
Evan Cheng760d1942010-01-04 21:22:48 +000012241 return SDValue();
12242}
12243
Chris Lattner149a4e52008-02-22 02:09:43 +000012244/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012245static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000012246 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000012247 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
12248 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000012249 // A preferable solution to the general problem is to figure out the right
12250 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000012251
12252 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012253 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012254 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012255 if (VT.getSizeInBits() != 64)
12256 return SDValue();
12257
Devang Patel578efa92009-06-05 21:57:13 +000012258 const Function *F = DAG.getMachineFunction().getFunction();
12259 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012260 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012261 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012262 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012263 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012264 isa<LoadSDNode>(St->getValue()) &&
12265 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12266 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012267 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012268 LoadSDNode *Ld = 0;
12269 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012270 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012271 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012272 // Must be a store of a load. We currently handle two cases: the load
12273 // is a direct child, and it's under an intervening TokenFactor. It is
12274 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012275 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012276 Ld = cast<LoadSDNode>(St->getChain());
12277 else if (St->getValue().hasOneUse() &&
12278 ChainVal->getOpcode() == ISD::TokenFactor) {
12279 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012280 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012281 TokenFactorIndex = i;
12282 Ld = cast<LoadSDNode>(St->getValue());
12283 } else
12284 Ops.push_back(ChainVal->getOperand(i));
12285 }
12286 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012287
Evan Cheng536e6672009-03-12 05:59:15 +000012288 if (!Ld || !ISD::isNormalLoad(Ld))
12289 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012290
Evan Cheng536e6672009-03-12 05:59:15 +000012291 // If this is not the MMX case, i.e. we are just turning i64 load/store
12292 // into f64 load/store, avoid the transformation if there are multiple
12293 // uses of the loaded value.
12294 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12295 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012296
Evan Cheng536e6672009-03-12 05:59:15 +000012297 DebugLoc LdDL = Ld->getDebugLoc();
12298 DebugLoc StDL = N->getDebugLoc();
12299 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12300 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12301 // pair instead.
12302 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012303 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012304 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12305 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012306 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012307 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012308 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012309 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012310 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012311 Ops.size());
12312 }
Evan Cheng536e6672009-03-12 05:59:15 +000012313 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012314 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012315 St->isVolatile(), St->isNonTemporal(),
12316 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012317 }
Evan Cheng536e6672009-03-12 05:59:15 +000012318
12319 // Otherwise, lower to two pairs of 32-bit loads / stores.
12320 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012321 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12322 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012323
Owen Anderson825b72b2009-08-11 20:47:22 +000012324 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012325 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012326 Ld->isVolatile(), Ld->isNonTemporal(),
12327 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012328 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012329 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012330 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012331 MinAlign(Ld->getAlignment(), 4));
12332
12333 SDValue NewChain = LoLd.getValue(1);
12334 if (TokenFactorIndex != -1) {
12335 Ops.push_back(LoLd);
12336 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012337 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012338 Ops.size());
12339 }
12340
12341 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012342 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12343 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012344
12345 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012346 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012347 St->isVolatile(), St->isNonTemporal(),
12348 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012349 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012350 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012351 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012352 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012353 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012354 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012355 }
Dan Gohman475871a2008-07-27 21:46:04 +000012356 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012357}
12358
Chris Lattner6cf73262008-01-25 06:14:17 +000012359/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12360/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012361static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012362 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12363 // F[X]OR(0.0, x) -> x
12364 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012365 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12366 if (C->getValueAPF().isPosZero())
12367 return N->getOperand(1);
12368 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12369 if (C->getValueAPF().isPosZero())
12370 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012371 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012372}
12373
12374/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012375static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012376 // FAND(0.0, x) -> 0.0
12377 // FAND(x, 0.0) -> 0.0
12378 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12379 if (C->getValueAPF().isPosZero())
12380 return N->getOperand(0);
12381 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12382 if (C->getValueAPF().isPosZero())
12383 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012384 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012385}
12386
Dan Gohmane5af2d32009-01-29 01:59:02 +000012387static SDValue PerformBTCombine(SDNode *N,
12388 SelectionDAG &DAG,
12389 TargetLowering::DAGCombinerInfo &DCI) {
12390 // BT ignores high bits in the bit index operand.
12391 SDValue Op1 = N->getOperand(1);
12392 if (Op1.hasOneUse()) {
12393 unsigned BitWidth = Op1.getValueSizeInBits();
12394 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12395 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012396 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12397 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012398 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012399 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12400 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12401 DCI.CommitTargetLoweringOpt(TLO);
12402 }
12403 return SDValue();
12404}
Chris Lattner83e6c992006-10-04 06:57:07 +000012405
Eli Friedman7a5e5552009-06-07 06:52:44 +000012406static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12407 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012408 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012409 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012410 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012411 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012412 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012413 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012414 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012415 }
12416 return SDValue();
12417}
12418
Evan Cheng2e489c42009-12-16 00:53:11 +000012419static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12420 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12421 // (and (i32 x86isd::setcc_carry), 1)
12422 // This eliminates the zext. This transformation is necessary because
12423 // ISD::SETCC is always legalized to i8.
12424 DebugLoc dl = N->getDebugLoc();
12425 SDValue N0 = N->getOperand(0);
12426 EVT VT = N->getValueType(0);
12427 if (N0.getOpcode() == ISD::AND &&
12428 N0.hasOneUse() &&
12429 N0.getOperand(0).hasOneUse()) {
12430 SDValue N00 = N0.getOperand(0);
12431 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12432 return SDValue();
12433 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12434 if (!C || C->getZExtValue() != 1)
12435 return SDValue();
12436 return DAG.getNode(ISD::AND, dl, VT,
12437 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12438 N00.getOperand(0), N00.getOperand(1)),
12439 DAG.getConstant(1, VT));
12440 }
12441
12442 return SDValue();
12443}
12444
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012445// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12446static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12447 unsigned X86CC = N->getConstantOperandVal(0);
12448 SDValue EFLAG = N->getOperand(1);
12449 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012450
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012451 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12452 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12453 // cases.
12454 if (X86CC == X86::COND_B)
12455 return DAG.getNode(ISD::AND, DL, MVT::i8,
12456 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12457 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12458 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012459
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012460 return SDValue();
12461}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012462
Benjamin Kramer1396c402011-06-18 11:09:41 +000012463static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12464 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012465 SDValue Op0 = N->getOperand(0);
12466 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12467 // a 32-bit target where SSE doesn't support i64->FP operations.
12468 if (Op0.getOpcode() == ISD::LOAD) {
12469 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12470 EVT VT = Ld->getValueType(0);
12471 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12472 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12473 !XTLI->getSubtarget()->is64Bit() &&
12474 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012475 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12476 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012477 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12478 return FILDChain;
12479 }
12480 }
12481 return SDValue();
12482}
12483
Chris Lattner23a01992010-12-20 01:37:09 +000012484// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12485static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12486 X86TargetLowering::DAGCombinerInfo &DCI) {
12487 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12488 // the result is either zero or one (depending on the input carry bit).
12489 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12490 if (X86::isZeroNode(N->getOperand(0)) &&
12491 X86::isZeroNode(N->getOperand(1)) &&
12492 // We don't have a good way to replace an EFLAGS use, so only do this when
12493 // dead right now.
12494 SDValue(N, 1).use_empty()) {
12495 DebugLoc DL = N->getDebugLoc();
12496 EVT VT = N->getValueType(0);
12497 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12498 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12499 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12500 DAG.getConstant(X86::COND_B,MVT::i8),
12501 N->getOperand(2)),
12502 DAG.getConstant(1, VT));
12503 return DCI.CombineTo(N, Res1, CarryOut);
12504 }
12505
12506 return SDValue();
12507}
12508
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012509// fold (add Y, (sete X, 0)) -> adc 0, Y
12510// (add Y, (setne X, 0)) -> sbb -1, Y
12511// (sub (sete X, 0), Y) -> sbb 0, Y
12512// (sub (setne X, 0), Y) -> adc -1, Y
12513static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12514 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012515
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012516 // Look through ZExts.
12517 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12518 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12519 return SDValue();
12520
12521 SDValue SetCC = Ext.getOperand(0);
12522 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12523 return SDValue();
12524
12525 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12526 if (CC != X86::COND_E && CC != X86::COND_NE)
12527 return SDValue();
12528
12529 SDValue Cmp = SetCC.getOperand(1);
12530 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012531 !X86::isZeroNode(Cmp.getOperand(1)) ||
12532 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012533 return SDValue();
12534
12535 SDValue CmpOp0 = Cmp.getOperand(0);
12536 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12537 DAG.getConstant(1, CmpOp0.getValueType()));
12538
12539 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12540 if (CC == X86::COND_NE)
12541 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12542 DL, OtherVal.getValueType(), OtherVal,
12543 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12544 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12545 DL, OtherVal.getValueType(), OtherVal,
12546 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12547}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012548
Dan Gohman475871a2008-07-27 21:46:04 +000012549SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012550 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012551 SelectionDAG &DAG = DCI.DAG;
12552 switch (N->getOpcode()) {
12553 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012554 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012555 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012556 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012557 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012558 case ISD::ADD:
12559 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012560 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012561 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012562 case ISD::SHL:
12563 case ISD::SRA:
12564 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012565 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012566 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012567 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012568 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012569 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012570 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12571 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012572 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012573 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012574 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012575 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012576 case X86ISD::SHUFPS: // Handle all target specific shuffles
12577 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012578 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012579 case X86ISD::PUNPCKHBW:
12580 case X86ISD::PUNPCKHWD:
12581 case X86ISD::PUNPCKHDQ:
12582 case X86ISD::PUNPCKHQDQ:
12583 case X86ISD::UNPCKHPS:
12584 case X86ISD::UNPCKHPD:
12585 case X86ISD::PUNPCKLBW:
12586 case X86ISD::PUNPCKLWD:
12587 case X86ISD::PUNPCKLDQ:
12588 case X86ISD::PUNPCKLQDQ:
12589 case X86ISD::UNPCKLPS:
12590 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012591 case X86ISD::VUNPCKLPS:
12592 case X86ISD::VUNPCKLPD:
12593 case X86ISD::VUNPCKLPSY:
12594 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012595 case X86ISD::MOVHLPS:
12596 case X86ISD::MOVLHPS:
12597 case X86ISD::PSHUFD:
12598 case X86ISD::PSHUFHW:
12599 case X86ISD::PSHUFLW:
12600 case X86ISD::MOVSS:
12601 case X86ISD::MOVSD:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +000012602 case X86ISD::VPERMIL:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012603 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012604 }
12605
Dan Gohman475871a2008-07-27 21:46:04 +000012606 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012607}
12608
Evan Chenge5b51ac2010-04-17 06:13:15 +000012609/// isTypeDesirableForOp - Return true if the target has native support for
12610/// the specified value type and it is 'desirable' to use the type for the
12611/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12612/// instruction encodings are longer and some i16 instructions are slow.
12613bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12614 if (!isTypeLegal(VT))
12615 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012616 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012617 return true;
12618
12619 switch (Opc) {
12620 default:
12621 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012622 case ISD::LOAD:
12623 case ISD::SIGN_EXTEND:
12624 case ISD::ZERO_EXTEND:
12625 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012626 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012627 case ISD::SRL:
12628 case ISD::SUB:
12629 case ISD::ADD:
12630 case ISD::MUL:
12631 case ISD::AND:
12632 case ISD::OR:
12633 case ISD::XOR:
12634 return false;
12635 }
12636}
12637
12638/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012639/// beneficial for dag combiner to promote the specified node. If true, it
12640/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012641bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012642 EVT VT = Op.getValueType();
12643 if (VT != MVT::i16)
12644 return false;
12645
Evan Cheng4c26e932010-04-19 19:29:22 +000012646 bool Promote = false;
12647 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012648 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012649 default: break;
12650 case ISD::LOAD: {
12651 LoadSDNode *LD = cast<LoadSDNode>(Op);
12652 // If the non-extending load has a single use and it's not live out, then it
12653 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012654 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12655 Op.hasOneUse()*/) {
12656 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12657 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12658 // The only case where we'd want to promote LOAD (rather then it being
12659 // promoted as an operand is when it's only use is liveout.
12660 if (UI->getOpcode() != ISD::CopyToReg)
12661 return false;
12662 }
12663 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012664 Promote = true;
12665 break;
12666 }
12667 case ISD::SIGN_EXTEND:
12668 case ISD::ZERO_EXTEND:
12669 case ISD::ANY_EXTEND:
12670 Promote = true;
12671 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012672 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012673 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012674 SDValue N0 = Op.getOperand(0);
12675 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012676 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012677 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012678 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012679 break;
12680 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012681 case ISD::ADD:
12682 case ISD::MUL:
12683 case ISD::AND:
12684 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012685 case ISD::XOR:
12686 Commute = true;
12687 // fallthrough
12688 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012689 SDValue N0 = Op.getOperand(0);
12690 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012691 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012692 return false;
12693 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012694 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012695 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012696 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012697 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012698 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012699 }
12700 }
12701
12702 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012703 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012704}
12705
Evan Cheng60c07e12006-07-05 22:17:51 +000012706//===----------------------------------------------------------------------===//
12707// X86 Inline Assembly Support
12708//===----------------------------------------------------------------------===//
12709
Chris Lattnerb8105652009-07-20 17:51:36 +000012710bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12711 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012712
12713 std::string AsmStr = IA->getAsmString();
12714
12715 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012716 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012717 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012718
12719 switch (AsmPieces.size()) {
12720 default: return false;
12721 case 1:
12722 AsmStr = AsmPieces[0];
12723 AsmPieces.clear();
12724 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12725
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012726 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012727 // we will turn this bswap into something that will be lowered to logical ops
12728 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12729 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012730 // bswap $0
12731 if (AsmPieces.size() == 2 &&
12732 (AsmPieces[0] == "bswap" ||
12733 AsmPieces[0] == "bswapq" ||
12734 AsmPieces[0] == "bswapl") &&
12735 (AsmPieces[1] == "$0" ||
12736 AsmPieces[1] == "${0:q}")) {
12737 // No need to check constraints, nothing other than the equivalent of
12738 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012739 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012740 if (!Ty || Ty->getBitWidth() % 16 != 0)
12741 return false;
12742 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012743 }
12744 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012745 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012746 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012747 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012748 AsmPieces[1] == "$$8," &&
12749 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012750 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12751 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012752 const std::string &ConstraintsStr = IA->getConstraintString();
12753 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012754 std::sort(AsmPieces.begin(), AsmPieces.end());
12755 if (AsmPieces.size() == 4 &&
12756 AsmPieces[0] == "~{cc}" &&
12757 AsmPieces[1] == "~{dirflag}" &&
12758 AsmPieces[2] == "~{flags}" &&
12759 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012760 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012761 if (!Ty || Ty->getBitWidth() % 16 != 0)
12762 return false;
12763 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012764 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012765 }
12766 break;
12767 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012768 if (CI->getType()->isIntegerTy(32) &&
12769 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12770 SmallVector<StringRef, 4> Words;
12771 SplitString(AsmPieces[0], Words, " \t,");
12772 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12773 Words[2] == "${0:w}") {
12774 Words.clear();
12775 SplitString(AsmPieces[1], Words, " \t,");
12776 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12777 Words[2] == "$0") {
12778 Words.clear();
12779 SplitString(AsmPieces[2], Words, " \t,");
12780 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12781 Words[2] == "${0:w}") {
12782 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012783 const std::string &ConstraintsStr = IA->getConstraintString();
12784 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012785 std::sort(AsmPieces.begin(), AsmPieces.end());
12786 if (AsmPieces.size() == 4 &&
12787 AsmPieces[0] == "~{cc}" &&
12788 AsmPieces[1] == "~{dirflag}" &&
12789 AsmPieces[2] == "~{flags}" &&
12790 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012791 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012792 if (!Ty || Ty->getBitWidth() % 16 != 0)
12793 return false;
12794 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012795 }
12796 }
12797 }
12798 }
12799 }
Evan Cheng55d42002011-01-08 01:24:27 +000012800
12801 if (CI->getType()->isIntegerTy(64)) {
12802 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12803 if (Constraints.size() >= 2 &&
12804 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12805 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12806 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12807 SmallVector<StringRef, 4> Words;
12808 SplitString(AsmPieces[0], Words, " \t");
12809 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012810 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012811 SplitString(AsmPieces[1], Words, " \t");
12812 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12813 Words.clear();
12814 SplitString(AsmPieces[2], Words, " \t,");
12815 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12816 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012817 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012818 if (!Ty || Ty->getBitWidth() % 16 != 0)
12819 return false;
12820 return IntrinsicLowering::LowerToByteSwap(CI);
12821 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012822 }
12823 }
12824 }
12825 }
12826 break;
12827 }
12828 return false;
12829}
12830
12831
12832
Chris Lattnerf4dff842006-07-11 02:54:03 +000012833/// getConstraintType - Given a constraint letter, return the type of
12834/// constraint it is for this target.
12835X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012836X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12837 if (Constraint.size() == 1) {
12838 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012839 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012840 case 'q':
12841 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012842 case 'f':
12843 case 't':
12844 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012845 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012846 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012847 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000012848 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000012849 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012850 case 'a':
12851 case 'b':
12852 case 'c':
12853 case 'd':
12854 case 'S':
12855 case 'D':
12856 case 'A':
12857 return C_Register;
12858 case 'I':
12859 case 'J':
12860 case 'K':
12861 case 'L':
12862 case 'M':
12863 case 'N':
12864 case 'G':
12865 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012866 case 'e':
12867 case 'Z':
12868 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012869 default:
12870 break;
12871 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012872 }
Chris Lattner4234f572007-03-25 02:14:49 +000012873 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012874}
12875
John Thompson44ab89e2010-10-29 17:29:13 +000012876/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012877/// This object must already have been set up with the operand type
12878/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012879TargetLowering::ConstraintWeight
12880 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012881 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012882 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012883 Value *CallOperandVal = info.CallOperandVal;
12884 // If we don't have a value, we can't do a match,
12885 // but allow it at the lowest weight.
12886 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012887 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012888 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012889 // Look at the constraint type.
12890 switch (*constraint) {
12891 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012892 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12893 case 'R':
12894 case 'q':
12895 case 'Q':
12896 case 'a':
12897 case 'b':
12898 case 'c':
12899 case 'd':
12900 case 'S':
12901 case 'D':
12902 case 'A':
12903 if (CallOperandVal->getType()->isIntegerTy())
12904 weight = CW_SpecificReg;
12905 break;
12906 case 'f':
12907 case 't':
12908 case 'u':
12909 if (type->isFloatingPointTy())
12910 weight = CW_SpecificReg;
12911 break;
12912 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012913 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012914 weight = CW_SpecificReg;
12915 break;
12916 case 'x':
12917 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012918 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012919 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012920 break;
12921 case 'I':
12922 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12923 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012924 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012925 }
12926 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012927 case 'J':
12928 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12929 if (C->getZExtValue() <= 63)
12930 weight = CW_Constant;
12931 }
12932 break;
12933 case 'K':
12934 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12935 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12936 weight = CW_Constant;
12937 }
12938 break;
12939 case 'L':
12940 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12941 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12942 weight = CW_Constant;
12943 }
12944 break;
12945 case 'M':
12946 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12947 if (C->getZExtValue() <= 3)
12948 weight = CW_Constant;
12949 }
12950 break;
12951 case 'N':
12952 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12953 if (C->getZExtValue() <= 0xff)
12954 weight = CW_Constant;
12955 }
12956 break;
12957 case 'G':
12958 case 'C':
12959 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12960 weight = CW_Constant;
12961 }
12962 break;
12963 case 'e':
12964 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12965 if ((C->getSExtValue() >= -0x80000000LL) &&
12966 (C->getSExtValue() <= 0x7fffffffLL))
12967 weight = CW_Constant;
12968 }
12969 break;
12970 case 'Z':
12971 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12972 if (C->getZExtValue() <= 0xffffffff)
12973 weight = CW_Constant;
12974 }
12975 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012976 }
12977 return weight;
12978}
12979
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012980/// LowerXConstraint - try to replace an X constraint, which matches anything,
12981/// with another that has more specific requirements based on the type of the
12982/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012983const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012984LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012985 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12986 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012987 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012988 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012989 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012990 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012991 return "x";
12992 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012993
Chris Lattner5e764232008-04-26 23:02:14 +000012994 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012995}
12996
Chris Lattner48884cd2007-08-25 00:47:38 +000012997/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12998/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012999void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000013000 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000013001 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000013002 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000013003 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000013004
Eric Christopher100c8332011-06-02 23:16:42 +000013005 // Only support length 1 constraints for now.
13006 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000013007
Eric Christopher100c8332011-06-02 23:16:42 +000013008 char ConstraintLetter = Constraint[0];
13009 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013010 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000013011 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000013012 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000013013 if (C->getZExtValue() <= 31) {
13014 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000013015 break;
13016 }
Devang Patel84f7fd22007-03-17 00:13:28 +000013017 }
Chris Lattner48884cd2007-08-25 00:47:38 +000013018 return;
Evan Cheng364091e2008-09-22 23:57:37 +000013019 case 'J':
13020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000013021 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000013022 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
13023 break;
13024 }
13025 }
13026 return;
13027 case 'K':
13028 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000013029 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000013030 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
13031 break;
13032 }
13033 }
13034 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000013035 case 'N':
13036 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000013037 if (C->getZExtValue() <= 255) {
13038 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000013039 break;
13040 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000013041 }
Chris Lattner48884cd2007-08-25 00:47:38 +000013042 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000013043 case 'e': {
13044 // 32-bit signed value
13045 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000013046 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
13047 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000013048 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000013049 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000013050 break;
13051 }
13052 // FIXME gcc accepts some relocatable values here too, but only in certain
13053 // memory models; it's complicated.
13054 }
13055 return;
13056 }
13057 case 'Z': {
13058 // 32-bit unsigned value
13059 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000013060 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
13061 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000013062 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
13063 break;
13064 }
13065 }
13066 // FIXME gcc accepts some relocatable values here too, but only in certain
13067 // memory models; it's complicated.
13068 return;
13069 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000013070 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013071 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000013072 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000013073 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000013074 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000013075 break;
13076 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013077
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000013078 // In any sort of PIC mode addresses need to be computed at runtime by
13079 // adding in a register or some sort of table lookup. These can't
13080 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000013081 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000013082 return;
13083
Chris Lattnerdc43a882007-05-03 16:52:29 +000013084 // If we are in non-pic codegen mode, we allow the address of a global (with
13085 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000013086 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000013087 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000013088
Chris Lattner49921962009-05-08 18:23:14 +000013089 // Match either (GA), (GA+C), (GA+C1+C2), etc.
13090 while (1) {
13091 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
13092 Offset += GA->getOffset();
13093 break;
13094 } else if (Op.getOpcode() == ISD::ADD) {
13095 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
13096 Offset += C->getZExtValue();
13097 Op = Op.getOperand(0);
13098 continue;
13099 }
13100 } else if (Op.getOpcode() == ISD::SUB) {
13101 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
13102 Offset += -C->getZExtValue();
13103 Op = Op.getOperand(0);
13104 continue;
13105 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000013106 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000013107
Chris Lattner49921962009-05-08 18:23:14 +000013108 // Otherwise, this isn't something we can handle, reject it.
13109 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000013110 }
Eric Christopherfd179292009-08-27 18:07:15 +000013111
Dan Gohman46510a72010-04-15 01:51:59 +000013112 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000013113 // If we require an extra load to get this address, as in PIC mode, we
13114 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000013115 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
13116 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000013117 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000013118
Devang Patel0d881da2010-07-06 22:08:15 +000013119 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
13120 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000013121 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013122 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000013123 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013124
Gabor Greifba36cb52008-08-28 21:40:38 +000013125 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000013126 Ops.push_back(Result);
13127 return;
13128 }
Dale Johannesen1784d162010-06-25 21:55:36 +000013129 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013130}
13131
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013132std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000013133X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000013134 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000013135 // First, see if this is a constraint that directly corresponds to an LLVM
13136 // register class.
13137 if (Constraint.size() == 1) {
13138 // GCC Constraint Letters
13139 switch (Constraint[0]) {
13140 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000013141 // TODO: Slight differences here in allocation order and leaving
13142 // RIP in the class. Do they matter any more here than they do
13143 // in the normal allocation?
13144 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
13145 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000013146 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000013147 return std::make_pair(0U, X86::GR32RegisterClass);
13148 else if (VT == MVT::i16)
13149 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000013150 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000013151 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000013152 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000013153 return std::make_pair(0U, X86::GR64RegisterClass);
13154 break;
13155 }
13156 // 32-bit fallthrough
13157 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000013158 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000013159 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
13160 else if (VT == MVT::i16)
13161 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000013162 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000013163 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
13164 else if (VT == MVT::i64)
13165 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
13166 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000013167 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000013168 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000013169 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000013170 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013171 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000013172 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000013173 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000013174 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000013175 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000013176 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000013177 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000013178 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
13179 if (VT == MVT::i16)
13180 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
13181 if (VT == MVT::i32 || !Subtarget->is64Bit())
13182 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
13183 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013184 case 'f': // FP Stack registers.
13185 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
13186 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000013187 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013188 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013189 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013190 return std::make_pair(0U, X86::RFP64RegisterClass);
13191 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000013192 case 'y': // MMX_REGS if MMX allowed.
13193 if (!Subtarget->hasMMX()) break;
13194 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013195 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013196 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000013197 // FALL THROUGH.
13198 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013199 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000013200
Owen Anderson825b72b2009-08-11 20:47:22 +000013201 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000013202 default: break;
13203 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013204 case MVT::f32:
13205 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000013206 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013207 case MVT::f64:
13208 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000013209 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013210 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013211 case MVT::v16i8:
13212 case MVT::v8i16:
13213 case MVT::v4i32:
13214 case MVT::v2i64:
13215 case MVT::v4f32:
13216 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000013217 return std::make_pair(0U, X86::VR128RegisterClass);
13218 }
Chris Lattnerad043e82007-04-09 05:11:28 +000013219 break;
13220 }
13221 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013222
Chris Lattnerf76d1802006-07-31 23:26:50 +000013223 // Use the default implementation in TargetLowering to convert the register
13224 // constraint into a member of a register class.
13225 std::pair<unsigned, const TargetRegisterClass*> Res;
13226 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000013227
13228 // Not found as a standard register?
13229 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013230 // Map st(0) -> st(7) -> ST0
13231 if (Constraint.size() == 7 && Constraint[0] == '{' &&
13232 tolower(Constraint[1]) == 's' &&
13233 tolower(Constraint[2]) == 't' &&
13234 Constraint[3] == '(' &&
13235 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
13236 Constraint[5] == ')' &&
13237 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000013238
Chris Lattner56d77c72009-09-13 22:41:48 +000013239 Res.first = X86::ST0+Constraint[4]-'0';
13240 Res.second = X86::RFP80RegisterClass;
13241 return Res;
13242 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013243
Chris Lattner56d77c72009-09-13 22:41:48 +000013244 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013245 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000013246 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000013247 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013248 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013249 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013250
13251 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013252 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013253 Res.first = X86::EFLAGS;
13254 Res.second = X86::CCRRegisterClass;
13255 return Res;
13256 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013257
Dale Johannesen330169f2008-11-13 21:52:36 +000013258 // 'A' means EAX + EDX.
13259 if (Constraint == "A") {
13260 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013261 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013262 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013263 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013264 return Res;
13265 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013266
Chris Lattnerf76d1802006-07-31 23:26:50 +000013267 // Otherwise, check to see if this is a register class of the wrong value
13268 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13269 // turn into {ax},{dx}.
13270 if (Res.second->hasType(VT))
13271 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013272
Chris Lattnerf76d1802006-07-31 23:26:50 +000013273 // All of the single-register GCC register classes map their values onto
13274 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13275 // really want an 8-bit or 32-bit register, map to the appropriate register
13276 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013277 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013278 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013279 unsigned DestReg = 0;
13280 switch (Res.first) {
13281 default: break;
13282 case X86::AX: DestReg = X86::AL; break;
13283 case X86::DX: DestReg = X86::DL; break;
13284 case X86::CX: DestReg = X86::CL; break;
13285 case X86::BX: DestReg = X86::BL; break;
13286 }
13287 if (DestReg) {
13288 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013289 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013290 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013291 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013292 unsigned DestReg = 0;
13293 switch (Res.first) {
13294 default: break;
13295 case X86::AX: DestReg = X86::EAX; break;
13296 case X86::DX: DestReg = X86::EDX; break;
13297 case X86::CX: DestReg = X86::ECX; break;
13298 case X86::BX: DestReg = X86::EBX; break;
13299 case X86::SI: DestReg = X86::ESI; break;
13300 case X86::DI: DestReg = X86::EDI; break;
13301 case X86::BP: DestReg = X86::EBP; break;
13302 case X86::SP: DestReg = X86::ESP; break;
13303 }
13304 if (DestReg) {
13305 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013306 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013307 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013308 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013309 unsigned DestReg = 0;
13310 switch (Res.first) {
13311 default: break;
13312 case X86::AX: DestReg = X86::RAX; break;
13313 case X86::DX: DestReg = X86::RDX; break;
13314 case X86::CX: DestReg = X86::RCX; break;
13315 case X86::BX: DestReg = X86::RBX; break;
13316 case X86::SI: DestReg = X86::RSI; break;
13317 case X86::DI: DestReg = X86::RDI; break;
13318 case X86::BP: DestReg = X86::RBP; break;
13319 case X86::SP: DestReg = X86::RSP; break;
13320 }
13321 if (DestReg) {
13322 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013323 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013324 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013325 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013326 } else if (Res.second == X86::FR32RegisterClass ||
13327 Res.second == X86::FR64RegisterClass ||
13328 Res.second == X86::VR128RegisterClass) {
13329 // Handle references to XMM physical registers that got mapped into the
13330 // wrong class. This can happen with constraints like {xmm0} where the
13331 // target independent register mapper will just pick the first match it can
13332 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013333 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013334 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013335 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013336 Res.second = X86::FR64RegisterClass;
13337 else if (X86::VR128RegisterClass->hasType(VT))
13338 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013339 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013340
Chris Lattnerf76d1802006-07-31 23:26:50 +000013341 return Res;
13342}