blob: 17d47e7c8228291ab8eb471a31fa9a188b872d50 [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 Lopes589b8972011-07-22 00:14:53 +00005379/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5380/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005381static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005382LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005383 SDValue V1 = SVOp->getOperand(0);
5384 SDValue V2 = SVOp->getOperand(1);
5385 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005386 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005387
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005388 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5389
Evan Chengace3c172008-07-22 21:13:36 +00005390 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005391 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005392 SmallVector<int, 8> Mask1(4U, -1);
5393 SmallVector<int, 8> PermMask;
5394 SVOp->getMask(PermMask);
5395
Evan Chengace3c172008-07-22 21:13:36 +00005396 unsigned NumHi = 0;
5397 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005398 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005399 int Idx = PermMask[i];
5400 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005401 Locs[i] = std::make_pair(-1, -1);
5402 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005403 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5404 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005405 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005406 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005407 NumLo++;
5408 } else {
5409 Locs[i] = std::make_pair(1, NumHi);
5410 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005411 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005412 NumHi++;
5413 }
5414 }
5415 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005416
Evan Chengace3c172008-07-22 21:13:36 +00005417 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005418 // If no more than two elements come from either vector. This can be
5419 // implemented with two shuffles. First shuffle gather the elements.
5420 // The second shuffle, which takes the first shuffle as both of its
5421 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005422 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005423
Nate Begeman9008ca62009-04-27 18:41:29 +00005424 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005425
Evan Chengace3c172008-07-22 21:13:36 +00005426 for (unsigned i = 0; i != 4; ++i) {
5427 if (Locs[i].first == -1)
5428 continue;
5429 else {
5430 unsigned Idx = (i < 2) ? 0 : 4;
5431 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005432 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005433 }
5434 }
5435
Nate Begeman9008ca62009-04-27 18:41:29 +00005436 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005437 } else if (NumLo == 3 || NumHi == 3) {
5438 // Otherwise, we must have three elements from one vector, call it X, and
5439 // one element from the other, call it Y. First, use a shufps to build an
5440 // intermediate vector with the one element from Y and the element from X
5441 // that will be in the same half in the final destination (the indexes don't
5442 // matter). Then, use a shufps to build the final vector, taking the half
5443 // containing the element from Y from the intermediate, and the other half
5444 // from X.
5445 if (NumHi == 3) {
5446 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005447 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005448 std::swap(V1, V2);
5449 }
5450
5451 // Find the element from V2.
5452 unsigned HiIndex;
5453 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005454 int Val = PermMask[HiIndex];
5455 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005456 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005457 if (Val >= 4)
5458 break;
5459 }
5460
Nate Begeman9008ca62009-04-27 18:41:29 +00005461 Mask1[0] = PermMask[HiIndex];
5462 Mask1[1] = -1;
5463 Mask1[2] = PermMask[HiIndex^1];
5464 Mask1[3] = -1;
5465 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005466
5467 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005468 Mask1[0] = PermMask[0];
5469 Mask1[1] = PermMask[1];
5470 Mask1[2] = HiIndex & 1 ? 6 : 4;
5471 Mask1[3] = HiIndex & 1 ? 4 : 6;
5472 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005473 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005474 Mask1[0] = HiIndex & 1 ? 2 : 0;
5475 Mask1[1] = HiIndex & 1 ? 0 : 2;
5476 Mask1[2] = PermMask[2];
5477 Mask1[3] = PermMask[3];
5478 if (Mask1[2] >= 0)
5479 Mask1[2] += 4;
5480 if (Mask1[3] >= 0)
5481 Mask1[3] += 4;
5482 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005483 }
Evan Chengace3c172008-07-22 21:13:36 +00005484 }
5485
5486 // Break it into (shuffle shuffle_hi, shuffle_lo).
5487 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005488 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005489 SmallVector<int,8> LoMask(4U, -1);
5490 SmallVector<int,8> HiMask(4U, -1);
5491
5492 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005493 unsigned MaskIdx = 0;
5494 unsigned LoIdx = 0;
5495 unsigned HiIdx = 2;
5496 for (unsigned i = 0; i != 4; ++i) {
5497 if (i == 2) {
5498 MaskPtr = &HiMask;
5499 MaskIdx = 1;
5500 LoIdx = 0;
5501 HiIdx = 2;
5502 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005503 int Idx = PermMask[i];
5504 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005505 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005506 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005507 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005508 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005509 LoIdx++;
5510 } else {
5511 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005512 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005513 HiIdx++;
5514 }
5515 }
5516
Nate Begeman9008ca62009-04-27 18:41:29 +00005517 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5518 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5519 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005520 for (unsigned i = 0; i != 4; ++i) {
5521 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005522 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005523 } else {
5524 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005525 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005526 }
5527 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005528 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005529}
5530
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005531static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005532 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005533 V = V.getOperand(0);
5534 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5535 V = V.getOperand(0);
5536 if (MayFoldLoad(V))
5537 return true;
5538 return false;
5539}
5540
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005541// FIXME: the version above should always be used. Since there's
5542// a bug where several vector shuffles can't be folded because the
5543// DAG is not updated during lowering and a node claims to have two
5544// uses while it only has one, use this version, and let isel match
5545// another instruction if the load really happens to have more than
5546// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005547// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005548static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005549 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005550 V = V.getOperand(0);
5551 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5552 V = V.getOperand(0);
5553 if (ISD::isNormalLoad(V.getNode()))
5554 return true;
5555 return false;
5556}
5557
5558/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5559/// a vector extract, and if both can be later optimized into a single load.
5560/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5561/// here because otherwise a target specific shuffle node is going to be
5562/// emitted for this shuffle, and the optimization not done.
5563/// FIXME: This is probably not the best approach, but fix the problem
5564/// until the right path is decided.
5565static
5566bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5567 const TargetLowering &TLI) {
5568 EVT VT = V.getValueType();
5569 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5570
5571 // Be sure that the vector shuffle is present in a pattern like this:
5572 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5573 if (!V.hasOneUse())
5574 return false;
5575
5576 SDNode *N = *V.getNode()->use_begin();
5577 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5578 return false;
5579
5580 SDValue EltNo = N->getOperand(1);
5581 if (!isa<ConstantSDNode>(EltNo))
5582 return false;
5583
5584 // If the bit convert changed the number of elements, it is unsafe
5585 // to examine the mask.
5586 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005587 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005588 EVT SrcVT = V.getOperand(0).getValueType();
5589 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5590 return false;
5591 V = V.getOperand(0);
5592 HasShuffleIntoBitcast = true;
5593 }
5594
5595 // Select the input vector, guarding against out of range extract vector.
5596 unsigned NumElems = VT.getVectorNumElements();
5597 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5598 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5599 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5600
5601 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005602 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005603 V = V.getOperand(0);
5604
5605 if (ISD::isNormalLoad(V.getNode())) {
5606 // Is the original load suitable?
5607 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5608
5609 // FIXME: avoid the multi-use bug that is preventing lots of
5610 // of foldings to be detected, this is still wrong of course, but
5611 // give the temporary desired behavior, and if it happens that
5612 // the load has real more uses, during isel it will not fold, and
5613 // will generate poor code.
5614 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5615 return false;
5616
5617 if (!HasShuffleIntoBitcast)
5618 return true;
5619
5620 // If there's a bitcast before the shuffle, check if the load type and
5621 // alignment is valid.
5622 unsigned Align = LN0->getAlignment();
5623 unsigned NewAlign =
5624 TLI.getTargetData()->getABITypeAlignment(
5625 VT.getTypeForEVT(*DAG.getContext()));
5626
5627 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5628 return false;
5629 }
5630
5631 return true;
5632}
5633
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005634static
Evan Cheng835580f2010-10-07 20:50:20 +00005635SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5636 EVT VT = Op.getValueType();
5637
5638 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005639 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5640 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005641 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5642 V1, DAG));
5643}
5644
5645static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005646SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5647 bool HasSSE2) {
5648 SDValue V1 = Op.getOperand(0);
5649 SDValue V2 = Op.getOperand(1);
5650 EVT VT = Op.getValueType();
5651
5652 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5653
5654 if (HasSSE2 && VT == MVT::v2f64)
5655 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5656
5657 // v4f32 or v4i32
5658 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5659}
5660
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005661static
5662SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5663 SDValue V1 = Op.getOperand(0);
5664 SDValue V2 = Op.getOperand(1);
5665 EVT VT = Op.getValueType();
5666
5667 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5668 "unsupported shuffle type");
5669
5670 if (V2.getOpcode() == ISD::UNDEF)
5671 V2 = V1;
5672
5673 // v4i32 or v4f32
5674 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5675}
5676
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005677static
5678SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5679 SDValue V1 = Op.getOperand(0);
5680 SDValue V2 = Op.getOperand(1);
5681 EVT VT = Op.getValueType();
5682 unsigned NumElems = VT.getVectorNumElements();
5683
5684 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5685 // operand of these instructions is only memory, so check if there's a
5686 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5687 // same masks.
5688 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005689
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005690 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005691 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005692 CanFoldLoad = true;
5693
5694 // When V1 is a load, it can be folded later into a store in isel, example:
5695 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5696 // turns into:
5697 // (MOVLPSmr addr:$src1, VR128:$src2)
5698 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005699 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005700 CanFoldLoad = true;
5701
Eric Christopher893a8822011-02-20 05:04:42 +00005702 // Both of them can't be memory operations though.
5703 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5704 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005705
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005706 if (CanFoldLoad) {
5707 if (HasSSE2 && NumElems == 2)
5708 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5709
5710 if (NumElems == 4)
5711 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5712 }
5713
5714 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5715 // movl and movlp will both match v2i64, but v2i64 is never matched by
5716 // movl earlier because we make it strict to avoid messing with the movlp load
5717 // folding logic (see the code above getMOVLP call). Match it here then,
5718 // this is horrible, but will stay like this until we move all shuffle
5719 // matching to x86 specific nodes. Note that for the 1st condition all
5720 // types are matched with movsd.
5721 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5722 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5723 else if (HasSSE2)
5724 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5725
5726
5727 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5728
5729 // Invert the operand order and use SHUFPS to match it.
5730 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5731 X86::getShuffleSHUFImmediate(SVOp), DAG);
5732}
5733
David Greenec4db4e52011-02-28 19:06:56 +00005734static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005735 switch(VT.getSimpleVT().SimpleTy) {
5736 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5737 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005738 case MVT::v4f32:
5739 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5740 case MVT::v2f64:
5741 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5742 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5743 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005744 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5745 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5746 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005747 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005748 }
5749 return 0;
5750}
5751
5752static inline unsigned getUNPCKHOpcode(EVT VT) {
5753 switch(VT.getSimpleVT().SimpleTy) {
5754 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5755 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5756 case MVT::v4f32: return X86ISD::UNPCKHPS;
5757 case MVT::v2f64: return X86ISD::UNPCKHPD;
5758 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5759 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5760 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005761 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005762 }
5763 return 0;
5764}
5765
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005766static
5767SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005768 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005769 const X86Subtarget *Subtarget) {
5770 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5771 EVT VT = Op.getValueType();
5772 DebugLoc dl = Op.getDebugLoc();
5773 SDValue V1 = Op.getOperand(0);
5774 SDValue V2 = Op.getOperand(1);
5775
5776 if (isZeroShuffle(SVOp))
5777 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5778
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005779 // Handle splat operations
5780 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00005781 unsigned NumElem = VT.getVectorNumElements();
5782 // Special case, this is the only place now where it's allowed to return
5783 // a vector_shuffle operation without using a target specific node, because
5784 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
5785 // this be moved to DAGCombine instead?
5786 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005787 return Op;
5788
5789 // Handle splats by matching through known masks
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00005790 if ((VT.is128BitVector() && NumElem <= 4) ||
5791 (VT.is256BitVector() && NumElem <= 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005792 return SDValue();
5793
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00005794 // All i16 and i8 vector types can't be used directly by a generic shuffle
5795 // instruction because the target has no such instruction. Generate shuffles
5796 // which repeat i16 and i8 several times until they fit in i32, and then can
5797 // be manipulated by target suported shuffles. After the insertion of the
5798 // necessary shuffles, the result is bitcasted back to v4f32 or v8f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005799 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005800 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005801
5802 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5803 // do it!
5804 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5805 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5806 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005807 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005808 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5809 // FIXME: Figure out a cleaner way to do this.
5810 // Try to make use of movq to zero out the top part.
5811 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5812 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5813 if (NewOp.getNode()) {
5814 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5815 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5816 DAG, Subtarget, dl);
5817 }
5818 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5819 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5820 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5821 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5822 DAG, Subtarget, dl);
5823 }
5824 }
5825 return SDValue();
5826}
5827
Dan Gohman475871a2008-07-27 21:46:04 +00005828SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005829X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005830 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005831 SDValue V1 = Op.getOperand(0);
5832 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005833 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005834 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005835 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005836 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005837 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5838 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005839 bool V1IsSplat = false;
5840 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005841 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005842 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005843 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005844 MachineFunction &MF = DAG.getMachineFunction();
5845 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005846
Dale Johannesen0488fb62010-09-30 23:57:10 +00005847 // Shuffle operations on MMX not supported.
5848 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005849 return Op;
5850
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005851 // Vector shuffle lowering takes 3 steps:
5852 //
5853 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5854 // narrowing and commutation of operands should be handled.
5855 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5856 // shuffle nodes.
5857 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5858 // so the shuffle can be broken into other shuffles and the legalizer can
5859 // try the lowering again.
5860 //
5861 // The general ideia is that no vector_shuffle operation should be left to
5862 // be matched during isel, all of them must be converted to a target specific
5863 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005864
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005865 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5866 // narrowing and commutation of operands should be handled. The actual code
5867 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005868 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005869 if (NewOp.getNode())
5870 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005871
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005872 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5873 // unpckh_undef). Only use pshufd if speed is more important than size.
5874 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5875 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005876 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005877 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5878 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5879 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005880
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005881 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005882 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005883 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005884
Dale Johannesen0488fb62010-09-30 23:57:10 +00005885 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005886 return getMOVHighToLow(Op, dl, DAG);
5887
5888 // Use to match splats
5889 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5890 (VT == MVT::v2f64 || VT == MVT::v2i64))
5891 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5892
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005893 if (X86::isPSHUFDMask(SVOp)) {
5894 // The actual implementation will match the mask in the if above and then
5895 // during isel it can match several different instructions, not only pshufd
5896 // as its name says, sad but true, emulate the behavior for now...
5897 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5898 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5899
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005900 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5901
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005902 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005903 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5904
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005905 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005906 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5907 TargetMask, DAG);
5908
5909 if (VT == MVT::v4f32)
5910 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5911 TargetMask, DAG);
5912 }
Eric Christopherfd179292009-08-27 18:07:15 +00005913
Evan Chengf26ffe92008-05-29 08:22:04 +00005914 // Check if this can be converted into a logical shift.
5915 bool isLeft = false;
5916 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005917 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005918 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005919 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005920 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005921 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005922 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005923 EVT EltVT = VT.getVectorElementType();
5924 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005925 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005926 }
Eric Christopherfd179292009-08-27 18:07:15 +00005927
Nate Begeman9008ca62009-04-27 18:41:29 +00005928 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005929 if (V1IsUndef)
5930 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005931 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005932 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005933 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005934 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005935 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5936
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005937 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005938 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5939 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005940 }
Eric Christopherfd179292009-08-27 18:07:15 +00005941
Nate Begeman9008ca62009-04-27 18:41:29 +00005942 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005943 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5944 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005945
Dale Johannesen0488fb62010-09-30 23:57:10 +00005946 if (X86::isMOVHLPSMask(SVOp))
5947 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005948
Dale Johannesen0488fb62010-09-30 23:57:10 +00005949 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5950 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005951
Dale Johannesen0488fb62010-09-30 23:57:10 +00005952 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5953 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005954
Dale Johannesen0488fb62010-09-30 23:57:10 +00005955 if (X86::isMOVLPMask(SVOp))
5956 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005957
Nate Begeman9008ca62009-04-27 18:41:29 +00005958 if (ShouldXformToMOVHLPS(SVOp) ||
5959 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5960 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005961
Evan Chengf26ffe92008-05-29 08:22:04 +00005962 if (isShift) {
5963 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005964 EVT EltVT = VT.getVectorElementType();
5965 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005966 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005967 }
Eric Christopherfd179292009-08-27 18:07:15 +00005968
Evan Cheng9eca5e82006-10-25 21:49:50 +00005969 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005970 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5971 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005972 V1IsSplat = isSplatVector(V1.getNode());
5973 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005974
Chris Lattner8a594482007-11-25 00:24:49 +00005975 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005976 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005977 Op = CommuteVectorShuffle(SVOp, DAG);
5978 SVOp = cast<ShuffleVectorSDNode>(Op);
5979 V1 = SVOp->getOperand(0);
5980 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005981 std::swap(V1IsSplat, V2IsSplat);
5982 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005983 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005984 }
5985
Nate Begeman9008ca62009-04-27 18:41:29 +00005986 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5987 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005988 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005989 return V1;
5990 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5991 // the instruction selector will not match, so get a canonical MOVL with
5992 // swapped operands to undo the commute.
5993 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005994 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005995
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005996 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005997 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5998 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005999
6000 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006001 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006002
Evan Cheng9bbbb982006-10-25 20:48:19 +00006003 if (V2IsSplat) {
6004 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006005 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006006 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006007 SDValue NewMask = NormalizeMask(SVOp, DAG);
6008 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6009 if (NSVOp != SVOp) {
6010 if (X86::isUNPCKLMask(NSVOp, true)) {
6011 return NewMask;
6012 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6013 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006014 }
6015 }
6016 }
6017
Evan Cheng9eca5e82006-10-25 21:49:50 +00006018 if (Commuted) {
6019 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006020 // FIXME: this seems wrong.
6021 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6022 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006023
6024 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00006025 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
6026 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006027
6028 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006029 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006030 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006031
Nate Begeman9008ca62009-04-27 18:41:29 +00006032 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006033 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006034 return CommuteVectorShuffle(SVOp, DAG);
6035
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006036 // The checks below are all present in isShuffleMaskLegal, but they are
6037 // inlined here right now to enable us to directly emit target specific
6038 // nodes, and remove one by one until they don't return Op anymore.
6039 SmallVector<int, 16> M;
6040 SVOp->getMask(M);
6041
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006042 if (isPALIGNRMask(M, VT, HasSSSE3))
6043 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6044 X86::getShufflePALIGNRImmediate(SVOp),
6045 DAG);
6046
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006047 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6048 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00006049 if (VT == MVT::v2f64) {
6050 X86ISD::NodeType Opcode =
6051 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
6052 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
6053 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006054 if (VT == MVT::v2i64)
6055 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6056 }
6057
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006058 if (isPSHUFHWMask(M, VT))
6059 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6060 X86::getShufflePSHUFHWImmediate(SVOp),
6061 DAG);
6062
6063 if (isPSHUFLWMask(M, VT))
6064 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6065 X86::getShufflePSHUFLWImmediate(SVOp),
6066 DAG);
6067
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006068 if (isSHUFPMask(M, VT)) {
6069 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6070 if (VT == MVT::v4f32 || VT == MVT::v4i32)
6071 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
6072 TargetMask, DAG);
6073 if (VT == MVT::v2f64 || VT == MVT::v2i64)
6074 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
6075 TargetMask, DAG);
6076 }
6077
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006078 if (X86::isUNPCKL_v_undef_Mask(SVOp))
6079 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00006080 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
6081 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006082 if (X86::isUNPCKH_v_undef_Mask(SVOp))
6083 if (VT != MVT::v2i64 && VT != MVT::v2f64)
6084 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6085
Evan Cheng14b32e12007-12-11 01:46:18 +00006086 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00006087 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00006088 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006089 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00006090 return NewOp;
6091 }
6092
Owen Anderson825b72b2009-08-11 20:47:22 +00006093 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006094 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006095 if (NewOp.getNode())
6096 return NewOp;
6097 }
Eric Christopherfd179292009-08-27 18:07:15 +00006098
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006099 // Handle all 128-bit wide vectors with 4 elements, and match them with
6100 // several different shuffle types.
6101 if (NumElems == 4 && VT.getSizeInBits() == 128)
6102 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006103
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006104 // Handle VPERMIL permutations
6105 if (isVPERMILMask(M, VT)) {
6106 unsigned TargetMask = getShuffleVPERMILImmediate(SVOp);
6107 if (VT == MVT::v8f32)
6108 return getTargetShuffleNode(X86ISD::VPERMIL, dl, VT, V1, TargetMask, DAG);
6109 }
6110
Dan Gohman475871a2008-07-27 21:46:04 +00006111 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006112}
6113
Dan Gohman475871a2008-07-27 21:46:04 +00006114SDValue
6115X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006116 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006117 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006118 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006119 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006120 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006121 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006122 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006123 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006124 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006125 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006126 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6127 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6128 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006129 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6130 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006131 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006132 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006133 Op.getOperand(0)),
6134 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006135 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006136 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006137 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006138 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006139 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006140 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006141 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6142 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006143 // result has a single use which is a store or a bitcast to i32. And in
6144 // the case of a store, it's not worth it if the index is a constant 0,
6145 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006146 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006147 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006148 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006149 if ((User->getOpcode() != ISD::STORE ||
6150 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6151 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006152 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006153 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006154 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006155 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006156 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006157 Op.getOperand(0)),
6158 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006159 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006160 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006161 // ExtractPS works with constant index.
6162 if (isa<ConstantSDNode>(Op.getOperand(1)))
6163 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006164 }
Dan Gohman475871a2008-07-27 21:46:04 +00006165 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006166}
6167
6168
Dan Gohman475871a2008-07-27 21:46:04 +00006169SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006170X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6171 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006172 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006173 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006174
David Greene74a579d2011-02-10 16:57:36 +00006175 SDValue Vec = Op.getOperand(0);
6176 EVT VecVT = Vec.getValueType();
6177
6178 // If this is a 256-bit vector result, first extract the 128-bit
6179 // vector and then extract from the 128-bit vector.
6180 if (VecVT.getSizeInBits() > 128) {
6181 DebugLoc dl = Op.getNode()->getDebugLoc();
6182 unsigned NumElems = VecVT.getVectorNumElements();
6183 SDValue Idx = Op.getOperand(1);
6184
6185 if (!isa<ConstantSDNode>(Idx))
6186 return SDValue();
6187
6188 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6189 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6190
6191 // Get the 128-bit vector.
6192 bool Upper = IdxVal >= ExtractNumElems;
6193 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6194
6195 // Extract from it.
6196 SDValue ScaledIdx = Idx;
6197 if (Upper)
6198 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6199 DAG.getConstant(ExtractNumElems,
6200 Idx.getValueType()));
6201 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6202 ScaledIdx);
6203 }
6204
6205 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6206
Evan Cheng62a3f152008-03-24 21:52:23 +00006207 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006208 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006209 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006210 return Res;
6211 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006212
Owen Andersone50ed302009-08-10 22:56:29 +00006213 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006214 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006215 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006216 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006217 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006218 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006219 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006220 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6221 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006222 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006223 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006224 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006225 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006226 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006227 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006228 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006229 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006230 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006231 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006232 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006233 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006234 if (Idx == 0)
6235 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006236
Evan Cheng0db9fe62006-04-25 20:13:52 +00006237 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006238 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006239 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006240 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006241 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006242 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006243 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006244 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006245 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6246 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6247 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006248 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006249 if (Idx == 0)
6250 return Op;
6251
6252 // UNPCKHPD the element to the lowest double word, then movsd.
6253 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6254 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006255 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006256 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006257 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006258 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006259 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006260 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006261 }
6262
Dan Gohman475871a2008-07-27 21:46:04 +00006263 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006264}
6265
Dan Gohman475871a2008-07-27 21:46:04 +00006266SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006267X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6268 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006269 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006270 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006271 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006272
Dan Gohman475871a2008-07-27 21:46:04 +00006273 SDValue N0 = Op.getOperand(0);
6274 SDValue N1 = Op.getOperand(1);
6275 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006276
Dan Gohman8a55ce42009-09-23 21:02:20 +00006277 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006278 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006279 unsigned Opc;
6280 if (VT == MVT::v8i16)
6281 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006282 else if (VT == MVT::v16i8)
6283 Opc = X86ISD::PINSRB;
6284 else
6285 Opc = X86ISD::PINSRB;
6286
Nate Begeman14d12ca2008-02-11 04:19:36 +00006287 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6288 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006289 if (N1.getValueType() != MVT::i32)
6290 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6291 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006292 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006293 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006294 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006295 // Bits [7:6] of the constant are the source select. This will always be
6296 // zero here. The DAG Combiner may combine an extract_elt index into these
6297 // bits. For example (insert (extract, 3), 2) could be matched by putting
6298 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006299 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006300 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006301 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006302 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006303 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006304 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006305 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006306 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006307 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006308 // PINSR* works with constant index.
6309 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006310 }
Dan Gohman475871a2008-07-27 21:46:04 +00006311 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006312}
6313
Dan Gohman475871a2008-07-27 21:46:04 +00006314SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006315X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006316 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006317 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006318
David Greene6b381262011-02-09 15:32:06 +00006319 DebugLoc dl = Op.getDebugLoc();
6320 SDValue N0 = Op.getOperand(0);
6321 SDValue N1 = Op.getOperand(1);
6322 SDValue N2 = Op.getOperand(2);
6323
6324 // If this is a 256-bit vector result, first insert into a 128-bit
6325 // vector and then insert into the 256-bit vector.
6326 if (VT.getSizeInBits() > 128) {
6327 if (!isa<ConstantSDNode>(N2))
6328 return SDValue();
6329
6330 // Get the 128-bit vector.
6331 unsigned NumElems = VT.getVectorNumElements();
6332 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6333 bool Upper = IdxVal >= NumElems / 2;
6334
6335 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6336
6337 // Insert into it.
6338 SDValue ScaledN2 = N2;
6339 if (Upper)
6340 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006341 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006342 (VT.getSizeInBits() / 128),
6343 N2.getValueType()));
6344 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6345 N1, ScaledN2);
6346
6347 // Insert the 128-bit vector
6348 // FIXME: Why UNDEF?
6349 return Insert128BitVector(N0, Op, N2, DAG, dl);
6350 }
6351
Nate Begeman14d12ca2008-02-11 04:19:36 +00006352 if (Subtarget->hasSSE41())
6353 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6354
Dan Gohman8a55ce42009-09-23 21:02:20 +00006355 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006356 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006357
Dan Gohman8a55ce42009-09-23 21:02:20 +00006358 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006359 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6360 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006361 if (N1.getValueType() != MVT::i32)
6362 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6363 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006364 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006365 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006366 }
Dan Gohman475871a2008-07-27 21:46:04 +00006367 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006368}
6369
Dan Gohman475871a2008-07-27 21:46:04 +00006370SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006371X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006372 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006373 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006374 EVT OpVT = Op.getValueType();
6375
6376 // If this is a 256-bit vector result, first insert into a 128-bit
6377 // vector and then insert into the 256-bit vector.
6378 if (OpVT.getSizeInBits() > 128) {
6379 // Insert into a 128-bit vector.
6380 EVT VT128 = EVT::getVectorVT(*Context,
6381 OpVT.getVectorElementType(),
6382 OpVT.getVectorNumElements() / 2);
6383
6384 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6385
6386 // Insert the 128-bit vector.
6387 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6388 DAG.getConstant(0, MVT::i32),
6389 DAG, dl);
6390 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006391
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006392 if (Op.getValueType() == MVT::v1i64 &&
6393 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006394 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006395
Owen Anderson825b72b2009-08-11 20:47:22 +00006396 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006397 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6398 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006399 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006400 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006401}
6402
David Greene91585092011-01-26 15:38:49 +00006403// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6404// a simple subregister reference or explicit instructions to grab
6405// upper bits of a vector.
6406SDValue
6407X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6408 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006409 DebugLoc dl = Op.getNode()->getDebugLoc();
6410 SDValue Vec = Op.getNode()->getOperand(0);
6411 SDValue Idx = Op.getNode()->getOperand(1);
6412
6413 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6414 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6415 return Extract128BitVector(Vec, Idx, DAG, dl);
6416 }
David Greene91585092011-01-26 15:38:49 +00006417 }
6418 return SDValue();
6419}
6420
David Greenecfe33c42011-01-26 19:13:22 +00006421// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6422// simple superregister reference or explicit instructions to insert
6423// the upper bits of a vector.
6424SDValue
6425X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6426 if (Subtarget->hasAVX()) {
6427 DebugLoc dl = Op.getNode()->getDebugLoc();
6428 SDValue Vec = Op.getNode()->getOperand(0);
6429 SDValue SubVec = Op.getNode()->getOperand(1);
6430 SDValue Idx = Op.getNode()->getOperand(2);
6431
6432 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6433 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006434 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006435 }
6436 }
6437 return SDValue();
6438}
6439
Bill Wendling056292f2008-09-16 21:48:12 +00006440// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6441// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6442// one of the above mentioned nodes. It has to be wrapped because otherwise
6443// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6444// be used to form addressing mode. These wrapped nodes will be selected
6445// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006446SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006447X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006448 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006449
Chris Lattner41621a22009-06-26 19:22:52 +00006450 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6451 // global base reg.
6452 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006453 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006454 CodeModel::Model M = getTargetMachine().getCodeModel();
6455
Chris Lattner4f066492009-07-11 20:29:19 +00006456 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006457 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006458 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006459 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006460 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006461 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006462 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006463
Evan Cheng1606e8e2009-03-13 07:51:59 +00006464 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006465 CP->getAlignment(),
6466 CP->getOffset(), OpFlag);
6467 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006468 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006469 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006470 if (OpFlag) {
6471 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006472 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006473 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006474 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006475 }
6476
6477 return Result;
6478}
6479
Dan Gohmand858e902010-04-17 15:26:15 +00006480SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006481 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006482
Chris Lattner18c59872009-06-27 04:16:01 +00006483 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6484 // global base reg.
6485 unsigned char OpFlag = 0;
6486 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006487 CodeModel::Model M = getTargetMachine().getCodeModel();
6488
Chris Lattner4f066492009-07-11 20:29:19 +00006489 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006490 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006491 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006492 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006493 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006494 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006495 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006496
Chris Lattner18c59872009-06-27 04:16:01 +00006497 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6498 OpFlag);
6499 DebugLoc DL = JT->getDebugLoc();
6500 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006501
Chris Lattner18c59872009-06-27 04:16:01 +00006502 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006503 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006504 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6505 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006506 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006507 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006508
Chris Lattner18c59872009-06-27 04:16:01 +00006509 return Result;
6510}
6511
6512SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006513X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006514 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006515
Chris Lattner18c59872009-06-27 04:16:01 +00006516 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6517 // global base reg.
6518 unsigned char OpFlag = 0;
6519 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006520 CodeModel::Model M = getTargetMachine().getCodeModel();
6521
Chris Lattner4f066492009-07-11 20:29:19 +00006522 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006523 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006524 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006525 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006526 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006527 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006528 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006529
Chris Lattner18c59872009-06-27 04:16:01 +00006530 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006531
Chris Lattner18c59872009-06-27 04:16:01 +00006532 DebugLoc DL = Op.getDebugLoc();
6533 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006534
6535
Chris Lattner18c59872009-06-27 04:16:01 +00006536 // With PIC, the address is actually $g + Offset.
6537 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006538 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006539 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6540 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006541 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006542 Result);
6543 }
Eric Christopherfd179292009-08-27 18:07:15 +00006544
Chris Lattner18c59872009-06-27 04:16:01 +00006545 return Result;
6546}
6547
Dan Gohman475871a2008-07-27 21:46:04 +00006548SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006549X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006550 // Create the TargetBlockAddressAddress node.
6551 unsigned char OpFlags =
6552 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006553 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006554 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006555 DebugLoc dl = Op.getDebugLoc();
6556 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6557 /*isTarget=*/true, OpFlags);
6558
Dan Gohmanf705adb2009-10-30 01:28:02 +00006559 if (Subtarget->isPICStyleRIPRel() &&
6560 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006561 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6562 else
6563 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006564
Dan Gohman29cbade2009-11-20 23:18:13 +00006565 // With PIC, the address is actually $g + Offset.
6566 if (isGlobalRelativeToPICBase(OpFlags)) {
6567 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6568 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6569 Result);
6570 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006571
6572 return Result;
6573}
6574
6575SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006576X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006577 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006578 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006579 // Create the TargetGlobalAddress node, folding in the constant
6580 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006581 unsigned char OpFlags =
6582 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006583 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006584 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006585 if (OpFlags == X86II::MO_NO_FLAG &&
6586 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006587 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006588 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006589 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006590 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006591 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006592 }
Eric Christopherfd179292009-08-27 18:07:15 +00006593
Chris Lattner4f066492009-07-11 20:29:19 +00006594 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006595 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006596 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6597 else
6598 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006599
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006600 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006601 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006602 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6603 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006604 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006606
Chris Lattner36c25012009-07-10 07:34:39 +00006607 // For globals that require a load from a stub to get the address, emit the
6608 // load.
6609 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006610 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006611 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006612
Dan Gohman6520e202008-10-18 02:06:02 +00006613 // If there was a non-zero offset that we didn't fold, create an explicit
6614 // addition for it.
6615 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006616 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006617 DAG.getConstant(Offset, getPointerTy()));
6618
Evan Cheng0db9fe62006-04-25 20:13:52 +00006619 return Result;
6620}
6621
Evan Chengda43bcf2008-09-24 00:05:32 +00006622SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006623X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006624 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006625 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006626 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006627}
6628
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006629static SDValue
6630GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006631 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006632 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006633 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006634 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006635 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006636 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006637 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006638 GA->getOffset(),
6639 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006640 if (InFlag) {
6641 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006642 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006643 } else {
6644 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006645 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006646 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006647
6648 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006649 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006650
Rafael Espindola15f1b662009-04-24 12:59:40 +00006651 SDValue Flag = Chain.getValue(1);
6652 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006653}
6654
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006655// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006656static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006657LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006658 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006659 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006660 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6661 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006662 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006663 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006664 InFlag = Chain.getValue(1);
6665
Chris Lattnerb903bed2009-06-26 21:20:29 +00006666 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006667}
6668
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006669// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006670static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006671LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006672 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006673 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6674 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006675}
6676
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006677// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6678// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006679static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006680 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006681 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006682 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006683
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006684 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6685 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6686 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006687
Michael J. Spencerec38de22010-10-10 22:04:20 +00006688 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006689 DAG.getIntPtrConstant(0),
6690 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006691
Chris Lattnerb903bed2009-06-26 21:20:29 +00006692 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006693 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6694 // initialexec.
6695 unsigned WrapperKind = X86ISD::Wrapper;
6696 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006697 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006698 } else if (is64Bit) {
6699 assert(model == TLSModel::InitialExec);
6700 OperandFlags = X86II::MO_GOTTPOFF;
6701 WrapperKind = X86ISD::WrapperRIP;
6702 } else {
6703 assert(model == TLSModel::InitialExec);
6704 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006705 }
Eric Christopherfd179292009-08-27 18:07:15 +00006706
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006707 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6708 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006709 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006710 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006711 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006712 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006713
Rafael Espindola9a580232009-02-27 13:37:18 +00006714 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006715 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006716 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006717
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006718 // The address of the thread local variable is the add of the thread
6719 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006720 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006721}
6722
Dan Gohman475871a2008-07-27 21:46:04 +00006723SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006724X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006725
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006726 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006727 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006728
Eric Christopher30ef0e52010-06-03 04:07:48 +00006729 if (Subtarget->isTargetELF()) {
6730 // TODO: implement the "local dynamic" model
6731 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006732
Eric Christopher30ef0e52010-06-03 04:07:48 +00006733 // If GV is an alias then use the aliasee for determining
6734 // thread-localness.
6735 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6736 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006737
6738 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006739 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006740
Eric Christopher30ef0e52010-06-03 04:07:48 +00006741 switch (model) {
6742 case TLSModel::GeneralDynamic:
6743 case TLSModel::LocalDynamic: // not implemented
6744 if (Subtarget->is64Bit())
6745 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6746 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006747
Eric Christopher30ef0e52010-06-03 04:07:48 +00006748 case TLSModel::InitialExec:
6749 case TLSModel::LocalExec:
6750 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6751 Subtarget->is64Bit());
6752 }
6753 } else if (Subtarget->isTargetDarwin()) {
6754 // Darwin only has one model of TLS. Lower to that.
6755 unsigned char OpFlag = 0;
6756 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6757 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006758
Eric Christopher30ef0e52010-06-03 04:07:48 +00006759 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6760 // global base reg.
6761 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6762 !Subtarget->is64Bit();
6763 if (PIC32)
6764 OpFlag = X86II::MO_TLVP_PIC_BASE;
6765 else
6766 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006767 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006768 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006769 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006770 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006771 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006772
Eric Christopher30ef0e52010-06-03 04:07:48 +00006773 // With PIC32, the address is actually $g + Offset.
6774 if (PIC32)
6775 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6776 DAG.getNode(X86ISD::GlobalBaseReg,
6777 DebugLoc(), getPointerTy()),
6778 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006779
Eric Christopher30ef0e52010-06-03 04:07:48 +00006780 // Lowering the machine isd will make sure everything is in the right
6781 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006782 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006783 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006784 SDValue Args[] = { Chain, Offset };
6785 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006786
Eric Christopher30ef0e52010-06-03 04:07:48 +00006787 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6788 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6789 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006790
Eric Christopher30ef0e52010-06-03 04:07:48 +00006791 // And our return value (tls address) is in the standard call return value
6792 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006793 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6794 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006795 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006796
Eric Christopher30ef0e52010-06-03 04:07:48 +00006797 assert(false &&
6798 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006799
Torok Edwinc23197a2009-07-14 16:55:14 +00006800 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006801 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006802}
6803
Evan Cheng0db9fe62006-04-25 20:13:52 +00006804
Nadav Rotem43012222011-05-11 08:12:09 +00006805/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006806/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006807SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006808 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006809 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006810 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006811 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006812 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006813 SDValue ShOpLo = Op.getOperand(0);
6814 SDValue ShOpHi = Op.getOperand(1);
6815 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006816 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006817 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006818 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006819
Dan Gohman475871a2008-07-27 21:46:04 +00006820 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006821 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006822 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6823 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006824 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006825 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6826 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006827 }
Evan Chenge3413162006-01-09 18:33:28 +00006828
Owen Anderson825b72b2009-08-11 20:47:22 +00006829 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6830 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006831 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006832 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006833
Dan Gohman475871a2008-07-27 21:46:04 +00006834 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006835 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006836 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6837 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006838
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006839 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006840 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6841 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006842 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006843 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6844 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006845 }
6846
Dan Gohman475871a2008-07-27 21:46:04 +00006847 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006848 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006849}
Evan Chenga3195e82006-01-12 22:54:21 +00006850
Dan Gohmand858e902010-04-17 15:26:15 +00006851SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6852 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006853 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006854
Dale Johannesen0488fb62010-09-30 23:57:10 +00006855 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006856 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006857
Owen Anderson825b72b2009-08-11 20:47:22 +00006858 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006859 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006860
Eli Friedman36df4992009-05-27 00:47:34 +00006861 // These are really Legal; return the operand so the caller accepts it as
6862 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006863 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006864 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006865 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006866 Subtarget->is64Bit()) {
6867 return Op;
6868 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006869
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006870 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006871 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006872 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006873 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006874 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006875 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006876 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006877 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006878 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006879 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6880}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006881
Owen Andersone50ed302009-08-10 22:56:29 +00006882SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006883 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006884 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006885 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006886 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006887 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006888 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006889 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006890 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006891 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006892 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006893
Chris Lattner492a43e2010-09-22 01:28:21 +00006894 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006895
Stuart Hastings84be9582011-06-02 15:57:11 +00006896 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6897 MachineMemOperand *MMO;
6898 if (FI) {
6899 int SSFI = FI->getIndex();
6900 MMO =
6901 DAG.getMachineFunction()
6902 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6903 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6904 } else {
6905 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6906 StackSlot = StackSlot.getOperand(1);
6907 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006908 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006909 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6910 X86ISD::FILD, DL,
6911 Tys, Ops, array_lengthof(Ops),
6912 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006913
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006914 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006915 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006916 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006917
6918 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6919 // shouldn't be necessary except that RFP cannot be live across
6920 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006921 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006922 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6923 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006924 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006925 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006926 SDValue Ops[] = {
6927 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6928 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006929 MachineMemOperand *MMO =
6930 DAG.getMachineFunction()
6931 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006932 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006933
Chris Lattner492a43e2010-09-22 01:28:21 +00006934 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6935 Ops, array_lengthof(Ops),
6936 Op.getValueType(), MMO);
6937 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006938 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006939 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006940 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006941
Evan Cheng0db9fe62006-04-25 20:13:52 +00006942 return Result;
6943}
6944
Bill Wendling8b8a6362009-01-17 03:56:04 +00006945// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006946SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6947 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006948 // This algorithm is not obvious. Here it is in C code, more or less:
6949 /*
6950 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6951 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6952 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006953
Bill Wendling8b8a6362009-01-17 03:56:04 +00006954 // Copy ints to xmm registers.
6955 __m128i xh = _mm_cvtsi32_si128( hi );
6956 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006957
Bill Wendling8b8a6362009-01-17 03:56:04 +00006958 // Combine into low half of a single xmm register.
6959 __m128i x = _mm_unpacklo_epi32( xh, xl );
6960 __m128d d;
6961 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006962
Bill Wendling8b8a6362009-01-17 03:56:04 +00006963 // Merge in appropriate exponents to give the integer bits the right
6964 // magnitude.
6965 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006966
Bill Wendling8b8a6362009-01-17 03:56:04 +00006967 // Subtract away the biases to deal with the IEEE-754 double precision
6968 // implicit 1.
6969 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006970
Bill Wendling8b8a6362009-01-17 03:56:04 +00006971 // All conversions up to here are exact. The correctly rounded result is
6972 // calculated using the current rounding mode using the following
6973 // horizontal add.
6974 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6975 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6976 // store doesn't really need to be here (except
6977 // maybe to zero the other double)
6978 return sd;
6979 }
6980 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006981
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006982 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006983 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006984
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006985 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006986 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006987 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6988 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6989 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6990 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006991 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006992 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006993
Bill Wendling8b8a6362009-01-17 03:56:04 +00006994 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006995 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006996 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006997 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006998 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006999 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007000 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007001
Owen Anderson825b72b2009-08-11 20:47:22 +00007002 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7003 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007004 Op.getOperand(0),
7005 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007006 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7007 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007008 Op.getOperand(0),
7009 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007010 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7011 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007012 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007013 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007014 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007015 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007016 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007017 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007018 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007019 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007020
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007021 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007022 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007023 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7024 DAG.getUNDEF(MVT::v2f64), ShufMask);
7025 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7026 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007027 DAG.getIntPtrConstant(0));
7028}
7029
Bill Wendling8b8a6362009-01-17 03:56:04 +00007030// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007031SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7032 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007033 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007034 // FP constant to bias correct the final result.
7035 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007036 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007037
7038 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007039 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7040 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00007041 Op.getOperand(0),
7042 DAG.getIntPtrConstant(0)));
7043
Owen Anderson825b72b2009-08-11 20:47:22 +00007044 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007045 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007046 DAG.getIntPtrConstant(0));
7047
7048 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007050 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007051 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007053 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007054 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007055 MVT::v2f64, Bias)));
7056 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007057 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007058 DAG.getIntPtrConstant(0));
7059
7060 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007062
7063 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007064 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007065
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007067 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007068 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007069 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007070 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007071 }
7072
7073 // Handle final rounding.
7074 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007075}
7076
Dan Gohmand858e902010-04-17 15:26:15 +00007077SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7078 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007079 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007080 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007081
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007082 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007083 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7084 // the optimization here.
7085 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007086 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007087
Owen Andersone50ed302009-08-10 22:56:29 +00007088 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007089 EVT DstVT = Op.getValueType();
7090 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007091 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007092 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007093 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007094
7095 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007096 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007097 if (SrcVT == MVT::i32) {
7098 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7099 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7100 getPointerTy(), StackSlot, WordOff);
7101 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007102 StackSlot, MachinePointerInfo(),
7103 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007104 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007105 OffsetSlot, MachinePointerInfo(),
7106 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007107 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7108 return Fild;
7109 }
7110
7111 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7112 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007113 StackSlot, MachinePointerInfo(),
7114 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007115 // For i64 source, we need to add the appropriate power of 2 if the input
7116 // was negative. This is the same as the optimization in
7117 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7118 // we must be careful to do the computation in x87 extended precision, not
7119 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007120 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7121 MachineMemOperand *MMO =
7122 DAG.getMachineFunction()
7123 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7124 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007125
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007126 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7127 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007128 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7129 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007130
7131 APInt FF(32, 0x5F800000ULL);
7132
7133 // Check whether the sign bit is set.
7134 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7135 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7136 ISD::SETLT);
7137
7138 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7139 SDValue FudgePtr = DAG.getConstantPool(
7140 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7141 getPointerTy());
7142
7143 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7144 SDValue Zero = DAG.getIntPtrConstant(0);
7145 SDValue Four = DAG.getIntPtrConstant(4);
7146 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7147 Zero, Four);
7148 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7149
7150 // Load the value out, extending it from f32 to f80.
7151 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007152 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007153 FudgePtr, MachinePointerInfo::getConstantPool(),
7154 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007155 // Extend everything to 80 bits to force it to be done on x87.
7156 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7157 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007158}
7159
Dan Gohman475871a2008-07-27 21:46:04 +00007160std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007161FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007162 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007163
Owen Andersone50ed302009-08-10 22:56:29 +00007164 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007165
7166 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007167 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7168 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007169 }
7170
Owen Anderson825b72b2009-08-11 20:47:22 +00007171 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7172 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007173 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007174
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007175 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007176 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007177 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007178 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007179 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007180 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007181 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007182 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007183
Evan Cheng87c89352007-10-15 20:11:21 +00007184 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7185 // stack slot.
7186 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007187 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007188 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007189 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007190
Michael J. Spencerec38de22010-10-10 22:04:20 +00007191
7192
Evan Cheng0db9fe62006-04-25 20:13:52 +00007193 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007194 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007195 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007196 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7197 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7198 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007199 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007200
Dan Gohman475871a2008-07-27 21:46:04 +00007201 SDValue Chain = DAG.getEntryNode();
7202 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007203 EVT TheVT = Op.getOperand(0).getValueType();
7204 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007205 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007206 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007207 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007208 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007209 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007210 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007211 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007212 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007213
Chris Lattner492a43e2010-09-22 01:28:21 +00007214 MachineMemOperand *MMO =
7215 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7216 MachineMemOperand::MOLoad, MemSize, MemSize);
7217 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7218 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007219 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007220 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007221 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7222 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007223
Chris Lattner07290932010-09-22 01:05:16 +00007224 MachineMemOperand *MMO =
7225 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7226 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007227
Evan Cheng0db9fe62006-04-25 20:13:52 +00007228 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007229 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007230 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7231 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007232
Chris Lattner27a6c732007-11-24 07:07:01 +00007233 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007234}
7235
Dan Gohmand858e902010-04-17 15:26:15 +00007236SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7237 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007238 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007239 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007240
Eli Friedman948e95a2009-05-23 09:59:16 +00007241 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007242 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007243 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7244 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007245
Chris Lattner27a6c732007-11-24 07:07:01 +00007246 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007247 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007248 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007249}
7250
Dan Gohmand858e902010-04-17 15:26:15 +00007251SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7252 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007253 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7254 SDValue FIST = Vals.first, StackSlot = Vals.second;
7255 assert(FIST.getNode() && "Unexpected failure");
7256
7257 // Load the result.
7258 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007259 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007260}
7261
Dan Gohmand858e902010-04-17 15:26:15 +00007262SDValue X86TargetLowering::LowerFABS(SDValue Op,
7263 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007264 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007265 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007266 EVT VT = Op.getValueType();
7267 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007268 if (VT.isVector())
7269 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007270 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007271 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007272 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007273 CV.push_back(C);
7274 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007275 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007276 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007277 CV.push_back(C);
7278 CV.push_back(C);
7279 CV.push_back(C);
7280 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007281 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007282 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007283 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007284 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007285 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007286 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007287 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007288}
7289
Dan Gohmand858e902010-04-17 15:26:15 +00007290SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007291 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007292 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007293 EVT VT = Op.getValueType();
7294 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007295 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007296 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007297 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007298 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007299 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007300 CV.push_back(C);
7301 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007302 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007303 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007304 CV.push_back(C);
7305 CV.push_back(C);
7306 CV.push_back(C);
7307 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007308 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007309 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007310 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007311 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007312 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007313 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007314 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007315 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007316 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007317 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007318 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007319 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007320 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007321 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007322 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007323}
7324
Dan Gohmand858e902010-04-17 15:26:15 +00007325SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007326 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007327 SDValue Op0 = Op.getOperand(0);
7328 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007329 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007330 EVT VT = Op.getValueType();
7331 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007332
7333 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007334 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007335 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007336 SrcVT = VT;
7337 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007338 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007339 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007340 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007341 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007342 }
7343
7344 // At this point the operands and the result should have the same
7345 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007346
Evan Cheng68c47cb2007-01-05 07:55:56 +00007347 // First get the sign bit of second operand.
7348 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007349 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007350 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7351 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007352 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007353 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7354 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7355 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7356 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007357 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007358 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007359 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007360 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007361 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007362 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007363 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007364
7365 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007366 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007367 // Op0 is MVT::f32, Op1 is MVT::f64.
7368 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7369 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7370 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007371 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007372 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007373 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007374 }
7375
Evan Cheng73d6cf12007-01-05 21:37:56 +00007376 // Clear first operand sign bit.
7377 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007378 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007379 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7380 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007381 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007382 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7383 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7384 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7385 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007386 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007387 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007388 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007389 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007390 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007391 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007392 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007393
7394 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007395 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007396}
7397
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007398SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7399 SDValue N0 = Op.getOperand(0);
7400 DebugLoc dl = Op.getDebugLoc();
7401 EVT VT = Op.getValueType();
7402
7403 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7404 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7405 DAG.getConstant(1, VT));
7406 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7407}
7408
Dan Gohman076aee32009-03-04 19:44:21 +00007409/// Emit nodes that will be selected as "test Op0,Op0", or something
7410/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007411SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007412 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007413 DebugLoc dl = Op.getDebugLoc();
7414
Dan Gohman31125812009-03-07 01:58:32 +00007415 // CF and OF aren't always set the way we want. Determine which
7416 // of these we need.
7417 bool NeedCF = false;
7418 bool NeedOF = false;
7419 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007420 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007421 case X86::COND_A: case X86::COND_AE:
7422 case X86::COND_B: case X86::COND_BE:
7423 NeedCF = true;
7424 break;
7425 case X86::COND_G: case X86::COND_GE:
7426 case X86::COND_L: case X86::COND_LE:
7427 case X86::COND_O: case X86::COND_NO:
7428 NeedOF = true;
7429 break;
Dan Gohman31125812009-03-07 01:58:32 +00007430 }
7431
Dan Gohman076aee32009-03-04 19:44:21 +00007432 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007433 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7434 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007435 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7436 // Emit a CMP with 0, which is the TEST pattern.
7437 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7438 DAG.getConstant(0, Op.getValueType()));
7439
7440 unsigned Opcode = 0;
7441 unsigned NumOperands = 0;
7442 switch (Op.getNode()->getOpcode()) {
7443 case ISD::ADD:
7444 // Due to an isel shortcoming, be conservative if this add is likely to be
7445 // selected as part of a load-modify-store instruction. When the root node
7446 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7447 // uses of other nodes in the match, such as the ADD in this case. This
7448 // leads to the ADD being left around and reselected, with the result being
7449 // two adds in the output. Alas, even if none our users are stores, that
7450 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7451 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7452 // climbing the DAG back to the root, and it doesn't seem to be worth the
7453 // effort.
7454 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007455 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007456 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7457 goto default_case;
7458
7459 if (ConstantSDNode *C =
7460 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7461 // An add of one will be selected as an INC.
7462 if (C->getAPIntValue() == 1) {
7463 Opcode = X86ISD::INC;
7464 NumOperands = 1;
7465 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007466 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007467
7468 // An add of negative one (subtract of one) will be selected as a DEC.
7469 if (C->getAPIntValue().isAllOnesValue()) {
7470 Opcode = X86ISD::DEC;
7471 NumOperands = 1;
7472 break;
7473 }
Dan Gohman076aee32009-03-04 19:44:21 +00007474 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007475
7476 // Otherwise use a regular EFLAGS-setting add.
7477 Opcode = X86ISD::ADD;
7478 NumOperands = 2;
7479 break;
7480 case ISD::AND: {
7481 // If the primary and result isn't used, don't bother using X86ISD::AND,
7482 // because a TEST instruction will be better.
7483 bool NonFlagUse = false;
7484 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7485 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7486 SDNode *User = *UI;
7487 unsigned UOpNo = UI.getOperandNo();
7488 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7489 // Look pass truncate.
7490 UOpNo = User->use_begin().getOperandNo();
7491 User = *User->use_begin();
7492 }
7493
7494 if (User->getOpcode() != ISD::BRCOND &&
7495 User->getOpcode() != ISD::SETCC &&
7496 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7497 NonFlagUse = true;
7498 break;
7499 }
Dan Gohman076aee32009-03-04 19:44:21 +00007500 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007501
7502 if (!NonFlagUse)
7503 break;
7504 }
7505 // FALL THROUGH
7506 case ISD::SUB:
7507 case ISD::OR:
7508 case ISD::XOR:
7509 // Due to the ISEL shortcoming noted above, be conservative if this op is
7510 // likely to be selected as part of a load-modify-store instruction.
7511 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7512 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7513 if (UI->getOpcode() == ISD::STORE)
7514 goto default_case;
7515
7516 // Otherwise use a regular EFLAGS-setting instruction.
7517 switch (Op.getNode()->getOpcode()) {
7518 default: llvm_unreachable("unexpected operator!");
7519 case ISD::SUB: Opcode = X86ISD::SUB; break;
7520 case ISD::OR: Opcode = X86ISD::OR; break;
7521 case ISD::XOR: Opcode = X86ISD::XOR; break;
7522 case ISD::AND: Opcode = X86ISD::AND; break;
7523 }
7524
7525 NumOperands = 2;
7526 break;
7527 case X86ISD::ADD:
7528 case X86ISD::SUB:
7529 case X86ISD::INC:
7530 case X86ISD::DEC:
7531 case X86ISD::OR:
7532 case X86ISD::XOR:
7533 case X86ISD::AND:
7534 return SDValue(Op.getNode(), 1);
7535 default:
7536 default_case:
7537 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007538 }
7539
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007540 if (Opcode == 0)
7541 // Emit a CMP with 0, which is the TEST pattern.
7542 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7543 DAG.getConstant(0, Op.getValueType()));
7544
7545 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7546 SmallVector<SDValue, 4> Ops;
7547 for (unsigned i = 0; i != NumOperands; ++i)
7548 Ops.push_back(Op.getOperand(i));
7549
7550 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7551 DAG.ReplaceAllUsesWith(Op, New);
7552 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007553}
7554
7555/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7556/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007557SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007558 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007559 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7560 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007561 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007562
7563 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007564 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007565}
7566
Evan Chengd40d03e2010-01-06 19:38:29 +00007567/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7568/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007569SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7570 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007571 SDValue Op0 = And.getOperand(0);
7572 SDValue Op1 = And.getOperand(1);
7573 if (Op0.getOpcode() == ISD::TRUNCATE)
7574 Op0 = Op0.getOperand(0);
7575 if (Op1.getOpcode() == ISD::TRUNCATE)
7576 Op1 = Op1.getOperand(0);
7577
Evan Chengd40d03e2010-01-06 19:38:29 +00007578 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007579 if (Op1.getOpcode() == ISD::SHL)
7580 std::swap(Op0, Op1);
7581 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007582 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7583 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007584 // If we looked past a truncate, check that it's only truncating away
7585 // known zeros.
7586 unsigned BitWidth = Op0.getValueSizeInBits();
7587 unsigned AndBitWidth = And.getValueSizeInBits();
7588 if (BitWidth > AndBitWidth) {
7589 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7590 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7591 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7592 return SDValue();
7593 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007594 LHS = Op1;
7595 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007596 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007597 } else if (Op1.getOpcode() == ISD::Constant) {
7598 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7599 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007600 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7601 LHS = AndLHS.getOperand(0);
7602 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007603 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007604 }
Evan Cheng0488db92007-09-25 01:57:46 +00007605
Evan Chengd40d03e2010-01-06 19:38:29 +00007606 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007607 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007608 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007609 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007610 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007611 // Also promote i16 to i32 for performance / code size reason.
7612 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007613 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007614 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007615
Evan Chengd40d03e2010-01-06 19:38:29 +00007616 // If the operand types disagree, extend the shift amount to match. Since
7617 // BT ignores high bits (like shifts) we can use anyextend.
7618 if (LHS.getValueType() != RHS.getValueType())
7619 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007620
Evan Chengd40d03e2010-01-06 19:38:29 +00007621 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7622 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7623 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7624 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007625 }
7626
Evan Cheng54de3ea2010-01-05 06:52:31 +00007627 return SDValue();
7628}
7629
Dan Gohmand858e902010-04-17 15:26:15 +00007630SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007631 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7632 SDValue Op0 = Op.getOperand(0);
7633 SDValue Op1 = Op.getOperand(1);
7634 DebugLoc dl = Op.getDebugLoc();
7635 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7636
7637 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007638 // Lower (X & (1 << N)) == 0 to BT(X, N).
7639 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7640 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007641 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007642 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007643 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007644 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7645 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7646 if (NewSetCC.getNode())
7647 return NewSetCC;
7648 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007649
Chris Lattner481eebc2010-12-19 21:23:48 +00007650 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7651 // these.
7652 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007653 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007654 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7655 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007656
Chris Lattner481eebc2010-12-19 21:23:48 +00007657 // If the input is a setcc, then reuse the input setcc or use a new one with
7658 // the inverted condition.
7659 if (Op0.getOpcode() == X86ISD::SETCC) {
7660 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7661 bool Invert = (CC == ISD::SETNE) ^
7662 cast<ConstantSDNode>(Op1)->isNullValue();
7663 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007664
Evan Cheng2c755ba2010-02-27 07:36:59 +00007665 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007666 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7667 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7668 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007669 }
7670
Evan Chenge5b51ac2010-04-17 06:13:15 +00007671 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007672 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007673 if (X86CC == X86::COND_INVALID)
7674 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007675
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007676 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007677 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007678 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007679}
7680
Dan Gohmand858e902010-04-17 15:26:15 +00007681SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007682 SDValue Cond;
7683 SDValue Op0 = Op.getOperand(0);
7684 SDValue Op1 = Op.getOperand(1);
7685 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007686 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007687 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7688 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007689 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007690
7691 if (isFP) {
7692 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007693 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007694 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7695 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007696 bool Swap = false;
7697
7698 switch (SetCCOpcode) {
7699 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007700 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007701 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007702 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007703 case ISD::SETGT: Swap = true; // Fallthrough
7704 case ISD::SETLT:
7705 case ISD::SETOLT: SSECC = 1; break;
7706 case ISD::SETOGE:
7707 case ISD::SETGE: Swap = true; // Fallthrough
7708 case ISD::SETLE:
7709 case ISD::SETOLE: SSECC = 2; break;
7710 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007711 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007712 case ISD::SETNE: SSECC = 4; break;
7713 case ISD::SETULE: Swap = true;
7714 case ISD::SETUGE: SSECC = 5; break;
7715 case ISD::SETULT: Swap = true;
7716 case ISD::SETUGT: SSECC = 6; break;
7717 case ISD::SETO: SSECC = 7; break;
7718 }
7719 if (Swap)
7720 std::swap(Op0, Op1);
7721
Nate Begemanfb8ead02008-07-25 19:05:58 +00007722 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007723 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007724 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007725 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007726 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7727 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007728 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007729 }
7730 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007731 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007732 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7733 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007734 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007735 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007736 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007737 }
7738 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007739 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007740 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007741
Nate Begeman30a0de92008-07-17 16:51:19 +00007742 // We are handling one of the integer comparisons here. Since SSE only has
7743 // GT and EQ comparisons for integer, swapping operands and multiple
7744 // operations may be required for some comparisons.
7745 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7746 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007747
Owen Anderson825b72b2009-08-11 20:47:22 +00007748 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007749 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007750 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007751 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007752 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7753 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007754 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007755
Nate Begeman30a0de92008-07-17 16:51:19 +00007756 switch (SetCCOpcode) {
7757 default: break;
7758 case ISD::SETNE: Invert = true;
7759 case ISD::SETEQ: Opc = EQOpc; break;
7760 case ISD::SETLT: Swap = true;
7761 case ISD::SETGT: Opc = GTOpc; break;
7762 case ISD::SETGE: Swap = true;
7763 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7764 case ISD::SETULT: Swap = true;
7765 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7766 case ISD::SETUGE: Swap = true;
7767 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7768 }
7769 if (Swap)
7770 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007771
Nate Begeman30a0de92008-07-17 16:51:19 +00007772 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7773 // bits of the inputs before performing those operations.
7774 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007775 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007776 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7777 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007778 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007779 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7780 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007781 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7782 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007783 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007784
Dale Johannesenace16102009-02-03 19:33:06 +00007785 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007786
7787 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007788 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007789 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007790
Nate Begeman30a0de92008-07-17 16:51:19 +00007791 return Result;
7792}
Evan Cheng0488db92007-09-25 01:57:46 +00007793
Evan Cheng370e5342008-12-03 08:38:43 +00007794// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007795static bool isX86LogicalCmp(SDValue Op) {
7796 unsigned Opc = Op.getNode()->getOpcode();
7797 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7798 return true;
7799 if (Op.getResNo() == 1 &&
7800 (Opc == X86ISD::ADD ||
7801 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007802 Opc == X86ISD::ADC ||
7803 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007804 Opc == X86ISD::SMUL ||
7805 Opc == X86ISD::UMUL ||
7806 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007807 Opc == X86ISD::DEC ||
7808 Opc == X86ISD::OR ||
7809 Opc == X86ISD::XOR ||
7810 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007811 return true;
7812
Chris Lattner9637d5b2010-12-05 07:49:54 +00007813 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7814 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007815
Dan Gohman076aee32009-03-04 19:44:21 +00007816 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007817}
7818
Chris Lattnera2b56002010-12-05 01:23:24 +00007819static bool isZero(SDValue V) {
7820 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7821 return C && C->isNullValue();
7822}
7823
Chris Lattner96908b12010-12-05 02:00:51 +00007824static bool isAllOnes(SDValue V) {
7825 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7826 return C && C->isAllOnesValue();
7827}
7828
Dan Gohmand858e902010-04-17 15:26:15 +00007829SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007830 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007831 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007832 SDValue Op1 = Op.getOperand(1);
7833 SDValue Op2 = Op.getOperand(2);
7834 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007835 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007836
Dan Gohman1a492952009-10-20 16:22:37 +00007837 if (Cond.getOpcode() == ISD::SETCC) {
7838 SDValue NewCond = LowerSETCC(Cond, DAG);
7839 if (NewCond.getNode())
7840 Cond = NewCond;
7841 }
Evan Cheng734503b2006-09-11 02:19:56 +00007842
Chris Lattnera2b56002010-12-05 01:23:24 +00007843 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007844 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007845 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007846 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007847 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007848 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7849 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007850 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007851
Chris Lattnera2b56002010-12-05 01:23:24 +00007852 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007853
7854 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007855 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7856 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007857
7858 SDValue CmpOp0 = Cmp.getOperand(0);
7859 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7860 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007861
Chris Lattner96908b12010-12-05 02:00:51 +00007862 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007863 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7864 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007865
Chris Lattner96908b12010-12-05 02:00:51 +00007866 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7867 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007868
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007869 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007870 if (N2C == 0 || !N2C->isNullValue())
7871 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7872 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007873 }
7874 }
7875
Chris Lattnera2b56002010-12-05 01:23:24 +00007876 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007877 if (Cond.getOpcode() == ISD::AND &&
7878 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7879 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007880 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007881 Cond = Cond.getOperand(0);
7882 }
7883
Evan Cheng3f41d662007-10-08 22:16:29 +00007884 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7885 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007886 if (Cond.getOpcode() == X86ISD::SETCC ||
7887 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007888 CC = Cond.getOperand(0);
7889
Dan Gohman475871a2008-07-27 21:46:04 +00007890 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007891 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007892 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007893
Evan Cheng3f41d662007-10-08 22:16:29 +00007894 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007895 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007896 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007897 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007898
Chris Lattnerd1980a52009-03-12 06:52:53 +00007899 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7900 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007901 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007902 addTest = false;
7903 }
7904 }
7905
7906 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007907 // Look pass the truncate.
7908 if (Cond.getOpcode() == ISD::TRUNCATE)
7909 Cond = Cond.getOperand(0);
7910
7911 // We know the result of AND is compared against zero. Try to match
7912 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007913 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007914 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007915 if (NewSetCC.getNode()) {
7916 CC = NewSetCC.getOperand(0);
7917 Cond = NewSetCC.getOperand(1);
7918 addTest = false;
7919 }
7920 }
7921 }
7922
7923 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007924 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007925 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007926 }
7927
Benjamin Kramere915ff32010-12-22 23:09:28 +00007928 // a < b ? -1 : 0 -> RES = ~setcc_carry
7929 // a < b ? 0 : -1 -> RES = setcc_carry
7930 // a >= b ? -1 : 0 -> RES = setcc_carry
7931 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7932 if (Cond.getOpcode() == X86ISD::CMP) {
7933 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7934
7935 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7936 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7937 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7938 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7939 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7940 return DAG.getNOT(DL, Res, Res.getValueType());
7941 return Res;
7942 }
7943 }
7944
Evan Cheng0488db92007-09-25 01:57:46 +00007945 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7946 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007947 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007948 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007949 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007950}
7951
Evan Cheng370e5342008-12-03 08:38:43 +00007952// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7953// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7954// from the AND / OR.
7955static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7956 Opc = Op.getOpcode();
7957 if (Opc != ISD::OR && Opc != ISD::AND)
7958 return false;
7959 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7960 Op.getOperand(0).hasOneUse() &&
7961 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7962 Op.getOperand(1).hasOneUse());
7963}
7964
Evan Cheng961d6d42009-02-02 08:19:07 +00007965// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7966// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007967static bool isXor1OfSetCC(SDValue Op) {
7968 if (Op.getOpcode() != ISD::XOR)
7969 return false;
7970 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7971 if (N1C && N1C->getAPIntValue() == 1) {
7972 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7973 Op.getOperand(0).hasOneUse();
7974 }
7975 return false;
7976}
7977
Dan Gohmand858e902010-04-17 15:26:15 +00007978SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007979 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007980 SDValue Chain = Op.getOperand(0);
7981 SDValue Cond = Op.getOperand(1);
7982 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007983 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007984 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007985
Dan Gohman1a492952009-10-20 16:22:37 +00007986 if (Cond.getOpcode() == ISD::SETCC) {
7987 SDValue NewCond = LowerSETCC(Cond, DAG);
7988 if (NewCond.getNode())
7989 Cond = NewCond;
7990 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007991#if 0
7992 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007993 else if (Cond.getOpcode() == X86ISD::ADD ||
7994 Cond.getOpcode() == X86ISD::SUB ||
7995 Cond.getOpcode() == X86ISD::SMUL ||
7996 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007997 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007998#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007999
Evan Chengad9c0a32009-12-15 00:53:42 +00008000 // Look pass (and (setcc_carry (cmp ...)), 1).
8001 if (Cond.getOpcode() == ISD::AND &&
8002 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8003 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008004 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008005 Cond = Cond.getOperand(0);
8006 }
8007
Evan Cheng3f41d662007-10-08 22:16:29 +00008008 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8009 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008010 if (Cond.getOpcode() == X86ISD::SETCC ||
8011 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008012 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008013
Dan Gohman475871a2008-07-27 21:46:04 +00008014 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008015 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008016 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008017 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008018 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008019 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008020 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008021 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008022 default: break;
8023 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008024 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008025 // These can only come from an arithmetic instruction with overflow,
8026 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008027 Cond = Cond.getNode()->getOperand(1);
8028 addTest = false;
8029 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008030 }
Evan Cheng0488db92007-09-25 01:57:46 +00008031 }
Evan Cheng370e5342008-12-03 08:38:43 +00008032 } else {
8033 unsigned CondOpc;
8034 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8035 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008036 if (CondOpc == ISD::OR) {
8037 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8038 // two branches instead of an explicit OR instruction with a
8039 // separate test.
8040 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008041 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008042 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008043 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008044 Chain, Dest, CC, Cmp);
8045 CC = Cond.getOperand(1).getOperand(0);
8046 Cond = Cmp;
8047 addTest = false;
8048 }
8049 } else { // ISD::AND
8050 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8051 // two branches instead of an explicit AND instruction with a
8052 // separate test. However, we only do this if this block doesn't
8053 // have a fall-through edge, because this requires an explicit
8054 // jmp when the condition is false.
8055 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008056 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008057 Op.getNode()->hasOneUse()) {
8058 X86::CondCode CCode =
8059 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8060 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008061 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008062 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008063 // Look for an unconditional branch following this conditional branch.
8064 // We need this because we need to reverse the successors in order
8065 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008066 if (User->getOpcode() == ISD::BR) {
8067 SDValue FalseBB = User->getOperand(1);
8068 SDNode *NewBR =
8069 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008070 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008071 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008072 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008073
Dale Johannesene4d209d2009-02-03 20:21:25 +00008074 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008075 Chain, Dest, CC, Cmp);
8076 X86::CondCode CCode =
8077 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8078 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008079 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008080 Cond = Cmp;
8081 addTest = false;
8082 }
8083 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008084 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008085 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8086 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8087 // It should be transformed during dag combiner except when the condition
8088 // is set by a arithmetics with overflow node.
8089 X86::CondCode CCode =
8090 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8091 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008092 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008093 Cond = Cond.getOperand(0).getOperand(1);
8094 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008095 }
Evan Cheng0488db92007-09-25 01:57:46 +00008096 }
8097
8098 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008099 // Look pass the truncate.
8100 if (Cond.getOpcode() == ISD::TRUNCATE)
8101 Cond = Cond.getOperand(0);
8102
8103 // We know the result of AND is compared against zero. Try to match
8104 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008105 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008106 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8107 if (NewSetCC.getNode()) {
8108 CC = NewSetCC.getOperand(0);
8109 Cond = NewSetCC.getOperand(1);
8110 addTest = false;
8111 }
8112 }
8113 }
8114
8115 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008116 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008117 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008118 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008119 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008120 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008121}
8122
Anton Korobeynikove060b532007-04-17 19:34:00 +00008123
8124// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8125// Calls to _alloca is needed to probe the stack when allocating more than 4k
8126// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8127// that the guard pages used by the OS virtual memory manager are allocated in
8128// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008129SDValue
8130X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008131 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008132 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008133 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008134 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008135 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008136
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008137 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008138 SDValue Chain = Op.getOperand(0);
8139 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008140 // FIXME: Ensure alignment here
8141
Dan Gohman475871a2008-07-27 21:46:04 +00008142 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008143
Owen Anderson825b72b2009-08-11 20:47:22 +00008144 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008145 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008146
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008147 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008148 Flag = Chain.getValue(1);
8149
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008150 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008151
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008152 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008153 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008154
Dale Johannesendd64c412009-02-04 00:33:20 +00008155 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008156
Dan Gohman475871a2008-07-27 21:46:04 +00008157 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008158 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008159}
8160
Dan Gohmand858e902010-04-17 15:26:15 +00008161SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008162 MachineFunction &MF = DAG.getMachineFunction();
8163 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8164
Dan Gohman69de1932008-02-06 22:27:42 +00008165 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008166 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008167
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008168 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008169 // vastart just stores the address of the VarArgsFrameIndex slot into the
8170 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008171 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8172 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008173 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8174 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008175 }
8176
8177 // __va_list_tag:
8178 // gp_offset (0 - 6 * 8)
8179 // fp_offset (48 - 48 + 8 * 16)
8180 // overflow_arg_area (point to parameters coming in memory).
8181 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008182 SmallVector<SDValue, 8> MemOps;
8183 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008184 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008185 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008186 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8187 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008188 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008189 MemOps.push_back(Store);
8190
8191 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008192 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008193 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008194 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008195 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8196 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008197 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008198 MemOps.push_back(Store);
8199
8200 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008201 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008202 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008203 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8204 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008205 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8206 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008207 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008208 MemOps.push_back(Store);
8209
8210 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008211 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008212 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008213 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8214 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008215 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8216 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008217 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008218 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008219 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008220}
8221
Dan Gohmand858e902010-04-17 15:26:15 +00008222SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008223 assert(Subtarget->is64Bit() &&
8224 "LowerVAARG only handles 64-bit va_arg!");
8225 assert((Subtarget->isTargetLinux() ||
8226 Subtarget->isTargetDarwin()) &&
8227 "Unhandled target in LowerVAARG");
8228 assert(Op.getNode()->getNumOperands() == 4);
8229 SDValue Chain = Op.getOperand(0);
8230 SDValue SrcPtr = Op.getOperand(1);
8231 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8232 unsigned Align = Op.getConstantOperandVal(3);
8233 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008234
Dan Gohman320afb82010-10-12 18:00:49 +00008235 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008236 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00008237 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8238 uint8_t ArgMode;
8239
8240 // Decide which area this value should be read from.
8241 // TODO: Implement the AMD64 ABI in its entirety. This simple
8242 // selection mechanism works only for the basic types.
8243 if (ArgVT == MVT::f80) {
8244 llvm_unreachable("va_arg for f80 not yet implemented");
8245 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8246 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8247 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8248 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8249 } else {
8250 llvm_unreachable("Unhandled argument type in LowerVAARG");
8251 }
8252
8253 if (ArgMode == 2) {
8254 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008255 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008256 !(DAG.getMachineFunction()
8257 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008258 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008259 }
8260
8261 // Insert VAARG_64 node into the DAG
8262 // VAARG_64 returns two values: Variable Argument Address, Chain
8263 SmallVector<SDValue, 11> InstOps;
8264 InstOps.push_back(Chain);
8265 InstOps.push_back(SrcPtr);
8266 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8267 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8268 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8269 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8270 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8271 VTs, &InstOps[0], InstOps.size(),
8272 MVT::i64,
8273 MachinePointerInfo(SV),
8274 /*Align=*/0,
8275 /*Volatile=*/false,
8276 /*ReadMem=*/true,
8277 /*WriteMem=*/true);
8278 Chain = VAARG.getValue(1);
8279
8280 // Load the next argument and return it
8281 return DAG.getLoad(ArgVT, dl,
8282 Chain,
8283 VAARG,
8284 MachinePointerInfo(),
8285 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008286}
8287
Dan Gohmand858e902010-04-17 15:26:15 +00008288SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008289 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008290 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008291 SDValue Chain = Op.getOperand(0);
8292 SDValue DstPtr = Op.getOperand(1);
8293 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008294 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8295 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008296 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008297
Chris Lattnere72f2022010-09-21 05:40:29 +00008298 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008299 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008300 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008301 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008302}
8303
Dan Gohman475871a2008-07-27 21:46:04 +00008304SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008305X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008306 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008307 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008308 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008309 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008310 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008311 case Intrinsic::x86_sse_comieq_ss:
8312 case Intrinsic::x86_sse_comilt_ss:
8313 case Intrinsic::x86_sse_comile_ss:
8314 case Intrinsic::x86_sse_comigt_ss:
8315 case Intrinsic::x86_sse_comige_ss:
8316 case Intrinsic::x86_sse_comineq_ss:
8317 case Intrinsic::x86_sse_ucomieq_ss:
8318 case Intrinsic::x86_sse_ucomilt_ss:
8319 case Intrinsic::x86_sse_ucomile_ss:
8320 case Intrinsic::x86_sse_ucomigt_ss:
8321 case Intrinsic::x86_sse_ucomige_ss:
8322 case Intrinsic::x86_sse_ucomineq_ss:
8323 case Intrinsic::x86_sse2_comieq_sd:
8324 case Intrinsic::x86_sse2_comilt_sd:
8325 case Intrinsic::x86_sse2_comile_sd:
8326 case Intrinsic::x86_sse2_comigt_sd:
8327 case Intrinsic::x86_sse2_comige_sd:
8328 case Intrinsic::x86_sse2_comineq_sd:
8329 case Intrinsic::x86_sse2_ucomieq_sd:
8330 case Intrinsic::x86_sse2_ucomilt_sd:
8331 case Intrinsic::x86_sse2_ucomile_sd:
8332 case Intrinsic::x86_sse2_ucomigt_sd:
8333 case Intrinsic::x86_sse2_ucomige_sd:
8334 case Intrinsic::x86_sse2_ucomineq_sd: {
8335 unsigned Opc = 0;
8336 ISD::CondCode CC = ISD::SETCC_INVALID;
8337 switch (IntNo) {
8338 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008339 case Intrinsic::x86_sse_comieq_ss:
8340 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008341 Opc = X86ISD::COMI;
8342 CC = ISD::SETEQ;
8343 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008344 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008345 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008346 Opc = X86ISD::COMI;
8347 CC = ISD::SETLT;
8348 break;
8349 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008350 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008351 Opc = X86ISD::COMI;
8352 CC = ISD::SETLE;
8353 break;
8354 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008355 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008356 Opc = X86ISD::COMI;
8357 CC = ISD::SETGT;
8358 break;
8359 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008360 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008361 Opc = X86ISD::COMI;
8362 CC = ISD::SETGE;
8363 break;
8364 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008365 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008366 Opc = X86ISD::COMI;
8367 CC = ISD::SETNE;
8368 break;
8369 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008370 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008371 Opc = X86ISD::UCOMI;
8372 CC = ISD::SETEQ;
8373 break;
8374 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008375 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008376 Opc = X86ISD::UCOMI;
8377 CC = ISD::SETLT;
8378 break;
8379 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008380 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008381 Opc = X86ISD::UCOMI;
8382 CC = ISD::SETLE;
8383 break;
8384 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008385 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008386 Opc = X86ISD::UCOMI;
8387 CC = ISD::SETGT;
8388 break;
8389 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008390 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008391 Opc = X86ISD::UCOMI;
8392 CC = ISD::SETGE;
8393 break;
8394 case Intrinsic::x86_sse_ucomineq_ss:
8395 case Intrinsic::x86_sse2_ucomineq_sd:
8396 Opc = X86ISD::UCOMI;
8397 CC = ISD::SETNE;
8398 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008399 }
Evan Cheng734503b2006-09-11 02:19:56 +00008400
Dan Gohman475871a2008-07-27 21:46:04 +00008401 SDValue LHS = Op.getOperand(1);
8402 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008403 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008404 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008405 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8406 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8407 DAG.getConstant(X86CC, MVT::i8), Cond);
8408 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008409 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008410 // ptest and testp intrinsics. The intrinsic these come from are designed to
8411 // return an integer value, not just an instruction so lower it to the ptest
8412 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008413 case Intrinsic::x86_sse41_ptestz:
8414 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008415 case Intrinsic::x86_sse41_ptestnzc:
8416 case Intrinsic::x86_avx_ptestz_256:
8417 case Intrinsic::x86_avx_ptestc_256:
8418 case Intrinsic::x86_avx_ptestnzc_256:
8419 case Intrinsic::x86_avx_vtestz_ps:
8420 case Intrinsic::x86_avx_vtestc_ps:
8421 case Intrinsic::x86_avx_vtestnzc_ps:
8422 case Intrinsic::x86_avx_vtestz_pd:
8423 case Intrinsic::x86_avx_vtestc_pd:
8424 case Intrinsic::x86_avx_vtestnzc_pd:
8425 case Intrinsic::x86_avx_vtestz_ps_256:
8426 case Intrinsic::x86_avx_vtestc_ps_256:
8427 case Intrinsic::x86_avx_vtestnzc_ps_256:
8428 case Intrinsic::x86_avx_vtestz_pd_256:
8429 case Intrinsic::x86_avx_vtestc_pd_256:
8430 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8431 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008432 unsigned X86CC = 0;
8433 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008434 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008435 case Intrinsic::x86_avx_vtestz_ps:
8436 case Intrinsic::x86_avx_vtestz_pd:
8437 case Intrinsic::x86_avx_vtestz_ps_256:
8438 case Intrinsic::x86_avx_vtestz_pd_256:
8439 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008440 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008441 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008442 // ZF = 1
8443 X86CC = X86::COND_E;
8444 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008445 case Intrinsic::x86_avx_vtestc_ps:
8446 case Intrinsic::x86_avx_vtestc_pd:
8447 case Intrinsic::x86_avx_vtestc_ps_256:
8448 case Intrinsic::x86_avx_vtestc_pd_256:
8449 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008450 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008451 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008452 // CF = 1
8453 X86CC = X86::COND_B;
8454 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008455 case Intrinsic::x86_avx_vtestnzc_ps:
8456 case Intrinsic::x86_avx_vtestnzc_pd:
8457 case Intrinsic::x86_avx_vtestnzc_ps_256:
8458 case Intrinsic::x86_avx_vtestnzc_pd_256:
8459 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008460 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008461 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008462 // ZF and CF = 0
8463 X86CC = X86::COND_A;
8464 break;
8465 }
Eric Christopherfd179292009-08-27 18:07:15 +00008466
Eric Christopher71c67532009-07-29 00:28:05 +00008467 SDValue LHS = Op.getOperand(1);
8468 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008469 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8470 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008471 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8472 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8473 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008474 }
Evan Cheng5759f972008-05-04 09:15:50 +00008475
8476 // Fix vector shift instructions where the last operand is a non-immediate
8477 // i32 value.
8478 case Intrinsic::x86_sse2_pslli_w:
8479 case Intrinsic::x86_sse2_pslli_d:
8480 case Intrinsic::x86_sse2_pslli_q:
8481 case Intrinsic::x86_sse2_psrli_w:
8482 case Intrinsic::x86_sse2_psrli_d:
8483 case Intrinsic::x86_sse2_psrli_q:
8484 case Intrinsic::x86_sse2_psrai_w:
8485 case Intrinsic::x86_sse2_psrai_d:
8486 case Intrinsic::x86_mmx_pslli_w:
8487 case Intrinsic::x86_mmx_pslli_d:
8488 case Intrinsic::x86_mmx_pslli_q:
8489 case Intrinsic::x86_mmx_psrli_w:
8490 case Intrinsic::x86_mmx_psrli_d:
8491 case Intrinsic::x86_mmx_psrli_q:
8492 case Intrinsic::x86_mmx_psrai_w:
8493 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008494 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008495 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008496 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008497
8498 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008499 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008500 switch (IntNo) {
8501 case Intrinsic::x86_sse2_pslli_w:
8502 NewIntNo = Intrinsic::x86_sse2_psll_w;
8503 break;
8504 case Intrinsic::x86_sse2_pslli_d:
8505 NewIntNo = Intrinsic::x86_sse2_psll_d;
8506 break;
8507 case Intrinsic::x86_sse2_pslli_q:
8508 NewIntNo = Intrinsic::x86_sse2_psll_q;
8509 break;
8510 case Intrinsic::x86_sse2_psrli_w:
8511 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8512 break;
8513 case Intrinsic::x86_sse2_psrli_d:
8514 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8515 break;
8516 case Intrinsic::x86_sse2_psrli_q:
8517 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8518 break;
8519 case Intrinsic::x86_sse2_psrai_w:
8520 NewIntNo = Intrinsic::x86_sse2_psra_w;
8521 break;
8522 case Intrinsic::x86_sse2_psrai_d:
8523 NewIntNo = Intrinsic::x86_sse2_psra_d;
8524 break;
8525 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008526 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008527 switch (IntNo) {
8528 case Intrinsic::x86_mmx_pslli_w:
8529 NewIntNo = Intrinsic::x86_mmx_psll_w;
8530 break;
8531 case Intrinsic::x86_mmx_pslli_d:
8532 NewIntNo = Intrinsic::x86_mmx_psll_d;
8533 break;
8534 case Intrinsic::x86_mmx_pslli_q:
8535 NewIntNo = Intrinsic::x86_mmx_psll_q;
8536 break;
8537 case Intrinsic::x86_mmx_psrli_w:
8538 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8539 break;
8540 case Intrinsic::x86_mmx_psrli_d:
8541 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8542 break;
8543 case Intrinsic::x86_mmx_psrli_q:
8544 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8545 break;
8546 case Intrinsic::x86_mmx_psrai_w:
8547 NewIntNo = Intrinsic::x86_mmx_psra_w;
8548 break;
8549 case Intrinsic::x86_mmx_psrai_d:
8550 NewIntNo = Intrinsic::x86_mmx_psra_d;
8551 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008552 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008553 }
8554 break;
8555 }
8556 }
Mon P Wangefa42202009-09-03 19:56:25 +00008557
8558 // The vector shift intrinsics with scalars uses 32b shift amounts but
8559 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8560 // to be zero.
8561 SDValue ShOps[4];
8562 ShOps[0] = ShAmt;
8563 ShOps[1] = DAG.getConstant(0, MVT::i32);
8564 if (ShAmtVT == MVT::v4i32) {
8565 ShOps[2] = DAG.getUNDEF(MVT::i32);
8566 ShOps[3] = DAG.getUNDEF(MVT::i32);
8567 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8568 } else {
8569 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008570// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008571 }
8572
Owen Andersone50ed302009-08-10 22:56:29 +00008573 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008574 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008575 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008576 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008577 Op.getOperand(1), ShAmt);
8578 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008579 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008580}
Evan Cheng72261582005-12-20 06:22:03 +00008581
Dan Gohmand858e902010-04-17 15:26:15 +00008582SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8583 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008584 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8585 MFI->setReturnAddressIsTaken(true);
8586
Bill Wendling64e87322009-01-16 19:25:27 +00008587 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008588 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008589
8590 if (Depth > 0) {
8591 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8592 SDValue Offset =
8593 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008594 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008595 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008596 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008597 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008598 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008599 }
8600
8601 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008602 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008603 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008604 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008605}
8606
Dan Gohmand858e902010-04-17 15:26:15 +00008607SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008608 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8609 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008610
Owen Andersone50ed302009-08-10 22:56:29 +00008611 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008612 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008613 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8614 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008615 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008616 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008617 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8618 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008619 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008620 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008621}
8622
Dan Gohman475871a2008-07-27 21:46:04 +00008623SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008624 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008625 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008626}
8627
Dan Gohmand858e902010-04-17 15:26:15 +00008628SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008629 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008630 SDValue Chain = Op.getOperand(0);
8631 SDValue Offset = Op.getOperand(1);
8632 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008633 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008634
Dan Gohmand8816272010-08-11 18:14:00 +00008635 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8636 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8637 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008638 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008639
Dan Gohmand8816272010-08-11 18:14:00 +00008640 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8641 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008642 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008643 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8644 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008645 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008646 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008647
Dale Johannesene4d209d2009-02-03 20:21:25 +00008648 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008649 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008650 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008651}
8652
Dan Gohman475871a2008-07-27 21:46:04 +00008653SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008654 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008655 SDValue Root = Op.getOperand(0);
8656 SDValue Trmp = Op.getOperand(1); // trampoline
8657 SDValue FPtr = Op.getOperand(2); // nested function
8658 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008659 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008660
Dan Gohman69de1932008-02-06 22:27:42 +00008661 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008662
8663 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008664 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008665
8666 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008667 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8668 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008669
Evan Cheng0e6a0522011-07-18 20:57:22 +00008670 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
8671 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008672
8673 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8674
8675 // Load the pointer to the nested function into R11.
8676 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008677 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008678 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008679 Addr, MachinePointerInfo(TrmpAddr),
8680 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008681
Owen Anderson825b72b2009-08-11 20:47:22 +00008682 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8683 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008684 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8685 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008686 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008687
8688 // Load the 'nest' parameter value into R10.
8689 // R10 is specified in X86CallingConv.td
8690 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008691 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8692 DAG.getConstant(10, MVT::i64));
8693 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008694 Addr, MachinePointerInfo(TrmpAddr, 10),
8695 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008696
Owen Anderson825b72b2009-08-11 20:47:22 +00008697 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8698 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008699 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8700 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008701 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008702
8703 // Jump to the nested function.
8704 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008705 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8706 DAG.getConstant(20, MVT::i64));
8707 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008708 Addr, MachinePointerInfo(TrmpAddr, 20),
8709 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008710
8711 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008712 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8713 DAG.getConstant(22, MVT::i64));
8714 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008715 MachinePointerInfo(TrmpAddr, 22),
8716 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008717
Dan Gohman475871a2008-07-27 21:46:04 +00008718 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008719 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008720 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008721 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008722 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008723 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008724 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008725 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008726
8727 switch (CC) {
8728 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008729 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008730 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008731 case CallingConv::X86_StdCall: {
8732 // Pass 'nest' parameter in ECX.
8733 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008734 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008735
8736 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008737 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008738 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008739
Chris Lattner58d74912008-03-12 17:45:29 +00008740 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008741 unsigned InRegCount = 0;
8742 unsigned Idx = 1;
8743
8744 for (FunctionType::param_iterator I = FTy->param_begin(),
8745 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008746 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008747 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008748 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008749
8750 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008751 report_fatal_error("Nest register in use - reduce number of inreg"
8752 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008753 }
8754 }
8755 break;
8756 }
8757 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008758 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008759 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008760 // Pass 'nest' parameter in EAX.
8761 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008762 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008763 break;
8764 }
8765
Dan Gohman475871a2008-07-27 21:46:04 +00008766 SDValue OutChains[4];
8767 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008768
Owen Anderson825b72b2009-08-11 20:47:22 +00008769 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8770 DAG.getConstant(10, MVT::i32));
8771 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008772
Chris Lattnera62fe662010-02-05 19:20:30 +00008773 // This is storing the opcode for MOV32ri.
8774 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00008775 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008776 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008777 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008778 Trmp, MachinePointerInfo(TrmpAddr),
8779 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008780
Owen Anderson825b72b2009-08-11 20:47:22 +00008781 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8782 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008783 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8784 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008785 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008786
Chris Lattnera62fe662010-02-05 19:20:30 +00008787 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008788 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8789 DAG.getConstant(5, MVT::i32));
8790 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008791 MachinePointerInfo(TrmpAddr, 5),
8792 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008793
Owen Anderson825b72b2009-08-11 20:47:22 +00008794 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8795 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008796 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8797 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008798 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008799
Dan Gohman475871a2008-07-27 21:46:04 +00008800 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008801 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008802 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008803 }
8804}
8805
Dan Gohmand858e902010-04-17 15:26:15 +00008806SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8807 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008808 /*
8809 The rounding mode is in bits 11:10 of FPSR, and has the following
8810 settings:
8811 00 Round to nearest
8812 01 Round to -inf
8813 10 Round to +inf
8814 11 Round to 0
8815
8816 FLT_ROUNDS, on the other hand, expects the following:
8817 -1 Undefined
8818 0 Round to 0
8819 1 Round to nearest
8820 2 Round to +inf
8821 3 Round to -inf
8822
8823 To perform the conversion, we do:
8824 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8825 */
8826
8827 MachineFunction &MF = DAG.getMachineFunction();
8828 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008829 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008830 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008831 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008832 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008833
8834 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008835 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008836 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008837
Michael J. Spencerec38de22010-10-10 22:04:20 +00008838
Chris Lattner2156b792010-09-22 01:11:26 +00008839 MachineMemOperand *MMO =
8840 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8841 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008842
Chris Lattner2156b792010-09-22 01:11:26 +00008843 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8844 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8845 DAG.getVTList(MVT::Other),
8846 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008847
8848 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008849 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008850 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008851
8852 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008853 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008854 DAG.getNode(ISD::SRL, DL, MVT::i16,
8855 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008856 CWD, DAG.getConstant(0x800, MVT::i16)),
8857 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008858 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008859 DAG.getNode(ISD::SRL, DL, MVT::i16,
8860 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008861 CWD, DAG.getConstant(0x400, MVT::i16)),
8862 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008863
Dan Gohman475871a2008-07-27 21:46:04 +00008864 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008865 DAG.getNode(ISD::AND, DL, MVT::i16,
8866 DAG.getNode(ISD::ADD, DL, MVT::i16,
8867 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008868 DAG.getConstant(1, MVT::i16)),
8869 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008870
8871
Duncan Sands83ec4b62008-06-06 12:08:01 +00008872 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008873 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008874}
8875
Dan Gohmand858e902010-04-17 15:26:15 +00008876SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008877 EVT VT = Op.getValueType();
8878 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008879 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008880 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008881
8882 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008883 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008884 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008885 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008886 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008887 }
Evan Cheng18efe262007-12-14 02:13:44 +00008888
Evan Cheng152804e2007-12-14 08:30:15 +00008889 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008890 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008891 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008892
8893 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008894 SDValue Ops[] = {
8895 Op,
8896 DAG.getConstant(NumBits+NumBits-1, OpVT),
8897 DAG.getConstant(X86::COND_E, MVT::i8),
8898 Op.getValue(1)
8899 };
8900 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008901
8902 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008903 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008904
Owen Anderson825b72b2009-08-11 20:47:22 +00008905 if (VT == MVT::i8)
8906 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008907 return Op;
8908}
8909
Dan Gohmand858e902010-04-17 15:26:15 +00008910SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008911 EVT VT = Op.getValueType();
8912 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008913 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008914 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008915
8916 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008917 if (VT == MVT::i8) {
8918 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008919 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008920 }
Evan Cheng152804e2007-12-14 08:30:15 +00008921
8922 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008923 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008924 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008925
8926 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008927 SDValue Ops[] = {
8928 Op,
8929 DAG.getConstant(NumBits, OpVT),
8930 DAG.getConstant(X86::COND_E, MVT::i8),
8931 Op.getValue(1)
8932 };
8933 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008934
Owen Anderson825b72b2009-08-11 20:47:22 +00008935 if (VT == MVT::i8)
8936 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008937 return Op;
8938}
8939
Dan Gohmand858e902010-04-17 15:26:15 +00008940SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008941 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008942 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008943 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008944
Mon P Wangaf9b9522008-12-18 21:42:19 +00008945 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8946 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8947 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8948 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8949 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8950 //
8951 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8952 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8953 // return AloBlo + AloBhi + AhiBlo;
8954
8955 SDValue A = Op.getOperand(0);
8956 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008957
Dale Johannesene4d209d2009-02-03 20:21:25 +00008958 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008959 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8960 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008961 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008962 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8963 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008964 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008965 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008966 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008967 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008968 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008969 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008970 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008971 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008972 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008973 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008974 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8975 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008976 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008977 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8978 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008979 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8980 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008981 return Res;
8982}
8983
Nadav Rotem43012222011-05-11 08:12:09 +00008984SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8985
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008986 EVT VT = Op.getValueType();
8987 DebugLoc dl = Op.getDebugLoc();
8988 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008989 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008990
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008991 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008992
Nadav Rotem43012222011-05-11 08:12:09 +00008993 // Must have SSE2.
8994 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008995
Nadav Rotem43012222011-05-11 08:12:09 +00008996 // Optimize shl/srl/sra with constant shift amount.
8997 if (isSplatVector(Amt.getNode())) {
8998 SDValue SclrAmt = Amt->getOperand(0);
8999 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9000 uint64_t ShiftAmt = C->getZExtValue();
9001
9002 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9003 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9004 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9005 R, DAG.getConstant(ShiftAmt, MVT::i32));
9006
9007 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9008 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9009 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9010 R, DAG.getConstant(ShiftAmt, MVT::i32));
9011
9012 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9013 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9014 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9015 R, DAG.getConstant(ShiftAmt, MVT::i32));
9016
9017 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9018 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9019 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9020 R, DAG.getConstant(ShiftAmt, MVT::i32));
9021
9022 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9023 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9024 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9025 R, DAG.getConstant(ShiftAmt, MVT::i32));
9026
9027 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9028 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9029 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9030 R, DAG.getConstant(ShiftAmt, MVT::i32));
9031
9032 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9033 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9034 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9035 R, DAG.getConstant(ShiftAmt, MVT::i32));
9036
9037 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9038 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9039 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9040 R, DAG.getConstant(ShiftAmt, MVT::i32));
9041 }
9042 }
9043
9044 // Lower SHL with variable shift amount.
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009045 // Cannot lower SHL without SSE2 or later.
9046 if (!Subtarget->hasSSE2()) return SDValue();
Nadav Rotem43012222011-05-11 08:12:09 +00009047
9048 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009049 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9050 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9051 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9052
9053 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009054
Nate Begeman51409212010-07-28 00:21:48 +00009055 std::vector<Constant*> CV(4, CI);
9056 Constant *C = ConstantVector::get(CV);
9057 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9058 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009059 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009060 false, false, 16);
9061
9062 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009063 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009064 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9065 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9066 }
Nadav Rotem43012222011-05-11 08:12:09 +00009067 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009068 // a = a << 5;
9069 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9070 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9071 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9072
9073 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9074 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9075
9076 std::vector<Constant*> CVM1(16, CM1);
9077 std::vector<Constant*> CVM2(16, CM2);
9078 Constant *C = ConstantVector::get(CVM1);
9079 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9080 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009081 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009082 false, false, 16);
9083
9084 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9085 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9086 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9087 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9088 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009089 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009090 // a += a
9091 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009092
Nate Begeman51409212010-07-28 00:21:48 +00009093 C = ConstantVector::get(CVM2);
9094 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9095 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009096 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009097 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009098
Nate Begeman51409212010-07-28 00:21:48 +00009099 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9100 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9101 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9102 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9103 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009104 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009105 // a += a
9106 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009107
Nate Begeman51409212010-07-28 00:21:48 +00009108 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009109 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00009110 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
9111 return R;
9112 }
9113 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009114}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009115
Dan Gohmand858e902010-04-17 15:26:15 +00009116SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009117 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9118 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009119 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9120 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009121 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009122 SDValue LHS = N->getOperand(0);
9123 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009124 unsigned BaseOp = 0;
9125 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009126 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009127 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009128 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009129 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009130 // A subtract of one will be selected as a INC. Note that INC doesn't
9131 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009132 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9133 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009134 BaseOp = X86ISD::INC;
9135 Cond = X86::COND_O;
9136 break;
9137 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009138 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009139 Cond = X86::COND_O;
9140 break;
9141 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009142 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009143 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009144 break;
9145 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009146 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9147 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009148 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9149 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009150 BaseOp = X86ISD::DEC;
9151 Cond = X86::COND_O;
9152 break;
9153 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009154 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009155 Cond = X86::COND_O;
9156 break;
9157 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009158 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009159 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009160 break;
9161 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009162 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009163 Cond = X86::COND_O;
9164 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009165 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9166 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9167 MVT::i32);
9168 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009169
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009170 SDValue SetCC =
9171 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9172 DAG.getConstant(X86::COND_O, MVT::i32),
9173 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009174
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009175 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9176 return Sum;
9177 }
Bill Wendling74c37652008-12-09 22:08:41 +00009178 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009179
Bill Wendling61edeb52008-12-02 01:06:39 +00009180 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009181 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009182 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009183
Bill Wendling61edeb52008-12-02 01:06:39 +00009184 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009185 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9186 DAG.getConstant(Cond, MVT::i32),
9187 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009188
Bill Wendling61edeb52008-12-02 01:06:39 +00009189 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9190 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009191}
9192
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009193SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
9194 DebugLoc dl = Op.getDebugLoc();
9195 SDNode* Node = Op.getNode();
9196 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
9197 EVT VT = Node->getValueType(0);
9198
9199 if (Subtarget->hasSSE2() && VT.isVector()) {
9200 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
9201 ExtraVT.getScalarType().getSizeInBits();
9202 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
9203
9204 unsigned SHLIntrinsicsID = 0;
9205 unsigned SRAIntrinsicsID = 0;
9206 switch (VT.getSimpleVT().SimpleTy) {
9207 default:
9208 return SDValue();
9209 case MVT::v2i64: {
9210 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
9211 SRAIntrinsicsID = 0;
9212 break;
9213 }
9214 case MVT::v4i32: {
9215 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
9216 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
9217 break;
9218 }
9219 case MVT::v8i16: {
9220 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
9221 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
9222 break;
9223 }
9224 }
9225
9226 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9227 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
9228 Node->getOperand(0), ShAmt);
9229
9230 // In case of 1 bit sext, no need to shr
9231 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
9232
9233 if (SRAIntrinsicsID) {
9234 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9235 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
9236 Tmp1, ShAmt);
9237 }
9238 return Tmp1;
9239 }
9240
9241 return SDValue();
9242}
9243
9244
Eric Christopher9a9d2752010-07-22 02:48:34 +00009245SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9246 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009247
Eric Christopher77ed1352011-07-08 00:04:56 +00009248 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
9249 // There isn't any reason to disable it if the target processor supports it.
9250 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009251 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +00009252 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009253 SDValue Ops[] = {
9254 DAG.getRegister(X86::ESP, MVT::i32), // Base
9255 DAG.getTargetConstant(1, MVT::i8), // Scale
9256 DAG.getRegister(0, MVT::i32), // Index
9257 DAG.getTargetConstant(0, MVT::i32), // Disp
9258 DAG.getRegister(0, MVT::i32), // Segment.
9259 Zero,
9260 Chain
9261 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009262 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009263 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9264 array_lengthof(Ops));
9265 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009266 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009267
Eric Christopher9a9d2752010-07-22 02:48:34 +00009268 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009269 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009270 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009271
Chris Lattner132929a2010-08-14 17:26:09 +00009272 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9273 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9274 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9275 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009276
Chris Lattner132929a2010-08-14 17:26:09 +00009277 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9278 if (!Op1 && !Op2 && !Op3 && Op4)
9279 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009280
Chris Lattner132929a2010-08-14 17:26:09 +00009281 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9282 if (Op1 && !Op2 && !Op3 && !Op4)
9283 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009284
9285 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009286 // (MFENCE)>;
9287 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009288}
9289
Dan Gohmand858e902010-04-17 15:26:15 +00009290SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009291 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009292 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009293 unsigned Reg = 0;
9294 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009295 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009296 default:
9297 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009298 case MVT::i8: Reg = X86::AL; size = 1; break;
9299 case MVT::i16: Reg = X86::AX; size = 2; break;
9300 case MVT::i32: Reg = X86::EAX; size = 4; break;
9301 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009302 assert(Subtarget->is64Bit() && "Node not type legal!");
9303 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009304 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009305 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009306 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009307 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009308 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009309 Op.getOperand(1),
9310 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009311 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009312 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009313 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009314 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9315 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9316 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009317 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009318 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009319 return cpOut;
9320}
9321
Duncan Sands1607f052008-12-01 11:39:25 +00009322SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009323 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009324 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009325 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009326 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009327 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009328 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009329 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9330 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009331 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009332 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9333 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009334 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009335 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009336 rdx.getValue(1)
9337 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009338 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009339}
9340
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009341SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009342 SelectionDAG &DAG) const {
9343 EVT SrcVT = Op.getOperand(0).getValueType();
9344 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009345 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9346 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009347 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009348 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009349 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009350 // i64 <=> MMX conversions are Legal.
9351 if (SrcVT==MVT::i64 && DstVT.isVector())
9352 return Op;
9353 if (DstVT==MVT::i64 && SrcVT.isVector())
9354 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009355 // MMX <=> MMX conversions are Legal.
9356 if (SrcVT.isVector() && DstVT.isVector())
9357 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009358 // All other conversions need to be expanded.
9359 return SDValue();
9360}
Chris Lattner5b856542010-12-20 00:59:46 +00009361
Dan Gohmand858e902010-04-17 15:26:15 +00009362SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009363 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009364 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009365 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009366 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009367 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009368 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009369 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009370 Node->getOperand(0),
9371 Node->getOperand(1), negOp,
9372 cast<AtomicSDNode>(Node)->getSrcValue(),
9373 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009374}
9375
Chris Lattner5b856542010-12-20 00:59:46 +00009376static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9377 EVT VT = Op.getNode()->getValueType(0);
9378
9379 // Let legalize expand this if it isn't a legal type yet.
9380 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9381 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009382
Chris Lattner5b856542010-12-20 00:59:46 +00009383 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009384
Chris Lattner5b856542010-12-20 00:59:46 +00009385 unsigned Opc;
9386 bool ExtraOp = false;
9387 switch (Op.getOpcode()) {
9388 default: assert(0 && "Invalid code");
9389 case ISD::ADDC: Opc = X86ISD::ADD; break;
9390 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9391 case ISD::SUBC: Opc = X86ISD::SUB; break;
9392 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9393 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009394
Chris Lattner5b856542010-12-20 00:59:46 +00009395 if (!ExtraOp)
9396 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9397 Op.getOperand(1));
9398 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9399 Op.getOperand(1), Op.getOperand(2));
9400}
9401
Evan Cheng0db9fe62006-04-25 20:13:52 +00009402/// LowerOperation - Provide custom lowering hooks for some operations.
9403///
Dan Gohmand858e902010-04-17 15:26:15 +00009404SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009405 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009406 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009407 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +00009408 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009409 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9410 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009411 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009412 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009413 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9414 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9415 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009416 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009417 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009418 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9419 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9420 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009421 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009422 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009423 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009424 case ISD::SHL_PARTS:
9425 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009426 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009427 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009428 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009429 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009430 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009431 case ISD::FABS: return LowerFABS(Op, DAG);
9432 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009433 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009434 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009435 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009436 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009437 case ISD::SELECT: return LowerSELECT(Op, DAG);
9438 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009439 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009440 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009441 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009442 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009443 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009444 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9445 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009446 case ISD::FRAME_TO_ARGS_OFFSET:
9447 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009448 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009449 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009450 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009451 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009452 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9453 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009454 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009455 case ISD::SRA:
9456 case ISD::SRL:
9457 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009458 case ISD::SADDO:
9459 case ISD::UADDO:
9460 case ISD::SSUBO:
9461 case ISD::USUBO:
9462 case ISD::SMULO:
9463 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009464 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009465 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009466 case ISD::ADDC:
9467 case ISD::ADDE:
9468 case ISD::SUBC:
9469 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009470 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009471}
9472
Duncan Sands1607f052008-12-01 11:39:25 +00009473void X86TargetLowering::
9474ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009475 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009476 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009477 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009478 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009479
9480 SDValue Chain = Node->getOperand(0);
9481 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009482 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009483 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009484 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009485 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009486 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009487 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009488 SDValue Result =
9489 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9490 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009491 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009492 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009493 Results.push_back(Result.getValue(2));
9494}
9495
Duncan Sands126d9072008-07-04 11:47:58 +00009496/// ReplaceNodeResults - Replace a node with an illegal result type
9497/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009498void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9499 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009500 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009501 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009502 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009503 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009504 assert(false && "Do not know how to custom type legalize this operation!");
9505 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009506 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +00009507 case ISD::ADDC:
9508 case ISD::ADDE:
9509 case ISD::SUBC:
9510 case ISD::SUBE:
9511 // We don't want to expand or promote these.
9512 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009513 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009514 std::pair<SDValue,SDValue> Vals =
9515 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009516 SDValue FIST = Vals.first, StackSlot = Vals.second;
9517 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009518 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009519 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009520 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9521 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009522 }
9523 return;
9524 }
9525 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009526 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009527 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009528 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009529 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009530 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009531 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009532 eax.getValue(2));
9533 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9534 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009535 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009536 Results.push_back(edx.getValue(1));
9537 return;
9538 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009539 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009540 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009541 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009542 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009543 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9544 DAG.getConstant(0, MVT::i32));
9545 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9546 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009547 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9548 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009549 cpInL.getValue(1));
9550 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009551 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9552 DAG.getConstant(0, MVT::i32));
9553 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9554 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009555 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009556 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009557 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009558 swapInL.getValue(1));
9559 SDValue Ops[] = { swapInH.getValue(0),
9560 N->getOperand(1),
9561 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009562 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009563 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9564 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9565 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009566 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009567 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009568 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009569 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009570 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009571 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009572 Results.push_back(cpOutH.getValue(1));
9573 return;
9574 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009575 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009576 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9577 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009578 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009579 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9580 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009581 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009582 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9583 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009584 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009585 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9586 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009587 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009588 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9589 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009590 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009591 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9592 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009593 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009594 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9595 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009596 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009597}
9598
Evan Cheng72261582005-12-20 06:22:03 +00009599const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9600 switch (Opcode) {
9601 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009602 case X86ISD::BSF: return "X86ISD::BSF";
9603 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009604 case X86ISD::SHLD: return "X86ISD::SHLD";
9605 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009606 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009607 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009608 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009609 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009610 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009611 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009612 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9613 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9614 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009615 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009616 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009617 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009618 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009619 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009620 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009621 case X86ISD::COMI: return "X86ISD::COMI";
9622 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009623 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009624 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009625 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9626 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009627 case X86ISD::CMOV: return "X86ISD::CMOV";
9628 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009629 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009630 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9631 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009632 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009633 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009634 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009635 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009636 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009637 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9638 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009639 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009640 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +00009641 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +00009642 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9643 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9644 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009645 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009646 case X86ISD::FMAX: return "X86ISD::FMAX";
9647 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009648 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9649 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009650 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009651 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009652 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009653 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009654 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009655 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9656 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009657 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9658 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9659 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9660 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9661 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9662 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009663 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9664 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009665 case X86ISD::VSHL: return "X86ISD::VSHL";
9666 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009667 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9668 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9669 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9670 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9671 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9672 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9673 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9674 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9675 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9676 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009677 case X86ISD::ADD: return "X86ISD::ADD";
9678 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009679 case X86ISD::ADC: return "X86ISD::ADC";
9680 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009681 case X86ISD::SMUL: return "X86ISD::SMUL";
9682 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009683 case X86ISD::INC: return "X86ISD::INC";
9684 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009685 case X86ISD::OR: return "X86ISD::OR";
9686 case X86ISD::XOR: return "X86ISD::XOR";
9687 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009688 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009689 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009690 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009691 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9692 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9693 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9694 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9695 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9696 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9697 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9698 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9699 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009700 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009701 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009702 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009703 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9704 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009705 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9706 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9707 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9708 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9709 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9710 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9711 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9712 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9713 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009714 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9715 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9716 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9717 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009718 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9719 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9720 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9721 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9722 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9723 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9724 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9725 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9726 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9727 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00009728 case X86ISD::VPERMIL: return "X86ISD::VPERMIL";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009729 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009730 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009731 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009732 }
9733}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009734
Chris Lattnerc9addb72007-03-30 23:15:24 +00009735// isLegalAddressingMode - Return true if the addressing mode represented
9736// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009737bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009738 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00009739 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009740 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009741 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009742
Chris Lattnerc9addb72007-03-30 23:15:24 +00009743 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009744 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009745 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009746
Chris Lattnerc9addb72007-03-30 23:15:24 +00009747 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009748 unsigned GVFlags =
9749 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009750
Chris Lattnerdfed4132009-07-10 07:38:24 +00009751 // If a reference to this global requires an extra load, we can't fold it.
9752 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009753 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009754
Chris Lattnerdfed4132009-07-10 07:38:24 +00009755 // If BaseGV requires a register for the PIC base, we cannot also have a
9756 // BaseReg specified.
9757 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009758 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009759
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009760 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009761 if ((M != CodeModel::Small || R != Reloc::Static) &&
9762 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009763 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009764 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009765
Chris Lattnerc9addb72007-03-30 23:15:24 +00009766 switch (AM.Scale) {
9767 case 0:
9768 case 1:
9769 case 2:
9770 case 4:
9771 case 8:
9772 // These scales always work.
9773 break;
9774 case 3:
9775 case 5:
9776 case 9:
9777 // These scales are formed with basereg+scalereg. Only accept if there is
9778 // no basereg yet.
9779 if (AM.HasBaseReg)
9780 return false;
9781 break;
9782 default: // Other stuff never works.
9783 return false;
9784 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009785
Chris Lattnerc9addb72007-03-30 23:15:24 +00009786 return true;
9787}
9788
9789
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009790bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009791 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009792 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009793 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9794 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009795 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009796 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009797 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009798}
9799
Owen Andersone50ed302009-08-10 22:56:29 +00009800bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009801 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009802 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009803 unsigned NumBits1 = VT1.getSizeInBits();
9804 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009805 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009806 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009807 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009808}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009809
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009810bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009811 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009812 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009813}
9814
Owen Andersone50ed302009-08-10 22:56:29 +00009815bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009816 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009817 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009818}
9819
Owen Andersone50ed302009-08-10 22:56:29 +00009820bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009821 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009822 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009823}
9824
Evan Cheng60c07e12006-07-05 22:17:51 +00009825/// isShuffleMaskLegal - Targets can use this to indicate that they only
9826/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9827/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9828/// are assumed to be legal.
9829bool
Eric Christopherfd179292009-08-27 18:07:15 +00009830X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009831 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009832 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009833 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009834 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009835
Nate Begemana09008b2009-10-19 02:17:23 +00009836 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009837 return (VT.getVectorNumElements() == 2 ||
9838 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9839 isMOVLMask(M, VT) ||
9840 isSHUFPMask(M, VT) ||
9841 isPSHUFDMask(M, VT) ||
9842 isPSHUFHWMask(M, VT) ||
9843 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009844 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009845 isUNPCKLMask(M, VT) ||
9846 isUNPCKHMask(M, VT) ||
9847 isUNPCKL_v_undef_Mask(M, VT) ||
9848 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009849}
9850
Dan Gohman7d8143f2008-04-09 20:09:42 +00009851bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009852X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009853 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009854 unsigned NumElts = VT.getVectorNumElements();
9855 // FIXME: This collection of masks seems suspect.
9856 if (NumElts == 2)
9857 return true;
9858 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9859 return (isMOVLMask(Mask, VT) ||
9860 isCommutedMOVLMask(Mask, VT, true) ||
9861 isSHUFPMask(Mask, VT) ||
9862 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009863 }
9864 return false;
9865}
9866
9867//===----------------------------------------------------------------------===//
9868// X86 Scheduler Hooks
9869//===----------------------------------------------------------------------===//
9870
Mon P Wang63307c32008-05-05 19:05:59 +00009871// private utility function
9872MachineBasicBlock *
9873X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9874 MachineBasicBlock *MBB,
9875 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009876 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009877 unsigned LoadOpc,
9878 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009879 unsigned notOpc,
9880 unsigned EAXreg,
9881 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009882 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009883 // For the atomic bitwise operator, we generate
9884 // thisMBB:
9885 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009886 // ld t1 = [bitinstr.addr]
9887 // op t2 = t1, [bitinstr.val]
9888 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009889 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9890 // bz newMBB
9891 // fallthrough -->nextMBB
9892 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9893 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009894 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009895 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009896
Mon P Wang63307c32008-05-05 19:05:59 +00009897 /// First build the CFG
9898 MachineFunction *F = MBB->getParent();
9899 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009900 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9901 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9902 F->insert(MBBIter, newMBB);
9903 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009904
Dan Gohman14152b42010-07-06 20:24:04 +00009905 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9906 nextMBB->splice(nextMBB->begin(), thisMBB,
9907 llvm::next(MachineBasicBlock::iterator(bInstr)),
9908 thisMBB->end());
9909 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009910
Mon P Wang63307c32008-05-05 19:05:59 +00009911 // Update thisMBB to fall through to newMBB
9912 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009913
Mon P Wang63307c32008-05-05 19:05:59 +00009914 // newMBB jumps to itself and fall through to nextMBB
9915 newMBB->addSuccessor(nextMBB);
9916 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009917
Mon P Wang63307c32008-05-05 19:05:59 +00009918 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009919 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009920 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009921 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009922 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009923 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009924 int numArgs = bInstr->getNumOperands() - 1;
9925 for (int i=0; i < numArgs; ++i)
9926 argOpers[i] = &bInstr->getOperand(i+1);
9927
9928 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009929 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009930 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009931
Dale Johannesen140be2d2008-08-19 18:47:28 +00009932 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009933 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009934 for (int i=0; i <= lastAddrIndx; ++i)
9935 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009936
Dale Johannesen140be2d2008-08-19 18:47:28 +00009937 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009938 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009939 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009940 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009941 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009942 tt = t1;
9943
Dale Johannesen140be2d2008-08-19 18:47:28 +00009944 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009945 assert((argOpers[valArgIndx]->isReg() ||
9946 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009947 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009948 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009949 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009950 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009951 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009952 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009953 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009954
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009955 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009956 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009957
Dale Johannesene4d209d2009-02-03 20:21:25 +00009958 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009959 for (int i=0; i <= lastAddrIndx; ++i)
9960 (*MIB).addOperand(*argOpers[i]);
9961 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009962 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009963 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9964 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009965
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009966 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009967 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009968
Mon P Wang63307c32008-05-05 19:05:59 +00009969 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009970 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009971
Dan Gohman14152b42010-07-06 20:24:04 +00009972 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009973 return nextMBB;
9974}
9975
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009976// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009977MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009978X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9979 MachineBasicBlock *MBB,
9980 unsigned regOpcL,
9981 unsigned regOpcH,
9982 unsigned immOpcL,
9983 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009984 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009985 // For the atomic bitwise operator, we generate
9986 // thisMBB (instructions are in pairs, except cmpxchg8b)
9987 // ld t1,t2 = [bitinstr.addr]
9988 // newMBB:
9989 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9990 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009991 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009992 // mov ECX, EBX <- t5, t6
9993 // mov EAX, EDX <- t1, t2
9994 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9995 // mov t3, t4 <- EAX, EDX
9996 // bz newMBB
9997 // result in out1, out2
9998 // fallthrough -->nextMBB
9999
10000 const TargetRegisterClass *RC = X86::GR32RegisterClass;
10001 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010002 const unsigned NotOpc = X86::NOT32r;
10003 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10004 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10005 MachineFunction::iterator MBBIter = MBB;
10006 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010007
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010008 /// First build the CFG
10009 MachineFunction *F = MBB->getParent();
10010 MachineBasicBlock *thisMBB = MBB;
10011 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10012 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10013 F->insert(MBBIter, newMBB);
10014 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010015
Dan Gohman14152b42010-07-06 20:24:04 +000010016 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10017 nextMBB->splice(nextMBB->begin(), thisMBB,
10018 llvm::next(MachineBasicBlock::iterator(bInstr)),
10019 thisMBB->end());
10020 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010021
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010022 // Update thisMBB to fall through to newMBB
10023 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010024
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010025 // newMBB jumps to itself and fall through to nextMBB
10026 newMBB->addSuccessor(nextMBB);
10027 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010028
Dale Johannesene4d209d2009-02-03 20:21:25 +000010029 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010030 // Insert instructions into newMBB based on incoming instruction
10031 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010032 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010033 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010034 MachineOperand& dest1Oper = bInstr->getOperand(0);
10035 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010036 MachineOperand* argOpers[2 + X86::AddrNumOperands];
10037 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010038 argOpers[i] = &bInstr->getOperand(i+2);
10039
Dan Gohman71ea4e52010-05-14 21:01:44 +000010040 // We use some of the operands multiple times, so conservatively just
10041 // clear any kill flags that might be present.
10042 if (argOpers[i]->isReg() && argOpers[i]->isUse())
10043 argOpers[i]->setIsKill(false);
10044 }
10045
Evan Chengad5b52f2010-01-08 19:14:57 +000010046 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010047 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000010048
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010049 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010050 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010051 for (int i=0; i <= lastAddrIndx; ++i)
10052 (*MIB).addOperand(*argOpers[i]);
10053 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010054 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000010055 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000010056 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010057 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000010058 MachineOperand newOp3 = *(argOpers[3]);
10059 if (newOp3.isImm())
10060 newOp3.setImm(newOp3.getImm()+4);
10061 else
10062 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010063 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000010064 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010065
10066 // t3/4 are defined later, at the bottom of the loop
10067 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
10068 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010069 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010070 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010071 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010072 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
10073
Evan Cheng306b4ca2010-01-08 23:41:50 +000010074 // The subsequent operations should be using the destination registers of
10075 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000010076 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000010077 t1 = F->getRegInfo().createVirtualRegister(RC);
10078 t2 = F->getRegInfo().createVirtualRegister(RC);
10079 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
10080 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010081 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000010082 t1 = dest1Oper.getReg();
10083 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010084 }
10085
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010086 int valArgIndx = lastAddrIndx + 1;
10087 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000010088 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010089 "invalid operand");
10090 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
10091 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010092 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010093 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010094 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010095 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000010096 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000010097 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010098 (*MIB).addOperand(*argOpers[valArgIndx]);
10099 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000010100 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010101 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000010102 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010103 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010104 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010105 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010106 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000010107 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000010108 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010109 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010110
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010111 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010112 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010113 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010114 MIB.addReg(t2);
10115
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010116 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010117 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010118 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010119 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000010120
Dale Johannesene4d209d2009-02-03 20:21:25 +000010121 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010122 for (int i=0; i <= lastAddrIndx; ++i)
10123 (*MIB).addOperand(*argOpers[i]);
10124
10125 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010126 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10127 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010128
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010129 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010130 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010131 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010132 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010133
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010134 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010135 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010136
Dan Gohman14152b42010-07-06 20:24:04 +000010137 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010138 return nextMBB;
10139}
10140
10141// private utility function
10142MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000010143X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
10144 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010145 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010146 // For the atomic min/max operator, we generate
10147 // thisMBB:
10148 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010149 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000010150 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000010151 // cmp t1, t2
10152 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000010153 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010154 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10155 // bz newMBB
10156 // fallthrough -->nextMBB
10157 //
10158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10159 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010160 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010161 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010162
Mon P Wang63307c32008-05-05 19:05:59 +000010163 /// First build the CFG
10164 MachineFunction *F = MBB->getParent();
10165 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010166 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10167 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10168 F->insert(MBBIter, newMBB);
10169 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010170
Dan Gohman14152b42010-07-06 20:24:04 +000010171 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10172 nextMBB->splice(nextMBB->begin(), thisMBB,
10173 llvm::next(MachineBasicBlock::iterator(mInstr)),
10174 thisMBB->end());
10175 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010176
Mon P Wang63307c32008-05-05 19:05:59 +000010177 // Update thisMBB to fall through to newMBB
10178 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010179
Mon P Wang63307c32008-05-05 19:05:59 +000010180 // newMBB jumps to newMBB and fall through to nextMBB
10181 newMBB->addSuccessor(nextMBB);
10182 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010183
Dale Johannesene4d209d2009-02-03 20:21:25 +000010184 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010185 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010186 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010187 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010188 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010189 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010190 int numArgs = mInstr->getNumOperands() - 1;
10191 for (int i=0; i < numArgs; ++i)
10192 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010193
Mon P Wang63307c32008-05-05 19:05:59 +000010194 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010195 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010196 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010197
Mon P Wangab3e7472008-05-05 22:56:23 +000010198 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010199 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010200 for (int i=0; i <= lastAddrIndx; ++i)
10201 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010202
Mon P Wang63307c32008-05-05 19:05:59 +000010203 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010204 assert((argOpers[valArgIndx]->isReg() ||
10205 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010206 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010207
10208 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010209 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010210 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010211 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010212 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010213 (*MIB).addOperand(*argOpers[valArgIndx]);
10214
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010215 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010216 MIB.addReg(t1);
10217
Dale Johannesene4d209d2009-02-03 20:21:25 +000010218 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010219 MIB.addReg(t1);
10220 MIB.addReg(t2);
10221
10222 // Generate movc
10223 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010224 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010225 MIB.addReg(t2);
10226 MIB.addReg(t1);
10227
10228 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010229 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010230 for (int i=0; i <= lastAddrIndx; ++i)
10231 (*MIB).addOperand(*argOpers[i]);
10232 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010233 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010234 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10235 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010236
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010237 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010238 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010239
Mon P Wang63307c32008-05-05 19:05:59 +000010240 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010241 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010242
Dan Gohman14152b42010-07-06 20:24:04 +000010243 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010244 return nextMBB;
10245}
10246
Eric Christopherf83a5de2009-08-27 18:08:16 +000010247// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010248// or XMM0_V32I8 in AVX all of this code can be replaced with that
10249// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010250MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010251X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010252 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010253 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10254 "Target must have SSE4.2 or AVX features enabled");
10255
Eric Christopherb120ab42009-08-18 22:50:32 +000010256 DebugLoc dl = MI->getDebugLoc();
10257 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010258 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010259 if (!Subtarget->hasAVX()) {
10260 if (memArg)
10261 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10262 else
10263 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10264 } else {
10265 if (memArg)
10266 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10267 else
10268 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10269 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010270
Eric Christopher41c902f2010-11-30 08:20:21 +000010271 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010272 for (unsigned i = 0; i < numArgs; ++i) {
10273 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010274 if (!(Op.isReg() && Op.isImplicit()))
10275 MIB.addOperand(Op);
10276 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010277 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010278 .addReg(X86::XMM0);
10279
Dan Gohman14152b42010-07-06 20:24:04 +000010280 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010281 return BB;
10282}
10283
10284MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010285X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010286 DebugLoc dl = MI->getDebugLoc();
10287 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010288
Eric Christopher228232b2010-11-30 07:20:12 +000010289 // Address into RAX/EAX, other two args into ECX, EDX.
10290 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10291 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10292 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10293 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010294 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010295
Eric Christopher228232b2010-11-30 07:20:12 +000010296 unsigned ValOps = X86::AddrNumOperands;
10297 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10298 .addReg(MI->getOperand(ValOps).getReg());
10299 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10300 .addReg(MI->getOperand(ValOps+1).getReg());
10301
10302 // The instruction doesn't actually take any operands though.
10303 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010304
Eric Christopher228232b2010-11-30 07:20:12 +000010305 MI->eraseFromParent(); // The pseudo is gone now.
10306 return BB;
10307}
10308
10309MachineBasicBlock *
10310X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010311 DebugLoc dl = MI->getDebugLoc();
10312 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010313
Eric Christopher228232b2010-11-30 07:20:12 +000010314 // First arg in ECX, the second in EAX.
10315 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10316 .addReg(MI->getOperand(0).getReg());
10317 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10318 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010319
Eric Christopher228232b2010-11-30 07:20:12 +000010320 // The instruction doesn't actually take any operands though.
10321 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010322
Eric Christopher228232b2010-11-30 07:20:12 +000010323 MI->eraseFromParent(); // The pseudo is gone now.
10324 return BB;
10325}
10326
10327MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010328X86TargetLowering::EmitVAARG64WithCustomInserter(
10329 MachineInstr *MI,
10330 MachineBasicBlock *MBB) const {
10331 // Emit va_arg instruction on X86-64.
10332
10333 // Operands to this pseudo-instruction:
10334 // 0 ) Output : destination address (reg)
10335 // 1-5) Input : va_list address (addr, i64mem)
10336 // 6 ) ArgSize : Size (in bytes) of vararg type
10337 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10338 // 8 ) Align : Alignment of type
10339 // 9 ) EFLAGS (implicit-def)
10340
10341 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10342 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10343
10344 unsigned DestReg = MI->getOperand(0).getReg();
10345 MachineOperand &Base = MI->getOperand(1);
10346 MachineOperand &Scale = MI->getOperand(2);
10347 MachineOperand &Index = MI->getOperand(3);
10348 MachineOperand &Disp = MI->getOperand(4);
10349 MachineOperand &Segment = MI->getOperand(5);
10350 unsigned ArgSize = MI->getOperand(6).getImm();
10351 unsigned ArgMode = MI->getOperand(7).getImm();
10352 unsigned Align = MI->getOperand(8).getImm();
10353
10354 // Memory Reference
10355 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10356 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10357 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10358
10359 // Machine Information
10360 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10361 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10362 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10363 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10364 DebugLoc DL = MI->getDebugLoc();
10365
10366 // struct va_list {
10367 // i32 gp_offset
10368 // i32 fp_offset
10369 // i64 overflow_area (address)
10370 // i64 reg_save_area (address)
10371 // }
10372 // sizeof(va_list) = 24
10373 // alignment(va_list) = 8
10374
10375 unsigned TotalNumIntRegs = 6;
10376 unsigned TotalNumXMMRegs = 8;
10377 bool UseGPOffset = (ArgMode == 1);
10378 bool UseFPOffset = (ArgMode == 2);
10379 unsigned MaxOffset = TotalNumIntRegs * 8 +
10380 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10381
10382 /* Align ArgSize to a multiple of 8 */
10383 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10384 bool NeedsAlign = (Align > 8);
10385
10386 MachineBasicBlock *thisMBB = MBB;
10387 MachineBasicBlock *overflowMBB;
10388 MachineBasicBlock *offsetMBB;
10389 MachineBasicBlock *endMBB;
10390
10391 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10392 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10393 unsigned OffsetReg = 0;
10394
10395 if (!UseGPOffset && !UseFPOffset) {
10396 // If we only pull from the overflow region, we don't create a branch.
10397 // We don't need to alter control flow.
10398 OffsetDestReg = 0; // unused
10399 OverflowDestReg = DestReg;
10400
10401 offsetMBB = NULL;
10402 overflowMBB = thisMBB;
10403 endMBB = thisMBB;
10404 } else {
10405 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10406 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10407 // If not, pull from overflow_area. (branch to overflowMBB)
10408 //
10409 // thisMBB
10410 // | .
10411 // | .
10412 // offsetMBB overflowMBB
10413 // | .
10414 // | .
10415 // endMBB
10416
10417 // Registers for the PHI in endMBB
10418 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10419 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10420
10421 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10422 MachineFunction *MF = MBB->getParent();
10423 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10424 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10425 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10426
10427 MachineFunction::iterator MBBIter = MBB;
10428 ++MBBIter;
10429
10430 // Insert the new basic blocks
10431 MF->insert(MBBIter, offsetMBB);
10432 MF->insert(MBBIter, overflowMBB);
10433 MF->insert(MBBIter, endMBB);
10434
10435 // Transfer the remainder of MBB and its successor edges to endMBB.
10436 endMBB->splice(endMBB->begin(), thisMBB,
10437 llvm::next(MachineBasicBlock::iterator(MI)),
10438 thisMBB->end());
10439 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10440
10441 // Make offsetMBB and overflowMBB successors of thisMBB
10442 thisMBB->addSuccessor(offsetMBB);
10443 thisMBB->addSuccessor(overflowMBB);
10444
10445 // endMBB is a successor of both offsetMBB and overflowMBB
10446 offsetMBB->addSuccessor(endMBB);
10447 overflowMBB->addSuccessor(endMBB);
10448
10449 // Load the offset value into a register
10450 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10451 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10452 .addOperand(Base)
10453 .addOperand(Scale)
10454 .addOperand(Index)
10455 .addDisp(Disp, UseFPOffset ? 4 : 0)
10456 .addOperand(Segment)
10457 .setMemRefs(MMOBegin, MMOEnd);
10458
10459 // Check if there is enough room left to pull this argument.
10460 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10461 .addReg(OffsetReg)
10462 .addImm(MaxOffset + 8 - ArgSizeA8);
10463
10464 // Branch to "overflowMBB" if offset >= max
10465 // Fall through to "offsetMBB" otherwise
10466 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10467 .addMBB(overflowMBB);
10468 }
10469
10470 // In offsetMBB, emit code to use the reg_save_area.
10471 if (offsetMBB) {
10472 assert(OffsetReg != 0);
10473
10474 // Read the reg_save_area address.
10475 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10476 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10477 .addOperand(Base)
10478 .addOperand(Scale)
10479 .addOperand(Index)
10480 .addDisp(Disp, 16)
10481 .addOperand(Segment)
10482 .setMemRefs(MMOBegin, MMOEnd);
10483
10484 // Zero-extend the offset
10485 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10486 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10487 .addImm(0)
10488 .addReg(OffsetReg)
10489 .addImm(X86::sub_32bit);
10490
10491 // Add the offset to the reg_save_area to get the final address.
10492 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10493 .addReg(OffsetReg64)
10494 .addReg(RegSaveReg);
10495
10496 // Compute the offset for the next argument
10497 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10498 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10499 .addReg(OffsetReg)
10500 .addImm(UseFPOffset ? 16 : 8);
10501
10502 // Store it back into the va_list.
10503 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10504 .addOperand(Base)
10505 .addOperand(Scale)
10506 .addOperand(Index)
10507 .addDisp(Disp, UseFPOffset ? 4 : 0)
10508 .addOperand(Segment)
10509 .addReg(NextOffsetReg)
10510 .setMemRefs(MMOBegin, MMOEnd);
10511
10512 // Jump to endMBB
10513 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10514 .addMBB(endMBB);
10515 }
10516
10517 //
10518 // Emit code to use overflow area
10519 //
10520
10521 // Load the overflow_area address into a register.
10522 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10523 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10524 .addOperand(Base)
10525 .addOperand(Scale)
10526 .addOperand(Index)
10527 .addDisp(Disp, 8)
10528 .addOperand(Segment)
10529 .setMemRefs(MMOBegin, MMOEnd);
10530
10531 // If we need to align it, do so. Otherwise, just copy the address
10532 // to OverflowDestReg.
10533 if (NeedsAlign) {
10534 // Align the overflow address
10535 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10536 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10537
10538 // aligned_addr = (addr + (align-1)) & ~(align-1)
10539 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10540 .addReg(OverflowAddrReg)
10541 .addImm(Align-1);
10542
10543 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10544 .addReg(TmpReg)
10545 .addImm(~(uint64_t)(Align-1));
10546 } else {
10547 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10548 .addReg(OverflowAddrReg);
10549 }
10550
10551 // Compute the next overflow address after this argument.
10552 // (the overflow address should be kept 8-byte aligned)
10553 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10554 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10555 .addReg(OverflowDestReg)
10556 .addImm(ArgSizeA8);
10557
10558 // Store the new overflow address.
10559 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10560 .addOperand(Base)
10561 .addOperand(Scale)
10562 .addOperand(Index)
10563 .addDisp(Disp, 8)
10564 .addOperand(Segment)
10565 .addReg(NextAddrReg)
10566 .setMemRefs(MMOBegin, MMOEnd);
10567
10568 // If we branched, emit the PHI to the front of endMBB.
10569 if (offsetMBB) {
10570 BuildMI(*endMBB, endMBB->begin(), DL,
10571 TII->get(X86::PHI), DestReg)
10572 .addReg(OffsetDestReg).addMBB(offsetMBB)
10573 .addReg(OverflowDestReg).addMBB(overflowMBB);
10574 }
10575
10576 // Erase the pseudo instruction
10577 MI->eraseFromParent();
10578
10579 return endMBB;
10580}
10581
10582MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010583X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10584 MachineInstr *MI,
10585 MachineBasicBlock *MBB) const {
10586 // Emit code to save XMM registers to the stack. The ABI says that the
10587 // number of registers to save is given in %al, so it's theoretically
10588 // possible to do an indirect jump trick to avoid saving all of them,
10589 // however this code takes a simpler approach and just executes all
10590 // of the stores if %al is non-zero. It's less code, and it's probably
10591 // easier on the hardware branch predictor, and stores aren't all that
10592 // expensive anyway.
10593
10594 // Create the new basic blocks. One block contains all the XMM stores,
10595 // and one block is the final destination regardless of whether any
10596 // stores were performed.
10597 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10598 MachineFunction *F = MBB->getParent();
10599 MachineFunction::iterator MBBIter = MBB;
10600 ++MBBIter;
10601 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10602 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10603 F->insert(MBBIter, XMMSaveMBB);
10604 F->insert(MBBIter, EndMBB);
10605
Dan Gohman14152b42010-07-06 20:24:04 +000010606 // Transfer the remainder of MBB and its successor edges to EndMBB.
10607 EndMBB->splice(EndMBB->begin(), MBB,
10608 llvm::next(MachineBasicBlock::iterator(MI)),
10609 MBB->end());
10610 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10611
Dan Gohmand6708ea2009-08-15 01:38:56 +000010612 // The original block will now fall through to the XMM save block.
10613 MBB->addSuccessor(XMMSaveMBB);
10614 // The XMMSaveMBB will fall through to the end block.
10615 XMMSaveMBB->addSuccessor(EndMBB);
10616
10617 // Now add the instructions.
10618 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10619 DebugLoc DL = MI->getDebugLoc();
10620
10621 unsigned CountReg = MI->getOperand(0).getReg();
10622 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10623 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10624
10625 if (!Subtarget->isTargetWin64()) {
10626 // If %al is 0, branch around the XMM save block.
10627 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010628 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010629 MBB->addSuccessor(EndMBB);
10630 }
10631
10632 // In the XMM save block, save all the XMM argument registers.
10633 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10634 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010635 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010636 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010637 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010638 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010639 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010640 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10641 .addFrameIndex(RegSaveFrameIndex)
10642 .addImm(/*Scale=*/1)
10643 .addReg(/*IndexReg=*/0)
10644 .addImm(/*Disp=*/Offset)
10645 .addReg(/*Segment=*/0)
10646 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010647 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010648 }
10649
Dan Gohman14152b42010-07-06 20:24:04 +000010650 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010651
10652 return EndMBB;
10653}
Mon P Wang63307c32008-05-05 19:05:59 +000010654
Evan Cheng60c07e12006-07-05 22:17:51 +000010655MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010656X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010657 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010658 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10659 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010660
Chris Lattner52600972009-09-02 05:57:00 +000010661 // To "insert" a SELECT_CC instruction, we actually have to insert the
10662 // diamond control-flow pattern. The incoming instruction knows the
10663 // destination vreg to set, the condition code register to branch on, the
10664 // true/false values to select between, and a branch opcode to use.
10665 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10666 MachineFunction::iterator It = BB;
10667 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010668
Chris Lattner52600972009-09-02 05:57:00 +000010669 // thisMBB:
10670 // ...
10671 // TrueVal = ...
10672 // cmpTY ccX, r1, r2
10673 // bCC copy1MBB
10674 // fallthrough --> copy0MBB
10675 MachineBasicBlock *thisMBB = BB;
10676 MachineFunction *F = BB->getParent();
10677 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10678 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010679 F->insert(It, copy0MBB);
10680 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010681
Bill Wendling730c07e2010-06-25 20:48:10 +000010682 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10683 // live into the sink and copy blocks.
10684 const MachineFunction *MF = BB->getParent();
10685 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10686 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010687
Dan Gohman14152b42010-07-06 20:24:04 +000010688 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10689 const MachineOperand &MO = MI->getOperand(I);
10690 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010691 unsigned Reg = MO.getReg();
10692 if (Reg != X86::EFLAGS) continue;
10693 copy0MBB->addLiveIn(Reg);
10694 sinkMBB->addLiveIn(Reg);
10695 }
10696
Dan Gohman14152b42010-07-06 20:24:04 +000010697 // Transfer the remainder of BB and its successor edges to sinkMBB.
10698 sinkMBB->splice(sinkMBB->begin(), BB,
10699 llvm::next(MachineBasicBlock::iterator(MI)),
10700 BB->end());
10701 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10702
10703 // Add the true and fallthrough blocks as its successors.
10704 BB->addSuccessor(copy0MBB);
10705 BB->addSuccessor(sinkMBB);
10706
10707 // Create the conditional branch instruction.
10708 unsigned Opc =
10709 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10710 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10711
Chris Lattner52600972009-09-02 05:57:00 +000010712 // copy0MBB:
10713 // %FalseValue = ...
10714 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010715 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010716
Chris Lattner52600972009-09-02 05:57:00 +000010717 // sinkMBB:
10718 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10719 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010720 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10721 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010722 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10723 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10724
Dan Gohman14152b42010-07-06 20:24:04 +000010725 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010726 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010727}
10728
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010729MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010730X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010731 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010732 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10733 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010734
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010735 assert(!Subtarget->isTargetEnvMacho());
10736
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010737 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10738 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010739
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010740 if (Subtarget->isTargetWin64()) {
10741 if (Subtarget->isTargetCygMing()) {
10742 // ___chkstk(Mingw64):
10743 // Clobbers R10, R11, RAX and EFLAGS.
10744 // Updates RSP.
10745 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10746 .addExternalSymbol("___chkstk")
10747 .addReg(X86::RAX, RegState::Implicit)
10748 .addReg(X86::RSP, RegState::Implicit)
10749 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10750 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10751 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10752 } else {
10753 // __chkstk(MSVCRT): does not update stack pointer.
10754 // Clobbers R10, R11 and EFLAGS.
10755 // FIXME: RAX(allocated size) might be reused and not killed.
10756 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10757 .addExternalSymbol("__chkstk")
10758 .addReg(X86::RAX, RegState::Implicit)
10759 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10760 // RAX has the offset to subtracted from RSP.
10761 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10762 .addReg(X86::RSP)
10763 .addReg(X86::RAX);
10764 }
10765 } else {
10766 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010767 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10768
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010769 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10770 .addExternalSymbol(StackProbeSymbol)
10771 .addReg(X86::EAX, RegState::Implicit)
10772 .addReg(X86::ESP, RegState::Implicit)
10773 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10774 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10775 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10776 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010777
Dan Gohman14152b42010-07-06 20:24:04 +000010778 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010779 return BB;
10780}
Chris Lattner52600972009-09-02 05:57:00 +000010781
10782MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010783X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10784 MachineBasicBlock *BB) const {
10785 // This is pretty easy. We're taking the value that we received from
10786 // our load from the relocation, sticking it in either RDI (x86-64)
10787 // or EAX and doing an indirect call. The return value will then
10788 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010789 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010790 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010791 DebugLoc DL = MI->getDebugLoc();
10792 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010793
10794 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010795 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010796
Eric Christopher30ef0e52010-06-03 04:07:48 +000010797 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010798 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10799 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010800 .addReg(X86::RIP)
10801 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010802 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010803 MI->getOperand(3).getTargetFlags())
10804 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010805 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010806 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010807 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010808 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10809 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010810 .addReg(0)
10811 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010812 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010813 MI->getOperand(3).getTargetFlags())
10814 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010815 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010816 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010817 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010818 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10819 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010820 .addReg(TII->getGlobalBaseReg(F))
10821 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010822 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010823 MI->getOperand(3).getTargetFlags())
10824 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010825 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010826 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010827 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010828
Dan Gohman14152b42010-07-06 20:24:04 +000010829 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010830 return BB;
10831}
10832
10833MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010834X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010835 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010836 switch (MI->getOpcode()) {
10837 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010838 case X86::TAILJMPd64:
10839 case X86::TAILJMPr64:
10840 case X86::TAILJMPm64:
10841 assert(!"TAILJMP64 would not be touched here.");
10842 case X86::TCRETURNdi64:
10843 case X86::TCRETURNri64:
10844 case X86::TCRETURNmi64:
10845 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10846 // On AMD64, additional defs should be added before register allocation.
10847 if (!Subtarget->isTargetWin64()) {
10848 MI->addRegisterDefined(X86::RSI);
10849 MI->addRegisterDefined(X86::RDI);
10850 MI->addRegisterDefined(X86::XMM6);
10851 MI->addRegisterDefined(X86::XMM7);
10852 MI->addRegisterDefined(X86::XMM8);
10853 MI->addRegisterDefined(X86::XMM9);
10854 MI->addRegisterDefined(X86::XMM10);
10855 MI->addRegisterDefined(X86::XMM11);
10856 MI->addRegisterDefined(X86::XMM12);
10857 MI->addRegisterDefined(X86::XMM13);
10858 MI->addRegisterDefined(X86::XMM14);
10859 MI->addRegisterDefined(X86::XMM15);
10860 }
10861 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010862 case X86::WIN_ALLOCA:
10863 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010864 case X86::TLSCall_32:
10865 case X86::TLSCall_64:
10866 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010867 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010868 case X86::CMOV_FR32:
10869 case X86::CMOV_FR64:
10870 case X86::CMOV_V4F32:
10871 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010872 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010873 case X86::CMOV_GR16:
10874 case X86::CMOV_GR32:
10875 case X86::CMOV_RFP32:
10876 case X86::CMOV_RFP64:
10877 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010878 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010879
Dale Johannesen849f2142007-07-03 00:53:03 +000010880 case X86::FP32_TO_INT16_IN_MEM:
10881 case X86::FP32_TO_INT32_IN_MEM:
10882 case X86::FP32_TO_INT64_IN_MEM:
10883 case X86::FP64_TO_INT16_IN_MEM:
10884 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010885 case X86::FP64_TO_INT64_IN_MEM:
10886 case X86::FP80_TO_INT16_IN_MEM:
10887 case X86::FP80_TO_INT32_IN_MEM:
10888 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010889 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10890 DebugLoc DL = MI->getDebugLoc();
10891
Evan Cheng60c07e12006-07-05 22:17:51 +000010892 // Change the floating point control register to use "round towards zero"
10893 // mode when truncating to an integer value.
10894 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010895 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010896 addFrameReference(BuildMI(*BB, MI, DL,
10897 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010898
10899 // Load the old value of the high byte of the control word...
10900 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010901 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010902 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010903 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010904
10905 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010906 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010907 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010908
10909 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010910 addFrameReference(BuildMI(*BB, MI, DL,
10911 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010912
10913 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010914 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010915 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010916
10917 // Get the X86 opcode to use.
10918 unsigned Opc;
10919 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010920 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010921 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10922 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10923 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10924 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10925 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10926 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010927 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10928 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10929 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010930 }
10931
10932 X86AddressMode AM;
10933 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010934 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010935 AM.BaseType = X86AddressMode::RegBase;
10936 AM.Base.Reg = Op.getReg();
10937 } else {
10938 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010939 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010940 }
10941 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010942 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010943 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010944 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010945 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010946 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010947 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010948 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010949 AM.GV = Op.getGlobal();
10950 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010951 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010952 }
Dan Gohman14152b42010-07-06 20:24:04 +000010953 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010954 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010955
10956 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010957 addFrameReference(BuildMI(*BB, MI, DL,
10958 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010959
Dan Gohman14152b42010-07-06 20:24:04 +000010960 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010961 return BB;
10962 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010963 // String/text processing lowering.
10964 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010965 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010966 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10967 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010968 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010969 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10970 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010971 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010972 return EmitPCMP(MI, BB, 5, false /* in mem */);
10973 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010974 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010975 return EmitPCMP(MI, BB, 5, true /* in mem */);
10976
Eric Christopher228232b2010-11-30 07:20:12 +000010977 // Thread synchronization.
10978 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010979 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010980 case X86::MWAIT:
10981 return EmitMwait(MI, BB);
10982
Eric Christopherb120ab42009-08-18 22:50:32 +000010983 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010984 case X86::ATOMAND32:
10985 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010986 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010987 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010988 X86::NOT32r, X86::EAX,
10989 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010990 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010991 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10992 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010993 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010994 X86::NOT32r, X86::EAX,
10995 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010996 case X86::ATOMXOR32:
10997 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010998 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010999 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011000 X86::NOT32r, X86::EAX,
11001 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011002 case X86::ATOMNAND32:
11003 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011004 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011005 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011006 X86::NOT32r, X86::EAX,
11007 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000011008 case X86::ATOMMIN32:
11009 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
11010 case X86::ATOMMAX32:
11011 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
11012 case X86::ATOMUMIN32:
11013 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
11014 case X86::ATOMUMAX32:
11015 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000011016
11017 case X86::ATOMAND16:
11018 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
11019 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011020 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011021 X86::NOT16r, X86::AX,
11022 X86::GR16RegisterClass);
11023 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000011024 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011025 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011026 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011027 X86::NOT16r, X86::AX,
11028 X86::GR16RegisterClass);
11029 case X86::ATOMXOR16:
11030 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
11031 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011032 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011033 X86::NOT16r, X86::AX,
11034 X86::GR16RegisterClass);
11035 case X86::ATOMNAND16:
11036 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
11037 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011038 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011039 X86::NOT16r, X86::AX,
11040 X86::GR16RegisterClass, true);
11041 case X86::ATOMMIN16:
11042 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
11043 case X86::ATOMMAX16:
11044 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
11045 case X86::ATOMUMIN16:
11046 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
11047 case X86::ATOMUMAX16:
11048 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
11049
11050 case X86::ATOMAND8:
11051 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
11052 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011053 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011054 X86::NOT8r, X86::AL,
11055 X86::GR8RegisterClass);
11056 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000011057 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011058 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011059 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011060 X86::NOT8r, X86::AL,
11061 X86::GR8RegisterClass);
11062 case X86::ATOMXOR8:
11063 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
11064 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011065 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011066 X86::NOT8r, X86::AL,
11067 X86::GR8RegisterClass);
11068 case X86::ATOMNAND8:
11069 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
11070 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011071 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011072 X86::NOT8r, X86::AL,
11073 X86::GR8RegisterClass, true);
11074 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011075 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000011076 case X86::ATOMAND64:
11077 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011078 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011079 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011080 X86::NOT64r, X86::RAX,
11081 X86::GR64RegisterClass);
11082 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000011083 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
11084 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011085 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011086 X86::NOT64r, X86::RAX,
11087 X86::GR64RegisterClass);
11088 case X86::ATOMXOR64:
11089 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011090 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011091 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011092 X86::NOT64r, X86::RAX,
11093 X86::GR64RegisterClass);
11094 case X86::ATOMNAND64:
11095 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
11096 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011097 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011098 X86::NOT64r, X86::RAX,
11099 X86::GR64RegisterClass, true);
11100 case X86::ATOMMIN64:
11101 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
11102 case X86::ATOMMAX64:
11103 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
11104 case X86::ATOMUMIN64:
11105 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
11106 case X86::ATOMUMAX64:
11107 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011108
11109 // This group does 64-bit operations on a 32-bit host.
11110 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011111 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011112 X86::AND32rr, X86::AND32rr,
11113 X86::AND32ri, X86::AND32ri,
11114 false);
11115 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011116 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011117 X86::OR32rr, X86::OR32rr,
11118 X86::OR32ri, X86::OR32ri,
11119 false);
11120 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011121 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011122 X86::XOR32rr, X86::XOR32rr,
11123 X86::XOR32ri, X86::XOR32ri,
11124 false);
11125 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011126 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011127 X86::AND32rr, X86::AND32rr,
11128 X86::AND32ri, X86::AND32ri,
11129 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011130 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011131 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011132 X86::ADD32rr, X86::ADC32rr,
11133 X86::ADD32ri, X86::ADC32ri,
11134 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011135 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011136 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011137 X86::SUB32rr, X86::SBB32rr,
11138 X86::SUB32ri, X86::SBB32ri,
11139 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000011140 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011141 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000011142 X86::MOV32rr, X86::MOV32rr,
11143 X86::MOV32ri, X86::MOV32ri,
11144 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011145 case X86::VASTART_SAVE_XMM_REGS:
11146 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000011147
11148 case X86::VAARG_64:
11149 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011150 }
11151}
11152
11153//===----------------------------------------------------------------------===//
11154// X86 Optimization Hooks
11155//===----------------------------------------------------------------------===//
11156
Dan Gohman475871a2008-07-27 21:46:04 +000011157void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000011158 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011159 APInt &KnownZero,
11160 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000011161 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000011162 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011163 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000011164 assert((Opc >= ISD::BUILTIN_OP_END ||
11165 Opc == ISD::INTRINSIC_WO_CHAIN ||
11166 Opc == ISD::INTRINSIC_W_CHAIN ||
11167 Opc == ISD::INTRINSIC_VOID) &&
11168 "Should use MaskedValueIsZero if you don't know whether Op"
11169 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011170
Dan Gohmanf4f92f52008-02-13 23:07:24 +000011171 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011172 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000011173 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011174 case X86ISD::ADD:
11175 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000011176 case X86ISD::ADC:
11177 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011178 case X86ISD::SMUL:
11179 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011180 case X86ISD::INC:
11181 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011182 case X86ISD::OR:
11183 case X86ISD::XOR:
11184 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011185 // These nodes' second result is a boolean.
11186 if (Op.getResNo() == 0)
11187 break;
11188 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011189 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011190 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11191 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011192 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011193 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011194}
Chris Lattner259e97c2006-01-31 19:43:35 +000011195
Owen Andersonbc146b02010-09-21 20:42:50 +000011196unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11197 unsigned Depth) const {
11198 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11199 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11200 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011201
Owen Andersonbc146b02010-09-21 20:42:50 +000011202 // Fallback case.
11203 return 1;
11204}
11205
Evan Cheng206ee9d2006-07-07 08:33:52 +000011206/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011207/// node is a GlobalAddress + offset.
11208bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011209 const GlobalValue* &GA,
11210 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011211 if (N->getOpcode() == X86ISD::Wrapper) {
11212 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011213 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011214 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011215 return true;
11216 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011217 }
Evan Chengad4196b2008-05-12 19:56:52 +000011218 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011219}
11220
Evan Cheng206ee9d2006-07-07 08:33:52 +000011221/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11222/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11223/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011224/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011225static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011226 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011227 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011228 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011229
Eli Friedman7a5e5552009-06-07 06:52:44 +000011230 if (VT.getSizeInBits() != 128)
11231 return SDValue();
11232
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011233 // Don't create instructions with illegal types after legalize types has run.
11234 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11235 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11236 return SDValue();
11237
Nate Begemanfdea31a2010-03-24 20:49:50 +000011238 SmallVector<SDValue, 16> Elts;
11239 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011240 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011241
Nate Begemanfdea31a2010-03-24 20:49:50 +000011242 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011243}
Evan Chengd880b972008-05-09 21:53:03 +000011244
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011245/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11246/// generation and convert it from being a bunch of shuffles and extracts
11247/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011248static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11249 const TargetLowering &TLI) {
11250 SDValue InputVector = N->getOperand(0);
11251
11252 // Only operate on vectors of 4 elements, where the alternative shuffling
11253 // gets to be more expensive.
11254 if (InputVector.getValueType() != MVT::v4i32)
11255 return SDValue();
11256
11257 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11258 // single use which is a sign-extend or zero-extend, and all elements are
11259 // used.
11260 SmallVector<SDNode *, 4> Uses;
11261 unsigned ExtractedElements = 0;
11262 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11263 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11264 if (UI.getUse().getResNo() != InputVector.getResNo())
11265 return SDValue();
11266
11267 SDNode *Extract = *UI;
11268 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11269 return SDValue();
11270
11271 if (Extract->getValueType(0) != MVT::i32)
11272 return SDValue();
11273 if (!Extract->hasOneUse())
11274 return SDValue();
11275 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11276 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11277 return SDValue();
11278 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11279 return SDValue();
11280
11281 // Record which element was extracted.
11282 ExtractedElements |=
11283 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11284
11285 Uses.push_back(Extract);
11286 }
11287
11288 // If not all the elements were used, this may not be worthwhile.
11289 if (ExtractedElements != 15)
11290 return SDValue();
11291
11292 // Ok, we've now decided to do the transformation.
11293 DebugLoc dl = InputVector.getDebugLoc();
11294
11295 // Store the value to a temporary stack slot.
11296 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011297 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11298 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011299
11300 // Replace each use (extract) with a load of the appropriate element.
11301 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11302 UE = Uses.end(); UI != UE; ++UI) {
11303 SDNode *Extract = *UI;
11304
Nadav Rotem86694292011-05-17 08:31:57 +000011305 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011306 SDValue Idx = Extract->getOperand(1);
11307 unsigned EltSize =
11308 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11309 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11310 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11311
Nadav Rotem86694292011-05-17 08:31:57 +000011312 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011313 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011314
11315 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011316 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011317 ScalarAddr, MachinePointerInfo(),
11318 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011319
11320 // Replace the exact with the load.
11321 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11322 }
11323
11324 // The replacement was made in place; don't return anything.
11325 return SDValue();
11326}
11327
Chris Lattner83e6c992006-10-04 06:57:07 +000011328/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011329static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011330 const X86Subtarget *Subtarget) {
11331 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011332 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011333 // Get the LHS/RHS of the select.
11334 SDValue LHS = N->getOperand(1);
11335 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011336
Dan Gohman670e5392009-09-21 18:03:22 +000011337 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011338 // instructions match the semantics of the common C idiom x<y?x:y but not
11339 // x<=y?x:y, because of how they handle negative zero (which can be
11340 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011341 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011342 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011343 Cond.getOpcode() == ISD::SETCC) {
11344 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011345
Chris Lattner47b4ce82009-03-11 05:48:52 +000011346 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011347 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011348 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11349 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011350 switch (CC) {
11351 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011352 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011353 // Converting this to a min would handle NaNs incorrectly, and swapping
11354 // the operands would cause it to handle comparisons between positive
11355 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011356 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011357 if (!UnsafeFPMath &&
11358 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11359 break;
11360 std::swap(LHS, RHS);
11361 }
Dan Gohman670e5392009-09-21 18:03:22 +000011362 Opcode = X86ISD::FMIN;
11363 break;
11364 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011365 // Converting this to a min would handle comparisons between positive
11366 // and negative zero incorrectly.
11367 if (!UnsafeFPMath &&
11368 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11369 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011370 Opcode = X86ISD::FMIN;
11371 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011372 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011373 // Converting this to a min would handle both negative zeros and NaNs
11374 // incorrectly, but we can swap the operands to fix both.
11375 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011376 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011377 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011378 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011379 Opcode = X86ISD::FMIN;
11380 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011381
Dan Gohman670e5392009-09-21 18:03:22 +000011382 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011383 // Converting this to a max would handle comparisons between positive
11384 // and negative zero incorrectly.
11385 if (!UnsafeFPMath &&
11386 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11387 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011388 Opcode = X86ISD::FMAX;
11389 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011390 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011391 // Converting this to a max would handle NaNs incorrectly, and swapping
11392 // the operands would cause it to handle comparisons between positive
11393 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011394 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011395 if (!UnsafeFPMath &&
11396 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11397 break;
11398 std::swap(LHS, RHS);
11399 }
Dan Gohman670e5392009-09-21 18:03:22 +000011400 Opcode = X86ISD::FMAX;
11401 break;
11402 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011403 // Converting this to a max would handle both negative zeros and NaNs
11404 // incorrectly, but we can swap the operands to fix both.
11405 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011406 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011407 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011408 case ISD::SETGE:
11409 Opcode = X86ISD::FMAX;
11410 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011411 }
Dan Gohman670e5392009-09-21 18:03:22 +000011412 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011413 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11414 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011415 switch (CC) {
11416 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011417 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011418 // Converting this to a min would handle comparisons between positive
11419 // and negative zero incorrectly, and swapping the operands would
11420 // cause it to handle NaNs incorrectly.
11421 if (!UnsafeFPMath &&
11422 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011423 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011424 break;
11425 std::swap(LHS, RHS);
11426 }
Dan Gohman670e5392009-09-21 18:03:22 +000011427 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011428 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011429 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011430 // Converting this to a min would handle NaNs incorrectly.
11431 if (!UnsafeFPMath &&
11432 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11433 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011434 Opcode = X86ISD::FMIN;
11435 break;
11436 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011437 // Converting this to a min would handle both negative zeros and NaNs
11438 // incorrectly, but we can swap the operands to fix both.
11439 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011440 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011441 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011442 case ISD::SETGE:
11443 Opcode = X86ISD::FMIN;
11444 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011445
Dan Gohman670e5392009-09-21 18:03:22 +000011446 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011447 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011448 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011449 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011450 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011451 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011452 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011453 // Converting this to a max would handle comparisons between positive
11454 // and negative zero incorrectly, and swapping the operands would
11455 // cause it to handle NaNs incorrectly.
11456 if (!UnsafeFPMath &&
11457 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011458 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011459 break;
11460 std::swap(LHS, RHS);
11461 }
Dan Gohman670e5392009-09-21 18:03:22 +000011462 Opcode = X86ISD::FMAX;
11463 break;
11464 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011465 // Converting this to a max would handle both negative zeros and NaNs
11466 // incorrectly, but we can swap the operands to fix both.
11467 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011468 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011469 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011470 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011471 Opcode = X86ISD::FMAX;
11472 break;
11473 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011474 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011475
Chris Lattner47b4ce82009-03-11 05:48:52 +000011476 if (Opcode)
11477 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011478 }
Eric Christopherfd179292009-08-27 18:07:15 +000011479
Chris Lattnerd1980a52009-03-12 06:52:53 +000011480 // If this is a select between two integer constants, try to do some
11481 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011482 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11483 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011484 // Don't do this for crazy integer types.
11485 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11486 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011487 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011488 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011489
Chris Lattnercee56e72009-03-13 05:53:31 +000011490 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011491 // Efficiently invertible.
11492 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11493 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11494 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11495 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011496 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011497 }
Eric Christopherfd179292009-08-27 18:07:15 +000011498
Chris Lattnerd1980a52009-03-12 06:52:53 +000011499 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011500 if (FalseC->getAPIntValue() == 0 &&
11501 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011502 if (NeedsCondInvert) // Invert the condition if needed.
11503 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11504 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011505
Chris Lattnerd1980a52009-03-12 06:52:53 +000011506 // Zero extend the condition if needed.
11507 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011508
Chris Lattnercee56e72009-03-13 05:53:31 +000011509 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011510 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011511 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011512 }
Eric Christopherfd179292009-08-27 18:07:15 +000011513
Chris Lattner97a29a52009-03-13 05:22:11 +000011514 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011515 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011516 if (NeedsCondInvert) // Invert the condition if needed.
11517 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11518 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011519
Chris Lattner97a29a52009-03-13 05:22:11 +000011520 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011521 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11522 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011523 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011524 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011525 }
Eric Christopherfd179292009-08-27 18:07:15 +000011526
Chris Lattnercee56e72009-03-13 05:53:31 +000011527 // Optimize cases that will turn into an LEA instruction. This requires
11528 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011529 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011530 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011531 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011532
Chris Lattnercee56e72009-03-13 05:53:31 +000011533 bool isFastMultiplier = false;
11534 if (Diff < 10) {
11535 switch ((unsigned char)Diff) {
11536 default: break;
11537 case 1: // result = add base, cond
11538 case 2: // result = lea base( , cond*2)
11539 case 3: // result = lea base(cond, cond*2)
11540 case 4: // result = lea base( , cond*4)
11541 case 5: // result = lea base(cond, cond*4)
11542 case 8: // result = lea base( , cond*8)
11543 case 9: // result = lea base(cond, cond*8)
11544 isFastMultiplier = true;
11545 break;
11546 }
11547 }
Eric Christopherfd179292009-08-27 18:07:15 +000011548
Chris Lattnercee56e72009-03-13 05:53:31 +000011549 if (isFastMultiplier) {
11550 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11551 if (NeedsCondInvert) // Invert the condition if needed.
11552 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11553 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011554
Chris Lattnercee56e72009-03-13 05:53:31 +000011555 // Zero extend the condition if needed.
11556 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11557 Cond);
11558 // Scale the condition by the difference.
11559 if (Diff != 1)
11560 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11561 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011562
Chris Lattnercee56e72009-03-13 05:53:31 +000011563 // Add the base if non-zero.
11564 if (FalseC->getAPIntValue() != 0)
11565 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11566 SDValue(FalseC, 0));
11567 return Cond;
11568 }
Eric Christopherfd179292009-08-27 18:07:15 +000011569 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011570 }
11571 }
Eric Christopherfd179292009-08-27 18:07:15 +000011572
Dan Gohman475871a2008-07-27 21:46:04 +000011573 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011574}
11575
Chris Lattnerd1980a52009-03-12 06:52:53 +000011576/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11577static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11578 TargetLowering::DAGCombinerInfo &DCI) {
11579 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011580
Chris Lattnerd1980a52009-03-12 06:52:53 +000011581 // If the flag operand isn't dead, don't touch this CMOV.
11582 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11583 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011584
Evan Chengb5a55d92011-05-24 01:48:22 +000011585 SDValue FalseOp = N->getOperand(0);
11586 SDValue TrueOp = N->getOperand(1);
11587 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11588 SDValue Cond = N->getOperand(3);
11589 if (CC == X86::COND_E || CC == X86::COND_NE) {
11590 switch (Cond.getOpcode()) {
11591 default: break;
11592 case X86ISD::BSR:
11593 case X86ISD::BSF:
11594 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11595 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11596 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11597 }
11598 }
11599
Chris Lattnerd1980a52009-03-12 06:52:53 +000011600 // If this is a select between two integer constants, try to do some
11601 // optimizations. Note that the operands are ordered the opposite of SELECT
11602 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011603 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11604 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011605 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11606 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011607 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11608 CC = X86::GetOppositeBranchCondition(CC);
11609 std::swap(TrueC, FalseC);
11610 }
Eric Christopherfd179292009-08-27 18:07:15 +000011611
Chris Lattnerd1980a52009-03-12 06:52:53 +000011612 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011613 // This is efficient for any integer data type (including i8/i16) and
11614 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011615 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011616 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11617 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011618
Chris Lattnerd1980a52009-03-12 06:52:53 +000011619 // Zero extend the condition if needed.
11620 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011621
Chris Lattnerd1980a52009-03-12 06:52:53 +000011622 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11623 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011624 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011625 if (N->getNumValues() == 2) // Dead flag value?
11626 return DCI.CombineTo(N, Cond, SDValue());
11627 return Cond;
11628 }
Eric Christopherfd179292009-08-27 18:07:15 +000011629
Chris Lattnercee56e72009-03-13 05:53:31 +000011630 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11631 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011632 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011633 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11634 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011635
Chris Lattner97a29a52009-03-13 05:22:11 +000011636 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011637 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11638 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011639 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11640 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011641
Chris Lattner97a29a52009-03-13 05:22:11 +000011642 if (N->getNumValues() == 2) // Dead flag value?
11643 return DCI.CombineTo(N, Cond, SDValue());
11644 return Cond;
11645 }
Eric Christopherfd179292009-08-27 18:07:15 +000011646
Chris Lattnercee56e72009-03-13 05:53:31 +000011647 // Optimize cases that will turn into an LEA instruction. This requires
11648 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011649 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011650 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011651 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011652
Chris Lattnercee56e72009-03-13 05:53:31 +000011653 bool isFastMultiplier = false;
11654 if (Diff < 10) {
11655 switch ((unsigned char)Diff) {
11656 default: break;
11657 case 1: // result = add base, cond
11658 case 2: // result = lea base( , cond*2)
11659 case 3: // result = lea base(cond, cond*2)
11660 case 4: // result = lea base( , cond*4)
11661 case 5: // result = lea base(cond, cond*4)
11662 case 8: // result = lea base( , cond*8)
11663 case 9: // result = lea base(cond, cond*8)
11664 isFastMultiplier = true;
11665 break;
11666 }
11667 }
Eric Christopherfd179292009-08-27 18:07:15 +000011668
Chris Lattnercee56e72009-03-13 05:53:31 +000011669 if (isFastMultiplier) {
11670 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011671 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11672 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011673 // Zero extend the condition if needed.
11674 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11675 Cond);
11676 // Scale the condition by the difference.
11677 if (Diff != 1)
11678 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11679 DAG.getConstant(Diff, Cond.getValueType()));
11680
11681 // Add the base if non-zero.
11682 if (FalseC->getAPIntValue() != 0)
11683 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11684 SDValue(FalseC, 0));
11685 if (N->getNumValues() == 2) // Dead flag value?
11686 return DCI.CombineTo(N, Cond, SDValue());
11687 return Cond;
11688 }
Eric Christopherfd179292009-08-27 18:07:15 +000011689 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011690 }
11691 }
11692 return SDValue();
11693}
11694
11695
Evan Cheng0b0cd912009-03-28 05:57:29 +000011696/// PerformMulCombine - Optimize a single multiply with constant into two
11697/// in order to implement it with two cheaper instructions, e.g.
11698/// LEA + SHL, LEA + LEA.
11699static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11700 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011701 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11702 return SDValue();
11703
Owen Andersone50ed302009-08-10 22:56:29 +000011704 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011705 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011706 return SDValue();
11707
11708 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11709 if (!C)
11710 return SDValue();
11711 uint64_t MulAmt = C->getZExtValue();
11712 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11713 return SDValue();
11714
11715 uint64_t MulAmt1 = 0;
11716 uint64_t MulAmt2 = 0;
11717 if ((MulAmt % 9) == 0) {
11718 MulAmt1 = 9;
11719 MulAmt2 = MulAmt / 9;
11720 } else if ((MulAmt % 5) == 0) {
11721 MulAmt1 = 5;
11722 MulAmt2 = MulAmt / 5;
11723 } else if ((MulAmt % 3) == 0) {
11724 MulAmt1 = 3;
11725 MulAmt2 = MulAmt / 3;
11726 }
11727 if (MulAmt2 &&
11728 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11729 DebugLoc DL = N->getDebugLoc();
11730
11731 if (isPowerOf2_64(MulAmt2) &&
11732 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11733 // If second multiplifer is pow2, issue it first. We want the multiply by
11734 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11735 // is an add.
11736 std::swap(MulAmt1, MulAmt2);
11737
11738 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011739 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011740 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011741 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011742 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011743 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011744 DAG.getConstant(MulAmt1, VT));
11745
Eric Christopherfd179292009-08-27 18:07:15 +000011746 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011747 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011748 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011749 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011750 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011751 DAG.getConstant(MulAmt2, VT));
11752
11753 // Do not add new nodes to DAG combiner worklist.
11754 DCI.CombineTo(N, NewMul, false);
11755 }
11756 return SDValue();
11757}
11758
Evan Chengad9c0a32009-12-15 00:53:42 +000011759static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11760 SDValue N0 = N->getOperand(0);
11761 SDValue N1 = N->getOperand(1);
11762 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11763 EVT VT = N0.getValueType();
11764
11765 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11766 // since the result of setcc_c is all zero's or all ones.
11767 if (N1C && N0.getOpcode() == ISD::AND &&
11768 N0.getOperand(1).getOpcode() == ISD::Constant) {
11769 SDValue N00 = N0.getOperand(0);
11770 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11771 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11772 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11773 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11774 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11775 APInt ShAmt = N1C->getAPIntValue();
11776 Mask = Mask.shl(ShAmt);
11777 if (Mask != 0)
11778 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11779 N00, DAG.getConstant(Mask, VT));
11780 }
11781 }
11782
11783 return SDValue();
11784}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011785
Nate Begeman740ab032009-01-26 00:52:55 +000011786/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11787/// when possible.
11788static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11789 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011790 EVT VT = N->getValueType(0);
11791 if (!VT.isVector() && VT.isInteger() &&
11792 N->getOpcode() == ISD::SHL)
11793 return PerformSHLCombine(N, DAG);
11794
Nate Begeman740ab032009-01-26 00:52:55 +000011795 // On X86 with SSE2 support, we can transform this to a vector shift if
11796 // all elements are shifted by the same amount. We can't do this in legalize
11797 // because the a constant vector is typically transformed to a constant pool
11798 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011799 if (!Subtarget->hasSSE2())
11800 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011801
Owen Anderson825b72b2009-08-11 20:47:22 +000011802 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011803 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011804
Mon P Wang3becd092009-01-28 08:12:05 +000011805 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011806 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011807 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011808 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011809 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11810 unsigned NumElts = VT.getVectorNumElements();
11811 unsigned i = 0;
11812 for (; i != NumElts; ++i) {
11813 SDValue Arg = ShAmtOp.getOperand(i);
11814 if (Arg.getOpcode() == ISD::UNDEF) continue;
11815 BaseShAmt = Arg;
11816 break;
11817 }
11818 for (; i != NumElts; ++i) {
11819 SDValue Arg = ShAmtOp.getOperand(i);
11820 if (Arg.getOpcode() == ISD::UNDEF) continue;
11821 if (Arg != BaseShAmt) {
11822 return SDValue();
11823 }
11824 }
11825 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011826 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011827 SDValue InVec = ShAmtOp.getOperand(0);
11828 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11829 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11830 unsigned i = 0;
11831 for (; i != NumElts; ++i) {
11832 SDValue Arg = InVec.getOperand(i);
11833 if (Arg.getOpcode() == ISD::UNDEF) continue;
11834 BaseShAmt = Arg;
11835 break;
11836 }
11837 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11838 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011839 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011840 if (C->getZExtValue() == SplatIdx)
11841 BaseShAmt = InVec.getOperand(1);
11842 }
11843 }
11844 if (BaseShAmt.getNode() == 0)
11845 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11846 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011847 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011848 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011849
Mon P Wangefa42202009-09-03 19:56:25 +000011850 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011851 if (EltVT.bitsGT(MVT::i32))
11852 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11853 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011854 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011855
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011856 // The shift amount is identical so we can do a vector shift.
11857 SDValue ValOp = N->getOperand(0);
11858 switch (N->getOpcode()) {
11859 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011860 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011861 break;
11862 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011863 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011864 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011865 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011866 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011867 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011868 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011869 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011870 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011871 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011872 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011873 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011874 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011875 break;
11876 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011877 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011878 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011879 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011880 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011881 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011882 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011883 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011884 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011885 break;
11886 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011887 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011888 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011889 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011890 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011891 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011892 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011893 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011894 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011895 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011896 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011897 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011898 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011899 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011900 }
11901 return SDValue();
11902}
11903
Nate Begemanb65c1752010-12-17 22:55:37 +000011904
Stuart Hastings865f0932011-06-03 23:53:54 +000011905// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11906// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11907// and friends. Likewise for OR -> CMPNEQSS.
11908static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11909 TargetLowering::DAGCombinerInfo &DCI,
11910 const X86Subtarget *Subtarget) {
11911 unsigned opcode;
11912
11913 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11914 // we're requiring SSE2 for both.
11915 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11916 SDValue N0 = N->getOperand(0);
11917 SDValue N1 = N->getOperand(1);
11918 SDValue CMP0 = N0->getOperand(1);
11919 SDValue CMP1 = N1->getOperand(1);
11920 DebugLoc DL = N->getDebugLoc();
11921
11922 // The SETCCs should both refer to the same CMP.
11923 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11924 return SDValue();
11925
11926 SDValue CMP00 = CMP0->getOperand(0);
11927 SDValue CMP01 = CMP0->getOperand(1);
11928 EVT VT = CMP00.getValueType();
11929
11930 if (VT == MVT::f32 || VT == MVT::f64) {
11931 bool ExpectingFlags = false;
11932 // Check for any users that want flags:
11933 for (SDNode::use_iterator UI = N->use_begin(),
11934 UE = N->use_end();
11935 !ExpectingFlags && UI != UE; ++UI)
11936 switch (UI->getOpcode()) {
11937 default:
11938 case ISD::BR_CC:
11939 case ISD::BRCOND:
11940 case ISD::SELECT:
11941 ExpectingFlags = true;
11942 break;
11943 case ISD::CopyToReg:
11944 case ISD::SIGN_EXTEND:
11945 case ISD::ZERO_EXTEND:
11946 case ISD::ANY_EXTEND:
11947 break;
11948 }
11949
11950 if (!ExpectingFlags) {
11951 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11952 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11953
11954 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11955 X86::CondCode tmp = cc0;
11956 cc0 = cc1;
11957 cc1 = tmp;
11958 }
11959
11960 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11961 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11962 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11963 X86ISD::NodeType NTOperator = is64BitFP ?
11964 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11965 // FIXME: need symbolic constants for these magic numbers.
11966 // See X86ATTInstPrinter.cpp:printSSECC().
11967 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11968 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11969 DAG.getConstant(x86cc, MVT::i8));
11970 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11971 OnesOrZeroesF);
11972 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11973 DAG.getConstant(1, MVT::i32));
11974 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11975 return OneBitOfTruth;
11976 }
11977 }
11978 }
11979 }
11980 return SDValue();
11981}
11982
Nate Begemanb65c1752010-12-17 22:55:37 +000011983static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11984 TargetLowering::DAGCombinerInfo &DCI,
11985 const X86Subtarget *Subtarget) {
11986 if (DCI.isBeforeLegalizeOps())
11987 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011988
Stuart Hastings865f0932011-06-03 23:53:54 +000011989 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11990 if (R.getNode())
11991 return R;
11992
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011993 // Want to form ANDNP nodes:
11994 // 1) In the hopes of then easily combining them with OR and AND nodes
11995 // to form PBLEND/PSIGN.
11996 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000011997 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011998 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000011999 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012000
Nate Begemanb65c1752010-12-17 22:55:37 +000012001 SDValue N0 = N->getOperand(0);
12002 SDValue N1 = N->getOperand(1);
12003 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012004
Nate Begemanb65c1752010-12-17 22:55:37 +000012005 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012006 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000012007 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012008 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000012009
12010 // Check RHS for vnot
12011 if (N1.getOpcode() == ISD::XOR &&
12012 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012013 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012014
Nate Begemanb65c1752010-12-17 22:55:37 +000012015 return SDValue();
12016}
12017
Evan Cheng760d1942010-01-04 21:22:48 +000012018static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000012019 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000012020 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000012021 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000012022 return SDValue();
12023
Stuart Hastings865f0932011-06-03 23:53:54 +000012024 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
12025 if (R.getNode())
12026 return R;
12027
Evan Cheng760d1942010-01-04 21:22:48 +000012028 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000012029 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000012030 return SDValue();
12031
Evan Cheng760d1942010-01-04 21:22:48 +000012032 SDValue N0 = N->getOperand(0);
12033 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012034
Nate Begemanb65c1752010-12-17 22:55:37 +000012035 // look for psign/blend
12036 if (Subtarget->hasSSSE3()) {
12037 if (VT == MVT::v2i64) {
12038 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012039 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000012040 std::swap(N0, N1);
12041 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012042 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000012043 SDValue Mask = N1.getOperand(0);
12044 SDValue X = N1.getOperand(1);
12045 SDValue Y;
12046 if (N0.getOperand(0) == Mask)
12047 Y = N0.getOperand(1);
12048 if (N0.getOperand(1) == Mask)
12049 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012050
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012051 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000012052 if (!Y.getNode())
12053 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012054
Nate Begemanb65c1752010-12-17 22:55:37 +000012055 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
12056 if (Mask.getOpcode() != ISD::BITCAST ||
12057 X.getOpcode() != ISD::BITCAST ||
12058 Y.getOpcode() != ISD::BITCAST)
12059 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012060
Nate Begemanb65c1752010-12-17 22:55:37 +000012061 // Look through mask bitcast.
12062 Mask = Mask.getOperand(0);
12063 EVT MaskVT = Mask.getValueType();
12064
12065 // Validate that the Mask operand is a vector sra node. The sra node
12066 // will be an intrinsic.
12067 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
12068 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012069
Nate Begemanb65c1752010-12-17 22:55:37 +000012070 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
12071 // there is no psrai.b
12072 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
12073 case Intrinsic::x86_sse2_psrai_w:
12074 case Intrinsic::x86_sse2_psrai_d:
12075 break;
12076 default: return SDValue();
12077 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012078
Nate Begemanb65c1752010-12-17 22:55:37 +000012079 // Check that the SRA is all signbits.
12080 SDValue SraC = Mask.getOperand(2);
12081 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
12082 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
12083 if ((SraAmt + 1) != EltBits)
12084 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012085
Nate Begemanb65c1752010-12-17 22:55:37 +000012086 DebugLoc DL = N->getDebugLoc();
12087
12088 // Now we know we at least have a plendvb with the mask val. See if
12089 // we can form a psignb/w/d.
12090 // psign = x.type == y.type == mask.type && y = sub(0, x);
12091 X = X.getOperand(0);
12092 Y = Y.getOperand(0);
12093 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
12094 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
12095 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
12096 unsigned Opc = 0;
12097 switch (EltBits) {
12098 case 8: Opc = X86ISD::PSIGNB; break;
12099 case 16: Opc = X86ISD::PSIGNW; break;
12100 case 32: Opc = X86ISD::PSIGND; break;
12101 default: break;
12102 }
12103 if (Opc) {
12104 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
12105 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
12106 }
12107 }
12108 // PBLENDVB only available on SSE 4.1
12109 if (!Subtarget->hasSSE41())
12110 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012111
Nate Begemanb65c1752010-12-17 22:55:37 +000012112 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
12113 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
12114 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000012115 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000012116 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
12117 }
12118 }
12119 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012120
Nate Begemanb65c1752010-12-17 22:55:37 +000012121 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000012122 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
12123 std::swap(N0, N1);
12124 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
12125 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000012126 if (!N0.hasOneUse() || !N1.hasOneUse())
12127 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000012128
12129 SDValue ShAmt0 = N0.getOperand(1);
12130 if (ShAmt0.getValueType() != MVT::i8)
12131 return SDValue();
12132 SDValue ShAmt1 = N1.getOperand(1);
12133 if (ShAmt1.getValueType() != MVT::i8)
12134 return SDValue();
12135 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
12136 ShAmt0 = ShAmt0.getOperand(0);
12137 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
12138 ShAmt1 = ShAmt1.getOperand(0);
12139
12140 DebugLoc DL = N->getDebugLoc();
12141 unsigned Opc = X86ISD::SHLD;
12142 SDValue Op0 = N0.getOperand(0);
12143 SDValue Op1 = N1.getOperand(0);
12144 if (ShAmt0.getOpcode() == ISD::SUB) {
12145 Opc = X86ISD::SHRD;
12146 std::swap(Op0, Op1);
12147 std::swap(ShAmt0, ShAmt1);
12148 }
12149
Evan Cheng8b1190a2010-04-28 01:18:01 +000012150 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000012151 if (ShAmt1.getOpcode() == ISD::SUB) {
12152 SDValue Sum = ShAmt1.getOperand(0);
12153 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000012154 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
12155 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
12156 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
12157 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000012158 return DAG.getNode(Opc, DL, VT,
12159 Op0, Op1,
12160 DAG.getNode(ISD::TRUNCATE, DL,
12161 MVT::i8, ShAmt0));
12162 }
12163 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
12164 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
12165 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000012166 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000012167 return DAG.getNode(Opc, DL, VT,
12168 N0.getOperand(0), N1.getOperand(0),
12169 DAG.getNode(ISD::TRUNCATE, DL,
12170 MVT::i8, ShAmt0));
12171 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012172
Evan Cheng760d1942010-01-04 21:22:48 +000012173 return SDValue();
12174}
12175
Chris Lattner149a4e52008-02-22 02:09:43 +000012176/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012177static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000012178 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000012179 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
12180 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000012181 // A preferable solution to the general problem is to figure out the right
12182 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000012183
12184 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012185 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012186 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012187 if (VT.getSizeInBits() != 64)
12188 return SDValue();
12189
Devang Patel578efa92009-06-05 21:57:13 +000012190 const Function *F = DAG.getMachineFunction().getFunction();
12191 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012192 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012193 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012194 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012195 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012196 isa<LoadSDNode>(St->getValue()) &&
12197 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12198 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012199 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012200 LoadSDNode *Ld = 0;
12201 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012202 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012203 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012204 // Must be a store of a load. We currently handle two cases: the load
12205 // is a direct child, and it's under an intervening TokenFactor. It is
12206 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012207 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012208 Ld = cast<LoadSDNode>(St->getChain());
12209 else if (St->getValue().hasOneUse() &&
12210 ChainVal->getOpcode() == ISD::TokenFactor) {
12211 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012212 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012213 TokenFactorIndex = i;
12214 Ld = cast<LoadSDNode>(St->getValue());
12215 } else
12216 Ops.push_back(ChainVal->getOperand(i));
12217 }
12218 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012219
Evan Cheng536e6672009-03-12 05:59:15 +000012220 if (!Ld || !ISD::isNormalLoad(Ld))
12221 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012222
Evan Cheng536e6672009-03-12 05:59:15 +000012223 // If this is not the MMX case, i.e. we are just turning i64 load/store
12224 // into f64 load/store, avoid the transformation if there are multiple
12225 // uses of the loaded value.
12226 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12227 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012228
Evan Cheng536e6672009-03-12 05:59:15 +000012229 DebugLoc LdDL = Ld->getDebugLoc();
12230 DebugLoc StDL = N->getDebugLoc();
12231 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12232 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12233 // pair instead.
12234 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012235 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012236 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12237 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012238 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012239 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012240 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012241 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012242 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012243 Ops.size());
12244 }
Evan Cheng536e6672009-03-12 05:59:15 +000012245 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012246 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012247 St->isVolatile(), St->isNonTemporal(),
12248 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012249 }
Evan Cheng536e6672009-03-12 05:59:15 +000012250
12251 // Otherwise, lower to two pairs of 32-bit loads / stores.
12252 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012253 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12254 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012255
Owen Anderson825b72b2009-08-11 20:47:22 +000012256 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012257 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012258 Ld->isVolatile(), Ld->isNonTemporal(),
12259 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012260 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012261 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012262 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012263 MinAlign(Ld->getAlignment(), 4));
12264
12265 SDValue NewChain = LoLd.getValue(1);
12266 if (TokenFactorIndex != -1) {
12267 Ops.push_back(LoLd);
12268 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012269 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012270 Ops.size());
12271 }
12272
12273 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012274 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12275 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012276
12277 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012278 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012279 St->isVolatile(), St->isNonTemporal(),
12280 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012281 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012282 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012283 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012284 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012285 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012286 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012287 }
Dan Gohman475871a2008-07-27 21:46:04 +000012288 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012289}
12290
Chris Lattner6cf73262008-01-25 06:14:17 +000012291/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12292/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012293static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012294 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12295 // F[X]OR(0.0, x) -> x
12296 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012297 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12298 if (C->getValueAPF().isPosZero())
12299 return N->getOperand(1);
12300 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12301 if (C->getValueAPF().isPosZero())
12302 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012303 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012304}
12305
12306/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012307static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012308 // FAND(0.0, x) -> 0.0
12309 // FAND(x, 0.0) -> 0.0
12310 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12311 if (C->getValueAPF().isPosZero())
12312 return N->getOperand(0);
12313 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12314 if (C->getValueAPF().isPosZero())
12315 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012316 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012317}
12318
Dan Gohmane5af2d32009-01-29 01:59:02 +000012319static SDValue PerformBTCombine(SDNode *N,
12320 SelectionDAG &DAG,
12321 TargetLowering::DAGCombinerInfo &DCI) {
12322 // BT ignores high bits in the bit index operand.
12323 SDValue Op1 = N->getOperand(1);
12324 if (Op1.hasOneUse()) {
12325 unsigned BitWidth = Op1.getValueSizeInBits();
12326 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12327 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012328 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12329 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012330 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012331 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12332 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12333 DCI.CommitTargetLoweringOpt(TLO);
12334 }
12335 return SDValue();
12336}
Chris Lattner83e6c992006-10-04 06:57:07 +000012337
Eli Friedman7a5e5552009-06-07 06:52:44 +000012338static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12339 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012340 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012341 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012342 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012343 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012344 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012345 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012346 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012347 }
12348 return SDValue();
12349}
12350
Evan Cheng2e489c42009-12-16 00:53:11 +000012351static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12352 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12353 // (and (i32 x86isd::setcc_carry), 1)
12354 // This eliminates the zext. This transformation is necessary because
12355 // ISD::SETCC is always legalized to i8.
12356 DebugLoc dl = N->getDebugLoc();
12357 SDValue N0 = N->getOperand(0);
12358 EVT VT = N->getValueType(0);
12359 if (N0.getOpcode() == ISD::AND &&
12360 N0.hasOneUse() &&
12361 N0.getOperand(0).hasOneUse()) {
12362 SDValue N00 = N0.getOperand(0);
12363 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12364 return SDValue();
12365 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12366 if (!C || C->getZExtValue() != 1)
12367 return SDValue();
12368 return DAG.getNode(ISD::AND, dl, VT,
12369 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12370 N00.getOperand(0), N00.getOperand(1)),
12371 DAG.getConstant(1, VT));
12372 }
12373
12374 return SDValue();
12375}
12376
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012377// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12378static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12379 unsigned X86CC = N->getConstantOperandVal(0);
12380 SDValue EFLAG = N->getOperand(1);
12381 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012382
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012383 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12384 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12385 // cases.
12386 if (X86CC == X86::COND_B)
12387 return DAG.getNode(ISD::AND, DL, MVT::i8,
12388 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12389 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12390 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012391
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012392 return SDValue();
12393}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012394
Benjamin Kramer1396c402011-06-18 11:09:41 +000012395static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12396 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012397 SDValue Op0 = N->getOperand(0);
12398 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12399 // a 32-bit target where SSE doesn't support i64->FP operations.
12400 if (Op0.getOpcode() == ISD::LOAD) {
12401 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12402 EVT VT = Ld->getValueType(0);
12403 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12404 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12405 !XTLI->getSubtarget()->is64Bit() &&
12406 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012407 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12408 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012409 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12410 return FILDChain;
12411 }
12412 }
12413 return SDValue();
12414}
12415
Chris Lattner23a01992010-12-20 01:37:09 +000012416// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12417static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12418 X86TargetLowering::DAGCombinerInfo &DCI) {
12419 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12420 // the result is either zero or one (depending on the input carry bit).
12421 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12422 if (X86::isZeroNode(N->getOperand(0)) &&
12423 X86::isZeroNode(N->getOperand(1)) &&
12424 // We don't have a good way to replace an EFLAGS use, so only do this when
12425 // dead right now.
12426 SDValue(N, 1).use_empty()) {
12427 DebugLoc DL = N->getDebugLoc();
12428 EVT VT = N->getValueType(0);
12429 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12430 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12431 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12432 DAG.getConstant(X86::COND_B,MVT::i8),
12433 N->getOperand(2)),
12434 DAG.getConstant(1, VT));
12435 return DCI.CombineTo(N, Res1, CarryOut);
12436 }
12437
12438 return SDValue();
12439}
12440
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012441// fold (add Y, (sete X, 0)) -> adc 0, Y
12442// (add Y, (setne X, 0)) -> sbb -1, Y
12443// (sub (sete X, 0), Y) -> sbb 0, Y
12444// (sub (setne X, 0), Y) -> adc -1, Y
12445static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12446 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012447
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012448 // Look through ZExts.
12449 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12450 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12451 return SDValue();
12452
12453 SDValue SetCC = Ext.getOperand(0);
12454 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12455 return SDValue();
12456
12457 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12458 if (CC != X86::COND_E && CC != X86::COND_NE)
12459 return SDValue();
12460
12461 SDValue Cmp = SetCC.getOperand(1);
12462 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012463 !X86::isZeroNode(Cmp.getOperand(1)) ||
12464 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012465 return SDValue();
12466
12467 SDValue CmpOp0 = Cmp.getOperand(0);
12468 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12469 DAG.getConstant(1, CmpOp0.getValueType()));
12470
12471 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12472 if (CC == X86::COND_NE)
12473 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12474 DL, OtherVal.getValueType(), OtherVal,
12475 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12476 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12477 DL, OtherVal.getValueType(), OtherVal,
12478 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12479}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012480
Dan Gohman475871a2008-07-27 21:46:04 +000012481SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012482 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012483 SelectionDAG &DAG = DCI.DAG;
12484 switch (N->getOpcode()) {
12485 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012486 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012487 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012488 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012489 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012490 case ISD::ADD:
12491 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012492 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012493 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012494 case ISD::SHL:
12495 case ISD::SRA:
12496 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012497 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012498 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012499 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012500 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012501 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012502 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12503 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012504 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012505 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012506 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012507 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012508 case X86ISD::SHUFPS: // Handle all target specific shuffles
12509 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012510 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012511 case X86ISD::PUNPCKHBW:
12512 case X86ISD::PUNPCKHWD:
12513 case X86ISD::PUNPCKHDQ:
12514 case X86ISD::PUNPCKHQDQ:
12515 case X86ISD::UNPCKHPS:
12516 case X86ISD::UNPCKHPD:
12517 case X86ISD::PUNPCKLBW:
12518 case X86ISD::PUNPCKLWD:
12519 case X86ISD::PUNPCKLDQ:
12520 case X86ISD::PUNPCKLQDQ:
12521 case X86ISD::UNPCKLPS:
12522 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012523 case X86ISD::VUNPCKLPS:
12524 case X86ISD::VUNPCKLPD:
12525 case X86ISD::VUNPCKLPSY:
12526 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012527 case X86ISD::MOVHLPS:
12528 case X86ISD::MOVLHPS:
12529 case X86ISD::PSHUFD:
12530 case X86ISD::PSHUFHW:
12531 case X86ISD::PSHUFLW:
12532 case X86ISD::MOVSS:
12533 case X86ISD::MOVSD:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +000012534 case X86ISD::VPERMIL:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012535 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012536 }
12537
Dan Gohman475871a2008-07-27 21:46:04 +000012538 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012539}
12540
Evan Chenge5b51ac2010-04-17 06:13:15 +000012541/// isTypeDesirableForOp - Return true if the target has native support for
12542/// the specified value type and it is 'desirable' to use the type for the
12543/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12544/// instruction encodings are longer and some i16 instructions are slow.
12545bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12546 if (!isTypeLegal(VT))
12547 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012548 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012549 return true;
12550
12551 switch (Opc) {
12552 default:
12553 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012554 case ISD::LOAD:
12555 case ISD::SIGN_EXTEND:
12556 case ISD::ZERO_EXTEND:
12557 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012558 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012559 case ISD::SRL:
12560 case ISD::SUB:
12561 case ISD::ADD:
12562 case ISD::MUL:
12563 case ISD::AND:
12564 case ISD::OR:
12565 case ISD::XOR:
12566 return false;
12567 }
12568}
12569
12570/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012571/// beneficial for dag combiner to promote the specified node. If true, it
12572/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012573bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012574 EVT VT = Op.getValueType();
12575 if (VT != MVT::i16)
12576 return false;
12577
Evan Cheng4c26e932010-04-19 19:29:22 +000012578 bool Promote = false;
12579 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012580 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012581 default: break;
12582 case ISD::LOAD: {
12583 LoadSDNode *LD = cast<LoadSDNode>(Op);
12584 // If the non-extending load has a single use and it's not live out, then it
12585 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012586 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12587 Op.hasOneUse()*/) {
12588 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12589 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12590 // The only case where we'd want to promote LOAD (rather then it being
12591 // promoted as an operand is when it's only use is liveout.
12592 if (UI->getOpcode() != ISD::CopyToReg)
12593 return false;
12594 }
12595 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012596 Promote = true;
12597 break;
12598 }
12599 case ISD::SIGN_EXTEND:
12600 case ISD::ZERO_EXTEND:
12601 case ISD::ANY_EXTEND:
12602 Promote = true;
12603 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012604 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012605 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012606 SDValue N0 = Op.getOperand(0);
12607 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012608 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012609 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012610 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012611 break;
12612 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012613 case ISD::ADD:
12614 case ISD::MUL:
12615 case ISD::AND:
12616 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012617 case ISD::XOR:
12618 Commute = true;
12619 // fallthrough
12620 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012621 SDValue N0 = Op.getOperand(0);
12622 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012623 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012624 return false;
12625 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012626 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012627 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012628 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012629 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012630 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012631 }
12632 }
12633
12634 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012635 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012636}
12637
Evan Cheng60c07e12006-07-05 22:17:51 +000012638//===----------------------------------------------------------------------===//
12639// X86 Inline Assembly Support
12640//===----------------------------------------------------------------------===//
12641
Chris Lattnerb8105652009-07-20 17:51:36 +000012642bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12643 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012644
12645 std::string AsmStr = IA->getAsmString();
12646
12647 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012648 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012649 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012650
12651 switch (AsmPieces.size()) {
12652 default: return false;
12653 case 1:
12654 AsmStr = AsmPieces[0];
12655 AsmPieces.clear();
12656 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12657
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012658 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012659 // we will turn this bswap into something that will be lowered to logical ops
12660 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12661 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012662 // bswap $0
12663 if (AsmPieces.size() == 2 &&
12664 (AsmPieces[0] == "bswap" ||
12665 AsmPieces[0] == "bswapq" ||
12666 AsmPieces[0] == "bswapl") &&
12667 (AsmPieces[1] == "$0" ||
12668 AsmPieces[1] == "${0:q}")) {
12669 // No need to check constraints, nothing other than the equivalent of
12670 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012671 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012672 if (!Ty || Ty->getBitWidth() % 16 != 0)
12673 return false;
12674 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012675 }
12676 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012677 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012678 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012679 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012680 AsmPieces[1] == "$$8," &&
12681 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012682 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12683 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012684 const std::string &ConstraintsStr = IA->getConstraintString();
12685 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012686 std::sort(AsmPieces.begin(), AsmPieces.end());
12687 if (AsmPieces.size() == 4 &&
12688 AsmPieces[0] == "~{cc}" &&
12689 AsmPieces[1] == "~{dirflag}" &&
12690 AsmPieces[2] == "~{flags}" &&
12691 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012692 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012693 if (!Ty || Ty->getBitWidth() % 16 != 0)
12694 return false;
12695 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012696 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012697 }
12698 break;
12699 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012700 if (CI->getType()->isIntegerTy(32) &&
12701 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12702 SmallVector<StringRef, 4> Words;
12703 SplitString(AsmPieces[0], Words, " \t,");
12704 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12705 Words[2] == "${0:w}") {
12706 Words.clear();
12707 SplitString(AsmPieces[1], Words, " \t,");
12708 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12709 Words[2] == "$0") {
12710 Words.clear();
12711 SplitString(AsmPieces[2], Words, " \t,");
12712 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12713 Words[2] == "${0:w}") {
12714 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012715 const std::string &ConstraintsStr = IA->getConstraintString();
12716 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012717 std::sort(AsmPieces.begin(), AsmPieces.end());
12718 if (AsmPieces.size() == 4 &&
12719 AsmPieces[0] == "~{cc}" &&
12720 AsmPieces[1] == "~{dirflag}" &&
12721 AsmPieces[2] == "~{flags}" &&
12722 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012723 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012724 if (!Ty || Ty->getBitWidth() % 16 != 0)
12725 return false;
12726 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012727 }
12728 }
12729 }
12730 }
12731 }
Evan Cheng55d42002011-01-08 01:24:27 +000012732
12733 if (CI->getType()->isIntegerTy(64)) {
12734 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12735 if (Constraints.size() >= 2 &&
12736 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12737 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12738 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12739 SmallVector<StringRef, 4> Words;
12740 SplitString(AsmPieces[0], Words, " \t");
12741 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012742 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012743 SplitString(AsmPieces[1], Words, " \t");
12744 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12745 Words.clear();
12746 SplitString(AsmPieces[2], Words, " \t,");
12747 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12748 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012749 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012750 if (!Ty || Ty->getBitWidth() % 16 != 0)
12751 return false;
12752 return IntrinsicLowering::LowerToByteSwap(CI);
12753 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012754 }
12755 }
12756 }
12757 }
12758 break;
12759 }
12760 return false;
12761}
12762
12763
12764
Chris Lattnerf4dff842006-07-11 02:54:03 +000012765/// getConstraintType - Given a constraint letter, return the type of
12766/// constraint it is for this target.
12767X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012768X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12769 if (Constraint.size() == 1) {
12770 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012771 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012772 case 'q':
12773 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012774 case 'f':
12775 case 't':
12776 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012777 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012778 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012779 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000012780 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000012781 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012782 case 'a':
12783 case 'b':
12784 case 'c':
12785 case 'd':
12786 case 'S':
12787 case 'D':
12788 case 'A':
12789 return C_Register;
12790 case 'I':
12791 case 'J':
12792 case 'K':
12793 case 'L':
12794 case 'M':
12795 case 'N':
12796 case 'G':
12797 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012798 case 'e':
12799 case 'Z':
12800 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012801 default:
12802 break;
12803 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012804 }
Chris Lattner4234f572007-03-25 02:14:49 +000012805 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012806}
12807
John Thompson44ab89e2010-10-29 17:29:13 +000012808/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012809/// This object must already have been set up with the operand type
12810/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012811TargetLowering::ConstraintWeight
12812 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012813 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012814 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012815 Value *CallOperandVal = info.CallOperandVal;
12816 // If we don't have a value, we can't do a match,
12817 // but allow it at the lowest weight.
12818 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012819 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012820 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012821 // Look at the constraint type.
12822 switch (*constraint) {
12823 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012824 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12825 case 'R':
12826 case 'q':
12827 case 'Q':
12828 case 'a':
12829 case 'b':
12830 case 'c':
12831 case 'd':
12832 case 'S':
12833 case 'D':
12834 case 'A':
12835 if (CallOperandVal->getType()->isIntegerTy())
12836 weight = CW_SpecificReg;
12837 break;
12838 case 'f':
12839 case 't':
12840 case 'u':
12841 if (type->isFloatingPointTy())
12842 weight = CW_SpecificReg;
12843 break;
12844 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012845 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012846 weight = CW_SpecificReg;
12847 break;
12848 case 'x':
12849 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012850 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012851 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012852 break;
12853 case 'I':
12854 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12855 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012856 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012857 }
12858 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012859 case 'J':
12860 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12861 if (C->getZExtValue() <= 63)
12862 weight = CW_Constant;
12863 }
12864 break;
12865 case 'K':
12866 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12867 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12868 weight = CW_Constant;
12869 }
12870 break;
12871 case 'L':
12872 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12873 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12874 weight = CW_Constant;
12875 }
12876 break;
12877 case 'M':
12878 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12879 if (C->getZExtValue() <= 3)
12880 weight = CW_Constant;
12881 }
12882 break;
12883 case 'N':
12884 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12885 if (C->getZExtValue() <= 0xff)
12886 weight = CW_Constant;
12887 }
12888 break;
12889 case 'G':
12890 case 'C':
12891 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12892 weight = CW_Constant;
12893 }
12894 break;
12895 case 'e':
12896 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12897 if ((C->getSExtValue() >= -0x80000000LL) &&
12898 (C->getSExtValue() <= 0x7fffffffLL))
12899 weight = CW_Constant;
12900 }
12901 break;
12902 case 'Z':
12903 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12904 if (C->getZExtValue() <= 0xffffffff)
12905 weight = CW_Constant;
12906 }
12907 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012908 }
12909 return weight;
12910}
12911
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012912/// LowerXConstraint - try to replace an X constraint, which matches anything,
12913/// with another that has more specific requirements based on the type of the
12914/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012915const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012916LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012917 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12918 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012919 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012920 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012921 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012922 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012923 return "x";
12924 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012925
Chris Lattner5e764232008-04-26 23:02:14 +000012926 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012927}
12928
Chris Lattner48884cd2007-08-25 00:47:38 +000012929/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12930/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012931void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012932 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012933 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012934 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012935 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012936
Eric Christopher100c8332011-06-02 23:16:42 +000012937 // Only support length 1 constraints for now.
12938 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012939
Eric Christopher100c8332011-06-02 23:16:42 +000012940 char ConstraintLetter = Constraint[0];
12941 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012942 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012943 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012944 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012945 if (C->getZExtValue() <= 31) {
12946 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012947 break;
12948 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012949 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012950 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012951 case 'J':
12952 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012953 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012954 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12955 break;
12956 }
12957 }
12958 return;
12959 case 'K':
12960 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012961 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012962 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12963 break;
12964 }
12965 }
12966 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012967 case 'N':
12968 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012969 if (C->getZExtValue() <= 255) {
12970 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012971 break;
12972 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012973 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012974 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012975 case 'e': {
12976 // 32-bit signed value
12977 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012978 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12979 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012980 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012981 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012982 break;
12983 }
12984 // FIXME gcc accepts some relocatable values here too, but only in certain
12985 // memory models; it's complicated.
12986 }
12987 return;
12988 }
12989 case 'Z': {
12990 // 32-bit unsigned value
12991 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012992 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12993 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012994 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12995 break;
12996 }
12997 }
12998 // FIXME gcc accepts some relocatable values here too, but only in certain
12999 // memory models; it's complicated.
13000 return;
13001 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000013002 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013003 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000013004 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000013005 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000013006 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000013007 break;
13008 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013009
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000013010 // In any sort of PIC mode addresses need to be computed at runtime by
13011 // adding in a register or some sort of table lookup. These can't
13012 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000013013 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000013014 return;
13015
Chris Lattnerdc43a882007-05-03 16:52:29 +000013016 // If we are in non-pic codegen mode, we allow the address of a global (with
13017 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000013018 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000013019 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000013020
Chris Lattner49921962009-05-08 18:23:14 +000013021 // Match either (GA), (GA+C), (GA+C1+C2), etc.
13022 while (1) {
13023 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
13024 Offset += GA->getOffset();
13025 break;
13026 } else if (Op.getOpcode() == ISD::ADD) {
13027 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
13028 Offset += C->getZExtValue();
13029 Op = Op.getOperand(0);
13030 continue;
13031 }
13032 } else if (Op.getOpcode() == ISD::SUB) {
13033 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
13034 Offset += -C->getZExtValue();
13035 Op = Op.getOperand(0);
13036 continue;
13037 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000013038 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000013039
Chris Lattner49921962009-05-08 18:23:14 +000013040 // Otherwise, this isn't something we can handle, reject it.
13041 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000013042 }
Eric Christopherfd179292009-08-27 18:07:15 +000013043
Dan Gohman46510a72010-04-15 01:51:59 +000013044 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000013045 // If we require an extra load to get this address, as in PIC mode, we
13046 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000013047 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
13048 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000013049 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000013050
Devang Patel0d881da2010-07-06 22:08:15 +000013051 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
13052 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000013053 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013054 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000013055 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013056
Gabor Greifba36cb52008-08-28 21:40:38 +000013057 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000013058 Ops.push_back(Result);
13059 return;
13060 }
Dale Johannesen1784d162010-06-25 21:55:36 +000013061 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013062}
13063
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013064std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000013065X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000013066 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000013067 // First, see if this is a constraint that directly corresponds to an LLVM
13068 // register class.
13069 if (Constraint.size() == 1) {
13070 // GCC Constraint Letters
13071 switch (Constraint[0]) {
13072 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000013073 // TODO: Slight differences here in allocation order and leaving
13074 // RIP in the class. Do they matter any more here than they do
13075 // in the normal allocation?
13076 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
13077 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000013078 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000013079 return std::make_pair(0U, X86::GR32RegisterClass);
13080 else if (VT == MVT::i16)
13081 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000013082 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000013083 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000013084 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000013085 return std::make_pair(0U, X86::GR64RegisterClass);
13086 break;
13087 }
13088 // 32-bit fallthrough
13089 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000013090 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000013091 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
13092 else if (VT == MVT::i16)
13093 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000013094 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000013095 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
13096 else if (VT == MVT::i64)
13097 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
13098 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000013099 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000013100 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000013101 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000013102 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013103 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000013104 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000013105 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000013106 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000013107 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000013108 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000013109 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000013110 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
13111 if (VT == MVT::i16)
13112 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
13113 if (VT == MVT::i32 || !Subtarget->is64Bit())
13114 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
13115 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013116 case 'f': // FP Stack registers.
13117 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
13118 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000013119 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013120 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013121 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013122 return std::make_pair(0U, X86::RFP64RegisterClass);
13123 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000013124 case 'y': // MMX_REGS if MMX allowed.
13125 if (!Subtarget->hasMMX()) break;
13126 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013127 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013128 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000013129 // FALL THROUGH.
13130 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013131 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000013132
Owen Anderson825b72b2009-08-11 20:47:22 +000013133 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000013134 default: break;
13135 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013136 case MVT::f32:
13137 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000013138 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013139 case MVT::f64:
13140 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000013141 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013142 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013143 case MVT::v16i8:
13144 case MVT::v8i16:
13145 case MVT::v4i32:
13146 case MVT::v2i64:
13147 case MVT::v4f32:
13148 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000013149 return std::make_pair(0U, X86::VR128RegisterClass);
13150 }
Chris Lattnerad043e82007-04-09 05:11:28 +000013151 break;
13152 }
13153 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013154
Chris Lattnerf76d1802006-07-31 23:26:50 +000013155 // Use the default implementation in TargetLowering to convert the register
13156 // constraint into a member of a register class.
13157 std::pair<unsigned, const TargetRegisterClass*> Res;
13158 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000013159
13160 // Not found as a standard register?
13161 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013162 // Map st(0) -> st(7) -> ST0
13163 if (Constraint.size() == 7 && Constraint[0] == '{' &&
13164 tolower(Constraint[1]) == 's' &&
13165 tolower(Constraint[2]) == 't' &&
13166 Constraint[3] == '(' &&
13167 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
13168 Constraint[5] == ')' &&
13169 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000013170
Chris Lattner56d77c72009-09-13 22:41:48 +000013171 Res.first = X86::ST0+Constraint[4]-'0';
13172 Res.second = X86::RFP80RegisterClass;
13173 return Res;
13174 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013175
Chris Lattner56d77c72009-09-13 22:41:48 +000013176 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013177 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000013178 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000013179 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013180 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013181 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013182
13183 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013184 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013185 Res.first = X86::EFLAGS;
13186 Res.second = X86::CCRRegisterClass;
13187 return Res;
13188 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013189
Dale Johannesen330169f2008-11-13 21:52:36 +000013190 // 'A' means EAX + EDX.
13191 if (Constraint == "A") {
13192 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013193 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013194 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013195 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013196 return Res;
13197 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013198
Chris Lattnerf76d1802006-07-31 23:26:50 +000013199 // Otherwise, check to see if this is a register class of the wrong value
13200 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13201 // turn into {ax},{dx}.
13202 if (Res.second->hasType(VT))
13203 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013204
Chris Lattnerf76d1802006-07-31 23:26:50 +000013205 // All of the single-register GCC register classes map their values onto
13206 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13207 // really want an 8-bit or 32-bit register, map to the appropriate register
13208 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013209 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013210 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013211 unsigned DestReg = 0;
13212 switch (Res.first) {
13213 default: break;
13214 case X86::AX: DestReg = X86::AL; break;
13215 case X86::DX: DestReg = X86::DL; break;
13216 case X86::CX: DestReg = X86::CL; break;
13217 case X86::BX: DestReg = X86::BL; break;
13218 }
13219 if (DestReg) {
13220 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013221 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013222 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013223 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013224 unsigned DestReg = 0;
13225 switch (Res.first) {
13226 default: break;
13227 case X86::AX: DestReg = X86::EAX; break;
13228 case X86::DX: DestReg = X86::EDX; break;
13229 case X86::CX: DestReg = X86::ECX; break;
13230 case X86::BX: DestReg = X86::EBX; break;
13231 case X86::SI: DestReg = X86::ESI; break;
13232 case X86::DI: DestReg = X86::EDI; break;
13233 case X86::BP: DestReg = X86::EBP; break;
13234 case X86::SP: DestReg = X86::ESP; break;
13235 }
13236 if (DestReg) {
13237 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013238 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013239 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013240 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013241 unsigned DestReg = 0;
13242 switch (Res.first) {
13243 default: break;
13244 case X86::AX: DestReg = X86::RAX; break;
13245 case X86::DX: DestReg = X86::RDX; break;
13246 case X86::CX: DestReg = X86::RCX; break;
13247 case X86::BX: DestReg = X86::RBX; break;
13248 case X86::SI: DestReg = X86::RSI; break;
13249 case X86::DI: DestReg = X86::RDI; break;
13250 case X86::BP: DestReg = X86::RBP; break;
13251 case X86::SP: DestReg = X86::RSP; break;
13252 }
13253 if (DestReg) {
13254 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013255 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013256 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013257 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013258 } else if (Res.second == X86::FR32RegisterClass ||
13259 Res.second == X86::FR64RegisterClass ||
13260 Res.second == X86::VR128RegisterClass) {
13261 // Handle references to XMM physical registers that got mapped into the
13262 // wrong class. This can happen with constraints like {xmm0} where the
13263 // target independent register mapper will just pick the first match it can
13264 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013265 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013266 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013267 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013268 Res.second = X86::FR64RegisterClass;
13269 else if (X86::VR128RegisterClass->hasType(VT))
13270 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013271 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013272
Chris Lattnerf76d1802006-07-31 23:26:50 +000013273 return Res;
13274}