blob: 8eb5df21262750482c23c64d4b011f894fb395b6 [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()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
974 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
975 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
976 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000977 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000978
Owen Anderson825b72b2009-08-11 20:47:22 +0000979 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000980 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
981 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
984 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
985 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
986 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
987 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
988 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000989
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
991 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
992 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
993 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
994 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
995 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000996
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +0000997 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +0000998 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +0000999 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1000 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1001 EVT VT = SVT;
1002
1003 // Extract subvector is special because the value type
1004 // (result) is 128-bit but the source is 256-bit wide.
1005 if (VT.is128BitVector())
1006 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1007
1008 // Do not attempt to custom lower other non-256-bit vectors
1009 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001010 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001011
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001012 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1013 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1014 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1015 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
1016 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
1017 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001018 }
1019
David Greene54d8eba2011-01-27 22:38:56 +00001020 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001021 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1022 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1023 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001024
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001025 // Do not attempt to promote non-256-bit vectors
1026 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001027 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001028
1029 setOperationAction(ISD::AND, SVT, Promote);
1030 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1031 setOperationAction(ISD::OR, SVT, Promote);
1032 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1033 setOperationAction(ISD::XOR, SVT, Promote);
1034 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1035 setOperationAction(ISD::LOAD, SVT, Promote);
1036 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1037 setOperationAction(ISD::SELECT, SVT, Promote);
1038 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001039 }
David Greene9b9838d2009-06-29 16:47:10 +00001040 }
1041
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001042 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1043 // of this type with custom code.
1044 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1045 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1046 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1047 }
1048
Evan Cheng6be2c582006-04-05 23:38:46 +00001049 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001050 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001051
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001052
Eli Friedman962f5492010-06-02 19:35:46 +00001053 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1054 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001055 //
Eli Friedman962f5492010-06-02 19:35:46 +00001056 // FIXME: We really should do custom legalization for addition and
1057 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1058 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001059 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1060 // Add/Sub/Mul with overflow operations are custom lowered.
1061 MVT VT = IntVTs[i];
1062 setOperationAction(ISD::SADDO, VT, Custom);
1063 setOperationAction(ISD::UADDO, VT, Custom);
1064 setOperationAction(ISD::SSUBO, VT, Custom);
1065 setOperationAction(ISD::USUBO, VT, Custom);
1066 setOperationAction(ISD::SMULO, VT, Custom);
1067 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001068 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001069
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001070 // There are no 8-bit 3-address imul/mul instructions
1071 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1072 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001073
Evan Chengd54f2d52009-03-31 19:38:51 +00001074 if (!Subtarget->is64Bit()) {
1075 // These libcalls are not available in 32-bit.
1076 setLibcallName(RTLIB::SHL_I128, 0);
1077 setLibcallName(RTLIB::SRL_I128, 0);
1078 setLibcallName(RTLIB::SRA_I128, 0);
1079 }
1080
Evan Cheng206ee9d2006-07-07 08:33:52 +00001081 // We have target-specific dag combine patterns for the following nodes:
1082 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001083 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001084 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001085 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001086 setTargetDAGCombine(ISD::SHL);
1087 setTargetDAGCombine(ISD::SRA);
1088 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001089 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001090 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001091 setTargetDAGCombine(ISD::ADD);
1092 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001093 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001094 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001095 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001096 if (Subtarget->is64Bit())
1097 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001098
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001099 computeRegisterProperties();
1100
Evan Cheng05219282011-01-06 06:52:41 +00001101 // On Darwin, -Os means optimize for size without hurting performance,
1102 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001103 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001104 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001105 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001106 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1107 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1108 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001109 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001110 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001111
1112 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001113}
1114
Scott Michel5b8f82e2008-03-10 15:42:14 +00001115
Owen Anderson825b72b2009-08-11 20:47:22 +00001116MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1117 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001118}
1119
1120
Evan Cheng29286502008-01-23 23:17:41 +00001121/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1122/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001123static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001124 if (MaxAlign == 16)
1125 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001126 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001127 if (VTy->getBitWidth() == 128)
1128 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001129 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001130 unsigned EltAlign = 0;
1131 getMaxByValAlign(ATy->getElementType(), EltAlign);
1132 if (EltAlign > MaxAlign)
1133 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001134 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001135 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1136 unsigned EltAlign = 0;
1137 getMaxByValAlign(STy->getElementType(i), EltAlign);
1138 if (EltAlign > MaxAlign)
1139 MaxAlign = EltAlign;
1140 if (MaxAlign == 16)
1141 break;
1142 }
1143 }
1144 return;
1145}
1146
1147/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1148/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001149/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1150/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001151unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001152 if (Subtarget->is64Bit()) {
1153 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001154 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001155 if (TyAlign > 8)
1156 return TyAlign;
1157 return 8;
1158 }
1159
Evan Cheng29286502008-01-23 23:17:41 +00001160 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001161 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001162 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001163 return Align;
1164}
Chris Lattner2b02a442007-02-25 08:29:00 +00001165
Evan Chengf0df0312008-05-15 08:39:06 +00001166/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001167/// and store operations as a result of memset, memcpy, and memmove
1168/// lowering. If DstAlign is zero that means it's safe to destination
1169/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1170/// means there isn't a need to check it against alignment requirement,
1171/// probably because the source does not need to be loaded. If
1172/// 'NonScalarIntSafe' is true, that means it's safe to return a
1173/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1174/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1175/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001176/// It returns EVT::Other if the type should be determined using generic
1177/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001178EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001179X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1180 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001181 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001182 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001183 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001184 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1185 // linux. This is because the stack realignment code can't handle certain
1186 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001187 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001188 if (NonScalarIntSafe &&
1189 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001190 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001191 (Subtarget->isUnalignedMemAccessFast() ||
1192 ((DstAlign == 0 || DstAlign >= 16) &&
1193 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001194 Subtarget->getStackAlignment() >= 16) {
1195 if (Subtarget->hasSSE2())
1196 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001197 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001198 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001199 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001200 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001201 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001202 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001203 // Do not use f64 to lower memcpy if source is string constant. It's
1204 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001205 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001206 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001207 }
Evan Chengf0df0312008-05-15 08:39:06 +00001208 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001209 return MVT::i64;
1210 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001211}
1212
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001213/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1214/// current function. The returned value is a member of the
1215/// MachineJumpTableInfo::JTEntryKind enum.
1216unsigned X86TargetLowering::getJumpTableEncoding() const {
1217 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1218 // symbol.
1219 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1220 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001221 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001222
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001223 // Otherwise, use the normal jump table encoding heuristics.
1224 return TargetLowering::getJumpTableEncoding();
1225}
1226
Chris Lattnerc64daab2010-01-26 05:02:42 +00001227const MCExpr *
1228X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1229 const MachineBasicBlock *MBB,
1230 unsigned uid,MCContext &Ctx) const{
1231 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1232 Subtarget->isPICStyleGOT());
1233 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1234 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001235 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1236 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001237}
1238
Evan Chengcc415862007-11-09 01:32:10 +00001239/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1240/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001241SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001242 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001243 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001244 // This doesn't have DebugLoc associated with it, but is not really the
1245 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001246 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001247 return Table;
1248}
1249
Chris Lattner589c6f62010-01-26 06:28:43 +00001250/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1251/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1252/// MCExpr.
1253const MCExpr *X86TargetLowering::
1254getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1255 MCContext &Ctx) const {
1256 // X86-64 uses RIP relative addressing based on the jump table label.
1257 if (Subtarget->isPICStyleRIPRel())
1258 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1259
1260 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001261 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001262}
1263
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001264// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001265std::pair<const TargetRegisterClass*, uint8_t>
1266X86TargetLowering::findRepresentativeClass(EVT VT) const{
1267 const TargetRegisterClass *RRC = 0;
1268 uint8_t Cost = 1;
1269 switch (VT.getSimpleVT().SimpleTy) {
1270 default:
1271 return TargetLowering::findRepresentativeClass(VT);
1272 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1273 RRC = (Subtarget->is64Bit()
1274 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1275 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001276 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001277 RRC = X86::VR64RegisterClass;
1278 break;
1279 case MVT::f32: case MVT::f64:
1280 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1281 case MVT::v4f32: case MVT::v2f64:
1282 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1283 case MVT::v4f64:
1284 RRC = X86::VR128RegisterClass;
1285 break;
1286 }
1287 return std::make_pair(RRC, Cost);
1288}
1289
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001290bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1291 unsigned &Offset) const {
1292 if (!Subtarget->isTargetLinux())
1293 return false;
1294
1295 if (Subtarget->is64Bit()) {
1296 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1297 Offset = 0x28;
1298 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1299 AddressSpace = 256;
1300 else
1301 AddressSpace = 257;
1302 } else {
1303 // %gs:0x14 on i386
1304 Offset = 0x14;
1305 AddressSpace = 256;
1306 }
1307 return true;
1308}
1309
1310
Chris Lattner2b02a442007-02-25 08:29:00 +00001311//===----------------------------------------------------------------------===//
1312// Return Value Calling Convention Implementation
1313//===----------------------------------------------------------------------===//
1314
Chris Lattner59ed56b2007-02-28 04:55:35 +00001315#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001316
Michael J. Spencerec38de22010-10-10 22:04:20 +00001317bool
Eric Christopher471e4222011-06-08 23:55:35 +00001318X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1319 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001320 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001321 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001322 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001323 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001324 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001325 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001326}
1327
Dan Gohman98ca4f22009-08-05 01:29:28 +00001328SDValue
1329X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001330 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001331 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001332 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001333 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001334 MachineFunction &MF = DAG.getMachineFunction();
1335 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001336
Chris Lattner9774c912007-02-27 05:28:59 +00001337 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001338 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339 RVLocs, *DAG.getContext());
1340 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001341
Evan Chengdcea1632010-02-04 02:40:39 +00001342 // Add the regs to the liveout set for the function.
1343 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1344 for (unsigned i = 0; i != RVLocs.size(); ++i)
1345 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1346 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001347
Dan Gohman475871a2008-07-27 21:46:04 +00001348 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001349
Dan Gohman475871a2008-07-27 21:46:04 +00001350 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001351 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1352 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001353 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1354 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001355
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001356 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001357 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1358 CCValAssign &VA = RVLocs[i];
1359 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001360 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001361 EVT ValVT = ValToCopy.getValueType();
1362
Dale Johannesenc4510512010-09-24 19:05:48 +00001363 // If this is x86-64, and we disabled SSE, we can't return FP values,
1364 // or SSE or MMX vectors.
1365 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1366 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001367 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001368 report_fatal_error("SSE register return with SSE disabled");
1369 }
1370 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1371 // llvm-gcc has never done it right and no one has noticed, so this
1372 // should be OK for now.
1373 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001374 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001375 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001376
Chris Lattner447ff682008-03-11 03:23:40 +00001377 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1378 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001379 if (VA.getLocReg() == X86::ST0 ||
1380 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001381 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1382 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001383 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001384 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001385 RetOps.push_back(ValToCopy);
1386 // Don't emit a copytoreg.
1387 continue;
1388 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001389
Evan Cheng242b38b2009-02-23 09:03:22 +00001390 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1391 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001392 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001393 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001394 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001395 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001396 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1397 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001398 // If we don't have SSE2 available, convert to v4f32 so the generated
1399 // register is legal.
1400 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001401 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001402 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001403 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001404 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001405
Dale Johannesendd64c412009-02-04 00:33:20 +00001406 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001407 Flag = Chain.getValue(1);
1408 }
Dan Gohman61a92132008-04-21 23:59:07 +00001409
1410 // The x86-64 ABI for returning structs by value requires that we copy
1411 // the sret argument into %rax for the return. We saved the argument into
1412 // a virtual register in the entry block, so now we copy the value out
1413 // and into %rax.
1414 if (Subtarget->is64Bit() &&
1415 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1416 MachineFunction &MF = DAG.getMachineFunction();
1417 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1418 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001419 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001420 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001421 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001422
Dale Johannesendd64c412009-02-04 00:33:20 +00001423 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001424 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001425
1426 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001427 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001428 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001429
Chris Lattner447ff682008-03-11 03:23:40 +00001430 RetOps[0] = Chain; // Update chain.
1431
1432 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001433 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001434 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001435
1436 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001437 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001438}
1439
Evan Cheng3d2125c2010-11-30 23:55:39 +00001440bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1441 if (N->getNumValues() != 1)
1442 return false;
1443 if (!N->hasNUsesOfValue(1, 0))
1444 return false;
1445
1446 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001447 if (Copy->getOpcode() != ISD::CopyToReg &&
1448 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001449 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001450
1451 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001452 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001453 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001454 if (UI->getOpcode() != X86ISD::RET_FLAG)
1455 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001456 HasRet = true;
1457 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001458
Evan Cheng1bf891a2010-12-01 22:59:46 +00001459 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001460}
1461
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001462EVT
1463X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001464 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001465 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001466 // TODO: Is this also valid on 32-bit?
1467 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001468 ReturnMVT = MVT::i8;
1469 else
1470 ReturnMVT = MVT::i32;
1471
1472 EVT MinVT = getRegisterType(Context, ReturnMVT);
1473 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001474}
1475
Dan Gohman98ca4f22009-08-05 01:29:28 +00001476/// LowerCallResult - Lower the result values of a call into the
1477/// appropriate copies out of appropriate physical registers.
1478///
1479SDValue
1480X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001481 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 const SmallVectorImpl<ISD::InputArg> &Ins,
1483 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001484 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001485
Chris Lattnere32bbf62007-02-28 07:09:55 +00001486 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001487 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001488 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001489 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1490 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001491 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001492
Chris Lattner3085e152007-02-25 08:59:22 +00001493 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001494 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001495 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001496 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001497
Torok Edwin3f142c32009-02-01 18:15:56 +00001498 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001499 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001500 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001501 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001502 }
1503
Evan Cheng79fb3b42009-02-20 20:43:02 +00001504 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001505
1506 // If this is a call to a function that returns an fp value on the floating
1507 // point stack, we must guarantee the the value is popped from the stack, so
1508 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001509 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001510 // instead.
1511 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1512 // If we prefer to use the value in xmm registers, copy it out as f80 and
1513 // use a truncate to move it from fp stack reg to xmm reg.
1514 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001515 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001516 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1517 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001518 Val = Chain.getValue(0);
1519
1520 // Round the f80 to the right size, which also moves it to the appropriate
1521 // xmm register.
1522 if (CopyVT != VA.getValVT())
1523 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1524 // This truncation won't change the value.
1525 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001526 } else {
1527 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1528 CopyVT, InFlag).getValue(1);
1529 Val = Chain.getValue(0);
1530 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001531 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001532 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001533 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001534
Dan Gohman98ca4f22009-08-05 01:29:28 +00001535 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001536}
1537
1538
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001539//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001540// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001541//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001542// StdCall calling convention seems to be standard for many Windows' API
1543// routines and around. It differs from C calling convention just a little:
1544// callee should clean up the stack, not caller. Symbols should be also
1545// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001546// For info on fast calling convention see Fast Calling Convention (tail call)
1547// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001548
Dan Gohman98ca4f22009-08-05 01:29:28 +00001549/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001550/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001551static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1552 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001553 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001554
Dan Gohman98ca4f22009-08-05 01:29:28 +00001555 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001556}
1557
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001558/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001559/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560static bool
1561ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1562 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001563 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001564
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001566}
1567
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001568/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1569/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001570/// the specific parameter attribute. The copy will be passed as a byval
1571/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001572static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001573CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001574 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1575 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001576 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001577
Dale Johannesendd64c412009-02-04 00:33:20 +00001578 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001579 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001580 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001581}
1582
Chris Lattner29689432010-03-11 00:22:57 +00001583/// IsTailCallConvention - Return true if the calling convention is one that
1584/// supports tail call optimization.
1585static bool IsTailCallConvention(CallingConv::ID CC) {
1586 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1587}
1588
Evan Cheng485fafc2011-03-21 01:19:09 +00001589bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1590 if (!CI->isTailCall())
1591 return false;
1592
1593 CallSite CS(CI);
1594 CallingConv::ID CalleeCC = CS.getCallingConv();
1595 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1596 return false;
1597
1598 return true;
1599}
1600
Evan Cheng0c439eb2010-01-27 00:07:07 +00001601/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1602/// a tailcall target by changing its ABI.
1603static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001604 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001605}
1606
Dan Gohman98ca4f22009-08-05 01:29:28 +00001607SDValue
1608X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001609 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610 const SmallVectorImpl<ISD::InputArg> &Ins,
1611 DebugLoc dl, SelectionDAG &DAG,
1612 const CCValAssign &VA,
1613 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001614 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001615 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001617 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001618 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001619 EVT ValVT;
1620
1621 // If value is passed by pointer we have address passed instead of the value
1622 // itself.
1623 if (VA.getLocInfo() == CCValAssign::Indirect)
1624 ValVT = VA.getLocVT();
1625 else
1626 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001627
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001628 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001629 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001630 // In case of tail call optimization mark all arguments mutable. Since they
1631 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001632 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001633 unsigned Bytes = Flags.getByValSize();
1634 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1635 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001636 return DAG.getFrameIndex(FI, getPointerTy());
1637 } else {
1638 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001639 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001640 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1641 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001642 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001643 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001644 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001645}
1646
Dan Gohman475871a2008-07-27 21:46:04 +00001647SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001648X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001649 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001650 bool isVarArg,
1651 const SmallVectorImpl<ISD::InputArg> &Ins,
1652 DebugLoc dl,
1653 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001654 SmallVectorImpl<SDValue> &InVals)
1655 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001656 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001657 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001658
Gordon Henriksen86737662008-01-05 16:56:59 +00001659 const Function* Fn = MF.getFunction();
1660 if (Fn->hasExternalLinkage() &&
1661 Subtarget->isTargetCygMing() &&
1662 Fn->getName() == "main")
1663 FuncInfo->setForceFramePointer(true);
1664
Evan Cheng1bc78042006-04-26 01:20:17 +00001665 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001667 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001668
Chris Lattner29689432010-03-11 00:22:57 +00001669 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1670 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001671
Chris Lattner638402b2007-02-28 07:00:42 +00001672 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001673 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001674 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001675 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001676
1677 // Allocate shadow area for Win64
1678 if (IsWin64) {
1679 CCInfo.AllocateStack(32, 8);
1680 }
1681
Duncan Sands45907662010-10-31 13:21:44 +00001682 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001683
Chris Lattnerf39f7712007-02-28 05:46:49 +00001684 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001685 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001686 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1687 CCValAssign &VA = ArgLocs[i];
1688 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1689 // places.
1690 assert(VA.getValNo() != LastVal &&
1691 "Don't support value assigned to multiple locs yet");
1692 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001693
Chris Lattnerf39f7712007-02-28 05:46:49 +00001694 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001695 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001696 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001698 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001700 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001704 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001705 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1706 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001707 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001708 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001709 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001710 RC = X86::VR64RegisterClass;
1711 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001712 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001713
Devang Patel68e6bee2011-02-21 23:21:26 +00001714 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001715 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001716
Chris Lattnerf39f7712007-02-28 05:46:49 +00001717 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1718 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1719 // right size.
1720 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001721 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001722 DAG.getValueType(VA.getValVT()));
1723 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001724 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001725 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001726 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001727 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001728
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001729 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001730 // Handle MMX values passed in XMM regs.
1731 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001732 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1733 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001734 } else
1735 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001736 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001737 } else {
1738 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001739 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001740 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001741
1742 // If value is passed via pointer - do a load.
1743 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001744 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1745 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001746
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001748 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001749
Dan Gohman61a92132008-04-21 23:59:07 +00001750 // The x86-64 ABI for returning structs by value requires that we copy
1751 // the sret argument into %rax for the return. Save the argument into
1752 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001753 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001754 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1755 unsigned Reg = FuncInfo->getSRetReturnReg();
1756 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001758 FuncInfo->setSRetReturnReg(Reg);
1759 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001760 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001762 }
1763
Chris Lattnerf39f7712007-02-28 05:46:49 +00001764 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001765 // Align stack specially for tail calls.
1766 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001767 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001768
Evan Cheng1bc78042006-04-26 01:20:17 +00001769 // If the function takes variable number of arguments, make a frame index for
1770 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001771 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001772 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1773 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001774 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001775 }
1776 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001777 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1778
1779 // FIXME: We should really autogenerate these arrays
1780 static const unsigned GPR64ArgRegsWin64[] = {
1781 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001782 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001783 static const unsigned GPR64ArgRegs64Bit[] = {
1784 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1785 };
1786 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001787 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1788 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1789 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001790 const unsigned *GPR64ArgRegs;
1791 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001792
1793 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001794 // The XMM registers which might contain var arg parameters are shadowed
1795 // in their paired GPR. So we only need to save the GPR to their home
1796 // slots.
1797 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001798 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001799 } else {
1800 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1801 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001802
1803 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001804 }
1805 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1806 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001807
Devang Patel578efa92009-06-05 21:57:13 +00001808 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001809 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001810 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001811 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001812 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001813 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001814 // Kernel mode asks for SSE to be disabled, so don't push them
1815 // on the stack.
1816 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001817
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001818 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001819 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001820 // Get to the caller-allocated home save location. Add 8 to account
1821 // for the return address.
1822 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001823 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001824 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001825 // Fixup to set vararg frame on shadow area (4 x i64).
1826 if (NumIntRegs < 4)
1827 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001828 } else {
1829 // For X86-64, if there are vararg parameters that are passed via
1830 // registers, then we must store them to their spots on the stack so they
1831 // may be loaded by deferencing the result of va_next.
1832 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1833 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1834 FuncInfo->setRegSaveFrameIndex(
1835 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001836 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001837 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001838
Gordon Henriksen86737662008-01-05 16:56:59 +00001839 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001841 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1842 getPointerTy());
1843 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001844 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001845 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1846 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001847 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001848 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001849 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001850 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001851 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001852 MachinePointerInfo::getFixedStack(
1853 FuncInfo->getRegSaveFrameIndex(), Offset),
1854 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001855 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001856 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001857 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001858
Dan Gohmanface41a2009-08-16 21:24:25 +00001859 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1860 // Now store the XMM (fp + vector) parameter registers.
1861 SmallVector<SDValue, 11> SaveXMMOps;
1862 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001863
Devang Patel68e6bee2011-02-21 23:21:26 +00001864 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001865 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1866 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001867
Dan Gohman1e93df62010-04-17 14:41:14 +00001868 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1869 FuncInfo->getRegSaveFrameIndex()));
1870 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1871 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001872
Dan Gohmanface41a2009-08-16 21:24:25 +00001873 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001874 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001875 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001876 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1877 SaveXMMOps.push_back(Val);
1878 }
1879 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1880 MVT::Other,
1881 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001882 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001883
1884 if (!MemOps.empty())
1885 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1886 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001887 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001888 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001889
Gordon Henriksen86737662008-01-05 16:56:59 +00001890 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001891 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001892 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001893 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001894 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001895 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001896 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001897 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001898 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001899
Gordon Henriksen86737662008-01-05 16:56:59 +00001900 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001901 // RegSaveFrameIndex is X86-64 only.
1902 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001903 if (CallConv == CallingConv::X86_FastCall ||
1904 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001905 // fastcc functions can't have varargs.
1906 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001907 }
Evan Cheng25caf632006-05-23 21:06:34 +00001908
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001910}
1911
Dan Gohman475871a2008-07-27 21:46:04 +00001912SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001913X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1914 SDValue StackPtr, SDValue Arg,
1915 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001916 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001917 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001918 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001919 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001920 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001921 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001922 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001923
1924 return DAG.getStore(Chain, dl, Arg, PtrOff,
1925 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001926 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001927}
1928
Bill Wendling64e87322009-01-16 19:25:27 +00001929/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001930/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001931SDValue
1932X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001933 SDValue &OutRetAddr, SDValue Chain,
1934 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001935 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001936 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001937 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001938 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001939
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001940 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001941 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1942 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001943 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001944}
1945
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001946/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001947/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001948static SDValue
1949EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001950 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001951 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001952 // Store the return address to the appropriate stack slot.
1953 if (!FPDiff) return Chain;
1954 // Calculate the new stack slot for the return address.
1955 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001956 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001957 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001958 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001959 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001960 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001961 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001962 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001963 return Chain;
1964}
1965
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001967X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001968 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001969 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001971 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001972 const SmallVectorImpl<ISD::InputArg> &Ins,
1973 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001974 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975 MachineFunction &MF = DAG.getMachineFunction();
1976 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001977 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001979 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980
Evan Cheng5f941932010-02-05 02:21:12 +00001981 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001982 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001983 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1984 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001985 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001986
1987 // Sibcalls are automatically detected tailcalls which do not require
1988 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001989 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001990 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001991
1992 if (isTailCall)
1993 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001994 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001995
Chris Lattner29689432010-03-11 00:22:57 +00001996 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1997 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001998
Chris Lattner638402b2007-02-28 07:00:42 +00001999 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002000 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002001 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002002 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002003
2004 // Allocate shadow area for Win64
2005 if (IsWin64) {
2006 CCInfo.AllocateStack(32, 8);
2007 }
2008
Duncan Sands45907662010-10-31 13:21:44 +00002009 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002010
Chris Lattner423c5f42007-02-28 05:31:48 +00002011 // Get a count of how many bytes are to be pushed on the stack.
2012 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002013 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002014 // This is a sibcall. The memory operands are available in caller's
2015 // own caller's stack.
2016 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002017 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002018 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002019
Gordon Henriksen86737662008-01-05 16:56:59 +00002020 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002021 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002022 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002023 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002024 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2025 FPDiff = NumBytesCallerPushed - NumBytes;
2026
2027 // Set the delta of movement of the returnaddr stackslot.
2028 // But only set if delta is greater than previous delta.
2029 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2030 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2031 }
2032
Evan Chengf22f9b32010-02-06 03:28:46 +00002033 if (!IsSibcall)
2034 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002035
Dan Gohman475871a2008-07-27 21:46:04 +00002036 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002037 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002038 if (isTailCall && FPDiff)
2039 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2040 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002041
Dan Gohman475871a2008-07-27 21:46:04 +00002042 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2043 SmallVector<SDValue, 8> MemOpChains;
2044 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002045
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002046 // Walk the register/memloc assignments, inserting copies/loads. In the case
2047 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002048 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2049 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002050 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002051 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002052 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002053 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002054
Chris Lattner423c5f42007-02-28 05:31:48 +00002055 // Promote the value if needed.
2056 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002057 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002058 case CCValAssign::Full: break;
2059 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002060 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002061 break;
2062 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002063 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002064 break;
2065 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002066 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2067 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002068 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002069 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2070 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002071 } else
2072 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2073 break;
2074 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002075 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002076 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002077 case CCValAssign::Indirect: {
2078 // Store the argument.
2079 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002080 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002081 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002082 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002083 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002084 Arg = SpillSlot;
2085 break;
2086 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002087 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Chris Lattner423c5f42007-02-28 05:31:48 +00002089 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002090 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2091 if (isVarArg && IsWin64) {
2092 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2093 // shadow reg if callee is a varargs function.
2094 unsigned ShadowReg = 0;
2095 switch (VA.getLocReg()) {
2096 case X86::XMM0: ShadowReg = X86::RCX; break;
2097 case X86::XMM1: ShadowReg = X86::RDX; break;
2098 case X86::XMM2: ShadowReg = X86::R8; break;
2099 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002100 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002101 if (ShadowReg)
2102 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002103 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002104 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002105 assert(VA.isMemLoc());
2106 if (StackPtr.getNode() == 0)
2107 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2108 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2109 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002110 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002112
Evan Cheng32fe1032006-05-25 00:59:30 +00002113 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002114 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002115 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002116
Evan Cheng347d5f72006-04-28 21:29:37 +00002117 // Build a sequence of copy-to-reg nodes chained together with token chain
2118 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002119 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002120 // Tail call byval lowering might overwrite argument registers so in case of
2121 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002122 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002123 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002124 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002125 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002126 InFlag = Chain.getValue(1);
2127 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002128
Chris Lattner88e1fd52009-07-09 04:24:46 +00002129 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002130 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2131 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002132 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002133 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2134 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002135 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002136 InFlag);
2137 InFlag = Chain.getValue(1);
2138 } else {
2139 // If we are tail calling and generating PIC/GOT style code load the
2140 // address of the callee into ECX. The value in ecx is used as target of
2141 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2142 // for tail calls on PIC/GOT architectures. Normally we would just put the
2143 // address of GOT into ebx and then call target@PLT. But for tail calls
2144 // ebx would be restored (since ebx is callee saved) before jumping to the
2145 // target@PLT.
2146
2147 // Note: The actual moving to ECX is done further down.
2148 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2149 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2150 !G->getGlobal()->hasProtectedVisibility())
2151 Callee = LowerGlobalAddress(Callee, DAG);
2152 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002153 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002154 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002155 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002156
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002157 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002158 // From AMD64 ABI document:
2159 // For calls that may call functions that use varargs or stdargs
2160 // (prototype-less calls or calls to functions containing ellipsis (...) in
2161 // the declaration) %al is used as hidden argument to specify the number
2162 // of SSE registers used. The contents of %al do not need to match exactly
2163 // the number of registers, but must be an ubound on the number of SSE
2164 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002165
Gordon Henriksen86737662008-01-05 16:56:59 +00002166 // Count the number of XMM registers allocated.
2167 static const unsigned XMMArgRegs[] = {
2168 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2169 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2170 };
2171 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002172 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002173 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002174
Dale Johannesendd64c412009-02-04 00:33:20 +00002175 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002177 InFlag = Chain.getValue(1);
2178 }
2179
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002180
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002181 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002182 if (isTailCall) {
2183 // Force all the incoming stack arguments to be loaded from the stack
2184 // before any new outgoing arguments are stored to the stack, because the
2185 // outgoing stack slots may alias the incoming argument stack slots, and
2186 // the alias isn't otherwise explicit. This is slightly more conservative
2187 // than necessary, because it means that each store effectively depends
2188 // on every argument instead of just those arguments it would clobber.
2189 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2190
Dan Gohman475871a2008-07-27 21:46:04 +00002191 SmallVector<SDValue, 8> MemOpChains2;
2192 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002193 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002194 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002195 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002196 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002197 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2198 CCValAssign &VA = ArgLocs[i];
2199 if (VA.isRegLoc())
2200 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002201 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002202 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002204 // Create frame index.
2205 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002206 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002207 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002208 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002209
Duncan Sands276dcbd2008-03-21 09:14:45 +00002210 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002211 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002212 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002213 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002214 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002215 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002216 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002217
Dan Gohman98ca4f22009-08-05 01:29:28 +00002218 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2219 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002220 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002221 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002222 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002223 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002224 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002225 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002226 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002227 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002228 }
2229 }
2230
2231 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002232 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002233 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002234
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002235 // Copy arguments to their registers.
2236 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002237 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002238 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002239 InFlag = Chain.getValue(1);
2240 }
Dan Gohman475871a2008-07-27 21:46:04 +00002241 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002242
Gordon Henriksen86737662008-01-05 16:56:59 +00002243 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002244 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002245 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002246 }
2247
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002248 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2249 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2250 // In the 64-bit large code model, we have to make all calls
2251 // through a register, since the call instruction's 32-bit
2252 // pc-relative offset may not be large enough to hold the whole
2253 // address.
2254 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002255 // If the callee is a GlobalAddress node (quite common, every direct call
2256 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2257 // it.
2258
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002259 // We should use extra load for direct calls to dllimported functions in
2260 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002261 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002262 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002263 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002264 bool ExtraLoad = false;
2265 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002266
Chris Lattner48a7d022009-07-09 05:02:21 +00002267 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2268 // external symbols most go through the PLT in PIC mode. If the symbol
2269 // has hidden or protected visibility, or if it is static or local, then
2270 // we don't need to use the PLT - we can directly call it.
2271 if (Subtarget->isTargetELF() &&
2272 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002273 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002274 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002275 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002276 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002277 (!Subtarget->getTargetTriple().isMacOSX() ||
2278 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002279 // PC-relative references to external symbols should go through $stub,
2280 // unless we're building with the leopard linker or later, which
2281 // automatically synthesizes these stubs.
2282 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002283 } else if (Subtarget->isPICStyleRIPRel() &&
2284 isa<Function>(GV) &&
2285 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2286 // If the function is marked as non-lazy, generate an indirect call
2287 // which loads from the GOT directly. This avoids runtime overhead
2288 // at the cost of eager binding (and one extra byte of encoding).
2289 OpFlags = X86II::MO_GOTPCREL;
2290 WrapperKind = X86ISD::WrapperRIP;
2291 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002292 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002293
Devang Patel0d881da2010-07-06 22:08:15 +00002294 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002295 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002296
2297 // Add a wrapper if needed.
2298 if (WrapperKind != ISD::DELETED_NODE)
2299 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2300 // Add extra indirection if needed.
2301 if (ExtraLoad)
2302 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2303 MachinePointerInfo::getGOT(),
2304 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002305 }
Bill Wendling056292f2008-09-16 21:48:12 +00002306 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002307 unsigned char OpFlags = 0;
2308
Evan Cheng1bf891a2010-12-01 22:59:46 +00002309 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2310 // external symbols should go through the PLT.
2311 if (Subtarget->isTargetELF() &&
2312 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2313 OpFlags = X86II::MO_PLT;
2314 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002315 (!Subtarget->getTargetTriple().isMacOSX() ||
2316 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002317 // PC-relative references to external symbols should go through $stub,
2318 // unless we're building with the leopard linker or later, which
2319 // automatically synthesizes these stubs.
2320 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002321 }
Eric Christopherfd179292009-08-27 18:07:15 +00002322
Chris Lattner48a7d022009-07-09 05:02:21 +00002323 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2324 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002325 }
2326
Chris Lattnerd96d0722007-02-25 06:40:16 +00002327 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002328 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002329 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002330
Evan Chengf22f9b32010-02-06 03:28:46 +00002331 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002332 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2333 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002334 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002335 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002336
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002337 Ops.push_back(Chain);
2338 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002339
Dan Gohman98ca4f22009-08-05 01:29:28 +00002340 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002341 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002342
Gordon Henriksen86737662008-01-05 16:56:59 +00002343 // Add argument registers to the end of the list so that they are known live
2344 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002345 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2346 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2347 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002348
Evan Cheng586ccac2008-03-18 23:36:35 +00002349 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002350 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002351 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2352
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002353 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002354 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002355 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002356
Gabor Greifba36cb52008-08-28 21:40:38 +00002357 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002358 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002359
Dan Gohman98ca4f22009-08-05 01:29:28 +00002360 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002361 // We used to do:
2362 //// If this is the first return lowered for this function, add the regs
2363 //// to the liveout set for the function.
2364 // This isn't right, although it's probably harmless on x86; liveouts
2365 // should be computed from returns not tail calls. Consider a void
2366 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002367 return DAG.getNode(X86ISD::TC_RETURN, dl,
2368 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002369 }
2370
Dale Johannesenace16102009-02-03 19:33:06 +00002371 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002372 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002373
Chris Lattner2d297092006-05-23 18:50:38 +00002374 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002375 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002376 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002377 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002378 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002379 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002380 // pops the hidden struct pointer, so we have to push it back.
2381 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002382 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002383 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002384 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002385
Gordon Henriksenae636f82008-01-03 16:47:34 +00002386 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002387 if (!IsSibcall) {
2388 Chain = DAG.getCALLSEQ_END(Chain,
2389 DAG.getIntPtrConstant(NumBytes, true),
2390 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2391 true),
2392 InFlag);
2393 InFlag = Chain.getValue(1);
2394 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002395
Chris Lattner3085e152007-02-25 08:59:22 +00002396 // Handle result values, copying them out of physregs into vregs that we
2397 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002398 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2399 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002400}
2401
Evan Cheng25ab6902006-09-08 06:48:29 +00002402
2403//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002404// Fast Calling Convention (tail call) implementation
2405//===----------------------------------------------------------------------===//
2406
2407// Like std call, callee cleans arguments, convention except that ECX is
2408// reserved for storing the tail called function address. Only 2 registers are
2409// free for argument passing (inreg). Tail call optimization is performed
2410// provided:
2411// * tailcallopt is enabled
2412// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002413// On X86_64 architecture with GOT-style position independent code only local
2414// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002415// To keep the stack aligned according to platform abi the function
2416// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2417// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002418// If a tail called function callee has more arguments than the caller the
2419// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002420// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002421// original REtADDR, but before the saved framepointer or the spilled registers
2422// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2423// stack layout:
2424// arg1
2425// arg2
2426// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002427// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002428// move area ]
2429// (possible EBP)
2430// ESI
2431// EDI
2432// local1 ..
2433
2434/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2435/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002436unsigned
2437X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2438 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002439 MachineFunction &MF = DAG.getMachineFunction();
2440 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002441 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002442 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002443 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002444 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002445 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002446 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2447 // Number smaller than 12 so just add the difference.
2448 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2449 } else {
2450 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002451 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002452 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002453 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002454 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002455}
2456
Evan Cheng5f941932010-02-05 02:21:12 +00002457/// MatchingStackOffset - Return true if the given stack call argument is
2458/// already available in the same position (relatively) of the caller's
2459/// incoming argument stack.
2460static
2461bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2462 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2463 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002464 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2465 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002466 if (Arg.getOpcode() == ISD::CopyFromReg) {
2467 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002468 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002469 return false;
2470 MachineInstr *Def = MRI->getVRegDef(VR);
2471 if (!Def)
2472 return false;
2473 if (!Flags.isByVal()) {
2474 if (!TII->isLoadFromStackSlot(Def, FI))
2475 return false;
2476 } else {
2477 unsigned Opcode = Def->getOpcode();
2478 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2479 Def->getOperand(1).isFI()) {
2480 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002481 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002482 } else
2483 return false;
2484 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002485 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2486 if (Flags.isByVal())
2487 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002488 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002489 // define @foo(%struct.X* %A) {
2490 // tail call @bar(%struct.X* byval %A)
2491 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002492 return false;
2493 SDValue Ptr = Ld->getBasePtr();
2494 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2495 if (!FINode)
2496 return false;
2497 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002498 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002499 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002500 FI = FINode->getIndex();
2501 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002502 } else
2503 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002504
Evan Cheng4cae1332010-03-05 08:38:04 +00002505 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002506 if (!MFI->isFixedObjectIndex(FI))
2507 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002508 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002509}
2510
Dan Gohman98ca4f22009-08-05 01:29:28 +00002511/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2512/// for tail call optimization. Targets which want to do tail call
2513/// optimization should implement this function.
2514bool
2515X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002516 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002517 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002518 bool isCalleeStructRet,
2519 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002520 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002521 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002522 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002523 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002524 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002525 CalleeCC != CallingConv::C)
2526 return false;
2527
Evan Cheng7096ae42010-01-29 06:45:59 +00002528 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002529 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002530 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002531 CallingConv::ID CallerCC = CallerF->getCallingConv();
2532 bool CCMatch = CallerCC == CalleeCC;
2533
Dan Gohman1797ed52010-02-08 20:27:50 +00002534 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002535 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002536 return true;
2537 return false;
2538 }
2539
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002540 // Look for obvious safe cases to perform tail call optimization that do not
2541 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002542
Evan Cheng2c12cb42010-03-26 16:26:03 +00002543 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2544 // emit a special epilogue.
2545 if (RegInfo->needsStackRealignment(MF))
2546 return false;
2547
Evan Chenga375d472010-03-15 18:54:48 +00002548 // Also avoid sibcall optimization if either caller or callee uses struct
2549 // return semantics.
2550 if (isCalleeStructRet || isCallerStructRet)
2551 return false;
2552
Chad Rosier2416da32011-06-24 21:15:36 +00002553 // An stdcall caller is expected to clean up its arguments; the callee
2554 // isn't going to do that.
2555 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2556 return false;
2557
Chad Rosier871f6642011-05-18 19:59:50 +00002558 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002559 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002560 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002561
2562 // Optimizing for varargs on Win64 is unlikely to be safe without
2563 // additional testing.
2564 if (Subtarget->isTargetWin64())
2565 return false;
2566
Chad Rosier871f6642011-05-18 19:59:50 +00002567 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002568 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2569 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002570
Chad Rosier871f6642011-05-18 19:59:50 +00002571 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2572 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2573 if (!ArgLocs[i].isRegLoc())
2574 return false;
2575 }
2576
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002577 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2578 // Therefore if it's not used by the call it is not safe to optimize this into
2579 // a sibcall.
2580 bool Unused = false;
2581 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2582 if (!Ins[i].Used) {
2583 Unused = true;
2584 break;
2585 }
2586 }
2587 if (Unused) {
2588 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002589 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2590 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002591 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002592 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002593 CCValAssign &VA = RVLocs[i];
2594 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2595 return false;
2596 }
2597 }
2598
Evan Cheng13617962010-04-30 01:12:32 +00002599 // If the calling conventions do not match, then we'd better make sure the
2600 // results are returned in the same way as what the caller expects.
2601 if (!CCMatch) {
2602 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002603 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2604 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002605 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2606
2607 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002608 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2609 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002610 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2611
2612 if (RVLocs1.size() != RVLocs2.size())
2613 return false;
2614 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2615 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2616 return false;
2617 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2618 return false;
2619 if (RVLocs1[i].isRegLoc()) {
2620 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2621 return false;
2622 } else {
2623 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2624 return false;
2625 }
2626 }
2627 }
2628
Evan Chenga6bff982010-01-30 01:22:00 +00002629 // If the callee takes no arguments then go on to check the results of the
2630 // call.
2631 if (!Outs.empty()) {
2632 // Check if stack adjustment is needed. For now, do not do this if any
2633 // argument is passed on the stack.
2634 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002635 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2636 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002637
2638 // Allocate shadow area for Win64
2639 if (Subtarget->isTargetWin64()) {
2640 CCInfo.AllocateStack(32, 8);
2641 }
2642
Duncan Sands45907662010-10-31 13:21:44 +00002643 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002644 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002645 MachineFunction &MF = DAG.getMachineFunction();
2646 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2647 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002648
2649 // Check if the arguments are already laid out in the right way as
2650 // the caller's fixed stack objects.
2651 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002652 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2653 const X86InstrInfo *TII =
2654 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002655 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2656 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002657 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002658 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002659 if (VA.getLocInfo() == CCValAssign::Indirect)
2660 return false;
2661 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002662 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2663 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002664 return false;
2665 }
2666 }
2667 }
Evan Cheng9c044672010-05-29 01:35:22 +00002668
2669 // If the tailcall address may be in a register, then make sure it's
2670 // possible to register allocate for it. In 32-bit, the call address can
2671 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002672 // callee-saved registers are restored. These happen to be the same
2673 // registers used to pass 'inreg' arguments so watch out for those.
2674 if (!Subtarget->is64Bit() &&
2675 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002676 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002677 unsigned NumInRegs = 0;
2678 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2679 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002680 if (!VA.isRegLoc())
2681 continue;
2682 unsigned Reg = VA.getLocReg();
2683 switch (Reg) {
2684 default: break;
2685 case X86::EAX: case X86::EDX: case X86::ECX:
2686 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002687 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002688 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002689 }
2690 }
2691 }
Evan Chenga6bff982010-01-30 01:22:00 +00002692 }
Evan Chengb1712452010-01-27 06:25:16 +00002693
Evan Cheng86809cc2010-02-03 03:28:02 +00002694 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002695}
2696
Dan Gohman3df24e62008-09-03 23:12:08 +00002697FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002698X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2699 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002700}
2701
2702
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002703//===----------------------------------------------------------------------===//
2704// Other Lowering Hooks
2705//===----------------------------------------------------------------------===//
2706
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002707static bool MayFoldLoad(SDValue Op) {
2708 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2709}
2710
2711static bool MayFoldIntoStore(SDValue Op) {
2712 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2713}
2714
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002715static bool isTargetShuffle(unsigned Opcode) {
2716 switch(Opcode) {
2717 default: return false;
2718 case X86ISD::PSHUFD:
2719 case X86ISD::PSHUFHW:
2720 case X86ISD::PSHUFLW:
2721 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002722 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002723 case X86ISD::SHUFPS:
2724 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002725 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002726 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002727 case X86ISD::MOVLPS:
2728 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002729 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002730 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002731 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002732 case X86ISD::MOVSS:
2733 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002734 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002735 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002736 case X86ISD::VUNPCKLPS:
2737 case X86ISD::VUNPCKLPD:
2738 case X86ISD::VUNPCKLPSY:
2739 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002740 case X86ISD::PUNPCKLWD:
2741 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002742 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002743 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002744 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002745 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002746 case X86ISD::PUNPCKHWD:
2747 case X86ISD::PUNPCKHBW:
2748 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002749 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002750 return true;
2751 }
2752 return false;
2753}
2754
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002755static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002756 SDValue V1, SelectionDAG &DAG) {
2757 switch(Opc) {
2758 default: llvm_unreachable("Unknown x86 shuffle node");
2759 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002760 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002761 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002762 return DAG.getNode(Opc, dl, VT, V1);
2763 }
2764
2765 return SDValue();
2766}
2767
2768static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002769 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002770 switch(Opc) {
2771 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002772 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002773 case X86ISD::PSHUFHW:
2774 case X86ISD::PSHUFLW:
2775 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2776 }
2777
2778 return SDValue();
2779}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002780
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002781static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2782 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2783 switch(Opc) {
2784 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002785 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002786 case X86ISD::SHUFPD:
2787 case X86ISD::SHUFPS:
2788 return DAG.getNode(Opc, dl, VT, V1, V2,
2789 DAG.getConstant(TargetMask, MVT::i8));
2790 }
2791 return SDValue();
2792}
2793
2794static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2795 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2796 switch(Opc) {
2797 default: llvm_unreachable("Unknown x86 shuffle node");
2798 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002799 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002800 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002801 case X86ISD::MOVLPS:
2802 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002803 case X86ISD::MOVSS:
2804 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002805 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002806 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002807 case X86ISD::VUNPCKLPS:
2808 case X86ISD::VUNPCKLPD:
2809 case X86ISD::VUNPCKLPSY:
2810 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002811 case X86ISD::PUNPCKLWD:
2812 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002813 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002814 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002815 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002816 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002817 case X86ISD::PUNPCKHWD:
2818 case X86ISD::PUNPCKHBW:
2819 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002820 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002821 return DAG.getNode(Opc, dl, VT, V1, V2);
2822 }
2823 return SDValue();
2824}
2825
Dan Gohmand858e902010-04-17 15:26:15 +00002826SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002827 MachineFunction &MF = DAG.getMachineFunction();
2828 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2829 int ReturnAddrIndex = FuncInfo->getRAIndex();
2830
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002831 if (ReturnAddrIndex == 0) {
2832 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002833 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002834 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002835 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002836 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002837 }
2838
Evan Cheng25ab6902006-09-08 06:48:29 +00002839 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002840}
2841
2842
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002843bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2844 bool hasSymbolicDisplacement) {
2845 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002846 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002847 return false;
2848
2849 // If we don't have a symbolic displacement - we don't have any extra
2850 // restrictions.
2851 if (!hasSymbolicDisplacement)
2852 return true;
2853
2854 // FIXME: Some tweaks might be needed for medium code model.
2855 if (M != CodeModel::Small && M != CodeModel::Kernel)
2856 return false;
2857
2858 // For small code model we assume that latest object is 16MB before end of 31
2859 // bits boundary. We may also accept pretty large negative constants knowing
2860 // that all objects are in the positive half of address space.
2861 if (M == CodeModel::Small && Offset < 16*1024*1024)
2862 return true;
2863
2864 // For kernel code model we know that all object resist in the negative half
2865 // of 32bits address space. We may not accept negative offsets, since they may
2866 // be just off and we may accept pretty large positive ones.
2867 if (M == CodeModel::Kernel && Offset > 0)
2868 return true;
2869
2870 return false;
2871}
2872
Evan Chengef41ff62011-06-23 17:54:54 +00002873/// isCalleePop - Determines whether the callee is required to pop its
2874/// own arguments. Callee pop is necessary to support tail calls.
2875bool X86::isCalleePop(CallingConv::ID CallingConv,
2876 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2877 if (IsVarArg)
2878 return false;
2879
2880 switch (CallingConv) {
2881 default:
2882 return false;
2883 case CallingConv::X86_StdCall:
2884 return !is64Bit;
2885 case CallingConv::X86_FastCall:
2886 return !is64Bit;
2887 case CallingConv::X86_ThisCall:
2888 return !is64Bit;
2889 case CallingConv::Fast:
2890 return TailCallOpt;
2891 case CallingConv::GHC:
2892 return TailCallOpt;
2893 }
2894}
2895
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002896/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2897/// specific condition code, returning the condition code and the LHS/RHS of the
2898/// comparison to make.
2899static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2900 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002901 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002902 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2903 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2904 // X > -1 -> X == 0, jump !sign.
2905 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002906 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002907 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2908 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002909 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002910 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002911 // X < 1 -> X <= 0
2912 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002913 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002914 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002915 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002916
Evan Chengd9558e02006-01-06 00:43:03 +00002917 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002918 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002919 case ISD::SETEQ: return X86::COND_E;
2920 case ISD::SETGT: return X86::COND_G;
2921 case ISD::SETGE: return X86::COND_GE;
2922 case ISD::SETLT: return X86::COND_L;
2923 case ISD::SETLE: return X86::COND_LE;
2924 case ISD::SETNE: return X86::COND_NE;
2925 case ISD::SETULT: return X86::COND_B;
2926 case ISD::SETUGT: return X86::COND_A;
2927 case ISD::SETULE: return X86::COND_BE;
2928 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002929 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002930 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002931
Chris Lattner4c78e022008-12-23 23:42:27 +00002932 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002933
Chris Lattner4c78e022008-12-23 23:42:27 +00002934 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002935 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2936 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002937 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2938 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002939 }
2940
Chris Lattner4c78e022008-12-23 23:42:27 +00002941 switch (SetCCOpcode) {
2942 default: break;
2943 case ISD::SETOLT:
2944 case ISD::SETOLE:
2945 case ISD::SETUGT:
2946 case ISD::SETUGE:
2947 std::swap(LHS, RHS);
2948 break;
2949 }
2950
2951 // On a floating point condition, the flags are set as follows:
2952 // ZF PF CF op
2953 // 0 | 0 | 0 | X > Y
2954 // 0 | 0 | 1 | X < Y
2955 // 1 | 0 | 0 | X == Y
2956 // 1 | 1 | 1 | unordered
2957 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002958 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002959 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002960 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002961 case ISD::SETOLT: // flipped
2962 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002963 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002964 case ISD::SETOLE: // flipped
2965 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002966 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002967 case ISD::SETUGT: // flipped
2968 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002969 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002970 case ISD::SETUGE: // flipped
2971 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002972 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002973 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002974 case ISD::SETNE: return X86::COND_NE;
2975 case ISD::SETUO: return X86::COND_P;
2976 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002977 case ISD::SETOEQ:
2978 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002979 }
Evan Chengd9558e02006-01-06 00:43:03 +00002980}
2981
Evan Cheng4a460802006-01-11 00:33:36 +00002982/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2983/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002984/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002985static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002986 switch (X86CC) {
2987 default:
2988 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002989 case X86::COND_B:
2990 case X86::COND_BE:
2991 case X86::COND_E:
2992 case X86::COND_P:
2993 case X86::COND_A:
2994 case X86::COND_AE:
2995 case X86::COND_NE:
2996 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002997 return true;
2998 }
2999}
3000
Evan Chengeb2f9692009-10-27 19:56:55 +00003001/// isFPImmLegal - Returns true if the target can instruction select the
3002/// specified FP immediate natively. If false, the legalizer will
3003/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003004bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003005 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3006 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3007 return true;
3008 }
3009 return false;
3010}
3011
Nate Begeman9008ca62009-04-27 18:41:29 +00003012/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3013/// the specified range (L, H].
3014static bool isUndefOrInRange(int Val, int Low, int Hi) {
3015 return (Val < 0) || (Val >= Low && Val < Hi);
3016}
3017
3018/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3019/// specified value.
3020static bool isUndefOrEqual(int Val, int CmpVal) {
3021 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003022 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003023 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003024}
3025
Nate Begeman9008ca62009-04-27 18:41:29 +00003026/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3027/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3028/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003029static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003030 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003031 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003032 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 return (Mask[0] < 2 && Mask[1] < 2);
3034 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003035}
3036
Nate Begeman9008ca62009-04-27 18:41:29 +00003037bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003038 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 N->getMask(M);
3040 return ::isPSHUFDMask(M, N->getValueType(0));
3041}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003042
Nate Begeman9008ca62009-04-27 18:41:29 +00003043/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3044/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003045static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003046 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003047 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003048
Nate Begeman9008ca62009-04-27 18:41:29 +00003049 // Lower quadword copied in order or undef.
3050 for (int i = 0; i != 4; ++i)
3051 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003052 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003053
Evan Cheng506d3df2006-03-29 23:07:14 +00003054 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003055 for (int i = 4; i != 8; ++i)
3056 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003057 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003058
Evan Cheng506d3df2006-03-29 23:07:14 +00003059 return true;
3060}
3061
Nate Begeman9008ca62009-04-27 18:41:29 +00003062bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003063 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 N->getMask(M);
3065 return ::isPSHUFHWMask(M, N->getValueType(0));
3066}
Evan Cheng506d3df2006-03-29 23:07:14 +00003067
Nate Begeman9008ca62009-04-27 18:41:29 +00003068/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3069/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003070static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003071 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003072 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003073
Rafael Espindola15684b22009-04-24 12:40:33 +00003074 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003075 for (int i = 4; i != 8; ++i)
3076 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003077 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003078
Rafael Espindola15684b22009-04-24 12:40:33 +00003079 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 for (int i = 0; i != 4; ++i)
3081 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003082 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003083
Rafael Espindola15684b22009-04-24 12:40:33 +00003084 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003085}
3086
Nate Begeman9008ca62009-04-27 18:41:29 +00003087bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003088 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003089 N->getMask(M);
3090 return ::isPSHUFLWMask(M, N->getValueType(0));
3091}
3092
Nate Begemana09008b2009-10-19 02:17:23 +00003093/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3094/// is suitable for input to PALIGNR.
3095static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3096 bool hasSSSE3) {
3097 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003098
Nate Begemana09008b2009-10-19 02:17:23 +00003099 // Do not handle v2i64 / v2f64 shuffles with palignr.
3100 if (e < 4 || !hasSSSE3)
3101 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003102
Nate Begemana09008b2009-10-19 02:17:23 +00003103 for (i = 0; i != e; ++i)
3104 if (Mask[i] >= 0)
3105 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003106
Nate Begemana09008b2009-10-19 02:17:23 +00003107 // All undef, not a palignr.
3108 if (i == e)
3109 return false;
3110
3111 // Determine if it's ok to perform a palignr with only the LHS, since we
3112 // don't have access to the actual shuffle elements to see if RHS is undef.
3113 bool Unary = Mask[i] < (int)e;
3114 bool NeedsUnary = false;
3115
3116 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003117
Nate Begemana09008b2009-10-19 02:17:23 +00003118 // Check the rest of the elements to see if they are consecutive.
3119 for (++i; i != e; ++i) {
3120 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003121 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003122 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003123
Nate Begemana09008b2009-10-19 02:17:23 +00003124 Unary = Unary && (m < (int)e);
3125 NeedsUnary = NeedsUnary || (m < s);
3126
3127 if (NeedsUnary && !Unary)
3128 return false;
3129 if (Unary && m != ((s+i) & (e-1)))
3130 return false;
3131 if (!Unary && m != (s+i))
3132 return false;
3133 }
3134 return true;
3135}
3136
3137bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3138 SmallVector<int, 8> M;
3139 N->getMask(M);
3140 return ::isPALIGNRMask(M, N->getValueType(0), true);
3141}
3142
Evan Cheng14aed5e2006-03-24 01:18:28 +00003143/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3144/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003145static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003146 int NumElems = VT.getVectorNumElements();
3147 if (NumElems != 2 && NumElems != 4)
3148 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003149
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 int Half = NumElems / 2;
3151 for (int i = 0; i < Half; ++i)
3152 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003153 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 for (int i = Half; i < NumElems; ++i)
3155 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003156 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003157
Evan Cheng14aed5e2006-03-24 01:18:28 +00003158 return true;
3159}
3160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3162 SmallVector<int, 8> M;
3163 N->getMask(M);
3164 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003165}
3166
Evan Cheng213d2cf2007-05-17 18:45:50 +00003167/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003168/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3169/// half elements to come from vector 1 (which would equal the dest.) and
3170/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003171static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003173
3174 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003176
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 int Half = NumElems / 2;
3178 for (int i = 0; i < Half; ++i)
3179 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003180 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003181 for (int i = Half; i < NumElems; ++i)
3182 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003183 return false;
3184 return true;
3185}
3186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3188 SmallVector<int, 8> M;
3189 N->getMask(M);
3190 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003191}
3192
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003193/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3194/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003195bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3196 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003197 return false;
3198
Evan Cheng2064a2b2006-03-28 06:50:32 +00003199 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3201 isUndefOrEqual(N->getMaskElt(1), 7) &&
3202 isUndefOrEqual(N->getMaskElt(2), 2) &&
3203 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003204}
3205
Nate Begeman0b10b912009-11-07 23:17:15 +00003206/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3207/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3208/// <2, 3, 2, 3>
3209bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3210 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003211
Nate Begeman0b10b912009-11-07 23:17:15 +00003212 if (NumElems != 4)
3213 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003214
Nate Begeman0b10b912009-11-07 23:17:15 +00003215 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3216 isUndefOrEqual(N->getMaskElt(1), 3) &&
3217 isUndefOrEqual(N->getMaskElt(2), 2) &&
3218 isUndefOrEqual(N->getMaskElt(3), 3);
3219}
3220
Evan Cheng5ced1d82006-04-06 23:23:56 +00003221/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3222/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003223bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3224 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003225
Evan Cheng5ced1d82006-04-06 23:23:56 +00003226 if (NumElems != 2 && NumElems != 4)
3227 return false;
3228
Evan Chengc5cdff22006-04-07 21:53:05 +00003229 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003230 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003231 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003232
Evan Chengc5cdff22006-04-07 21:53:05 +00003233 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003234 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003235 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003236
3237 return true;
3238}
3239
Nate Begeman0b10b912009-11-07 23:17:15 +00003240/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3241/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3242bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003243 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003244
David Greenea20244d2011-03-02 17:23:43 +00003245 if ((NumElems != 2 && NumElems != 4)
3246 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003247 return false;
3248
Evan Chengc5cdff22006-04-07 21:53:05 +00003249 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003250 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003251 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003252
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 for (unsigned i = 0; i < NumElems/2; ++i)
3254 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003255 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003256
3257 return true;
3258}
3259
Evan Cheng0038e592006-03-28 00:39:58 +00003260/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3261/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003262static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003263 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003264 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003265 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003266 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003267
David Greenea20244d2011-03-02 17:23:43 +00003268 // Handle vector lengths > 128 bits. Define a "section" as a set of
3269 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3270 // sections.
3271 unsigned NumSections = VT.getSizeInBits() / 128;
3272 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3273 unsigned NumSectionElts = NumElts / NumSections;
3274
3275 unsigned Start = 0;
3276 unsigned End = NumSectionElts;
3277 for (unsigned s = 0; s < NumSections; ++s) {
3278 for (unsigned i = Start, j = s * NumSectionElts;
3279 i != End;
3280 i += 2, ++j) {
3281 int BitI = Mask[i];
3282 int BitI1 = Mask[i+1];
3283 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003284 return false;
David Greenea20244d2011-03-02 17:23:43 +00003285 if (V2IsSplat) {
3286 if (!isUndefOrEqual(BitI1, NumElts))
3287 return false;
3288 } else {
3289 if (!isUndefOrEqual(BitI1, j + NumElts))
3290 return false;
3291 }
Evan Cheng39623da2006-04-20 08:58:49 +00003292 }
David Greenea20244d2011-03-02 17:23:43 +00003293 // Process the next 128 bits.
3294 Start += NumSectionElts;
3295 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003296 }
David Greenea20244d2011-03-02 17:23:43 +00003297
Evan Cheng0038e592006-03-28 00:39:58 +00003298 return true;
3299}
3300
Nate Begeman9008ca62009-04-27 18:41:29 +00003301bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3302 SmallVector<int, 8> M;
3303 N->getMask(M);
3304 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003305}
3306
Evan Cheng4fcb9222006-03-28 02:43:26 +00003307/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3308/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003309static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003310 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003311 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003312 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003313 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003314
Nate Begeman9008ca62009-04-27 18:41:29 +00003315 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3316 int BitI = Mask[i];
3317 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003318 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003319 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003320 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003321 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003322 return false;
3323 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003324 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003325 return false;
3326 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003327 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003328 return true;
3329}
3330
Nate Begeman9008ca62009-04-27 18:41:29 +00003331bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3332 SmallVector<int, 8> M;
3333 N->getMask(M);
3334 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003335}
3336
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003337/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3338/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3339/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003340static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003341 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003342 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003343 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003344
David Greenea20244d2011-03-02 17:23:43 +00003345 // Handle vector lengths > 128 bits. Define a "section" as a set of
3346 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3347 // sections.
3348 unsigned NumSections = VT.getSizeInBits() / 128;
3349 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3350 unsigned NumSectionElts = NumElems / NumSections;
3351
3352 for (unsigned s = 0; s < NumSections; ++s) {
3353 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3354 i != NumSectionElts * (s + 1);
3355 i += 2, ++j) {
3356 int BitI = Mask[i];
3357 int BitI1 = Mask[i+1];
3358
3359 if (!isUndefOrEqual(BitI, j))
3360 return false;
3361 if (!isUndefOrEqual(BitI1, j))
3362 return false;
3363 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003364 }
David Greenea20244d2011-03-02 17:23:43 +00003365
Rafael Espindola15684b22009-04-24 12:40:33 +00003366 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003367}
3368
Nate Begeman9008ca62009-04-27 18:41:29 +00003369bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3370 SmallVector<int, 8> M;
3371 N->getMask(M);
3372 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3373}
3374
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003375/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3376/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3377/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003378static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003380 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3381 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003382
Nate Begeman9008ca62009-04-27 18:41:29 +00003383 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3384 int BitI = Mask[i];
3385 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003386 if (!isUndefOrEqual(BitI, j))
3387 return false;
3388 if (!isUndefOrEqual(BitI1, j))
3389 return false;
3390 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003391 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003392}
3393
Nate Begeman9008ca62009-04-27 18:41:29 +00003394bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3395 SmallVector<int, 8> M;
3396 N->getMask(M);
3397 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3398}
3399
Evan Cheng017dcc62006-04-21 01:05:10 +00003400/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3401/// specifies a shuffle of elements that is suitable for input to MOVSS,
3402/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003403static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003404 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003405 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003406
3407 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003408
Nate Begeman9008ca62009-04-27 18:41:29 +00003409 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003410 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003411
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 for (int i = 1; i < NumElts; ++i)
3413 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003414 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003415
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003416 return true;
3417}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003418
Nate Begeman9008ca62009-04-27 18:41:29 +00003419bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3420 SmallVector<int, 8> M;
3421 N->getMask(M);
3422 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003423}
3424
Evan Cheng017dcc62006-04-21 01:05:10 +00003425/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3426/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003427/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003428static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 bool V2IsSplat = false, bool V2IsUndef = false) {
3430 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003431 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003432 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003433
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003435 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003436
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 for (int i = 1; i < NumOps; ++i)
3438 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3439 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3440 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003441 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003442
Evan Cheng39623da2006-04-20 08:58:49 +00003443 return true;
3444}
3445
Nate Begeman9008ca62009-04-27 18:41:29 +00003446static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003447 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 SmallVector<int, 8> M;
3449 N->getMask(M);
3450 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003451}
3452
Evan Chengd9539472006-04-14 21:59:03 +00003453/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3454/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003455bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3456 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003457 return false;
3458
3459 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003460 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003461 int Elt = N->getMaskElt(i);
3462 if (Elt >= 0 && Elt != 1)
3463 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003464 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003465
3466 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003467 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003468 int Elt = N->getMaskElt(i);
3469 if (Elt >= 0 && Elt != 3)
3470 return false;
3471 if (Elt == 3)
3472 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003473 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003474 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003476 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003477}
3478
3479/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3480/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003481bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3482 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003483 return false;
3484
3485 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 for (unsigned i = 0; i < 2; ++i)
3487 if (N->getMaskElt(i) > 0)
3488 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003489
3490 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003491 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003492 int Elt = N->getMaskElt(i);
3493 if (Elt >= 0 && Elt != 2)
3494 return false;
3495 if (Elt == 2)
3496 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003497 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003499 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003500}
3501
Evan Cheng0b457f02008-09-25 20:50:48 +00003502/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3503/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003504bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3505 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003506
Nate Begeman9008ca62009-04-27 18:41:29 +00003507 for (int i = 0; i < e; ++i)
3508 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003509 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003510 for (int i = 0; i < e; ++i)
3511 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003512 return false;
3513 return true;
3514}
3515
David Greenec38a03e2011-02-03 15:50:00 +00003516/// isVEXTRACTF128Index - Return true if the specified
3517/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3518/// suitable for input to VEXTRACTF128.
3519bool X86::isVEXTRACTF128Index(SDNode *N) {
3520 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3521 return false;
3522
3523 // The index should be aligned on a 128-bit boundary.
3524 uint64_t Index =
3525 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3526
3527 unsigned VL = N->getValueType(0).getVectorNumElements();
3528 unsigned VBits = N->getValueType(0).getSizeInBits();
3529 unsigned ElSize = VBits / VL;
3530 bool Result = (Index * ElSize) % 128 == 0;
3531
3532 return Result;
3533}
3534
David Greeneccacdc12011-02-04 16:08:29 +00003535/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3536/// operand specifies a subvector insert that is suitable for input to
3537/// VINSERTF128.
3538bool X86::isVINSERTF128Index(SDNode *N) {
3539 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3540 return false;
3541
3542 // The index should be aligned on a 128-bit boundary.
3543 uint64_t Index =
3544 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3545
3546 unsigned VL = N->getValueType(0).getVectorNumElements();
3547 unsigned VBits = N->getValueType(0).getSizeInBits();
3548 unsigned ElSize = VBits / VL;
3549 bool Result = (Index * ElSize) % 128 == 0;
3550
3551 return Result;
3552}
3553
Evan Cheng63d33002006-03-22 08:01:21 +00003554/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003555/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003556unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003557 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3558 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3559
Evan Chengb9df0ca2006-03-22 02:53:00 +00003560 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3561 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003562 for (int i = 0; i < NumOperands; ++i) {
3563 int Val = SVOp->getMaskElt(NumOperands-i-1);
3564 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003565 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003566 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003567 if (i != NumOperands - 1)
3568 Mask <<= Shift;
3569 }
Evan Cheng63d33002006-03-22 08:01:21 +00003570 return Mask;
3571}
3572
Evan Cheng506d3df2006-03-29 23:07:14 +00003573/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003574/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003575unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003576 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003577 unsigned Mask = 0;
3578 // 8 nodes, but we only care about the last 4.
3579 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003580 int Val = SVOp->getMaskElt(i);
3581 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003582 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003583 if (i != 4)
3584 Mask <<= 2;
3585 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003586 return Mask;
3587}
3588
3589/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003590/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003591unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003592 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003593 unsigned Mask = 0;
3594 // 8 nodes, but we only care about the first 4.
3595 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003596 int Val = SVOp->getMaskElt(i);
3597 if (Val >= 0)
3598 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003599 if (i != 0)
3600 Mask <<= 2;
3601 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003602 return Mask;
3603}
3604
Nate Begemana09008b2009-10-19 02:17:23 +00003605/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3606/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3607unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3608 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3609 EVT VVT = N->getValueType(0);
3610 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3611 int Val = 0;
3612
3613 unsigned i, e;
3614 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3615 Val = SVOp->getMaskElt(i);
3616 if (Val >= 0)
3617 break;
3618 }
3619 return (Val - i) * EltSize;
3620}
3621
David Greenec38a03e2011-02-03 15:50:00 +00003622/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3623/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3624/// instructions.
3625unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3626 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3627 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3628
3629 uint64_t Index =
3630 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3631
3632 EVT VecVT = N->getOperand(0).getValueType();
3633 EVT ElVT = VecVT.getVectorElementType();
3634
3635 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00003636 return Index / NumElemsPerChunk;
3637}
3638
David Greeneccacdc12011-02-04 16:08:29 +00003639/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3640/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3641/// instructions.
3642unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3643 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3644 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3645
3646 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003647 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003648
3649 EVT VecVT = N->getValueType(0);
3650 EVT ElVT = VecVT.getVectorElementType();
3651
3652 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00003653 return Index / NumElemsPerChunk;
3654}
3655
Evan Cheng37b73872009-07-30 08:33:02 +00003656/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3657/// constant +0.0.
3658bool X86::isZeroNode(SDValue Elt) {
3659 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003660 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003661 (isa<ConstantFPSDNode>(Elt) &&
3662 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3663}
3664
Nate Begeman9008ca62009-04-27 18:41:29 +00003665/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3666/// their permute mask.
3667static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3668 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003669 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003670 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003671 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003672
Nate Begeman5a5ca152009-04-29 05:20:52 +00003673 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003674 int idx = SVOp->getMaskElt(i);
3675 if (idx < 0)
3676 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003677 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003678 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003679 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003680 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003681 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003682 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3683 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003684}
3685
Evan Cheng779ccea2007-12-07 21:30:01 +00003686/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3687/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003688static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003689 unsigned NumElems = VT.getVectorNumElements();
3690 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003691 int idx = Mask[i];
3692 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003693 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003694 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003695 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003696 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003697 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003698 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003699}
3700
Evan Cheng533a0aa2006-04-19 20:35:22 +00003701/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3702/// match movhlps. The lower half elements should come from upper half of
3703/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003704/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003705static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3706 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003707 return false;
3708 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003709 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003710 return false;
3711 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003713 return false;
3714 return true;
3715}
3716
Evan Cheng5ced1d82006-04-06 23:23:56 +00003717/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003718/// is promoted to a vector. It also returns the LoadSDNode by reference if
3719/// required.
3720static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003721 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3722 return false;
3723 N = N->getOperand(0).getNode();
3724 if (!ISD::isNON_EXTLoad(N))
3725 return false;
3726 if (LD)
3727 *LD = cast<LoadSDNode>(N);
3728 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003729}
3730
Evan Cheng533a0aa2006-04-19 20:35:22 +00003731/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3732/// match movlp{s|d}. The lower half elements should come from lower half of
3733/// V1 (and in order), and the upper half elements should come from the upper
3734/// half of V2 (and in order). And since V1 will become the source of the
3735/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003736static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3737 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003738 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003739 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003740 // Is V2 is a vector load, don't do this transformation. We will try to use
3741 // load folding shufps op.
3742 if (ISD::isNON_EXTLoad(V2))
3743 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003744
Nate Begeman5a5ca152009-04-29 05:20:52 +00003745 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003746
Evan Cheng533a0aa2006-04-19 20:35:22 +00003747 if (NumElems != 2 && NumElems != 4)
3748 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003749 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003750 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003751 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003752 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003753 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003754 return false;
3755 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003756}
3757
Evan Cheng39623da2006-04-20 08:58:49 +00003758/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3759/// all the same.
3760static bool isSplatVector(SDNode *N) {
3761 if (N->getOpcode() != ISD::BUILD_VECTOR)
3762 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003763
Dan Gohman475871a2008-07-27 21:46:04 +00003764 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003765 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3766 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003767 return false;
3768 return true;
3769}
3770
Evan Cheng213d2cf2007-05-17 18:45:50 +00003771/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003772/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003773/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003774static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003775 SDValue V1 = N->getOperand(0);
3776 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003777 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3778 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003779 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003780 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003781 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003782 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3783 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003784 if (Opc != ISD::BUILD_VECTOR ||
3785 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 return false;
3787 } else if (Idx >= 0) {
3788 unsigned Opc = V1.getOpcode();
3789 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3790 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003791 if (Opc != ISD::BUILD_VECTOR ||
3792 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003793 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003794 }
3795 }
3796 return true;
3797}
3798
3799/// getZeroVector - Returns a vector of specified type with all zero elements.
3800///
Owen Andersone50ed302009-08-10 22:56:29 +00003801static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003802 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003803 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003804
Dale Johannesen0488fb62010-09-30 23:57:10 +00003805 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003806 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003807 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003808 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003809 if (HasSSE2) { // SSE2
3810 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3811 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3812 } else { // SSE1
3813 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3814 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3815 }
3816 } else if (VT.getSizeInBits() == 256) { // AVX
3817 // 256-bit logic and arithmetic instructions in AVX are
3818 // all floating-point, no support for integer ops. Default
3819 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003820 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003821 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3822 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003823 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003824 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003825}
3826
Chris Lattner8a594482007-11-25 00:24:49 +00003827/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003828/// Always build ones vectors as <4 x i32> or <8 x i32> bitcasted to
3829/// their original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00003830static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003831 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003832 assert((VT.is128BitVector() || VT.is256BitVector())
3833 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003834
Owen Anderson825b72b2009-08-11 20:47:22 +00003835 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003836
Dan Gohman475871a2008-07-27 21:46:04 +00003837 SDValue Vec;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003838 if (VT.is256BitVector()) {
3839 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3840 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
3841 } else
3842 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003843 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003844}
3845
Evan Cheng39623da2006-04-20 08:58:49 +00003846/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3847/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003848static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003849 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003850 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003851
Evan Cheng39623da2006-04-20 08:58:49 +00003852 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003853 SmallVector<int, 8> MaskVec;
3854 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003855
Nate Begeman5a5ca152009-04-29 05:20:52 +00003856 for (unsigned i = 0; i != NumElems; ++i) {
3857 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003858 MaskVec[i] = NumElems;
3859 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003860 }
Evan Cheng39623da2006-04-20 08:58:49 +00003861 }
Evan Cheng39623da2006-04-20 08:58:49 +00003862 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003863 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3864 SVOp->getOperand(1), &MaskVec[0]);
3865 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003866}
3867
Evan Cheng017dcc62006-04-21 01:05:10 +00003868/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3869/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003870static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 SDValue V2) {
3872 unsigned NumElems = VT.getVectorNumElements();
3873 SmallVector<int, 8> Mask;
3874 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003875 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003876 Mask.push_back(i);
3877 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003878}
3879
Nate Begeman9008ca62009-04-27 18:41:29 +00003880/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003881static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003882 SDValue V2) {
3883 unsigned NumElems = VT.getVectorNumElements();
3884 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003885 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003886 Mask.push_back(i);
3887 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003888 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003890}
3891
Nate Begeman9008ca62009-04-27 18:41:29 +00003892/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003893static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003894 SDValue V2) {
3895 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003896 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003897 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003898 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003899 Mask.push_back(i + Half);
3900 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003901 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003902 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003903}
3904
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003905/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3906static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003907 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003908 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 DebugLoc dl = SV->getDebugLoc();
3910 SDValue V1 = SV->getOperand(0);
3911 int NumElems = VT.getVectorNumElements();
3912 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003913
Nate Begeman9008ca62009-04-27 18:41:29 +00003914 // unpack elements to the correct location
3915 while (NumElems > 4) {
3916 if (EltNo < NumElems/2) {
3917 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3918 } else {
3919 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3920 EltNo -= NumElems/2;
3921 }
3922 NumElems >>= 1;
3923 }
Eric Christopherfd179292009-08-27 18:07:15 +00003924
Nate Begeman9008ca62009-04-27 18:41:29 +00003925 // Perform the splat.
3926 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003927 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003928 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003929 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003930}
3931
Evan Chengba05f722006-04-21 23:03:30 +00003932/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003933/// vector of zero or undef vector. This produces a shuffle where the low
3934/// element of V2 is swizzled into the zero/undef vector, landing at element
3935/// 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 +00003936static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003937 bool isZero, bool HasSSE2,
3938 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003939 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003940 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3942 unsigned NumElems = VT.getVectorNumElements();
3943 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003944 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003945 // If this is the insertion idx, put the low elt of V2 here.
3946 MaskVec.push_back(i == Idx ? NumElems : i);
3947 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003948}
3949
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003950/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3951/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003952static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3953 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003954 if (Depth == 6)
3955 return SDValue(); // Limit search depth.
3956
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003957 SDValue V = SDValue(N, 0);
3958 EVT VT = V.getValueType();
3959 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003960
3961 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3962 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3963 Index = SV->getMaskElt(Index);
3964
3965 if (Index < 0)
3966 return DAG.getUNDEF(VT.getVectorElementType());
3967
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003968 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003969 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003970 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003971 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003972
3973 // Recurse into target specific vector shuffles to find scalars.
3974 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003975 int NumElems = VT.getVectorNumElements();
3976 SmallVector<unsigned, 16> ShuffleMask;
3977 SDValue ImmN;
3978
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003979 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003980 case X86ISD::SHUFPS:
3981 case X86ISD::SHUFPD:
3982 ImmN = N->getOperand(N->getNumOperands()-1);
3983 DecodeSHUFPSMask(NumElems,
3984 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3985 ShuffleMask);
3986 break;
3987 case X86ISD::PUNPCKHBW:
3988 case X86ISD::PUNPCKHWD:
3989 case X86ISD::PUNPCKHDQ:
3990 case X86ISD::PUNPCKHQDQ:
3991 DecodePUNPCKHMask(NumElems, ShuffleMask);
3992 break;
3993 case X86ISD::UNPCKHPS:
3994 case X86ISD::UNPCKHPD:
3995 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3996 break;
3997 case X86ISD::PUNPCKLBW:
3998 case X86ISD::PUNPCKLWD:
3999 case X86ISD::PUNPCKLDQ:
4000 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004001 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004002 break;
4003 case X86ISD::UNPCKLPS:
4004 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004005 case X86ISD::VUNPCKLPS:
4006 case X86ISD::VUNPCKLPD:
4007 case X86ISD::VUNPCKLPSY:
4008 case X86ISD::VUNPCKLPDY:
4009 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004010 break;
4011 case X86ISD::MOVHLPS:
4012 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4013 break;
4014 case X86ISD::MOVLHPS:
4015 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4016 break;
4017 case X86ISD::PSHUFD:
4018 ImmN = N->getOperand(N->getNumOperands()-1);
4019 DecodePSHUFMask(NumElems,
4020 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4021 ShuffleMask);
4022 break;
4023 case X86ISD::PSHUFHW:
4024 ImmN = N->getOperand(N->getNumOperands()-1);
4025 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4026 ShuffleMask);
4027 break;
4028 case X86ISD::PSHUFLW:
4029 ImmN = N->getOperand(N->getNumOperands()-1);
4030 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4031 ShuffleMask);
4032 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004033 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004034 case X86ISD::MOVSD: {
4035 // The index 0 always comes from the first element of the second source,
4036 // this is why MOVSS and MOVSD are used in the first place. The other
4037 // elements come from the other positions of the first source vector.
4038 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004039 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4040 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004041 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004042 default:
4043 assert("not implemented for target shuffle node");
4044 return SDValue();
4045 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004046
4047 Index = ShuffleMask[Index];
4048 if (Index < 0)
4049 return DAG.getUNDEF(VT.getVectorElementType());
4050
4051 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4052 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4053 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004054 }
4055
4056 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004057 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004058 V = V.getOperand(0);
4059 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004060 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004061
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004062 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004063 return SDValue();
4064 }
4065
4066 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4067 return (Index == 0) ? V.getOperand(0)
4068 : DAG.getUNDEF(VT.getVectorElementType());
4069
4070 if (V.getOpcode() == ISD::BUILD_VECTOR)
4071 return V.getOperand(Index);
4072
4073 return SDValue();
4074}
4075
4076/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4077/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004078/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004079static
4080unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4081 bool ZerosFromLeft, SelectionDAG &DAG) {
4082 int i = 0;
4083
4084 while (i < NumElems) {
4085 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004086 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004087 if (!(Elt.getNode() &&
4088 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4089 break;
4090 ++i;
4091 }
4092
4093 return i;
4094}
4095
4096/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4097/// MaskE correspond consecutively to elements from one of the vector operands,
4098/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4099static
4100bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4101 int OpIdx, int NumElems, unsigned &OpNum) {
4102 bool SeenV1 = false;
4103 bool SeenV2 = false;
4104
4105 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4106 int Idx = SVOp->getMaskElt(i);
4107 // Ignore undef indicies
4108 if (Idx < 0)
4109 continue;
4110
4111 if (Idx < NumElems)
4112 SeenV1 = true;
4113 else
4114 SeenV2 = true;
4115
4116 // Only accept consecutive elements from the same vector
4117 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4118 return false;
4119 }
4120
4121 OpNum = SeenV1 ? 0 : 1;
4122 return true;
4123}
4124
4125/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4126/// logical left shift of a vector.
4127static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4128 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4129 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4130 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4131 false /* check zeros from right */, DAG);
4132 unsigned OpSrc;
4133
4134 if (!NumZeros)
4135 return false;
4136
4137 // Considering the elements in the mask that are not consecutive zeros,
4138 // check if they consecutively come from only one of the source vectors.
4139 //
4140 // V1 = {X, A, B, C} 0
4141 // \ \ \ /
4142 // vector_shuffle V1, V2 <1, 2, 3, X>
4143 //
4144 if (!isShuffleMaskConsecutive(SVOp,
4145 0, // Mask Start Index
4146 NumElems-NumZeros-1, // Mask End Index
4147 NumZeros, // Where to start looking in the src vector
4148 NumElems, // Number of elements in vector
4149 OpSrc)) // Which source operand ?
4150 return false;
4151
4152 isLeft = false;
4153 ShAmt = NumZeros;
4154 ShVal = SVOp->getOperand(OpSrc);
4155 return true;
4156}
4157
4158/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4159/// logical left shift of a vector.
4160static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4161 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4162 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4163 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4164 true /* check zeros from left */, DAG);
4165 unsigned OpSrc;
4166
4167 if (!NumZeros)
4168 return false;
4169
4170 // Considering the elements in the mask that are not consecutive zeros,
4171 // check if they consecutively come from only one of the source vectors.
4172 //
4173 // 0 { A, B, X, X } = V2
4174 // / \ / /
4175 // vector_shuffle V1, V2 <X, X, 4, 5>
4176 //
4177 if (!isShuffleMaskConsecutive(SVOp,
4178 NumZeros, // Mask Start Index
4179 NumElems-1, // Mask End Index
4180 0, // Where to start looking in the src vector
4181 NumElems, // Number of elements in vector
4182 OpSrc)) // Which source operand ?
4183 return false;
4184
4185 isLeft = true;
4186 ShAmt = NumZeros;
4187 ShVal = SVOp->getOperand(OpSrc);
4188 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004189}
4190
4191/// isVectorShift - Returns true if the shuffle can be implemented as a
4192/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004193static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004194 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004195 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4196 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4197 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004198
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004199 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004200}
4201
Evan Chengc78d3b42006-04-24 18:01:45 +00004202/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4203///
Dan Gohman475871a2008-07-27 21:46:04 +00004204static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004205 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004206 SelectionDAG &DAG,
4207 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004208 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004209 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004210
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004211 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004212 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004213 bool First = true;
4214 for (unsigned i = 0; i < 16; ++i) {
4215 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4216 if (ThisIsNonZero && First) {
4217 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004219 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004221 First = false;
4222 }
4223
4224 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004225 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004226 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4227 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004228 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004229 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004230 }
4231 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4233 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4234 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004235 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004237 } else
4238 ThisElt = LastElt;
4239
Gabor Greifba36cb52008-08-28 21:40:38 +00004240 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004241 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004242 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004243 }
4244 }
4245
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004246 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004247}
4248
Bill Wendlinga348c562007-03-22 18:42:45 +00004249/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004250///
Dan Gohman475871a2008-07-27 21:46:04 +00004251static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004252 unsigned NumNonZero, unsigned NumZero,
4253 SelectionDAG &DAG,
4254 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004255 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004256 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004257
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004258 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004259 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004260 bool First = true;
4261 for (unsigned i = 0; i < 8; ++i) {
4262 bool isNonZero = (NonZeros & (1 << i)) != 0;
4263 if (isNonZero) {
4264 if (First) {
4265 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004266 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004267 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004268 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004269 First = false;
4270 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004271 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004272 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004273 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004274 }
4275 }
4276
4277 return V;
4278}
4279
Evan Chengf26ffe92008-05-29 08:22:04 +00004280/// getVShift - Return a vector logical shift node.
4281///
Owen Andersone50ed302009-08-10 22:56:29 +00004282static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004283 unsigned NumBits, SelectionDAG &DAG,
4284 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004285 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004286 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004287 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4288 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004289 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004290 DAG.getConstant(NumBits,
4291 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004292}
4293
Dan Gohman475871a2008-07-27 21:46:04 +00004294SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004295X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004296 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004297
Evan Chengc3630942009-12-09 21:00:30 +00004298 // Check if the scalar load can be widened into a vector load. And if
4299 // the address is "base + cst" see if the cst can be "absorbed" into
4300 // the shuffle mask.
4301 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4302 SDValue Ptr = LD->getBasePtr();
4303 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4304 return SDValue();
4305 EVT PVT = LD->getValueType(0);
4306 if (PVT != MVT::i32 && PVT != MVT::f32)
4307 return SDValue();
4308
4309 int FI = -1;
4310 int64_t Offset = 0;
4311 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4312 FI = FINode->getIndex();
4313 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004314 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004315 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4316 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4317 Offset = Ptr.getConstantOperandVal(1);
4318 Ptr = Ptr.getOperand(0);
4319 } else {
4320 return SDValue();
4321 }
4322
4323 SDValue Chain = LD->getChain();
4324 // Make sure the stack object alignment is at least 16.
4325 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4326 if (DAG.InferPtrAlignment(Ptr) < 16) {
4327 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004328 // Can't change the alignment. FIXME: It's possible to compute
4329 // the exact stack offset and reference FI + adjust offset instead.
4330 // If someone *really* cares about this. That's the way to implement it.
4331 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004332 } else {
4333 MFI->setObjectAlignment(FI, 16);
4334 }
4335 }
4336
4337 // (Offset % 16) must be multiple of 4. Then address is then
4338 // Ptr + (Offset & ~15).
4339 if (Offset < 0)
4340 return SDValue();
4341 if ((Offset % 16) & 3)
4342 return SDValue();
4343 int64_t StartOffset = Offset & ~15;
4344 if (StartOffset)
4345 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4346 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4347
4348 int EltNo = (Offset - StartOffset) >> 2;
4349 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4350 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004351 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4352 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004353 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004354 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004355 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4356 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004357 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004358 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004359 }
4360
4361 return SDValue();
4362}
4363
Michael J. Spencerec38de22010-10-10 22:04:20 +00004364/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4365/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004366/// load which has the same value as a build_vector whose operands are 'elts'.
4367///
4368/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004369///
Nate Begeman1449f292010-03-24 22:19:06 +00004370/// FIXME: we'd also like to handle the case where the last elements are zero
4371/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4372/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004373static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004374 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004375 EVT EltVT = VT.getVectorElementType();
4376 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004377
Nate Begemanfdea31a2010-03-24 20:49:50 +00004378 LoadSDNode *LDBase = NULL;
4379 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004380
Nate Begeman1449f292010-03-24 22:19:06 +00004381 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004382 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004383 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004384 for (unsigned i = 0; i < NumElems; ++i) {
4385 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004386
Nate Begemanfdea31a2010-03-24 20:49:50 +00004387 if (!Elt.getNode() ||
4388 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4389 return SDValue();
4390 if (!LDBase) {
4391 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4392 return SDValue();
4393 LDBase = cast<LoadSDNode>(Elt.getNode());
4394 LastLoadedElt = i;
4395 continue;
4396 }
4397 if (Elt.getOpcode() == ISD::UNDEF)
4398 continue;
4399
4400 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4401 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4402 return SDValue();
4403 LastLoadedElt = i;
4404 }
Nate Begeman1449f292010-03-24 22:19:06 +00004405
4406 // If we have found an entire vector of loads and undefs, then return a large
4407 // load of the entire vector width starting at the base pointer. If we found
4408 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004409 if (LastLoadedElt == NumElems - 1) {
4410 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004411 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004412 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004413 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004414 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004415 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004416 LDBase->isVolatile(), LDBase->isNonTemporal(),
4417 LDBase->getAlignment());
4418 } else if (NumElems == 4 && LastLoadedElt == 1) {
4419 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4420 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004421 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4422 Ops, 2, MVT::i32,
4423 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004424 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004425 }
4426 return SDValue();
4427}
4428
Evan Chengc3630942009-12-09 21:00:30 +00004429SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004430X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004431 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004432
David Greenef125a292011-02-08 19:04:41 +00004433 EVT VT = Op.getValueType();
4434 EVT ExtVT = VT.getVectorElementType();
4435
4436 unsigned NumElems = Op.getNumOperands();
4437
4438 // For AVX-length vectors, build the individual 128-bit pieces and
4439 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004440 if (VT.getSizeInBits() > 256 &&
4441 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004442 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4443 SmallVector<SDValue, 8> V;
4444 V.resize(NumElems);
4445 for (unsigned i = 0; i < NumElems; ++i) {
4446 V[i] = Op.getOperand(i);
4447 }
Owen Anderson95771af2011-02-25 21:41:48 +00004448
David Greenef125a292011-02-08 19:04:41 +00004449 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4450
4451 // Build the lower subvector.
4452 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4453 // Build the upper subvector.
4454 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4455 NumElems/2);
4456
4457 return ConcatVectors(Lower, Upper, DAG);
4458 }
4459
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004460 // All zero's:
4461 // - pxor (SSE2), xorps (SSE1), vpxor (128 AVX), xorp[s|d] (256 AVX)
4462 // All one's:
4463 // - pcmpeqd (SSE2 and 128 AVX), fallback to constant pools (256 AVX)
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004464 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004465 ISD::isBuildVectorAllOnes(Op.getNode())) {
4466 // Canonicalize this to <4 x i32> or <8 x 32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004467 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4468 // eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004469 if (Op.getValueType() == MVT::v4i32 ||
4470 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004471 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004472
Gabor Greifba36cb52008-08-28 21:40:38 +00004473 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004474 return getOnesVector(Op.getValueType(), DAG, dl);
4475 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004476 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004477
Owen Andersone50ed302009-08-10 22:56:29 +00004478 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004479
Evan Cheng0db9fe62006-04-25 20:13:52 +00004480 unsigned NumZero = 0;
4481 unsigned NumNonZero = 0;
4482 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004483 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004484 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004485 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004486 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004487 if (Elt.getOpcode() == ISD::UNDEF)
4488 continue;
4489 Values.insert(Elt);
4490 if (Elt.getOpcode() != ISD::Constant &&
4491 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004492 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004493 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004494 NumZero++;
4495 else {
4496 NonZeros |= (1 << i);
4497 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004498 }
4499 }
4500
Chris Lattner97a2a562010-08-26 05:24:29 +00004501 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4502 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004503 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004504
Chris Lattner67f453a2008-03-09 05:42:06 +00004505 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004506 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004507 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004508 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004509
Chris Lattner62098042008-03-09 01:05:04 +00004510 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4511 // the value are obviously zero, truncate the value to i32 and do the
4512 // insertion that way. Only do this if the value is non-constant or if the
4513 // value is a constant being inserted into element 0. It is cheaper to do
4514 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004515 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004516 (!IsAllConstants || Idx == 0)) {
4517 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004518 // Handle SSE only.
4519 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4520 EVT VecVT = MVT::v4i32;
4521 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004522
Chris Lattner62098042008-03-09 01:05:04 +00004523 // Truncate the value (which may itself be a constant) to i32, and
4524 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004525 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004526 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004527 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4528 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004529
Chris Lattner62098042008-03-09 01:05:04 +00004530 // Now we have our 32-bit value zero extended in the low element of
4531 // a vector. If Idx != 0, swizzle it into place.
4532 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004533 SmallVector<int, 4> Mask;
4534 Mask.push_back(Idx);
4535 for (unsigned i = 1; i != VecElts; ++i)
4536 Mask.push_back(i);
4537 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004538 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004539 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004540 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004541 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004542 }
4543 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004544
Chris Lattner19f79692008-03-08 22:59:52 +00004545 // If we have a constant or non-constant insertion into the low element of
4546 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4547 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004548 // depending on what the source datatype is.
4549 if (Idx == 0) {
4550 if (NumZero == 0) {
4551 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004552 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4553 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004554 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4555 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4556 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4557 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004558 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4559 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004560 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4561 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004562 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4563 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4564 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004565 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004566 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004567 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004568
4569 // Is it a vector logical left shift?
4570 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004571 X86::isZeroNode(Op.getOperand(0)) &&
4572 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004573 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004574 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004575 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004576 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004577 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004578 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004579
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004580 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004581 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004582
Chris Lattner19f79692008-03-08 22:59:52 +00004583 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4584 // is a non-constant being inserted into an element other than the low one,
4585 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4586 // movd/movss) to move this into the low element, then shuffle it into
4587 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004588 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004589 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004590
Evan Cheng0db9fe62006-04-25 20:13:52 +00004591 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004592 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4593 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004594 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004595 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004596 MaskVec.push_back(i == Idx ? 0 : 1);
4597 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 }
4599 }
4600
Chris Lattner67f453a2008-03-09 05:42:06 +00004601 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004602 if (Values.size() == 1) {
4603 if (EVTBits == 32) {
4604 // Instead of a shuffle like this:
4605 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4606 // Check if it's possible to issue this instead.
4607 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4608 unsigned Idx = CountTrailingZeros_32(NonZeros);
4609 SDValue Item = Op.getOperand(Idx);
4610 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4611 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4612 }
Dan Gohman475871a2008-07-27 21:46:04 +00004613 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004614 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004615
Dan Gohmana3941172007-07-24 22:55:08 +00004616 // A vector full of immediates; various special cases are already
4617 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004618 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004619 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004620
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004621 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004622 if (EVTBits == 64) {
4623 if (NumNonZero == 1) {
4624 // One half is zero or undef.
4625 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004626 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004627 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004628 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4629 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004630 }
Dan Gohman475871a2008-07-27 21:46:04 +00004631 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004632 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004633
4634 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004635 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004636 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004637 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004638 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004639 }
4640
Bill Wendling826f36f2007-03-28 00:57:11 +00004641 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004642 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004643 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004644 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004645 }
4646
4647 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004648 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004649 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004650 if (NumElems == 4 && NumZero > 0) {
4651 for (unsigned i = 0; i < 4; ++i) {
4652 bool isZero = !(NonZeros & (1 << i));
4653 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004654 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004655 else
Dale Johannesenace16102009-02-03 19:33:06 +00004656 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004657 }
4658
4659 for (unsigned i = 0; i < 2; ++i) {
4660 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4661 default: break;
4662 case 0:
4663 V[i] = V[i*2]; // Must be a zero vector.
4664 break;
4665 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004666 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004667 break;
4668 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004669 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004670 break;
4671 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004672 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004673 break;
4674 }
4675 }
4676
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004678 bool Reverse = (NonZeros & 0x3) == 2;
4679 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004680 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004681 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4682 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004683 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4684 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004685 }
4686
Nate Begemanfdea31a2010-03-24 20:49:50 +00004687 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4688 // Check for a build vector of consecutive loads.
4689 for (unsigned i = 0; i < NumElems; ++i)
4690 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004691
Nate Begemanfdea31a2010-03-24 20:49:50 +00004692 // Check for elements which are consecutive loads.
4693 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4694 if (LD.getNode())
4695 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004696
4697 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004698 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004699 SDValue Result;
4700 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4701 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4702 else
4703 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004704
Chris Lattner24faf612010-08-28 17:59:08 +00004705 for (unsigned i = 1; i < NumElems; ++i) {
4706 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4707 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004708 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004709 }
4710 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004711 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004712
Chris Lattner6e80e442010-08-28 17:15:43 +00004713 // Otherwise, expand into a number of unpckl*, start by extending each of
4714 // our (non-undef) elements to the full vector width with the element in the
4715 // bottom slot of the vector (which generates no code for SSE).
4716 for (unsigned i = 0; i < NumElems; ++i) {
4717 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4718 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4719 else
4720 V[i] = DAG.getUNDEF(VT);
4721 }
4722
4723 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004724 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4725 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4726 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004727 unsigned EltStride = NumElems >> 1;
4728 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004729 for (unsigned i = 0; i < EltStride; ++i) {
4730 // If V[i+EltStride] is undef and this is the first round of mixing,
4731 // then it is safe to just drop this shuffle: V[i] is already in the
4732 // right place, the one element (since it's the first round) being
4733 // inserted as undef can be dropped. This isn't safe for successive
4734 // rounds because they will permute elements within both vectors.
4735 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4736 EltStride == NumElems/2)
4737 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004738
Chris Lattner6e80e442010-08-28 17:15:43 +00004739 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004740 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004741 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004742 }
4743 return V[0];
4744 }
Dan Gohman475871a2008-07-27 21:46:04 +00004745 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746}
4747
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004748SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004749X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004750 // We support concatenate two MMX registers and place them in a MMX
4751 // register. This is better than doing a stack convert.
4752 DebugLoc dl = Op.getDebugLoc();
4753 EVT ResVT = Op.getValueType();
4754 assert(Op.getNumOperands() == 2);
4755 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4756 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4757 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004758 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004759 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4760 InVec = Op.getOperand(1);
4761 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4762 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004763 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004764 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4765 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4766 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004767 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004768 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4769 Mask[0] = 0; Mask[1] = 2;
4770 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4771 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004772 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004773}
4774
Nate Begemanb9a47b82009-02-23 08:49:38 +00004775// v8i16 shuffles - Prefer shuffles in the following order:
4776// 1. [all] pshuflw, pshufhw, optional move
4777// 2. [ssse3] 1 x pshufb
4778// 3. [ssse3] 2 x pshufb + 1 x por
4779// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004780SDValue
4781X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4782 SelectionDAG &DAG) const {
4783 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004784 SDValue V1 = SVOp->getOperand(0);
4785 SDValue V2 = SVOp->getOperand(1);
4786 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004788
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 // Determine if more than 1 of the words in each of the low and high quadwords
4790 // of the result come from the same quadword of one of the two inputs. Undef
4791 // mask values count as coming from any quadword, for better codegen.
4792 SmallVector<unsigned, 4> LoQuad(4);
4793 SmallVector<unsigned, 4> HiQuad(4);
4794 BitVector InputQuads(4);
4795 for (unsigned i = 0; i < 8; ++i) {
4796 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004797 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004798 MaskVals.push_back(EltIdx);
4799 if (EltIdx < 0) {
4800 ++Quad[0];
4801 ++Quad[1];
4802 ++Quad[2];
4803 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004804 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 }
4806 ++Quad[EltIdx / 4];
4807 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004808 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004809
Nate Begemanb9a47b82009-02-23 08:49:38 +00004810 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004811 unsigned MaxQuad = 1;
4812 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 if (LoQuad[i] > MaxQuad) {
4814 BestLoQuad = i;
4815 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004816 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004817 }
4818
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004820 MaxQuad = 1;
4821 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004822 if (HiQuad[i] > MaxQuad) {
4823 BestHiQuad = i;
4824 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004825 }
4826 }
4827
Nate Begemanb9a47b82009-02-23 08:49:38 +00004828 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004829 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004830 // single pshufb instruction is necessary. If There are more than 2 input
4831 // quads, disable the next transformation since it does not help SSSE3.
4832 bool V1Used = InputQuads[0] || InputQuads[1];
4833 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004834 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 if (InputQuads.count() == 2 && V1Used && V2Used) {
4836 BestLoQuad = InputQuads.find_first();
4837 BestHiQuad = InputQuads.find_next(BestLoQuad);
4838 }
4839 if (InputQuads.count() > 2) {
4840 BestLoQuad = -1;
4841 BestHiQuad = -1;
4842 }
4843 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004844
Nate Begemanb9a47b82009-02-23 08:49:38 +00004845 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4846 // the shuffle mask. If a quad is scored as -1, that means that it contains
4847 // words from all 4 input quadwords.
4848 SDValue NewV;
4849 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004850 SmallVector<int, 8> MaskV;
4851 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4852 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004853 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004854 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4855 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4856 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004857
Nate Begemanb9a47b82009-02-23 08:49:38 +00004858 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4859 // source words for the shuffle, to aid later transformations.
4860 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004861 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004862 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004863 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004864 if (idx != (int)i)
4865 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004866 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004867 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004868 AllWordsInNewV = false;
4869 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004870 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004871
Nate Begemanb9a47b82009-02-23 08:49:38 +00004872 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4873 if (AllWordsInNewV) {
4874 for (int i = 0; i != 8; ++i) {
4875 int idx = MaskVals[i];
4876 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004877 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004878 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 if ((idx != i) && idx < 4)
4880 pshufhw = false;
4881 if ((idx != i) && idx > 3)
4882 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004883 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004884 V1 = NewV;
4885 V2Used = false;
4886 BestLoQuad = 0;
4887 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004888 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004889
Nate Begemanb9a47b82009-02-23 08:49:38 +00004890 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4891 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004892 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004893 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4894 unsigned TargetMask = 0;
4895 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004896 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004897 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4898 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4899 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004900 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004901 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004902 }
Eric Christopherfd179292009-08-27 18:07:15 +00004903
Nate Begemanb9a47b82009-02-23 08:49:38 +00004904 // If we have SSSE3, and all words of the result are from 1 input vector,
4905 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4906 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004907 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004908 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004909
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004911 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004912 // mask, and elements that come from V1 in the V2 mask, so that the two
4913 // results can be OR'd together.
4914 bool TwoInputs = V1Used && V2Used;
4915 for (unsigned i = 0; i != 8; ++i) {
4916 int EltIdx = MaskVals[i] * 2;
4917 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004918 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4919 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004920 continue;
4921 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004922 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4923 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004924 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004925 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004926 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004927 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004928 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004929 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004930 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004931
Nate Begemanb9a47b82009-02-23 08:49:38 +00004932 // Calculate the shuffle mask for the second input, shuffle it, and
4933 // OR it with the first shuffled input.
4934 pshufbMask.clear();
4935 for (unsigned i = 0; i != 8; ++i) {
4936 int EltIdx = MaskVals[i] * 2;
4937 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004938 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4939 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004940 continue;
4941 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4943 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004944 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004945 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004946 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004947 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004948 MVT::v16i8, &pshufbMask[0], 16));
4949 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004950 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004951 }
4952
4953 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4954 // and update MaskVals with new element order.
4955 BitVector InOrder(8);
4956 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004957 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004958 for (int i = 0; i != 4; ++i) {
4959 int idx = MaskVals[i];
4960 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004961 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004962 InOrder.set(i);
4963 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004964 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004965 InOrder.set(i);
4966 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004967 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004968 }
4969 }
4970 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004971 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004972 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004973 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004974
4975 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4976 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4977 NewV.getOperand(0),
4978 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4979 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004980 }
Eric Christopherfd179292009-08-27 18:07:15 +00004981
Nate Begemanb9a47b82009-02-23 08:49:38 +00004982 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4983 // and update MaskVals with the new element order.
4984 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004985 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004986 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004987 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004988 for (unsigned i = 4; i != 8; ++i) {
4989 int idx = MaskVals[i];
4990 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004991 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004992 InOrder.set(i);
4993 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004994 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004995 InOrder.set(i);
4996 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004997 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004998 }
4999 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005000 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005001 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005002
5003 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5004 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5005 NewV.getOperand(0),
5006 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5007 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005008 }
Eric Christopherfd179292009-08-27 18:07:15 +00005009
Nate Begemanb9a47b82009-02-23 08:49:38 +00005010 // In case BestHi & BestLo were both -1, which means each quadword has a word
5011 // from each of the four input quadwords, calculate the InOrder bitvector now
5012 // before falling through to the insert/extract cleanup.
5013 if (BestLoQuad == -1 && BestHiQuad == -1) {
5014 NewV = V1;
5015 for (int i = 0; i != 8; ++i)
5016 if (MaskVals[i] < 0 || MaskVals[i] == i)
5017 InOrder.set(i);
5018 }
Eric Christopherfd179292009-08-27 18:07:15 +00005019
Nate Begemanb9a47b82009-02-23 08:49:38 +00005020 // The other elements are put in the right place using pextrw and pinsrw.
5021 for (unsigned i = 0; i != 8; ++i) {
5022 if (InOrder[i])
5023 continue;
5024 int EltIdx = MaskVals[i];
5025 if (EltIdx < 0)
5026 continue;
5027 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005028 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005029 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005030 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005031 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005032 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 DAG.getIntPtrConstant(i));
5034 }
5035 return NewV;
5036}
5037
5038// v16i8 shuffles - Prefer shuffles in the following order:
5039// 1. [ssse3] 1 x pshufb
5040// 2. [ssse3] 2 x pshufb + 1 x por
5041// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5042static
Nate Begeman9008ca62009-04-27 18:41:29 +00005043SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005044 SelectionDAG &DAG,
5045 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005046 SDValue V1 = SVOp->getOperand(0);
5047 SDValue V2 = SVOp->getOperand(1);
5048 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005049 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005050 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005051
Nate Begemanb9a47b82009-02-23 08:49:38 +00005052 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005053 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005054 // present, fall back to case 3.
5055 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5056 bool V1Only = true;
5057 bool V2Only = true;
5058 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005059 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005060 if (EltIdx < 0)
5061 continue;
5062 if (EltIdx < 16)
5063 V2Only = false;
5064 else
5065 V1Only = false;
5066 }
Eric Christopherfd179292009-08-27 18:07:15 +00005067
Nate Begemanb9a47b82009-02-23 08:49:38 +00005068 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5069 if (TLI.getSubtarget()->hasSSSE3()) {
5070 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005071
Nate Begemanb9a47b82009-02-23 08:49:38 +00005072 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005073 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005074 //
5075 // Otherwise, we have elements from both input vectors, and must zero out
5076 // elements that come from V2 in the first mask, and V1 in the second mask
5077 // so that we can OR them together.
5078 bool TwoInputs = !(V1Only || V2Only);
5079 for (unsigned i = 0; i != 16; ++i) {
5080 int EltIdx = MaskVals[i];
5081 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005082 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005083 continue;
5084 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005085 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005086 }
5087 // If all the elements are from V2, assign it to V1 and return after
5088 // building the first pshufb.
5089 if (V2Only)
5090 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005091 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005092 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005093 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005094 if (!TwoInputs)
5095 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005096
Nate Begemanb9a47b82009-02-23 08:49:38 +00005097 // Calculate the shuffle mask for the second input, shuffle it, and
5098 // OR it with the first shuffled input.
5099 pshufbMask.clear();
5100 for (unsigned i = 0; i != 16; ++i) {
5101 int EltIdx = MaskVals[i];
5102 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005103 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005104 continue;
5105 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005106 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005107 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005108 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005109 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 MVT::v16i8, &pshufbMask[0], 16));
5111 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005112 }
Eric Christopherfd179292009-08-27 18:07:15 +00005113
Nate Begemanb9a47b82009-02-23 08:49:38 +00005114 // No SSSE3 - Calculate in place words and then fix all out of place words
5115 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5116 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005117 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5118 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005119 SDValue NewV = V2Only ? V2 : V1;
5120 for (int i = 0; i != 8; ++i) {
5121 int Elt0 = MaskVals[i*2];
5122 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005123
Nate Begemanb9a47b82009-02-23 08:49:38 +00005124 // This word of the result is all undef, skip it.
5125 if (Elt0 < 0 && Elt1 < 0)
5126 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005127
Nate Begemanb9a47b82009-02-23 08:49:38 +00005128 // This word of the result is already in the correct place, skip it.
5129 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5130 continue;
5131 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5132 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005133
Nate Begemanb9a47b82009-02-23 08:49:38 +00005134 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5135 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5136 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005137
5138 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5139 // using a single extract together, load it and store it.
5140 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005141 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005142 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005144 DAG.getIntPtrConstant(i));
5145 continue;
5146 }
5147
Nate Begemanb9a47b82009-02-23 08:49:38 +00005148 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005149 // source byte is not also odd, shift the extracted word left 8 bits
5150 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005151 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005152 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005153 DAG.getIntPtrConstant(Elt1 / 2));
5154 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005155 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005156 DAG.getConstant(8,
5157 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005158 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005159 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5160 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005161 }
5162 // If Elt0 is defined, extract it from the appropriate source. If the
5163 // source byte is not also even, shift the extracted word right 8 bits. If
5164 // Elt1 was also defined, OR the extracted values together before
5165 // inserting them in the result.
5166 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005167 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005168 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5169 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005170 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005171 DAG.getConstant(8,
5172 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005173 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005174 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5175 DAG.getConstant(0x00FF, MVT::i16));
5176 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005177 : InsElt0;
5178 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005179 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005180 DAG.getIntPtrConstant(i));
5181 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005182 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005183}
5184
Evan Cheng7a831ce2007-12-15 03:00:47 +00005185/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005186/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005187/// done when every pair / quad of shuffle mask elements point to elements in
5188/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005189/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005190static
Nate Begeman9008ca62009-04-27 18:41:29 +00005191SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005192 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005193 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005194 SDValue V1 = SVOp->getOperand(0);
5195 SDValue V2 = SVOp->getOperand(1);
5196 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005197 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005198 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005199 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005200 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005201 case MVT::v4f32: NewVT = MVT::v2f64; break;
5202 case MVT::v4i32: NewVT = MVT::v2i64; break;
5203 case MVT::v8i16: NewVT = MVT::v4i32; break;
5204 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005205 }
5206
Nate Begeman9008ca62009-04-27 18:41:29 +00005207 int Scale = NumElems / NewWidth;
5208 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005209 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005210 int StartIdx = -1;
5211 for (int j = 0; j < Scale; ++j) {
5212 int EltIdx = SVOp->getMaskElt(i+j);
5213 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005214 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005215 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005216 StartIdx = EltIdx - (EltIdx % Scale);
5217 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005218 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005219 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005220 if (StartIdx == -1)
5221 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005222 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005223 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005224 }
5225
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005226 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5227 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005228 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005229}
5230
Evan Chengd880b972008-05-09 21:53:03 +00005231/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005232///
Owen Andersone50ed302009-08-10 22:56:29 +00005233static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005234 SDValue SrcOp, SelectionDAG &DAG,
5235 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005236 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005237 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005238 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005239 LD = dyn_cast<LoadSDNode>(SrcOp);
5240 if (!LD) {
5241 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5242 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005243 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005244 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005245 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005246 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005247 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005248 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005249 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005250 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005251 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5252 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5253 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005254 SrcOp.getOperand(0)
5255 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005256 }
5257 }
5258 }
5259
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005260 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005261 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005262 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005263 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005264}
5265
Evan Chengace3c172008-07-22 21:13:36 +00005266/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5267/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005268static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005269LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5270 SDValue V1 = SVOp->getOperand(0);
5271 SDValue V2 = SVOp->getOperand(1);
5272 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005273 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005274
Evan Chengace3c172008-07-22 21:13:36 +00005275 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005276 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005277 SmallVector<int, 8> Mask1(4U, -1);
5278 SmallVector<int, 8> PermMask;
5279 SVOp->getMask(PermMask);
5280
Evan Chengace3c172008-07-22 21:13:36 +00005281 unsigned NumHi = 0;
5282 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005283 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005284 int Idx = PermMask[i];
5285 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005286 Locs[i] = std::make_pair(-1, -1);
5287 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005288 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5289 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005290 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005291 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005292 NumLo++;
5293 } else {
5294 Locs[i] = std::make_pair(1, NumHi);
5295 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005296 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005297 NumHi++;
5298 }
5299 }
5300 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005301
Evan Chengace3c172008-07-22 21:13:36 +00005302 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005303 // If no more than two elements come from either vector. This can be
5304 // implemented with two shuffles. First shuffle gather the elements.
5305 // The second shuffle, which takes the first shuffle as both of its
5306 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005307 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005308
Nate Begeman9008ca62009-04-27 18:41:29 +00005309 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005310
Evan Chengace3c172008-07-22 21:13:36 +00005311 for (unsigned i = 0; i != 4; ++i) {
5312 if (Locs[i].first == -1)
5313 continue;
5314 else {
5315 unsigned Idx = (i < 2) ? 0 : 4;
5316 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005317 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005318 }
5319 }
5320
Nate Begeman9008ca62009-04-27 18:41:29 +00005321 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005322 } else if (NumLo == 3 || NumHi == 3) {
5323 // Otherwise, we must have three elements from one vector, call it X, and
5324 // one element from the other, call it Y. First, use a shufps to build an
5325 // intermediate vector with the one element from Y and the element from X
5326 // that will be in the same half in the final destination (the indexes don't
5327 // matter). Then, use a shufps to build the final vector, taking the half
5328 // containing the element from Y from the intermediate, and the other half
5329 // from X.
5330 if (NumHi == 3) {
5331 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005332 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005333 std::swap(V1, V2);
5334 }
5335
5336 // Find the element from V2.
5337 unsigned HiIndex;
5338 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 int Val = PermMask[HiIndex];
5340 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005341 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005342 if (Val >= 4)
5343 break;
5344 }
5345
Nate Begeman9008ca62009-04-27 18:41:29 +00005346 Mask1[0] = PermMask[HiIndex];
5347 Mask1[1] = -1;
5348 Mask1[2] = PermMask[HiIndex^1];
5349 Mask1[3] = -1;
5350 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005351
5352 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005353 Mask1[0] = PermMask[0];
5354 Mask1[1] = PermMask[1];
5355 Mask1[2] = HiIndex & 1 ? 6 : 4;
5356 Mask1[3] = HiIndex & 1 ? 4 : 6;
5357 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005358 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005359 Mask1[0] = HiIndex & 1 ? 2 : 0;
5360 Mask1[1] = HiIndex & 1 ? 0 : 2;
5361 Mask1[2] = PermMask[2];
5362 Mask1[3] = PermMask[3];
5363 if (Mask1[2] >= 0)
5364 Mask1[2] += 4;
5365 if (Mask1[3] >= 0)
5366 Mask1[3] += 4;
5367 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005368 }
Evan Chengace3c172008-07-22 21:13:36 +00005369 }
5370
5371 // Break it into (shuffle shuffle_hi, shuffle_lo).
5372 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005373 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005374 SmallVector<int,8> LoMask(4U, -1);
5375 SmallVector<int,8> HiMask(4U, -1);
5376
5377 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005378 unsigned MaskIdx = 0;
5379 unsigned LoIdx = 0;
5380 unsigned HiIdx = 2;
5381 for (unsigned i = 0; i != 4; ++i) {
5382 if (i == 2) {
5383 MaskPtr = &HiMask;
5384 MaskIdx = 1;
5385 LoIdx = 0;
5386 HiIdx = 2;
5387 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005388 int Idx = PermMask[i];
5389 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005390 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005391 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005392 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005393 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005394 LoIdx++;
5395 } else {
5396 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005397 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005398 HiIdx++;
5399 }
5400 }
5401
Nate Begeman9008ca62009-04-27 18:41:29 +00005402 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5403 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5404 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005405 for (unsigned i = 0; i != 4; ++i) {
5406 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005407 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005408 } else {
5409 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005410 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005411 }
5412 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005413 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005414}
5415
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005416static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005417 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005418 V = V.getOperand(0);
5419 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5420 V = V.getOperand(0);
5421 if (MayFoldLoad(V))
5422 return true;
5423 return false;
5424}
5425
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005426// FIXME: the version above should always be used. Since there's
5427// a bug where several vector shuffles can't be folded because the
5428// DAG is not updated during lowering and a node claims to have two
5429// uses while it only has one, use this version, and let isel match
5430// another instruction if the load really happens to have more than
5431// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005432// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005433static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005434 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005435 V = V.getOperand(0);
5436 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5437 V = V.getOperand(0);
5438 if (ISD::isNormalLoad(V.getNode()))
5439 return true;
5440 return false;
5441}
5442
5443/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5444/// a vector extract, and if both can be later optimized into a single load.
5445/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5446/// here because otherwise a target specific shuffle node is going to be
5447/// emitted for this shuffle, and the optimization not done.
5448/// FIXME: This is probably not the best approach, but fix the problem
5449/// until the right path is decided.
5450static
5451bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5452 const TargetLowering &TLI) {
5453 EVT VT = V.getValueType();
5454 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5455
5456 // Be sure that the vector shuffle is present in a pattern like this:
5457 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5458 if (!V.hasOneUse())
5459 return false;
5460
5461 SDNode *N = *V.getNode()->use_begin();
5462 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5463 return false;
5464
5465 SDValue EltNo = N->getOperand(1);
5466 if (!isa<ConstantSDNode>(EltNo))
5467 return false;
5468
5469 // If the bit convert changed the number of elements, it is unsafe
5470 // to examine the mask.
5471 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005472 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005473 EVT SrcVT = V.getOperand(0).getValueType();
5474 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5475 return false;
5476 V = V.getOperand(0);
5477 HasShuffleIntoBitcast = true;
5478 }
5479
5480 // Select the input vector, guarding against out of range extract vector.
5481 unsigned NumElems = VT.getVectorNumElements();
5482 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5483 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5484 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5485
5486 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005487 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005488 V = V.getOperand(0);
5489
5490 if (ISD::isNormalLoad(V.getNode())) {
5491 // Is the original load suitable?
5492 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5493
5494 // FIXME: avoid the multi-use bug that is preventing lots of
5495 // of foldings to be detected, this is still wrong of course, but
5496 // give the temporary desired behavior, and if it happens that
5497 // the load has real more uses, during isel it will not fold, and
5498 // will generate poor code.
5499 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5500 return false;
5501
5502 if (!HasShuffleIntoBitcast)
5503 return true;
5504
5505 // If there's a bitcast before the shuffle, check if the load type and
5506 // alignment is valid.
5507 unsigned Align = LN0->getAlignment();
5508 unsigned NewAlign =
5509 TLI.getTargetData()->getABITypeAlignment(
5510 VT.getTypeForEVT(*DAG.getContext()));
5511
5512 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5513 return false;
5514 }
5515
5516 return true;
5517}
5518
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005519static
Evan Cheng835580f2010-10-07 20:50:20 +00005520SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5521 EVT VT = Op.getValueType();
5522
5523 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005524 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5525 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005526 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5527 V1, DAG));
5528}
5529
5530static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005531SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5532 bool HasSSE2) {
5533 SDValue V1 = Op.getOperand(0);
5534 SDValue V2 = Op.getOperand(1);
5535 EVT VT = Op.getValueType();
5536
5537 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5538
5539 if (HasSSE2 && VT == MVT::v2f64)
5540 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5541
5542 // v4f32 or v4i32
5543 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5544}
5545
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005546static
5547SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5548 SDValue V1 = Op.getOperand(0);
5549 SDValue V2 = Op.getOperand(1);
5550 EVT VT = Op.getValueType();
5551
5552 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5553 "unsupported shuffle type");
5554
5555 if (V2.getOpcode() == ISD::UNDEF)
5556 V2 = V1;
5557
5558 // v4i32 or v4f32
5559 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5560}
5561
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005562static
5563SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5564 SDValue V1 = Op.getOperand(0);
5565 SDValue V2 = Op.getOperand(1);
5566 EVT VT = Op.getValueType();
5567 unsigned NumElems = VT.getVectorNumElements();
5568
5569 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5570 // operand of these instructions is only memory, so check if there's a
5571 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5572 // same masks.
5573 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005574
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005575 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005576 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005577 CanFoldLoad = true;
5578
5579 // When V1 is a load, it can be folded later into a store in isel, example:
5580 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5581 // turns into:
5582 // (MOVLPSmr addr:$src1, VR128:$src2)
5583 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005584 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005585 CanFoldLoad = true;
5586
Eric Christopher893a8822011-02-20 05:04:42 +00005587 // Both of them can't be memory operations though.
5588 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5589 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005590
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005591 if (CanFoldLoad) {
5592 if (HasSSE2 && NumElems == 2)
5593 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5594
5595 if (NumElems == 4)
5596 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5597 }
5598
5599 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5600 // movl and movlp will both match v2i64, but v2i64 is never matched by
5601 // movl earlier because we make it strict to avoid messing with the movlp load
5602 // folding logic (see the code above getMOVLP call). Match it here then,
5603 // this is horrible, but will stay like this until we move all shuffle
5604 // matching to x86 specific nodes. Note that for the 1st condition all
5605 // types are matched with movsd.
5606 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5607 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5608 else if (HasSSE2)
5609 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5610
5611
5612 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5613
5614 // Invert the operand order and use SHUFPS to match it.
5615 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5616 X86::getShuffleSHUFImmediate(SVOp), DAG);
5617}
5618
David Greenec4db4e52011-02-28 19:06:56 +00005619static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005620 switch(VT.getSimpleVT().SimpleTy) {
5621 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5622 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005623 case MVT::v4f32:
5624 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5625 case MVT::v2f64:
5626 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5627 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5628 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005629 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5630 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5631 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005632 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005633 }
5634 return 0;
5635}
5636
5637static inline unsigned getUNPCKHOpcode(EVT VT) {
5638 switch(VT.getSimpleVT().SimpleTy) {
5639 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5640 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5641 case MVT::v4f32: return X86ISD::UNPCKHPS;
5642 case MVT::v2f64: return X86ISD::UNPCKHPD;
5643 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5644 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5645 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005646 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005647 }
5648 return 0;
5649}
5650
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005651static
5652SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005653 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005654 const X86Subtarget *Subtarget) {
5655 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5656 EVT VT = Op.getValueType();
5657 DebugLoc dl = Op.getDebugLoc();
5658 SDValue V1 = Op.getOperand(0);
5659 SDValue V2 = Op.getOperand(1);
5660
5661 if (isZeroShuffle(SVOp))
5662 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5663
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005664 // Handle splat operations
5665 if (SVOp->isSplat()) {
5666 // Special case, this is the only place now where it's
5667 // allowed to return a vector_shuffle operation without
5668 // using a target specific node, because *hopefully* it
5669 // will be optimized away by the dag combiner.
5670 if (VT.getVectorNumElements() <= 4 &&
5671 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5672 return Op;
5673
5674 // Handle splats by matching through known masks
5675 if (VT.getVectorNumElements() <= 4)
5676 return SDValue();
5677
Evan Cheng835580f2010-10-07 20:50:20 +00005678 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005679 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005680 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005681
5682 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5683 // do it!
5684 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5685 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5686 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005687 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005688 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5689 // FIXME: Figure out a cleaner way to do this.
5690 // Try to make use of movq to zero out the top part.
5691 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5692 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5693 if (NewOp.getNode()) {
5694 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5695 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5696 DAG, Subtarget, dl);
5697 }
5698 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5699 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5700 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5701 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5702 DAG, Subtarget, dl);
5703 }
5704 }
5705 return SDValue();
5706}
5707
Dan Gohman475871a2008-07-27 21:46:04 +00005708SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005709X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005710 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005711 SDValue V1 = Op.getOperand(0);
5712 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005713 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005714 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005715 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005716 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005717 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5718 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005719 bool V1IsSplat = false;
5720 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005721 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005722 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005723 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005724 MachineFunction &MF = DAG.getMachineFunction();
5725 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726
Dale Johannesen0488fb62010-09-30 23:57:10 +00005727 // Shuffle operations on MMX not supported.
5728 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005729 return Op;
5730
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005731 // Vector shuffle lowering takes 3 steps:
5732 //
5733 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5734 // narrowing and commutation of operands should be handled.
5735 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5736 // shuffle nodes.
5737 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5738 // so the shuffle can be broken into other shuffles and the legalizer can
5739 // try the lowering again.
5740 //
5741 // The general ideia is that no vector_shuffle operation should be left to
5742 // be matched during isel, all of them must be converted to a target specific
5743 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005744
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005745 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5746 // narrowing and commutation of operands should be handled. The actual code
5747 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005748 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005749 if (NewOp.getNode())
5750 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005751
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005752 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5753 // unpckh_undef). Only use pshufd if speed is more important than size.
5754 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5755 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005756 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005757 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5758 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5759 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005760
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005761 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005762 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005763 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005764
Dale Johannesen0488fb62010-09-30 23:57:10 +00005765 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005766 return getMOVHighToLow(Op, dl, DAG);
5767
5768 // Use to match splats
5769 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5770 (VT == MVT::v2f64 || VT == MVT::v2i64))
5771 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5772
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005773 if (X86::isPSHUFDMask(SVOp)) {
5774 // The actual implementation will match the mask in the if above and then
5775 // during isel it can match several different instructions, not only pshufd
5776 // as its name says, sad but true, emulate the behavior for now...
5777 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5778 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5779
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005780 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5781
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005782 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005783 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5784
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005785 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005786 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5787 TargetMask, DAG);
5788
5789 if (VT == MVT::v4f32)
5790 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5791 TargetMask, DAG);
5792 }
Eric Christopherfd179292009-08-27 18:07:15 +00005793
Evan Chengf26ffe92008-05-29 08:22:04 +00005794 // Check if this can be converted into a logical shift.
5795 bool isLeft = false;
5796 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005797 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005798 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005799 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005800 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005801 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005802 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005803 EVT EltVT = VT.getVectorElementType();
5804 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005805 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005806 }
Eric Christopherfd179292009-08-27 18:07:15 +00005807
Nate Begeman9008ca62009-04-27 18:41:29 +00005808 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005809 if (V1IsUndef)
5810 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005811 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005812 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005813 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005814 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005815 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5816
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005817 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005818 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5819 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005820 }
Eric Christopherfd179292009-08-27 18:07:15 +00005821
Nate Begeman9008ca62009-04-27 18:41:29 +00005822 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005823 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5824 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005825
Dale Johannesen0488fb62010-09-30 23:57:10 +00005826 if (X86::isMOVHLPSMask(SVOp))
5827 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005828
Dale Johannesen0488fb62010-09-30 23:57:10 +00005829 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5830 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005831
Dale Johannesen0488fb62010-09-30 23:57:10 +00005832 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5833 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005834
Dale Johannesen0488fb62010-09-30 23:57:10 +00005835 if (X86::isMOVLPMask(SVOp))
5836 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005837
Nate Begeman9008ca62009-04-27 18:41:29 +00005838 if (ShouldXformToMOVHLPS(SVOp) ||
5839 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5840 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005841
Evan Chengf26ffe92008-05-29 08:22:04 +00005842 if (isShift) {
5843 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005844 EVT EltVT = VT.getVectorElementType();
5845 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005846 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005847 }
Eric Christopherfd179292009-08-27 18:07:15 +00005848
Evan Cheng9eca5e82006-10-25 21:49:50 +00005849 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005850 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5851 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005852 V1IsSplat = isSplatVector(V1.getNode());
5853 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005854
Chris Lattner8a594482007-11-25 00:24:49 +00005855 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005856 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005857 Op = CommuteVectorShuffle(SVOp, DAG);
5858 SVOp = cast<ShuffleVectorSDNode>(Op);
5859 V1 = SVOp->getOperand(0);
5860 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005861 std::swap(V1IsSplat, V2IsSplat);
5862 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005863 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005864 }
5865
Nate Begeman9008ca62009-04-27 18:41:29 +00005866 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5867 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005868 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005869 return V1;
5870 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5871 // the instruction selector will not match, so get a canonical MOVL with
5872 // swapped operands to undo the commute.
5873 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005874 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005875
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005876 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005877 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5878 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005879
5880 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005881 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005882
Evan Cheng9bbbb982006-10-25 20:48:19 +00005883 if (V2IsSplat) {
5884 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005885 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005886 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005887 SDValue NewMask = NormalizeMask(SVOp, DAG);
5888 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5889 if (NSVOp != SVOp) {
5890 if (X86::isUNPCKLMask(NSVOp, true)) {
5891 return NewMask;
5892 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5893 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005894 }
5895 }
5896 }
5897
Evan Cheng9eca5e82006-10-25 21:49:50 +00005898 if (Commuted) {
5899 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005900 // FIXME: this seems wrong.
5901 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5902 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005903
5904 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005905 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5906 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005907
5908 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005909 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005910 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005911
Nate Begeman9008ca62009-04-27 18:41:29 +00005912 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005913 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005914 return CommuteVectorShuffle(SVOp, DAG);
5915
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005916 // The checks below are all present in isShuffleMaskLegal, but they are
5917 // inlined here right now to enable us to directly emit target specific
5918 // nodes, and remove one by one until they don't return Op anymore.
5919 SmallVector<int, 16> M;
5920 SVOp->getMask(M);
5921
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005922 if (isPALIGNRMask(M, VT, HasSSSE3))
5923 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5924 X86::getShufflePALIGNRImmediate(SVOp),
5925 DAG);
5926
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005927 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5928 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005929 if (VT == MVT::v2f64) {
5930 X86ISD::NodeType Opcode =
5931 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5932 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5933 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005934 if (VT == MVT::v2i64)
5935 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5936 }
5937
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005938 if (isPSHUFHWMask(M, VT))
5939 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5940 X86::getShufflePSHUFHWImmediate(SVOp),
5941 DAG);
5942
5943 if (isPSHUFLWMask(M, VT))
5944 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5945 X86::getShufflePSHUFLWImmediate(SVOp),
5946 DAG);
5947
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005948 if (isSHUFPMask(M, VT)) {
5949 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5950 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5951 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5952 TargetMask, DAG);
5953 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5954 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5955 TargetMask, DAG);
5956 }
5957
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005958 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5959 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005960 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5961 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005962 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5963 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5964 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5965
Evan Cheng14b32e12007-12-11 01:46:18 +00005966 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005967 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005968 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005969 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005970 return NewOp;
5971 }
5972
Owen Anderson825b72b2009-08-11 20:47:22 +00005973 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005974 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005975 if (NewOp.getNode())
5976 return NewOp;
5977 }
Eric Christopherfd179292009-08-27 18:07:15 +00005978
Dale Johannesen0488fb62010-09-30 23:57:10 +00005979 // Handle all 4 wide cases with a number of shuffles.
5980 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005981 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005982
Dan Gohman475871a2008-07-27 21:46:04 +00005983 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005984}
5985
Dan Gohman475871a2008-07-27 21:46:04 +00005986SDValue
5987X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005988 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005989 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005990 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005991 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005993 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005994 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005995 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005996 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005997 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005998 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5999 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6000 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6002 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006003 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006005 Op.getOperand(0)),
6006 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006008 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006010 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006011 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006012 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006013 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6014 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006015 // result has a single use which is a store or a bitcast to i32. And in
6016 // the case of a store, it's not worth it if the index is a constant 0,
6017 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006018 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006019 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006020 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006021 if ((User->getOpcode() != ISD::STORE ||
6022 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6023 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006024 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006025 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006026 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006027 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006028 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006029 Op.getOperand(0)),
6030 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006031 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006033 // ExtractPS works with constant index.
6034 if (isa<ConstantSDNode>(Op.getOperand(1)))
6035 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006036 }
Dan Gohman475871a2008-07-27 21:46:04 +00006037 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006038}
6039
6040
Dan Gohman475871a2008-07-27 21:46:04 +00006041SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006042X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6043 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006044 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006045 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006046
David Greene74a579d2011-02-10 16:57:36 +00006047 SDValue Vec = Op.getOperand(0);
6048 EVT VecVT = Vec.getValueType();
6049
6050 // If this is a 256-bit vector result, first extract the 128-bit
6051 // vector and then extract from the 128-bit vector.
6052 if (VecVT.getSizeInBits() > 128) {
6053 DebugLoc dl = Op.getNode()->getDebugLoc();
6054 unsigned NumElems = VecVT.getVectorNumElements();
6055 SDValue Idx = Op.getOperand(1);
6056
6057 if (!isa<ConstantSDNode>(Idx))
6058 return SDValue();
6059
6060 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6061 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6062
6063 // Get the 128-bit vector.
6064 bool Upper = IdxVal >= ExtractNumElems;
6065 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6066
6067 // Extract from it.
6068 SDValue ScaledIdx = Idx;
6069 if (Upper)
6070 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6071 DAG.getConstant(ExtractNumElems,
6072 Idx.getValueType()));
6073 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6074 ScaledIdx);
6075 }
6076
6077 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6078
Evan Cheng62a3f152008-03-24 21:52:23 +00006079 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006080 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006081 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006082 return Res;
6083 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006084
Owen Andersone50ed302009-08-10 22:56:29 +00006085 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006086 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006087 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006088 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006089 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006090 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006091 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006092 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6093 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006094 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006095 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006096 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006097 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006098 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006099 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006100 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006101 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006102 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006103 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006104 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006105 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006106 if (Idx == 0)
6107 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006108
Evan Cheng0db9fe62006-04-25 20:13:52 +00006109 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006110 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006111 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006112 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006113 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006114 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006115 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006116 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006117 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6118 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6119 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006120 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006121 if (Idx == 0)
6122 return Op;
6123
6124 // UNPCKHPD the element to the lowest double word, then movsd.
6125 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6126 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006127 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006128 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006129 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006130 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006131 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006132 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006133 }
6134
Dan Gohman475871a2008-07-27 21:46:04 +00006135 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006136}
6137
Dan Gohman475871a2008-07-27 21:46:04 +00006138SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006139X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6140 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006141 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006142 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006143 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006144
Dan Gohman475871a2008-07-27 21:46:04 +00006145 SDValue N0 = Op.getOperand(0);
6146 SDValue N1 = Op.getOperand(1);
6147 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006148
Dan Gohman8a55ce42009-09-23 21:02:20 +00006149 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006150 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006151 unsigned Opc;
6152 if (VT == MVT::v8i16)
6153 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006154 else if (VT == MVT::v16i8)
6155 Opc = X86ISD::PINSRB;
6156 else
6157 Opc = X86ISD::PINSRB;
6158
Nate Begeman14d12ca2008-02-11 04:19:36 +00006159 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6160 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006161 if (N1.getValueType() != MVT::i32)
6162 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6163 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006164 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006165 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006166 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006167 // Bits [7:6] of the constant are the source select. This will always be
6168 // zero here. The DAG Combiner may combine an extract_elt index into these
6169 // bits. For example (insert (extract, 3), 2) could be matched by putting
6170 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006171 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006172 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006173 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006174 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006175 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006176 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006177 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006178 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006179 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006180 // PINSR* works with constant index.
6181 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006182 }
Dan Gohman475871a2008-07-27 21:46:04 +00006183 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006184}
6185
Dan Gohman475871a2008-07-27 21:46:04 +00006186SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006187X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006188 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006189 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006190
David Greene6b381262011-02-09 15:32:06 +00006191 DebugLoc dl = Op.getDebugLoc();
6192 SDValue N0 = Op.getOperand(0);
6193 SDValue N1 = Op.getOperand(1);
6194 SDValue N2 = Op.getOperand(2);
6195
6196 // If this is a 256-bit vector result, first insert into a 128-bit
6197 // vector and then insert into the 256-bit vector.
6198 if (VT.getSizeInBits() > 128) {
6199 if (!isa<ConstantSDNode>(N2))
6200 return SDValue();
6201
6202 // Get the 128-bit vector.
6203 unsigned NumElems = VT.getVectorNumElements();
6204 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6205 bool Upper = IdxVal >= NumElems / 2;
6206
6207 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6208
6209 // Insert into it.
6210 SDValue ScaledN2 = N2;
6211 if (Upper)
6212 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006213 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006214 (VT.getSizeInBits() / 128),
6215 N2.getValueType()));
6216 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6217 N1, ScaledN2);
6218
6219 // Insert the 128-bit vector
6220 // FIXME: Why UNDEF?
6221 return Insert128BitVector(N0, Op, N2, DAG, dl);
6222 }
6223
Nate Begeman14d12ca2008-02-11 04:19:36 +00006224 if (Subtarget->hasSSE41())
6225 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6226
Dan Gohman8a55ce42009-09-23 21:02:20 +00006227 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006228 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006229
Dan Gohman8a55ce42009-09-23 21:02:20 +00006230 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006231 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6232 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006233 if (N1.getValueType() != MVT::i32)
6234 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6235 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006236 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006237 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006238 }
Dan Gohman475871a2008-07-27 21:46:04 +00006239 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006240}
6241
Dan Gohman475871a2008-07-27 21:46:04 +00006242SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006243X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006244 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006245 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006246 EVT OpVT = Op.getValueType();
6247
6248 // If this is a 256-bit vector result, first insert into a 128-bit
6249 // vector and then insert into the 256-bit vector.
6250 if (OpVT.getSizeInBits() > 128) {
6251 // Insert into a 128-bit vector.
6252 EVT VT128 = EVT::getVectorVT(*Context,
6253 OpVT.getVectorElementType(),
6254 OpVT.getVectorNumElements() / 2);
6255
6256 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6257
6258 // Insert the 128-bit vector.
6259 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6260 DAG.getConstant(0, MVT::i32),
6261 DAG, dl);
6262 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006263
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006264 if (Op.getValueType() == MVT::v1i64 &&
6265 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006266 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006267
Owen Anderson825b72b2009-08-11 20:47:22 +00006268 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006269 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6270 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006271 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006272 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006273}
6274
David Greene91585092011-01-26 15:38:49 +00006275// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6276// a simple subregister reference or explicit instructions to grab
6277// upper bits of a vector.
6278SDValue
6279X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6280 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006281 DebugLoc dl = Op.getNode()->getDebugLoc();
6282 SDValue Vec = Op.getNode()->getOperand(0);
6283 SDValue Idx = Op.getNode()->getOperand(1);
6284
6285 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6286 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6287 return Extract128BitVector(Vec, Idx, DAG, dl);
6288 }
David Greene91585092011-01-26 15:38:49 +00006289 }
6290 return SDValue();
6291}
6292
David Greenecfe33c42011-01-26 19:13:22 +00006293// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6294// simple superregister reference or explicit instructions to insert
6295// the upper bits of a vector.
6296SDValue
6297X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6298 if (Subtarget->hasAVX()) {
6299 DebugLoc dl = Op.getNode()->getDebugLoc();
6300 SDValue Vec = Op.getNode()->getOperand(0);
6301 SDValue SubVec = Op.getNode()->getOperand(1);
6302 SDValue Idx = Op.getNode()->getOperand(2);
6303
6304 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6305 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006306 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006307 }
6308 }
6309 return SDValue();
6310}
6311
Bill Wendling056292f2008-09-16 21:48:12 +00006312// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6313// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6314// one of the above mentioned nodes. It has to be wrapped because otherwise
6315// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6316// be used to form addressing mode. These wrapped nodes will be selected
6317// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006318SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006319X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006320 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006321
Chris Lattner41621a22009-06-26 19:22:52 +00006322 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6323 // global base reg.
6324 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006325 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006326 CodeModel::Model M = getTargetMachine().getCodeModel();
6327
Chris Lattner4f066492009-07-11 20:29:19 +00006328 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006329 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006330 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006331 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006332 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006333 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006334 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006335
Evan Cheng1606e8e2009-03-13 07:51:59 +00006336 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006337 CP->getAlignment(),
6338 CP->getOffset(), OpFlag);
6339 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006340 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006341 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006342 if (OpFlag) {
6343 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006344 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006345 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006346 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006347 }
6348
6349 return Result;
6350}
6351
Dan Gohmand858e902010-04-17 15:26:15 +00006352SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006353 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006354
Chris Lattner18c59872009-06-27 04:16:01 +00006355 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6356 // global base reg.
6357 unsigned char OpFlag = 0;
6358 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006359 CodeModel::Model M = getTargetMachine().getCodeModel();
6360
Chris Lattner4f066492009-07-11 20:29:19 +00006361 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006362 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006363 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006364 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006365 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006366 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006367 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006368
Chris Lattner18c59872009-06-27 04:16:01 +00006369 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6370 OpFlag);
6371 DebugLoc DL = JT->getDebugLoc();
6372 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006373
Chris Lattner18c59872009-06-27 04:16:01 +00006374 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006375 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006376 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6377 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006378 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006379 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006380
Chris Lattner18c59872009-06-27 04:16:01 +00006381 return Result;
6382}
6383
6384SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006385X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006386 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006387
Chris Lattner18c59872009-06-27 04:16:01 +00006388 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6389 // global base reg.
6390 unsigned char OpFlag = 0;
6391 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006392 CodeModel::Model M = getTargetMachine().getCodeModel();
6393
Chris Lattner4f066492009-07-11 20:29:19 +00006394 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006395 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006396 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006397 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006398 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006399 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006400 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006401
Chris Lattner18c59872009-06-27 04:16:01 +00006402 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006403
Chris Lattner18c59872009-06-27 04:16:01 +00006404 DebugLoc DL = Op.getDebugLoc();
6405 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006406
6407
Chris Lattner18c59872009-06-27 04:16:01 +00006408 // With PIC, the address is actually $g + Offset.
6409 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006410 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006411 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6412 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006413 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006414 Result);
6415 }
Eric Christopherfd179292009-08-27 18:07:15 +00006416
Chris Lattner18c59872009-06-27 04:16:01 +00006417 return Result;
6418}
6419
Dan Gohman475871a2008-07-27 21:46:04 +00006420SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006421X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006422 // Create the TargetBlockAddressAddress node.
6423 unsigned char OpFlags =
6424 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006425 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006426 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006427 DebugLoc dl = Op.getDebugLoc();
6428 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6429 /*isTarget=*/true, OpFlags);
6430
Dan Gohmanf705adb2009-10-30 01:28:02 +00006431 if (Subtarget->isPICStyleRIPRel() &&
6432 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006433 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6434 else
6435 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006436
Dan Gohman29cbade2009-11-20 23:18:13 +00006437 // With PIC, the address is actually $g + Offset.
6438 if (isGlobalRelativeToPICBase(OpFlags)) {
6439 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6440 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6441 Result);
6442 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006443
6444 return Result;
6445}
6446
6447SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006448X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006449 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006450 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006451 // Create the TargetGlobalAddress node, folding in the constant
6452 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006453 unsigned char OpFlags =
6454 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006455 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006456 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006457 if (OpFlags == X86II::MO_NO_FLAG &&
6458 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006459 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006460 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006461 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006462 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006463 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006464 }
Eric Christopherfd179292009-08-27 18:07:15 +00006465
Chris Lattner4f066492009-07-11 20:29:19 +00006466 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006467 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006468 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6469 else
6470 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006471
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006472 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006473 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006474 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6475 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006476 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006477 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006478
Chris Lattner36c25012009-07-10 07:34:39 +00006479 // For globals that require a load from a stub to get the address, emit the
6480 // load.
6481 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006482 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006483 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006484
Dan Gohman6520e202008-10-18 02:06:02 +00006485 // If there was a non-zero offset that we didn't fold, create an explicit
6486 // addition for it.
6487 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006488 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006489 DAG.getConstant(Offset, getPointerTy()));
6490
Evan Cheng0db9fe62006-04-25 20:13:52 +00006491 return Result;
6492}
6493
Evan Chengda43bcf2008-09-24 00:05:32 +00006494SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006495X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006496 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006497 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006498 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006499}
6500
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006501static SDValue
6502GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006503 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006504 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006505 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006506 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006507 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006508 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006509 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006510 GA->getOffset(),
6511 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006512 if (InFlag) {
6513 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006514 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006515 } else {
6516 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006517 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006518 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006519
6520 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006521 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006522
Rafael Espindola15f1b662009-04-24 12:59:40 +00006523 SDValue Flag = Chain.getValue(1);
6524 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006525}
6526
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006527// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006528static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006529LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006530 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006531 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006532 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6533 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006534 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006535 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006536 InFlag = Chain.getValue(1);
6537
Chris Lattnerb903bed2009-06-26 21:20:29 +00006538 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006539}
6540
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006541// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006542static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006543LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006544 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006545 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6546 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006547}
6548
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006549// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6550// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006551static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006552 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006553 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006554 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006555
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006556 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6557 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6558 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006559
Michael J. Spencerec38de22010-10-10 22:04:20 +00006560 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006561 DAG.getIntPtrConstant(0),
6562 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006563
Chris Lattnerb903bed2009-06-26 21:20:29 +00006564 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006565 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6566 // initialexec.
6567 unsigned WrapperKind = X86ISD::Wrapper;
6568 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006569 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006570 } else if (is64Bit) {
6571 assert(model == TLSModel::InitialExec);
6572 OperandFlags = X86II::MO_GOTTPOFF;
6573 WrapperKind = X86ISD::WrapperRIP;
6574 } else {
6575 assert(model == TLSModel::InitialExec);
6576 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006577 }
Eric Christopherfd179292009-08-27 18:07:15 +00006578
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006579 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6580 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006581 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006582 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006583 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006584 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006585
Rafael Espindola9a580232009-02-27 13:37:18 +00006586 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006587 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006588 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006589
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006590 // The address of the thread local variable is the add of the thread
6591 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006592 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006593}
6594
Dan Gohman475871a2008-07-27 21:46:04 +00006595SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006596X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006597
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006598 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006599 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006600
Eric Christopher30ef0e52010-06-03 04:07:48 +00006601 if (Subtarget->isTargetELF()) {
6602 // TODO: implement the "local dynamic" model
6603 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006604
Eric Christopher30ef0e52010-06-03 04:07:48 +00006605 // If GV is an alias then use the aliasee for determining
6606 // thread-localness.
6607 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6608 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006609
6610 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006611 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006612
Eric Christopher30ef0e52010-06-03 04:07:48 +00006613 switch (model) {
6614 case TLSModel::GeneralDynamic:
6615 case TLSModel::LocalDynamic: // not implemented
6616 if (Subtarget->is64Bit())
6617 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6618 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006619
Eric Christopher30ef0e52010-06-03 04:07:48 +00006620 case TLSModel::InitialExec:
6621 case TLSModel::LocalExec:
6622 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6623 Subtarget->is64Bit());
6624 }
6625 } else if (Subtarget->isTargetDarwin()) {
6626 // Darwin only has one model of TLS. Lower to that.
6627 unsigned char OpFlag = 0;
6628 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6629 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006630
Eric Christopher30ef0e52010-06-03 04:07:48 +00006631 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6632 // global base reg.
6633 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6634 !Subtarget->is64Bit();
6635 if (PIC32)
6636 OpFlag = X86II::MO_TLVP_PIC_BASE;
6637 else
6638 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006639 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006640 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006641 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006642 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006643 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006644
Eric Christopher30ef0e52010-06-03 04:07:48 +00006645 // With PIC32, the address is actually $g + Offset.
6646 if (PIC32)
6647 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6648 DAG.getNode(X86ISD::GlobalBaseReg,
6649 DebugLoc(), getPointerTy()),
6650 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006651
Eric Christopher30ef0e52010-06-03 04:07:48 +00006652 // Lowering the machine isd will make sure everything is in the right
6653 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006654 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006655 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006656 SDValue Args[] = { Chain, Offset };
6657 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006658
Eric Christopher30ef0e52010-06-03 04:07:48 +00006659 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6660 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6661 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006662
Eric Christopher30ef0e52010-06-03 04:07:48 +00006663 // And our return value (tls address) is in the standard call return value
6664 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006665 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6666 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006667 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006668
Eric Christopher30ef0e52010-06-03 04:07:48 +00006669 assert(false &&
6670 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006671
Torok Edwinc23197a2009-07-14 16:55:14 +00006672 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006673 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006674}
6675
Evan Cheng0db9fe62006-04-25 20:13:52 +00006676
Nadav Rotem43012222011-05-11 08:12:09 +00006677/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006678/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006679SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006680 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006681 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006682 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006683 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006684 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006685 SDValue ShOpLo = Op.getOperand(0);
6686 SDValue ShOpHi = Op.getOperand(1);
6687 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006688 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006689 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006690 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006691
Dan Gohman475871a2008-07-27 21:46:04 +00006692 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006693 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006694 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6695 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006696 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006697 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6698 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006699 }
Evan Chenge3413162006-01-09 18:33:28 +00006700
Owen Anderson825b72b2009-08-11 20:47:22 +00006701 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6702 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006703 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006704 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006705
Dan Gohman475871a2008-07-27 21:46:04 +00006706 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006707 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006708 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6709 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006710
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006711 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006712 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6713 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006714 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006715 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6716 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006717 }
6718
Dan Gohman475871a2008-07-27 21:46:04 +00006719 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006720 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006721}
Evan Chenga3195e82006-01-12 22:54:21 +00006722
Dan Gohmand858e902010-04-17 15:26:15 +00006723SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6724 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006725 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006726
Dale Johannesen0488fb62010-09-30 23:57:10 +00006727 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006728 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006729
Owen Anderson825b72b2009-08-11 20:47:22 +00006730 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006731 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006732
Eli Friedman36df4992009-05-27 00:47:34 +00006733 // These are really Legal; return the operand so the caller accepts it as
6734 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006735 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006736 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006737 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006738 Subtarget->is64Bit()) {
6739 return Op;
6740 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006741
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006742 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006743 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006744 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006745 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006746 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006747 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006748 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006749 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006750 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006751 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6752}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006753
Owen Andersone50ed302009-08-10 22:56:29 +00006754SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006755 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006756 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006757 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006758 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006759 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006760 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006761 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006762 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006763 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006764 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006765
Chris Lattner492a43e2010-09-22 01:28:21 +00006766 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006767
Stuart Hastings84be9582011-06-02 15:57:11 +00006768 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6769 MachineMemOperand *MMO;
6770 if (FI) {
6771 int SSFI = FI->getIndex();
6772 MMO =
6773 DAG.getMachineFunction()
6774 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6775 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6776 } else {
6777 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6778 StackSlot = StackSlot.getOperand(1);
6779 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006780 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006781 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6782 X86ISD::FILD, DL,
6783 Tys, Ops, array_lengthof(Ops),
6784 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006785
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006786 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006787 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006788 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006789
6790 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6791 // shouldn't be necessary except that RFP cannot be live across
6792 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006793 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006794 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6795 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006796 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006797 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006798 SDValue Ops[] = {
6799 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6800 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006801 MachineMemOperand *MMO =
6802 DAG.getMachineFunction()
6803 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006804 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006805
Chris Lattner492a43e2010-09-22 01:28:21 +00006806 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6807 Ops, array_lengthof(Ops),
6808 Op.getValueType(), MMO);
6809 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006810 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006811 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006812 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006813
Evan Cheng0db9fe62006-04-25 20:13:52 +00006814 return Result;
6815}
6816
Bill Wendling8b8a6362009-01-17 03:56:04 +00006817// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006818SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6819 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006820 // This algorithm is not obvious. Here it is in C code, more or less:
6821 /*
6822 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6823 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6824 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006825
Bill Wendling8b8a6362009-01-17 03:56:04 +00006826 // Copy ints to xmm registers.
6827 __m128i xh = _mm_cvtsi32_si128( hi );
6828 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006829
Bill Wendling8b8a6362009-01-17 03:56:04 +00006830 // Combine into low half of a single xmm register.
6831 __m128i x = _mm_unpacklo_epi32( xh, xl );
6832 __m128d d;
6833 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006834
Bill Wendling8b8a6362009-01-17 03:56:04 +00006835 // Merge in appropriate exponents to give the integer bits the right
6836 // magnitude.
6837 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006838
Bill Wendling8b8a6362009-01-17 03:56:04 +00006839 // Subtract away the biases to deal with the IEEE-754 double precision
6840 // implicit 1.
6841 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006842
Bill Wendling8b8a6362009-01-17 03:56:04 +00006843 // All conversions up to here are exact. The correctly rounded result is
6844 // calculated using the current rounding mode using the following
6845 // horizontal add.
6846 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6847 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6848 // store doesn't really need to be here (except
6849 // maybe to zero the other double)
6850 return sd;
6851 }
6852 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006853
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006854 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006855 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006856
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006857 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006858 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006859 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6860 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6861 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6862 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006863 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006864 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006865
Bill Wendling8b8a6362009-01-17 03:56:04 +00006866 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006867 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006868 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006869 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006870 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006871 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006872 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006873
Owen Anderson825b72b2009-08-11 20:47:22 +00006874 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6875 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006876 Op.getOperand(0),
6877 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006878 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6879 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006880 Op.getOperand(0),
6881 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006882 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6883 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006884 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006885 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006886 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006887 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006888 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006889 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006890 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006891 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006892
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006893 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006894 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006895 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6896 DAG.getUNDEF(MVT::v2f64), ShufMask);
6897 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6898 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006899 DAG.getIntPtrConstant(0));
6900}
6901
Bill Wendling8b8a6362009-01-17 03:56:04 +00006902// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006903SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6904 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006905 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006906 // FP constant to bias correct the final result.
6907 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006908 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006909
6910 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006911 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6912 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006913 Op.getOperand(0),
6914 DAG.getIntPtrConstant(0)));
6915
Owen Anderson825b72b2009-08-11 20:47:22 +00006916 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006917 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006918 DAG.getIntPtrConstant(0));
6919
6920 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006921 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006922 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006923 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006924 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006925 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006926 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006927 MVT::v2f64, Bias)));
6928 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006929 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006930 DAG.getIntPtrConstant(0));
6931
6932 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006933 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006934
6935 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006936 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006937
Owen Anderson825b72b2009-08-11 20:47:22 +00006938 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006939 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006940 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006941 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006942 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006943 }
6944
6945 // Handle final rounding.
6946 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006947}
6948
Dan Gohmand858e902010-04-17 15:26:15 +00006949SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6950 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006951 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006952 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006953
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006954 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006955 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6956 // the optimization here.
6957 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006958 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006959
Owen Andersone50ed302009-08-10 22:56:29 +00006960 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006961 EVT DstVT = Op.getValueType();
6962 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006963 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006964 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006965 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006966
6967 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006968 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006969 if (SrcVT == MVT::i32) {
6970 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6971 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6972 getPointerTy(), StackSlot, WordOff);
6973 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006974 StackSlot, MachinePointerInfo(),
6975 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006976 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006977 OffsetSlot, MachinePointerInfo(),
6978 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006979 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6980 return Fild;
6981 }
6982
6983 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6984 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006985 StackSlot, MachinePointerInfo(),
6986 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006987 // For i64 source, we need to add the appropriate power of 2 if the input
6988 // was negative. This is the same as the optimization in
6989 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6990 // we must be careful to do the computation in x87 extended precision, not
6991 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006992 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6993 MachineMemOperand *MMO =
6994 DAG.getMachineFunction()
6995 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6996 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006997
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006998 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6999 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007000 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7001 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007002
7003 APInt FF(32, 0x5F800000ULL);
7004
7005 // Check whether the sign bit is set.
7006 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7007 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7008 ISD::SETLT);
7009
7010 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7011 SDValue FudgePtr = DAG.getConstantPool(
7012 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7013 getPointerTy());
7014
7015 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7016 SDValue Zero = DAG.getIntPtrConstant(0);
7017 SDValue Four = DAG.getIntPtrConstant(4);
7018 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7019 Zero, Four);
7020 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7021
7022 // Load the value out, extending it from f32 to f80.
7023 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007024 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007025 FudgePtr, MachinePointerInfo::getConstantPool(),
7026 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007027 // Extend everything to 80 bits to force it to be done on x87.
7028 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7029 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007030}
7031
Dan Gohman475871a2008-07-27 21:46:04 +00007032std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007033FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007034 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007035
Owen Andersone50ed302009-08-10 22:56:29 +00007036 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007037
7038 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007039 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7040 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007041 }
7042
Owen Anderson825b72b2009-08-11 20:47:22 +00007043 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7044 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007045 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007046
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007047 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007048 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007049 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007050 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007051 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007053 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007054 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007055
Evan Cheng87c89352007-10-15 20:11:21 +00007056 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7057 // stack slot.
7058 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007059 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007060 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007061 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007062
Michael J. Spencerec38de22010-10-10 22:04:20 +00007063
7064
Evan Cheng0db9fe62006-04-25 20:13:52 +00007065 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007067 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7069 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7070 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007071 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007072
Dan Gohman475871a2008-07-27 21:46:04 +00007073 SDValue Chain = DAG.getEntryNode();
7074 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007075 EVT TheVT = Op.getOperand(0).getValueType();
7076 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007077 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007078 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007079 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007080 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007081 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007082 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007083 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007084 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007085
Chris Lattner492a43e2010-09-22 01:28:21 +00007086 MachineMemOperand *MMO =
7087 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7088 MachineMemOperand::MOLoad, MemSize, MemSize);
7089 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7090 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007091 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007092 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007093 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7094 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007095
Chris Lattner07290932010-09-22 01:05:16 +00007096 MachineMemOperand *MMO =
7097 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7098 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007099
Evan Cheng0db9fe62006-04-25 20:13:52 +00007100 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007101 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007102 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7103 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007104
Chris Lattner27a6c732007-11-24 07:07:01 +00007105 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007106}
7107
Dan Gohmand858e902010-04-17 15:26:15 +00007108SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7109 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007110 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007111 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007112
Eli Friedman948e95a2009-05-23 09:59:16 +00007113 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007114 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007115 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7116 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007117
Chris Lattner27a6c732007-11-24 07:07:01 +00007118 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007119 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007120 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007121}
7122
Dan Gohmand858e902010-04-17 15:26:15 +00007123SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7124 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007125 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7126 SDValue FIST = Vals.first, StackSlot = Vals.second;
7127 assert(FIST.getNode() && "Unexpected failure");
7128
7129 // Load the result.
7130 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007131 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007132}
7133
Dan Gohmand858e902010-04-17 15:26:15 +00007134SDValue X86TargetLowering::LowerFABS(SDValue Op,
7135 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007136 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007137 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007138 EVT VT = Op.getValueType();
7139 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007140 if (VT.isVector())
7141 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007142 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007144 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007145 CV.push_back(C);
7146 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007147 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007148 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007149 CV.push_back(C);
7150 CV.push_back(C);
7151 CV.push_back(C);
7152 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007153 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007154 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007155 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007156 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007157 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007158 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007159 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007160}
7161
Dan Gohmand858e902010-04-17 15:26:15 +00007162SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007163 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007164 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007165 EVT VT = Op.getValueType();
7166 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007167 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007168 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007169 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007170 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007171 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007172 CV.push_back(C);
7173 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007174 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007175 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007176 CV.push_back(C);
7177 CV.push_back(C);
7178 CV.push_back(C);
7179 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007180 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007181 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007182 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007183 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007184 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007185 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007186 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007187 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007188 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007189 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007190 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007191 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007192 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007193 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007194 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007195}
7196
Dan Gohmand858e902010-04-17 15:26:15 +00007197SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007198 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007199 SDValue Op0 = Op.getOperand(0);
7200 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007201 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007202 EVT VT = Op.getValueType();
7203 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007204
7205 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007206 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007207 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007208 SrcVT = VT;
7209 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007210 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007211 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007212 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007213 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007214 }
7215
7216 // At this point the operands and the result should have the same
7217 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007218
Evan Cheng68c47cb2007-01-05 07:55:56 +00007219 // First get the sign bit of second operand.
7220 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007221 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007222 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7223 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007224 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007225 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7226 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7227 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7228 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007229 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007230 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007231 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007232 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007233 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007234 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007235 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007236
7237 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007238 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007239 // Op0 is MVT::f32, Op1 is MVT::f64.
7240 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7241 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7242 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007243 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007244 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007245 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007246 }
7247
Evan Cheng73d6cf12007-01-05 21:37:56 +00007248 // Clear first operand sign bit.
7249 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007250 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007251 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7252 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007253 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007254 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7255 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7256 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7257 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007258 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007259 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007260 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007261 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007262 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007263 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007264 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007265
7266 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007267 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007268}
7269
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007270SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7271 SDValue N0 = Op.getOperand(0);
7272 DebugLoc dl = Op.getDebugLoc();
7273 EVT VT = Op.getValueType();
7274
7275 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7276 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7277 DAG.getConstant(1, VT));
7278 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7279}
7280
Dan Gohman076aee32009-03-04 19:44:21 +00007281/// Emit nodes that will be selected as "test Op0,Op0", or something
7282/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007283SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007284 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007285 DebugLoc dl = Op.getDebugLoc();
7286
Dan Gohman31125812009-03-07 01:58:32 +00007287 // CF and OF aren't always set the way we want. Determine which
7288 // of these we need.
7289 bool NeedCF = false;
7290 bool NeedOF = false;
7291 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007292 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007293 case X86::COND_A: case X86::COND_AE:
7294 case X86::COND_B: case X86::COND_BE:
7295 NeedCF = true;
7296 break;
7297 case X86::COND_G: case X86::COND_GE:
7298 case X86::COND_L: case X86::COND_LE:
7299 case X86::COND_O: case X86::COND_NO:
7300 NeedOF = true;
7301 break;
Dan Gohman31125812009-03-07 01:58:32 +00007302 }
7303
Dan Gohman076aee32009-03-04 19:44:21 +00007304 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007305 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7306 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007307 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7308 // Emit a CMP with 0, which is the TEST pattern.
7309 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7310 DAG.getConstant(0, Op.getValueType()));
7311
7312 unsigned Opcode = 0;
7313 unsigned NumOperands = 0;
7314 switch (Op.getNode()->getOpcode()) {
7315 case ISD::ADD:
7316 // Due to an isel shortcoming, be conservative if this add is likely to be
7317 // selected as part of a load-modify-store instruction. When the root node
7318 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7319 // uses of other nodes in the match, such as the ADD in this case. This
7320 // leads to the ADD being left around and reselected, with the result being
7321 // two adds in the output. Alas, even if none our users are stores, that
7322 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7323 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7324 // climbing the DAG back to the root, and it doesn't seem to be worth the
7325 // effort.
7326 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007327 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007328 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7329 goto default_case;
7330
7331 if (ConstantSDNode *C =
7332 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7333 // An add of one will be selected as an INC.
7334 if (C->getAPIntValue() == 1) {
7335 Opcode = X86ISD::INC;
7336 NumOperands = 1;
7337 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007338 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007339
7340 // An add of negative one (subtract of one) will be selected as a DEC.
7341 if (C->getAPIntValue().isAllOnesValue()) {
7342 Opcode = X86ISD::DEC;
7343 NumOperands = 1;
7344 break;
7345 }
Dan Gohman076aee32009-03-04 19:44:21 +00007346 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007347
7348 // Otherwise use a regular EFLAGS-setting add.
7349 Opcode = X86ISD::ADD;
7350 NumOperands = 2;
7351 break;
7352 case ISD::AND: {
7353 // If the primary and result isn't used, don't bother using X86ISD::AND,
7354 // because a TEST instruction will be better.
7355 bool NonFlagUse = false;
7356 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7357 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7358 SDNode *User = *UI;
7359 unsigned UOpNo = UI.getOperandNo();
7360 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7361 // Look pass truncate.
7362 UOpNo = User->use_begin().getOperandNo();
7363 User = *User->use_begin();
7364 }
7365
7366 if (User->getOpcode() != ISD::BRCOND &&
7367 User->getOpcode() != ISD::SETCC &&
7368 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7369 NonFlagUse = true;
7370 break;
7371 }
Dan Gohman076aee32009-03-04 19:44:21 +00007372 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007373
7374 if (!NonFlagUse)
7375 break;
7376 }
7377 // FALL THROUGH
7378 case ISD::SUB:
7379 case ISD::OR:
7380 case ISD::XOR:
7381 // Due to the ISEL shortcoming noted above, be conservative if this op is
7382 // likely to be selected as part of a load-modify-store instruction.
7383 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7384 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7385 if (UI->getOpcode() == ISD::STORE)
7386 goto default_case;
7387
7388 // Otherwise use a regular EFLAGS-setting instruction.
7389 switch (Op.getNode()->getOpcode()) {
7390 default: llvm_unreachable("unexpected operator!");
7391 case ISD::SUB: Opcode = X86ISD::SUB; break;
7392 case ISD::OR: Opcode = X86ISD::OR; break;
7393 case ISD::XOR: Opcode = X86ISD::XOR; break;
7394 case ISD::AND: Opcode = X86ISD::AND; break;
7395 }
7396
7397 NumOperands = 2;
7398 break;
7399 case X86ISD::ADD:
7400 case X86ISD::SUB:
7401 case X86ISD::INC:
7402 case X86ISD::DEC:
7403 case X86ISD::OR:
7404 case X86ISD::XOR:
7405 case X86ISD::AND:
7406 return SDValue(Op.getNode(), 1);
7407 default:
7408 default_case:
7409 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007410 }
7411
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007412 if (Opcode == 0)
7413 // Emit a CMP with 0, which is the TEST pattern.
7414 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7415 DAG.getConstant(0, Op.getValueType()));
7416
7417 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7418 SmallVector<SDValue, 4> Ops;
7419 for (unsigned i = 0; i != NumOperands; ++i)
7420 Ops.push_back(Op.getOperand(i));
7421
7422 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7423 DAG.ReplaceAllUsesWith(Op, New);
7424 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007425}
7426
7427/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7428/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007429SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007430 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007431 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7432 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007433 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007434
7435 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007436 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007437}
7438
Evan Chengd40d03e2010-01-06 19:38:29 +00007439/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7440/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007441SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7442 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007443 SDValue Op0 = And.getOperand(0);
7444 SDValue Op1 = And.getOperand(1);
7445 if (Op0.getOpcode() == ISD::TRUNCATE)
7446 Op0 = Op0.getOperand(0);
7447 if (Op1.getOpcode() == ISD::TRUNCATE)
7448 Op1 = Op1.getOperand(0);
7449
Evan Chengd40d03e2010-01-06 19:38:29 +00007450 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007451 if (Op1.getOpcode() == ISD::SHL)
7452 std::swap(Op0, Op1);
7453 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007454 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7455 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007456 // If we looked past a truncate, check that it's only truncating away
7457 // known zeros.
7458 unsigned BitWidth = Op0.getValueSizeInBits();
7459 unsigned AndBitWidth = And.getValueSizeInBits();
7460 if (BitWidth > AndBitWidth) {
7461 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7462 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7463 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7464 return SDValue();
7465 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007466 LHS = Op1;
7467 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007468 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007469 } else if (Op1.getOpcode() == ISD::Constant) {
7470 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7471 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007472 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7473 LHS = AndLHS.getOperand(0);
7474 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007475 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007476 }
Evan Cheng0488db92007-09-25 01:57:46 +00007477
Evan Chengd40d03e2010-01-06 19:38:29 +00007478 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007479 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007480 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007481 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007482 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007483 // Also promote i16 to i32 for performance / code size reason.
7484 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007485 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007486 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007487
Evan Chengd40d03e2010-01-06 19:38:29 +00007488 // If the operand types disagree, extend the shift amount to match. Since
7489 // BT ignores high bits (like shifts) we can use anyextend.
7490 if (LHS.getValueType() != RHS.getValueType())
7491 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007492
Evan Chengd40d03e2010-01-06 19:38:29 +00007493 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7494 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7495 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7496 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007497 }
7498
Evan Cheng54de3ea2010-01-05 06:52:31 +00007499 return SDValue();
7500}
7501
Dan Gohmand858e902010-04-17 15:26:15 +00007502SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007503 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7504 SDValue Op0 = Op.getOperand(0);
7505 SDValue Op1 = Op.getOperand(1);
7506 DebugLoc dl = Op.getDebugLoc();
7507 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7508
7509 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007510 // Lower (X & (1 << N)) == 0 to BT(X, N).
7511 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7512 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007513 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007514 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007515 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007516 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7517 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7518 if (NewSetCC.getNode())
7519 return NewSetCC;
7520 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007521
Chris Lattner481eebc2010-12-19 21:23:48 +00007522 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7523 // these.
7524 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007525 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007526 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7527 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007528
Chris Lattner481eebc2010-12-19 21:23:48 +00007529 // If the input is a setcc, then reuse the input setcc or use a new one with
7530 // the inverted condition.
7531 if (Op0.getOpcode() == X86ISD::SETCC) {
7532 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7533 bool Invert = (CC == ISD::SETNE) ^
7534 cast<ConstantSDNode>(Op1)->isNullValue();
7535 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007536
Evan Cheng2c755ba2010-02-27 07:36:59 +00007537 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007538 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7539 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7540 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007541 }
7542
Evan Chenge5b51ac2010-04-17 06:13:15 +00007543 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007544 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007545 if (X86CC == X86::COND_INVALID)
7546 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007547
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007548 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007549 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007550 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007551}
7552
Dan Gohmand858e902010-04-17 15:26:15 +00007553SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007554 SDValue Cond;
7555 SDValue Op0 = Op.getOperand(0);
7556 SDValue Op1 = Op.getOperand(1);
7557 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007558 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007559 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7560 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007561 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007562
7563 if (isFP) {
7564 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007565 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007566 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7567 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007568 bool Swap = false;
7569
7570 switch (SetCCOpcode) {
7571 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007572 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007573 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007574 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007575 case ISD::SETGT: Swap = true; // Fallthrough
7576 case ISD::SETLT:
7577 case ISD::SETOLT: SSECC = 1; break;
7578 case ISD::SETOGE:
7579 case ISD::SETGE: Swap = true; // Fallthrough
7580 case ISD::SETLE:
7581 case ISD::SETOLE: SSECC = 2; break;
7582 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007583 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007584 case ISD::SETNE: SSECC = 4; break;
7585 case ISD::SETULE: Swap = true;
7586 case ISD::SETUGE: SSECC = 5; break;
7587 case ISD::SETULT: Swap = true;
7588 case ISD::SETUGT: SSECC = 6; break;
7589 case ISD::SETO: SSECC = 7; break;
7590 }
7591 if (Swap)
7592 std::swap(Op0, Op1);
7593
Nate Begemanfb8ead02008-07-25 19:05:58 +00007594 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007595 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007596 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007597 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007598 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7599 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007600 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007601 }
7602 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007603 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007604 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7605 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007606 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007607 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007608 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007609 }
7610 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007611 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007612 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007613
Nate Begeman30a0de92008-07-17 16:51:19 +00007614 // We are handling one of the integer comparisons here. Since SSE only has
7615 // GT and EQ comparisons for integer, swapping operands and multiple
7616 // operations may be required for some comparisons.
7617 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7618 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007619
Owen Anderson825b72b2009-08-11 20:47:22 +00007620 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007621 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007622 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007623 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007624 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7625 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007626 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007627
Nate Begeman30a0de92008-07-17 16:51:19 +00007628 switch (SetCCOpcode) {
7629 default: break;
7630 case ISD::SETNE: Invert = true;
7631 case ISD::SETEQ: Opc = EQOpc; break;
7632 case ISD::SETLT: Swap = true;
7633 case ISD::SETGT: Opc = GTOpc; break;
7634 case ISD::SETGE: Swap = true;
7635 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7636 case ISD::SETULT: Swap = true;
7637 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7638 case ISD::SETUGE: Swap = true;
7639 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7640 }
7641 if (Swap)
7642 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007643
Nate Begeman30a0de92008-07-17 16:51:19 +00007644 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7645 // bits of the inputs before performing those operations.
7646 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007647 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007648 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7649 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007650 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007651 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7652 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007653 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7654 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007655 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007656
Dale Johannesenace16102009-02-03 19:33:06 +00007657 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007658
7659 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007660 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007661 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007662
Nate Begeman30a0de92008-07-17 16:51:19 +00007663 return Result;
7664}
Evan Cheng0488db92007-09-25 01:57:46 +00007665
Evan Cheng370e5342008-12-03 08:38:43 +00007666// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007667static bool isX86LogicalCmp(SDValue Op) {
7668 unsigned Opc = Op.getNode()->getOpcode();
7669 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7670 return true;
7671 if (Op.getResNo() == 1 &&
7672 (Opc == X86ISD::ADD ||
7673 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007674 Opc == X86ISD::ADC ||
7675 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007676 Opc == X86ISD::SMUL ||
7677 Opc == X86ISD::UMUL ||
7678 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007679 Opc == X86ISD::DEC ||
7680 Opc == X86ISD::OR ||
7681 Opc == X86ISD::XOR ||
7682 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007683 return true;
7684
Chris Lattner9637d5b2010-12-05 07:49:54 +00007685 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7686 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007687
Dan Gohman076aee32009-03-04 19:44:21 +00007688 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007689}
7690
Chris Lattnera2b56002010-12-05 01:23:24 +00007691static bool isZero(SDValue V) {
7692 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7693 return C && C->isNullValue();
7694}
7695
Chris Lattner96908b12010-12-05 02:00:51 +00007696static bool isAllOnes(SDValue V) {
7697 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7698 return C && C->isAllOnesValue();
7699}
7700
Dan Gohmand858e902010-04-17 15:26:15 +00007701SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007702 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007703 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007704 SDValue Op1 = Op.getOperand(1);
7705 SDValue Op2 = Op.getOperand(2);
7706 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007707 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007708
Dan Gohman1a492952009-10-20 16:22:37 +00007709 if (Cond.getOpcode() == ISD::SETCC) {
7710 SDValue NewCond = LowerSETCC(Cond, DAG);
7711 if (NewCond.getNode())
7712 Cond = NewCond;
7713 }
Evan Cheng734503b2006-09-11 02:19:56 +00007714
Chris Lattnera2b56002010-12-05 01:23:24 +00007715 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007716 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007717 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007718 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007719 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007720 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7721 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007722 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007723
Chris Lattnera2b56002010-12-05 01:23:24 +00007724 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007725
7726 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007727 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7728 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007729
7730 SDValue CmpOp0 = Cmp.getOperand(0);
7731 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7732 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007733
Chris Lattner96908b12010-12-05 02:00:51 +00007734 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007735 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7736 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007737
Chris Lattner96908b12010-12-05 02:00:51 +00007738 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7739 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007740
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007741 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007742 if (N2C == 0 || !N2C->isNullValue())
7743 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7744 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007745 }
7746 }
7747
Chris Lattnera2b56002010-12-05 01:23:24 +00007748 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007749 if (Cond.getOpcode() == ISD::AND &&
7750 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7751 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007752 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007753 Cond = Cond.getOperand(0);
7754 }
7755
Evan Cheng3f41d662007-10-08 22:16:29 +00007756 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7757 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007758 if (Cond.getOpcode() == X86ISD::SETCC ||
7759 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007760 CC = Cond.getOperand(0);
7761
Dan Gohman475871a2008-07-27 21:46:04 +00007762 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007763 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007764 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007765
Evan Cheng3f41d662007-10-08 22:16:29 +00007766 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007767 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007768 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007769 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007770
Chris Lattnerd1980a52009-03-12 06:52:53 +00007771 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7772 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007773 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007774 addTest = false;
7775 }
7776 }
7777
7778 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007779 // Look pass the truncate.
7780 if (Cond.getOpcode() == ISD::TRUNCATE)
7781 Cond = Cond.getOperand(0);
7782
7783 // We know the result of AND is compared against zero. Try to match
7784 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007785 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007786 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007787 if (NewSetCC.getNode()) {
7788 CC = NewSetCC.getOperand(0);
7789 Cond = NewSetCC.getOperand(1);
7790 addTest = false;
7791 }
7792 }
7793 }
7794
7795 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007796 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007797 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007798 }
7799
Benjamin Kramere915ff32010-12-22 23:09:28 +00007800 // a < b ? -1 : 0 -> RES = ~setcc_carry
7801 // a < b ? 0 : -1 -> RES = setcc_carry
7802 // a >= b ? -1 : 0 -> RES = setcc_carry
7803 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7804 if (Cond.getOpcode() == X86ISD::CMP) {
7805 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7806
7807 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7808 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7809 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7810 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7811 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7812 return DAG.getNOT(DL, Res, Res.getValueType());
7813 return Res;
7814 }
7815 }
7816
Evan Cheng0488db92007-09-25 01:57:46 +00007817 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7818 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007819 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007820 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007821 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007822}
7823
Evan Cheng370e5342008-12-03 08:38:43 +00007824// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7825// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7826// from the AND / OR.
7827static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7828 Opc = Op.getOpcode();
7829 if (Opc != ISD::OR && Opc != ISD::AND)
7830 return false;
7831 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7832 Op.getOperand(0).hasOneUse() &&
7833 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7834 Op.getOperand(1).hasOneUse());
7835}
7836
Evan Cheng961d6d42009-02-02 08:19:07 +00007837// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7838// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007839static bool isXor1OfSetCC(SDValue Op) {
7840 if (Op.getOpcode() != ISD::XOR)
7841 return false;
7842 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7843 if (N1C && N1C->getAPIntValue() == 1) {
7844 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7845 Op.getOperand(0).hasOneUse();
7846 }
7847 return false;
7848}
7849
Dan Gohmand858e902010-04-17 15:26:15 +00007850SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007851 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007852 SDValue Chain = Op.getOperand(0);
7853 SDValue Cond = Op.getOperand(1);
7854 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007855 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007856 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007857
Dan Gohman1a492952009-10-20 16:22:37 +00007858 if (Cond.getOpcode() == ISD::SETCC) {
7859 SDValue NewCond = LowerSETCC(Cond, DAG);
7860 if (NewCond.getNode())
7861 Cond = NewCond;
7862 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007863#if 0
7864 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007865 else if (Cond.getOpcode() == X86ISD::ADD ||
7866 Cond.getOpcode() == X86ISD::SUB ||
7867 Cond.getOpcode() == X86ISD::SMUL ||
7868 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007869 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007870#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007871
Evan Chengad9c0a32009-12-15 00:53:42 +00007872 // Look pass (and (setcc_carry (cmp ...)), 1).
7873 if (Cond.getOpcode() == ISD::AND &&
7874 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7875 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007876 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007877 Cond = Cond.getOperand(0);
7878 }
7879
Evan Cheng3f41d662007-10-08 22:16:29 +00007880 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7881 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007882 if (Cond.getOpcode() == X86ISD::SETCC ||
7883 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007884 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007885
Dan Gohman475871a2008-07-27 21:46:04 +00007886 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007887 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007888 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007889 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007890 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007891 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007892 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007893 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007894 default: break;
7895 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007896 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007897 // These can only come from an arithmetic instruction with overflow,
7898 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007899 Cond = Cond.getNode()->getOperand(1);
7900 addTest = false;
7901 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007902 }
Evan Cheng0488db92007-09-25 01:57:46 +00007903 }
Evan Cheng370e5342008-12-03 08:38:43 +00007904 } else {
7905 unsigned CondOpc;
7906 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7907 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007908 if (CondOpc == ISD::OR) {
7909 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7910 // two branches instead of an explicit OR instruction with a
7911 // separate test.
7912 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007913 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007914 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007915 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007916 Chain, Dest, CC, Cmp);
7917 CC = Cond.getOperand(1).getOperand(0);
7918 Cond = Cmp;
7919 addTest = false;
7920 }
7921 } else { // ISD::AND
7922 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7923 // two branches instead of an explicit AND instruction with a
7924 // separate test. However, we only do this if this block doesn't
7925 // have a fall-through edge, because this requires an explicit
7926 // jmp when the condition is false.
7927 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007928 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007929 Op.getNode()->hasOneUse()) {
7930 X86::CondCode CCode =
7931 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7932 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007933 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007934 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007935 // Look for an unconditional branch following this conditional branch.
7936 // We need this because we need to reverse the successors in order
7937 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007938 if (User->getOpcode() == ISD::BR) {
7939 SDValue FalseBB = User->getOperand(1);
7940 SDNode *NewBR =
7941 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007942 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007943 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007944 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007945
Dale Johannesene4d209d2009-02-03 20:21:25 +00007946 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007947 Chain, Dest, CC, Cmp);
7948 X86::CondCode CCode =
7949 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7950 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007951 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007952 Cond = Cmp;
7953 addTest = false;
7954 }
7955 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007956 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007957 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7958 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7959 // It should be transformed during dag combiner except when the condition
7960 // is set by a arithmetics with overflow node.
7961 X86::CondCode CCode =
7962 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7963 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007965 Cond = Cond.getOperand(0).getOperand(1);
7966 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007967 }
Evan Cheng0488db92007-09-25 01:57:46 +00007968 }
7969
7970 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007971 // Look pass the truncate.
7972 if (Cond.getOpcode() == ISD::TRUNCATE)
7973 Cond = Cond.getOperand(0);
7974
7975 // We know the result of AND is compared against zero. Try to match
7976 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007977 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007978 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7979 if (NewSetCC.getNode()) {
7980 CC = NewSetCC.getOperand(0);
7981 Cond = NewSetCC.getOperand(1);
7982 addTest = false;
7983 }
7984 }
7985 }
7986
7987 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007988 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007989 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007990 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007991 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007992 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007993}
7994
Anton Korobeynikove060b532007-04-17 19:34:00 +00007995
7996// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7997// Calls to _alloca is needed to probe the stack when allocating more than 4k
7998// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7999// that the guard pages used by the OS virtual memory manager are allocated in
8000// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008001SDValue
8002X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008003 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008004 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008005 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008006 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008007 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008008
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008009 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008010 SDValue Chain = Op.getOperand(0);
8011 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008012 // FIXME: Ensure alignment here
8013
Dan Gohman475871a2008-07-27 21:46:04 +00008014 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008015
Owen Anderson825b72b2009-08-11 20:47:22 +00008016 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008017 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008018
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008019 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008020 Flag = Chain.getValue(1);
8021
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008022 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008023
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008024 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008025 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008026
Dale Johannesendd64c412009-02-04 00:33:20 +00008027 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008028
Dan Gohman475871a2008-07-27 21:46:04 +00008029 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008030 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008031}
8032
Dan Gohmand858e902010-04-17 15:26:15 +00008033SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008034 MachineFunction &MF = DAG.getMachineFunction();
8035 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8036
Dan Gohman69de1932008-02-06 22:27:42 +00008037 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008038 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008039
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008040 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008041 // vastart just stores the address of the VarArgsFrameIndex slot into the
8042 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008043 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8044 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008045 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8046 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008047 }
8048
8049 // __va_list_tag:
8050 // gp_offset (0 - 6 * 8)
8051 // fp_offset (48 - 48 + 8 * 16)
8052 // overflow_arg_area (point to parameters coming in memory).
8053 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008054 SmallVector<SDValue, 8> MemOps;
8055 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008056 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008057 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008058 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8059 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008060 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008061 MemOps.push_back(Store);
8062
8063 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008064 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008065 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008066 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008067 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8068 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008069 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008070 MemOps.push_back(Store);
8071
8072 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008073 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008074 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008075 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8076 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008077 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8078 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008079 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008080 MemOps.push_back(Store);
8081
8082 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008083 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008084 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008085 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8086 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008087 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8088 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008089 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008090 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008091 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008092}
8093
Dan Gohmand858e902010-04-17 15:26:15 +00008094SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008095 assert(Subtarget->is64Bit() &&
8096 "LowerVAARG only handles 64-bit va_arg!");
8097 assert((Subtarget->isTargetLinux() ||
8098 Subtarget->isTargetDarwin()) &&
8099 "Unhandled target in LowerVAARG");
8100 assert(Op.getNode()->getNumOperands() == 4);
8101 SDValue Chain = Op.getOperand(0);
8102 SDValue SrcPtr = Op.getOperand(1);
8103 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8104 unsigned Align = Op.getConstantOperandVal(3);
8105 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008106
Dan Gohman320afb82010-10-12 18:00:49 +00008107 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008108 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00008109 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8110 uint8_t ArgMode;
8111
8112 // Decide which area this value should be read from.
8113 // TODO: Implement the AMD64 ABI in its entirety. This simple
8114 // selection mechanism works only for the basic types.
8115 if (ArgVT == MVT::f80) {
8116 llvm_unreachable("va_arg for f80 not yet implemented");
8117 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8118 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8119 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8120 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8121 } else {
8122 llvm_unreachable("Unhandled argument type in LowerVAARG");
8123 }
8124
8125 if (ArgMode == 2) {
8126 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008127 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008128 !(DAG.getMachineFunction()
8129 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008130 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008131 }
8132
8133 // Insert VAARG_64 node into the DAG
8134 // VAARG_64 returns two values: Variable Argument Address, Chain
8135 SmallVector<SDValue, 11> InstOps;
8136 InstOps.push_back(Chain);
8137 InstOps.push_back(SrcPtr);
8138 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8139 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8140 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8141 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8142 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8143 VTs, &InstOps[0], InstOps.size(),
8144 MVT::i64,
8145 MachinePointerInfo(SV),
8146 /*Align=*/0,
8147 /*Volatile=*/false,
8148 /*ReadMem=*/true,
8149 /*WriteMem=*/true);
8150 Chain = VAARG.getValue(1);
8151
8152 // Load the next argument and return it
8153 return DAG.getLoad(ArgVT, dl,
8154 Chain,
8155 VAARG,
8156 MachinePointerInfo(),
8157 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008158}
8159
Dan Gohmand858e902010-04-17 15:26:15 +00008160SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008161 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008162 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008163 SDValue Chain = Op.getOperand(0);
8164 SDValue DstPtr = Op.getOperand(1);
8165 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008166 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8167 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008168 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008169
Chris Lattnere72f2022010-09-21 05:40:29 +00008170 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008171 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008172 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008173 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008174}
8175
Dan Gohman475871a2008-07-27 21:46:04 +00008176SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008177X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008178 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008179 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008180 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008181 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008182 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008183 case Intrinsic::x86_sse_comieq_ss:
8184 case Intrinsic::x86_sse_comilt_ss:
8185 case Intrinsic::x86_sse_comile_ss:
8186 case Intrinsic::x86_sse_comigt_ss:
8187 case Intrinsic::x86_sse_comige_ss:
8188 case Intrinsic::x86_sse_comineq_ss:
8189 case Intrinsic::x86_sse_ucomieq_ss:
8190 case Intrinsic::x86_sse_ucomilt_ss:
8191 case Intrinsic::x86_sse_ucomile_ss:
8192 case Intrinsic::x86_sse_ucomigt_ss:
8193 case Intrinsic::x86_sse_ucomige_ss:
8194 case Intrinsic::x86_sse_ucomineq_ss:
8195 case Intrinsic::x86_sse2_comieq_sd:
8196 case Intrinsic::x86_sse2_comilt_sd:
8197 case Intrinsic::x86_sse2_comile_sd:
8198 case Intrinsic::x86_sse2_comigt_sd:
8199 case Intrinsic::x86_sse2_comige_sd:
8200 case Intrinsic::x86_sse2_comineq_sd:
8201 case Intrinsic::x86_sse2_ucomieq_sd:
8202 case Intrinsic::x86_sse2_ucomilt_sd:
8203 case Intrinsic::x86_sse2_ucomile_sd:
8204 case Intrinsic::x86_sse2_ucomigt_sd:
8205 case Intrinsic::x86_sse2_ucomige_sd:
8206 case Intrinsic::x86_sse2_ucomineq_sd: {
8207 unsigned Opc = 0;
8208 ISD::CondCode CC = ISD::SETCC_INVALID;
8209 switch (IntNo) {
8210 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008211 case Intrinsic::x86_sse_comieq_ss:
8212 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008213 Opc = X86ISD::COMI;
8214 CC = ISD::SETEQ;
8215 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008216 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008217 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008218 Opc = X86ISD::COMI;
8219 CC = ISD::SETLT;
8220 break;
8221 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008222 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008223 Opc = X86ISD::COMI;
8224 CC = ISD::SETLE;
8225 break;
8226 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008227 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008228 Opc = X86ISD::COMI;
8229 CC = ISD::SETGT;
8230 break;
8231 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008232 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008233 Opc = X86ISD::COMI;
8234 CC = ISD::SETGE;
8235 break;
8236 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008237 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008238 Opc = X86ISD::COMI;
8239 CC = ISD::SETNE;
8240 break;
8241 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008242 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008243 Opc = X86ISD::UCOMI;
8244 CC = ISD::SETEQ;
8245 break;
8246 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008247 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008248 Opc = X86ISD::UCOMI;
8249 CC = ISD::SETLT;
8250 break;
8251 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008252 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008253 Opc = X86ISD::UCOMI;
8254 CC = ISD::SETLE;
8255 break;
8256 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008257 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008258 Opc = X86ISD::UCOMI;
8259 CC = ISD::SETGT;
8260 break;
8261 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008262 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008263 Opc = X86ISD::UCOMI;
8264 CC = ISD::SETGE;
8265 break;
8266 case Intrinsic::x86_sse_ucomineq_ss:
8267 case Intrinsic::x86_sse2_ucomineq_sd:
8268 Opc = X86ISD::UCOMI;
8269 CC = ISD::SETNE;
8270 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008271 }
Evan Cheng734503b2006-09-11 02:19:56 +00008272
Dan Gohman475871a2008-07-27 21:46:04 +00008273 SDValue LHS = Op.getOperand(1);
8274 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008275 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008276 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008277 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8278 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8279 DAG.getConstant(X86CC, MVT::i8), Cond);
8280 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008281 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008282 // ptest and testp intrinsics. The intrinsic these come from are designed to
8283 // return an integer value, not just an instruction so lower it to the ptest
8284 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008285 case Intrinsic::x86_sse41_ptestz:
8286 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008287 case Intrinsic::x86_sse41_ptestnzc:
8288 case Intrinsic::x86_avx_ptestz_256:
8289 case Intrinsic::x86_avx_ptestc_256:
8290 case Intrinsic::x86_avx_ptestnzc_256:
8291 case Intrinsic::x86_avx_vtestz_ps:
8292 case Intrinsic::x86_avx_vtestc_ps:
8293 case Intrinsic::x86_avx_vtestnzc_ps:
8294 case Intrinsic::x86_avx_vtestz_pd:
8295 case Intrinsic::x86_avx_vtestc_pd:
8296 case Intrinsic::x86_avx_vtestnzc_pd:
8297 case Intrinsic::x86_avx_vtestz_ps_256:
8298 case Intrinsic::x86_avx_vtestc_ps_256:
8299 case Intrinsic::x86_avx_vtestnzc_ps_256:
8300 case Intrinsic::x86_avx_vtestz_pd_256:
8301 case Intrinsic::x86_avx_vtestc_pd_256:
8302 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8303 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008304 unsigned X86CC = 0;
8305 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008306 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008307 case Intrinsic::x86_avx_vtestz_ps:
8308 case Intrinsic::x86_avx_vtestz_pd:
8309 case Intrinsic::x86_avx_vtestz_ps_256:
8310 case Intrinsic::x86_avx_vtestz_pd_256:
8311 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008312 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008313 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008314 // ZF = 1
8315 X86CC = X86::COND_E;
8316 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008317 case Intrinsic::x86_avx_vtestc_ps:
8318 case Intrinsic::x86_avx_vtestc_pd:
8319 case Intrinsic::x86_avx_vtestc_ps_256:
8320 case Intrinsic::x86_avx_vtestc_pd_256:
8321 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008322 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008323 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008324 // CF = 1
8325 X86CC = X86::COND_B;
8326 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008327 case Intrinsic::x86_avx_vtestnzc_ps:
8328 case Intrinsic::x86_avx_vtestnzc_pd:
8329 case Intrinsic::x86_avx_vtestnzc_ps_256:
8330 case Intrinsic::x86_avx_vtestnzc_pd_256:
8331 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008332 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008333 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008334 // ZF and CF = 0
8335 X86CC = X86::COND_A;
8336 break;
8337 }
Eric Christopherfd179292009-08-27 18:07:15 +00008338
Eric Christopher71c67532009-07-29 00:28:05 +00008339 SDValue LHS = Op.getOperand(1);
8340 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008341 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8342 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008343 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8344 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8345 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008346 }
Evan Cheng5759f972008-05-04 09:15:50 +00008347
8348 // Fix vector shift instructions where the last operand is a non-immediate
8349 // i32 value.
8350 case Intrinsic::x86_sse2_pslli_w:
8351 case Intrinsic::x86_sse2_pslli_d:
8352 case Intrinsic::x86_sse2_pslli_q:
8353 case Intrinsic::x86_sse2_psrli_w:
8354 case Intrinsic::x86_sse2_psrli_d:
8355 case Intrinsic::x86_sse2_psrli_q:
8356 case Intrinsic::x86_sse2_psrai_w:
8357 case Intrinsic::x86_sse2_psrai_d:
8358 case Intrinsic::x86_mmx_pslli_w:
8359 case Intrinsic::x86_mmx_pslli_d:
8360 case Intrinsic::x86_mmx_pslli_q:
8361 case Intrinsic::x86_mmx_psrli_w:
8362 case Intrinsic::x86_mmx_psrli_d:
8363 case Intrinsic::x86_mmx_psrli_q:
8364 case Intrinsic::x86_mmx_psrai_w:
8365 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008366 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008367 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008368 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008369
8370 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008371 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008372 switch (IntNo) {
8373 case Intrinsic::x86_sse2_pslli_w:
8374 NewIntNo = Intrinsic::x86_sse2_psll_w;
8375 break;
8376 case Intrinsic::x86_sse2_pslli_d:
8377 NewIntNo = Intrinsic::x86_sse2_psll_d;
8378 break;
8379 case Intrinsic::x86_sse2_pslli_q:
8380 NewIntNo = Intrinsic::x86_sse2_psll_q;
8381 break;
8382 case Intrinsic::x86_sse2_psrli_w:
8383 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8384 break;
8385 case Intrinsic::x86_sse2_psrli_d:
8386 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8387 break;
8388 case Intrinsic::x86_sse2_psrli_q:
8389 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8390 break;
8391 case Intrinsic::x86_sse2_psrai_w:
8392 NewIntNo = Intrinsic::x86_sse2_psra_w;
8393 break;
8394 case Intrinsic::x86_sse2_psrai_d:
8395 NewIntNo = Intrinsic::x86_sse2_psra_d;
8396 break;
8397 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008398 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008399 switch (IntNo) {
8400 case Intrinsic::x86_mmx_pslli_w:
8401 NewIntNo = Intrinsic::x86_mmx_psll_w;
8402 break;
8403 case Intrinsic::x86_mmx_pslli_d:
8404 NewIntNo = Intrinsic::x86_mmx_psll_d;
8405 break;
8406 case Intrinsic::x86_mmx_pslli_q:
8407 NewIntNo = Intrinsic::x86_mmx_psll_q;
8408 break;
8409 case Intrinsic::x86_mmx_psrli_w:
8410 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8411 break;
8412 case Intrinsic::x86_mmx_psrli_d:
8413 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8414 break;
8415 case Intrinsic::x86_mmx_psrli_q:
8416 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8417 break;
8418 case Intrinsic::x86_mmx_psrai_w:
8419 NewIntNo = Intrinsic::x86_mmx_psra_w;
8420 break;
8421 case Intrinsic::x86_mmx_psrai_d:
8422 NewIntNo = Intrinsic::x86_mmx_psra_d;
8423 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008424 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008425 }
8426 break;
8427 }
8428 }
Mon P Wangefa42202009-09-03 19:56:25 +00008429
8430 // The vector shift intrinsics with scalars uses 32b shift amounts but
8431 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8432 // to be zero.
8433 SDValue ShOps[4];
8434 ShOps[0] = ShAmt;
8435 ShOps[1] = DAG.getConstant(0, MVT::i32);
8436 if (ShAmtVT == MVT::v4i32) {
8437 ShOps[2] = DAG.getUNDEF(MVT::i32);
8438 ShOps[3] = DAG.getUNDEF(MVT::i32);
8439 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8440 } else {
8441 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008442// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008443 }
8444
Owen Andersone50ed302009-08-10 22:56:29 +00008445 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008446 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008447 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008448 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008449 Op.getOperand(1), ShAmt);
8450 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008451 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008452}
Evan Cheng72261582005-12-20 06:22:03 +00008453
Dan Gohmand858e902010-04-17 15:26:15 +00008454SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8455 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008456 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8457 MFI->setReturnAddressIsTaken(true);
8458
Bill Wendling64e87322009-01-16 19:25:27 +00008459 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008460 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008461
8462 if (Depth > 0) {
8463 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8464 SDValue Offset =
8465 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008466 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008467 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008468 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008469 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008470 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008471 }
8472
8473 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008474 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008475 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008476 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008477}
8478
Dan Gohmand858e902010-04-17 15:26:15 +00008479SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008480 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8481 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008482
Owen Andersone50ed302009-08-10 22:56:29 +00008483 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008484 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008485 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8486 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008487 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008488 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008489 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8490 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008491 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008492 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008493}
8494
Dan Gohman475871a2008-07-27 21:46:04 +00008495SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008496 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008497 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008498}
8499
Dan Gohmand858e902010-04-17 15:26:15 +00008500SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008501 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008502 SDValue Chain = Op.getOperand(0);
8503 SDValue Offset = Op.getOperand(1);
8504 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008505 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008506
Dan Gohmand8816272010-08-11 18:14:00 +00008507 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8508 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8509 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008510 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008511
Dan Gohmand8816272010-08-11 18:14:00 +00008512 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8513 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008514 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008515 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8516 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008517 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008518 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008519
Dale Johannesene4d209d2009-02-03 20:21:25 +00008520 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008521 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008522 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008523}
8524
Dan Gohman475871a2008-07-27 21:46:04 +00008525SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008526 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008527 SDValue Root = Op.getOperand(0);
8528 SDValue Trmp = Op.getOperand(1); // trampoline
8529 SDValue FPtr = Op.getOperand(2); // nested function
8530 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008531 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008532
Dan Gohman69de1932008-02-06 22:27:42 +00008533 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008534
8535 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008536 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008537
8538 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008539 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8540 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008541
Evan Cheng0e6a0522011-07-18 20:57:22 +00008542 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
8543 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008544
8545 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8546
8547 // Load the pointer to the nested function into R11.
8548 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008549 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008550 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008551 Addr, MachinePointerInfo(TrmpAddr),
8552 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008553
Owen Anderson825b72b2009-08-11 20:47:22 +00008554 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8555 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008556 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8557 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008558 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008559
8560 // Load the 'nest' parameter value into R10.
8561 // R10 is specified in X86CallingConv.td
8562 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008563 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8564 DAG.getConstant(10, MVT::i64));
8565 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008566 Addr, MachinePointerInfo(TrmpAddr, 10),
8567 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008568
Owen Anderson825b72b2009-08-11 20:47:22 +00008569 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8570 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008571 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8572 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008573 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008574
8575 // Jump to the nested function.
8576 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008577 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8578 DAG.getConstant(20, MVT::i64));
8579 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008580 Addr, MachinePointerInfo(TrmpAddr, 20),
8581 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008582
8583 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008584 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8585 DAG.getConstant(22, MVT::i64));
8586 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008587 MachinePointerInfo(TrmpAddr, 22),
8588 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008589
Dan Gohman475871a2008-07-27 21:46:04 +00008590 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008592 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008593 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008594 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008595 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008596 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008597 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008598
8599 switch (CC) {
8600 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008601 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008602 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008603 case CallingConv::X86_StdCall: {
8604 // Pass 'nest' parameter in ECX.
8605 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008606 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008607
8608 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008609 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008610 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008611
Chris Lattner58d74912008-03-12 17:45:29 +00008612 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008613 unsigned InRegCount = 0;
8614 unsigned Idx = 1;
8615
8616 for (FunctionType::param_iterator I = FTy->param_begin(),
8617 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008618 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008619 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008620 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008621
8622 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008623 report_fatal_error("Nest register in use - reduce number of inreg"
8624 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008625 }
8626 }
8627 break;
8628 }
8629 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008630 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008631 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008632 // Pass 'nest' parameter in EAX.
8633 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008634 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008635 break;
8636 }
8637
Dan Gohman475871a2008-07-27 21:46:04 +00008638 SDValue OutChains[4];
8639 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008640
Owen Anderson825b72b2009-08-11 20:47:22 +00008641 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8642 DAG.getConstant(10, MVT::i32));
8643 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008644
Chris Lattnera62fe662010-02-05 19:20:30 +00008645 // This is storing the opcode for MOV32ri.
8646 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00008647 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008648 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008649 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008650 Trmp, MachinePointerInfo(TrmpAddr),
8651 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008652
Owen Anderson825b72b2009-08-11 20:47:22 +00008653 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8654 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008655 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8656 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008657 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008658
Chris Lattnera62fe662010-02-05 19:20:30 +00008659 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008660 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8661 DAG.getConstant(5, MVT::i32));
8662 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008663 MachinePointerInfo(TrmpAddr, 5),
8664 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008665
Owen Anderson825b72b2009-08-11 20:47:22 +00008666 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8667 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008668 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8669 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008670 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008671
Dan Gohman475871a2008-07-27 21:46:04 +00008672 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008673 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008674 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008675 }
8676}
8677
Dan Gohmand858e902010-04-17 15:26:15 +00008678SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8679 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008680 /*
8681 The rounding mode is in bits 11:10 of FPSR, and has the following
8682 settings:
8683 00 Round to nearest
8684 01 Round to -inf
8685 10 Round to +inf
8686 11 Round to 0
8687
8688 FLT_ROUNDS, on the other hand, expects the following:
8689 -1 Undefined
8690 0 Round to 0
8691 1 Round to nearest
8692 2 Round to +inf
8693 3 Round to -inf
8694
8695 To perform the conversion, we do:
8696 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8697 */
8698
8699 MachineFunction &MF = DAG.getMachineFunction();
8700 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008701 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008702 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008703 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008704 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008705
8706 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008707 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008708 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008709
Michael J. Spencerec38de22010-10-10 22:04:20 +00008710
Chris Lattner2156b792010-09-22 01:11:26 +00008711 MachineMemOperand *MMO =
8712 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8713 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008714
Chris Lattner2156b792010-09-22 01:11:26 +00008715 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8716 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8717 DAG.getVTList(MVT::Other),
8718 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008719
8720 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008721 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008722 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008723
8724 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008725 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008726 DAG.getNode(ISD::SRL, DL, MVT::i16,
8727 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008728 CWD, DAG.getConstant(0x800, MVT::i16)),
8729 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008730 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008731 DAG.getNode(ISD::SRL, DL, MVT::i16,
8732 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008733 CWD, DAG.getConstant(0x400, MVT::i16)),
8734 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008735
Dan Gohman475871a2008-07-27 21:46:04 +00008736 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008737 DAG.getNode(ISD::AND, DL, MVT::i16,
8738 DAG.getNode(ISD::ADD, DL, MVT::i16,
8739 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008740 DAG.getConstant(1, MVT::i16)),
8741 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008742
8743
Duncan Sands83ec4b62008-06-06 12:08:01 +00008744 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008745 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008746}
8747
Dan Gohmand858e902010-04-17 15:26:15 +00008748SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008749 EVT VT = Op.getValueType();
8750 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008751 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008752 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008753
8754 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008755 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008756 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008757 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008758 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008759 }
Evan Cheng18efe262007-12-14 02:13:44 +00008760
Evan Cheng152804e2007-12-14 08:30:15 +00008761 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008762 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008763 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008764
8765 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008766 SDValue Ops[] = {
8767 Op,
8768 DAG.getConstant(NumBits+NumBits-1, OpVT),
8769 DAG.getConstant(X86::COND_E, MVT::i8),
8770 Op.getValue(1)
8771 };
8772 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008773
8774 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008775 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008776
Owen Anderson825b72b2009-08-11 20:47:22 +00008777 if (VT == MVT::i8)
8778 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008779 return Op;
8780}
8781
Dan Gohmand858e902010-04-17 15:26:15 +00008782SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008783 EVT VT = Op.getValueType();
8784 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008785 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008786 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008787
8788 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008789 if (VT == MVT::i8) {
8790 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008791 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008792 }
Evan Cheng152804e2007-12-14 08:30:15 +00008793
8794 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008795 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008796 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008797
8798 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008799 SDValue Ops[] = {
8800 Op,
8801 DAG.getConstant(NumBits, OpVT),
8802 DAG.getConstant(X86::COND_E, MVT::i8),
8803 Op.getValue(1)
8804 };
8805 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008806
Owen Anderson825b72b2009-08-11 20:47:22 +00008807 if (VT == MVT::i8)
8808 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008809 return Op;
8810}
8811
Dan Gohmand858e902010-04-17 15:26:15 +00008812SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008813 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008814 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008815 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008816
Mon P Wangaf9b9522008-12-18 21:42:19 +00008817 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8818 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8819 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8820 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8821 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8822 //
8823 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8824 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8825 // return AloBlo + AloBhi + AhiBlo;
8826
8827 SDValue A = Op.getOperand(0);
8828 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008829
Dale Johannesene4d209d2009-02-03 20:21:25 +00008830 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008831 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8832 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008833 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008834 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8835 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008836 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008837 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008838 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008839 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008840 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008841 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008842 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008843 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008844 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008845 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008846 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8847 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008848 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008849 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8850 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008851 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8852 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008853 return Res;
8854}
8855
Nadav Rotem43012222011-05-11 08:12:09 +00008856SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8857
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008858 EVT VT = Op.getValueType();
8859 DebugLoc dl = Op.getDebugLoc();
8860 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008861 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008862
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008863 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008864
Nadav Rotem43012222011-05-11 08:12:09 +00008865 // Must have SSE2.
8866 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008867
Nadav Rotem43012222011-05-11 08:12:09 +00008868 // Optimize shl/srl/sra with constant shift amount.
8869 if (isSplatVector(Amt.getNode())) {
8870 SDValue SclrAmt = Amt->getOperand(0);
8871 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8872 uint64_t ShiftAmt = C->getZExtValue();
8873
8874 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8875 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8876 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8877 R, DAG.getConstant(ShiftAmt, MVT::i32));
8878
8879 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8880 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8881 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8882 R, DAG.getConstant(ShiftAmt, MVT::i32));
8883
8884 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8885 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8886 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8887 R, DAG.getConstant(ShiftAmt, MVT::i32));
8888
8889 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8890 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8891 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8892 R, DAG.getConstant(ShiftAmt, MVT::i32));
8893
8894 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8895 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8896 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8897 R, DAG.getConstant(ShiftAmt, MVT::i32));
8898
8899 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8900 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8901 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8902 R, DAG.getConstant(ShiftAmt, MVT::i32));
8903
8904 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8905 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8906 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8907 R, DAG.getConstant(ShiftAmt, MVT::i32));
8908
8909 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8910 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8911 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8912 R, DAG.getConstant(ShiftAmt, MVT::i32));
8913 }
8914 }
8915
8916 // Lower SHL with variable shift amount.
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00008917 // Cannot lower SHL without SSE2 or later.
8918 if (!Subtarget->hasSSE2()) return SDValue();
Nadav Rotem43012222011-05-11 08:12:09 +00008919
8920 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008921 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8922 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8923 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8924
8925 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008926
Nate Begeman51409212010-07-28 00:21:48 +00008927 std::vector<Constant*> CV(4, CI);
8928 Constant *C = ConstantVector::get(CV);
8929 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8930 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008931 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008932 false, false, 16);
8933
8934 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008935 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008936 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8937 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8938 }
Nadav Rotem43012222011-05-11 08:12:09 +00008939 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008940 // a = a << 5;
8941 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8942 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8943 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8944
8945 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8946 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8947
8948 std::vector<Constant*> CVM1(16, CM1);
8949 std::vector<Constant*> CVM2(16, CM2);
8950 Constant *C = ConstantVector::get(CVM1);
8951 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8952 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008953 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008954 false, false, 16);
8955
8956 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8957 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8958 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8959 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8960 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008961 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008962 // a += a
8963 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008964
Nate Begeman51409212010-07-28 00:21:48 +00008965 C = ConstantVector::get(CVM2);
8966 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8967 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008968 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008969 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008970
Nate Begeman51409212010-07-28 00:21:48 +00008971 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8972 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8973 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8974 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8975 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008976 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008977 // a += a
8978 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008979
Nate Begeman51409212010-07-28 00:21:48 +00008980 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008981 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008982 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8983 return R;
8984 }
8985 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008986}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008987
Dan Gohmand858e902010-04-17 15:26:15 +00008988SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008989 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8990 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008991 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8992 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008993 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008994 SDValue LHS = N->getOperand(0);
8995 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008996 unsigned BaseOp = 0;
8997 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008998 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008999 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009000 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009001 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009002 // A subtract of one will be selected as a INC. Note that INC doesn't
9003 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009004 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9005 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009006 BaseOp = X86ISD::INC;
9007 Cond = X86::COND_O;
9008 break;
9009 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009010 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009011 Cond = X86::COND_O;
9012 break;
9013 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009014 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009015 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009016 break;
9017 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009018 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9019 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9021 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009022 BaseOp = X86ISD::DEC;
9023 Cond = X86::COND_O;
9024 break;
9025 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009026 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009027 Cond = X86::COND_O;
9028 break;
9029 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009030 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009031 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009032 break;
9033 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009034 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009035 Cond = X86::COND_O;
9036 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009037 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9038 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9039 MVT::i32);
9040 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009041
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009042 SDValue SetCC =
9043 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9044 DAG.getConstant(X86::COND_O, MVT::i32),
9045 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009046
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009047 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9048 return Sum;
9049 }
Bill Wendling74c37652008-12-09 22:08:41 +00009050 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009051
Bill Wendling61edeb52008-12-02 01:06:39 +00009052 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009053 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009054 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009055
Bill Wendling61edeb52008-12-02 01:06:39 +00009056 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009057 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9058 DAG.getConstant(Cond, MVT::i32),
9059 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009060
Bill Wendling61edeb52008-12-02 01:06:39 +00009061 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9062 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009063}
9064
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009065SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
9066 DebugLoc dl = Op.getDebugLoc();
9067 SDNode* Node = Op.getNode();
9068 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
9069 EVT VT = Node->getValueType(0);
9070
9071 if (Subtarget->hasSSE2() && VT.isVector()) {
9072 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
9073 ExtraVT.getScalarType().getSizeInBits();
9074 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
9075
9076 unsigned SHLIntrinsicsID = 0;
9077 unsigned SRAIntrinsicsID = 0;
9078 switch (VT.getSimpleVT().SimpleTy) {
9079 default:
9080 return SDValue();
9081 case MVT::v2i64: {
9082 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
9083 SRAIntrinsicsID = 0;
9084 break;
9085 }
9086 case MVT::v4i32: {
9087 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
9088 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
9089 break;
9090 }
9091 case MVT::v8i16: {
9092 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
9093 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
9094 break;
9095 }
9096 }
9097
9098 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9099 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
9100 Node->getOperand(0), ShAmt);
9101
9102 // In case of 1 bit sext, no need to shr
9103 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
9104
9105 if (SRAIntrinsicsID) {
9106 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9107 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
9108 Tmp1, ShAmt);
9109 }
9110 return Tmp1;
9111 }
9112
9113 return SDValue();
9114}
9115
9116
Eric Christopher9a9d2752010-07-22 02:48:34 +00009117SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9118 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009119
Eric Christopher77ed1352011-07-08 00:04:56 +00009120 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
9121 // There isn't any reason to disable it if the target processor supports it.
9122 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009123 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +00009124 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009125 SDValue Ops[] = {
9126 DAG.getRegister(X86::ESP, MVT::i32), // Base
9127 DAG.getTargetConstant(1, MVT::i8), // Scale
9128 DAG.getRegister(0, MVT::i32), // Index
9129 DAG.getTargetConstant(0, MVT::i32), // Disp
9130 DAG.getRegister(0, MVT::i32), // Segment.
9131 Zero,
9132 Chain
9133 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009134 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009135 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9136 array_lengthof(Ops));
9137 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009138 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009139
Eric Christopher9a9d2752010-07-22 02:48:34 +00009140 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009141 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009142 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009143
Chris Lattner132929a2010-08-14 17:26:09 +00009144 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9145 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9146 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9147 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009148
Chris Lattner132929a2010-08-14 17:26:09 +00009149 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9150 if (!Op1 && !Op2 && !Op3 && Op4)
9151 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009152
Chris Lattner132929a2010-08-14 17:26:09 +00009153 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9154 if (Op1 && !Op2 && !Op3 && !Op4)
9155 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009156
9157 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009158 // (MFENCE)>;
9159 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009160}
9161
Dan Gohmand858e902010-04-17 15:26:15 +00009162SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009163 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009164 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009165 unsigned Reg = 0;
9166 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009167 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009168 default:
9169 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009170 case MVT::i8: Reg = X86::AL; size = 1; break;
9171 case MVT::i16: Reg = X86::AX; size = 2; break;
9172 case MVT::i32: Reg = X86::EAX; size = 4; break;
9173 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009174 assert(Subtarget->is64Bit() && "Node not type legal!");
9175 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009176 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009177 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009178 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009179 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009180 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009181 Op.getOperand(1),
9182 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009183 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009184 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009185 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009186 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9187 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9188 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009189 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009190 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009191 return cpOut;
9192}
9193
Duncan Sands1607f052008-12-01 11:39:25 +00009194SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009195 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009196 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009197 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009198 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009199 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009200 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009201 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9202 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009203 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009204 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9205 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009206 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009207 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009208 rdx.getValue(1)
9209 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009210 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009211}
9212
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009213SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009214 SelectionDAG &DAG) const {
9215 EVT SrcVT = Op.getOperand(0).getValueType();
9216 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009217 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9218 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009219 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009220 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009221 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009222 // i64 <=> MMX conversions are Legal.
9223 if (SrcVT==MVT::i64 && DstVT.isVector())
9224 return Op;
9225 if (DstVT==MVT::i64 && SrcVT.isVector())
9226 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009227 // MMX <=> MMX conversions are Legal.
9228 if (SrcVT.isVector() && DstVT.isVector())
9229 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009230 // All other conversions need to be expanded.
9231 return SDValue();
9232}
Chris Lattner5b856542010-12-20 00:59:46 +00009233
Dan Gohmand858e902010-04-17 15:26:15 +00009234SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009235 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009236 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009237 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009238 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009239 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009240 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009241 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009242 Node->getOperand(0),
9243 Node->getOperand(1), negOp,
9244 cast<AtomicSDNode>(Node)->getSrcValue(),
9245 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009246}
9247
Chris Lattner5b856542010-12-20 00:59:46 +00009248static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9249 EVT VT = Op.getNode()->getValueType(0);
9250
9251 // Let legalize expand this if it isn't a legal type yet.
9252 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9253 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009254
Chris Lattner5b856542010-12-20 00:59:46 +00009255 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009256
Chris Lattner5b856542010-12-20 00:59:46 +00009257 unsigned Opc;
9258 bool ExtraOp = false;
9259 switch (Op.getOpcode()) {
9260 default: assert(0 && "Invalid code");
9261 case ISD::ADDC: Opc = X86ISD::ADD; break;
9262 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9263 case ISD::SUBC: Opc = X86ISD::SUB; break;
9264 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9265 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009266
Chris Lattner5b856542010-12-20 00:59:46 +00009267 if (!ExtraOp)
9268 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9269 Op.getOperand(1));
9270 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9271 Op.getOperand(1), Op.getOperand(2));
9272}
9273
Evan Cheng0db9fe62006-04-25 20:13:52 +00009274/// LowerOperation - Provide custom lowering hooks for some operations.
9275///
Dan Gohmand858e902010-04-17 15:26:15 +00009276SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009277 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009278 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009279 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +00009280 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009281 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9282 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009283 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009284 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009285 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9286 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9287 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009288 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009289 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009290 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9291 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9292 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009293 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009294 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009295 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009296 case ISD::SHL_PARTS:
9297 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009298 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009299 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009300 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009301 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009302 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009303 case ISD::FABS: return LowerFABS(Op, DAG);
9304 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009305 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009306 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009307 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009308 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009309 case ISD::SELECT: return LowerSELECT(Op, DAG);
9310 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009311 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009312 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009313 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009314 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009315 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009316 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9317 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009318 case ISD::FRAME_TO_ARGS_OFFSET:
9319 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009320 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009321 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009322 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009323 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009324 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9325 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009326 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009327 case ISD::SRA:
9328 case ISD::SRL:
9329 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009330 case ISD::SADDO:
9331 case ISD::UADDO:
9332 case ISD::SSUBO:
9333 case ISD::USUBO:
9334 case ISD::SMULO:
9335 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009336 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009337 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009338 case ISD::ADDC:
9339 case ISD::ADDE:
9340 case ISD::SUBC:
9341 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009342 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009343}
9344
Duncan Sands1607f052008-12-01 11:39:25 +00009345void X86TargetLowering::
9346ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009347 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009348 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009349 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009350 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009351
9352 SDValue Chain = Node->getOperand(0);
9353 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009354 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009355 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009356 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009357 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009358 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009359 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009360 SDValue Result =
9361 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9362 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009363 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009364 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009365 Results.push_back(Result.getValue(2));
9366}
9367
Duncan Sands126d9072008-07-04 11:47:58 +00009368/// ReplaceNodeResults - Replace a node with an illegal result type
9369/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009370void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9371 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009372 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009373 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009374 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009375 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009376 assert(false && "Do not know how to custom type legalize this operation!");
9377 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009378 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +00009379 case ISD::ADDC:
9380 case ISD::ADDE:
9381 case ISD::SUBC:
9382 case ISD::SUBE:
9383 // We don't want to expand or promote these.
9384 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009385 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009386 std::pair<SDValue,SDValue> Vals =
9387 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009388 SDValue FIST = Vals.first, StackSlot = Vals.second;
9389 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009390 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009391 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009392 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9393 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009394 }
9395 return;
9396 }
9397 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009398 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009399 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009400 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009401 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009402 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009403 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009404 eax.getValue(2));
9405 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9406 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009407 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009408 Results.push_back(edx.getValue(1));
9409 return;
9410 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009411 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009412 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009413 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009414 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009415 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9416 DAG.getConstant(0, MVT::i32));
9417 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9418 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009419 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9420 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009421 cpInL.getValue(1));
9422 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009423 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9424 DAG.getConstant(0, MVT::i32));
9425 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9426 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009427 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009428 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009429 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009430 swapInL.getValue(1));
9431 SDValue Ops[] = { swapInH.getValue(0),
9432 N->getOperand(1),
9433 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009434 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009435 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9436 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9437 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009438 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009439 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009440 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009441 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009442 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009443 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009444 Results.push_back(cpOutH.getValue(1));
9445 return;
9446 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009447 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009448 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9449 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009450 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009451 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9452 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009453 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009454 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9455 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009456 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009457 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9458 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009459 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009460 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9461 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009462 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009463 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9464 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009465 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009466 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9467 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009468 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009469}
9470
Evan Cheng72261582005-12-20 06:22:03 +00009471const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9472 switch (Opcode) {
9473 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009474 case X86ISD::BSF: return "X86ISD::BSF";
9475 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009476 case X86ISD::SHLD: return "X86ISD::SHLD";
9477 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009478 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009479 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009480 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009481 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009482 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009483 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009484 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9485 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9486 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009487 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009488 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009489 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009490 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009491 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009492 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009493 case X86ISD::COMI: return "X86ISD::COMI";
9494 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009495 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009496 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009497 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9498 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009499 case X86ISD::CMOV: return "X86ISD::CMOV";
9500 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009501 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009502 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9503 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009504 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009505 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009506 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009507 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009508 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009509 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9510 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009511 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009512 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +00009513 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +00009514 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9515 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9516 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009517 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009518 case X86ISD::FMAX: return "X86ISD::FMAX";
9519 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009520 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9521 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009522 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009523 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009524 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009525 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009526 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009527 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9528 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009529 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9530 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9531 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9532 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9533 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9534 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009535 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9536 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009537 case X86ISD::VSHL: return "X86ISD::VSHL";
9538 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009539 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9540 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9541 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9542 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9543 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9544 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9545 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9546 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9547 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9548 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009549 case X86ISD::ADD: return "X86ISD::ADD";
9550 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009551 case X86ISD::ADC: return "X86ISD::ADC";
9552 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009553 case X86ISD::SMUL: return "X86ISD::SMUL";
9554 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009555 case X86ISD::INC: return "X86ISD::INC";
9556 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009557 case X86ISD::OR: return "X86ISD::OR";
9558 case X86ISD::XOR: return "X86ISD::XOR";
9559 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009560 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009561 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009562 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009563 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9564 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9565 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9566 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9567 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9568 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9569 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9570 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9571 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009572 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009573 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009574 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009575 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9576 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009577 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9578 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9579 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9580 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9581 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9582 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9583 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9584 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9585 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009586 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9587 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9588 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9589 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009590 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9591 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9592 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9593 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9594 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9595 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9596 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9597 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9598 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9599 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009600 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009601 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009602 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009603 }
9604}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009605
Chris Lattnerc9addb72007-03-30 23:15:24 +00009606// isLegalAddressingMode - Return true if the addressing mode represented
9607// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009608bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009609 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00009610 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009611 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009612 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009613
Chris Lattnerc9addb72007-03-30 23:15:24 +00009614 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009615 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009616 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009617
Chris Lattnerc9addb72007-03-30 23:15:24 +00009618 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009619 unsigned GVFlags =
9620 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009621
Chris Lattnerdfed4132009-07-10 07:38:24 +00009622 // If a reference to this global requires an extra load, we can't fold it.
9623 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009624 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009625
Chris Lattnerdfed4132009-07-10 07:38:24 +00009626 // If BaseGV requires a register for the PIC base, we cannot also have a
9627 // BaseReg specified.
9628 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009629 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009630
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009631 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009632 if ((M != CodeModel::Small || R != Reloc::Static) &&
9633 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009634 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009635 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009636
Chris Lattnerc9addb72007-03-30 23:15:24 +00009637 switch (AM.Scale) {
9638 case 0:
9639 case 1:
9640 case 2:
9641 case 4:
9642 case 8:
9643 // These scales always work.
9644 break;
9645 case 3:
9646 case 5:
9647 case 9:
9648 // These scales are formed with basereg+scalereg. Only accept if there is
9649 // no basereg yet.
9650 if (AM.HasBaseReg)
9651 return false;
9652 break;
9653 default: // Other stuff never works.
9654 return false;
9655 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009656
Chris Lattnerc9addb72007-03-30 23:15:24 +00009657 return true;
9658}
9659
9660
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009661bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009662 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009663 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009664 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9665 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009666 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009667 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009668 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009669}
9670
Owen Andersone50ed302009-08-10 22:56:29 +00009671bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009672 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009673 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009674 unsigned NumBits1 = VT1.getSizeInBits();
9675 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009676 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009677 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009678 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009679}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009680
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009681bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009682 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009683 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009684}
9685
Owen Andersone50ed302009-08-10 22:56:29 +00009686bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009687 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009688 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009689}
9690
Owen Andersone50ed302009-08-10 22:56:29 +00009691bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009692 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009693 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009694}
9695
Evan Cheng60c07e12006-07-05 22:17:51 +00009696/// isShuffleMaskLegal - Targets can use this to indicate that they only
9697/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9698/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9699/// are assumed to be legal.
9700bool
Eric Christopherfd179292009-08-27 18:07:15 +00009701X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009702 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009703 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009704 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009705 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009706
Nate Begemana09008b2009-10-19 02:17:23 +00009707 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009708 return (VT.getVectorNumElements() == 2 ||
9709 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9710 isMOVLMask(M, VT) ||
9711 isSHUFPMask(M, VT) ||
9712 isPSHUFDMask(M, VT) ||
9713 isPSHUFHWMask(M, VT) ||
9714 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009715 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009716 isUNPCKLMask(M, VT) ||
9717 isUNPCKHMask(M, VT) ||
9718 isUNPCKL_v_undef_Mask(M, VT) ||
9719 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009720}
9721
Dan Gohman7d8143f2008-04-09 20:09:42 +00009722bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009723X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009724 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009725 unsigned NumElts = VT.getVectorNumElements();
9726 // FIXME: This collection of masks seems suspect.
9727 if (NumElts == 2)
9728 return true;
9729 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9730 return (isMOVLMask(Mask, VT) ||
9731 isCommutedMOVLMask(Mask, VT, true) ||
9732 isSHUFPMask(Mask, VT) ||
9733 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009734 }
9735 return false;
9736}
9737
9738//===----------------------------------------------------------------------===//
9739// X86 Scheduler Hooks
9740//===----------------------------------------------------------------------===//
9741
Mon P Wang63307c32008-05-05 19:05:59 +00009742// private utility function
9743MachineBasicBlock *
9744X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9745 MachineBasicBlock *MBB,
9746 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009747 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009748 unsigned LoadOpc,
9749 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009750 unsigned notOpc,
9751 unsigned EAXreg,
9752 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009753 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009754 // For the atomic bitwise operator, we generate
9755 // thisMBB:
9756 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009757 // ld t1 = [bitinstr.addr]
9758 // op t2 = t1, [bitinstr.val]
9759 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009760 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9761 // bz newMBB
9762 // fallthrough -->nextMBB
9763 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9764 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009765 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009766 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009767
Mon P Wang63307c32008-05-05 19:05:59 +00009768 /// First build the CFG
9769 MachineFunction *F = MBB->getParent();
9770 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009771 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9772 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9773 F->insert(MBBIter, newMBB);
9774 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009775
Dan Gohman14152b42010-07-06 20:24:04 +00009776 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9777 nextMBB->splice(nextMBB->begin(), thisMBB,
9778 llvm::next(MachineBasicBlock::iterator(bInstr)),
9779 thisMBB->end());
9780 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009781
Mon P Wang63307c32008-05-05 19:05:59 +00009782 // Update thisMBB to fall through to newMBB
9783 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009784
Mon P Wang63307c32008-05-05 19:05:59 +00009785 // newMBB jumps to itself and fall through to nextMBB
9786 newMBB->addSuccessor(nextMBB);
9787 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009788
Mon P Wang63307c32008-05-05 19:05:59 +00009789 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009790 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009791 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009792 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009793 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009794 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009795 int numArgs = bInstr->getNumOperands() - 1;
9796 for (int i=0; i < numArgs; ++i)
9797 argOpers[i] = &bInstr->getOperand(i+1);
9798
9799 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009800 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009801 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009802
Dale Johannesen140be2d2008-08-19 18:47:28 +00009803 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009804 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009805 for (int i=0; i <= lastAddrIndx; ++i)
9806 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009807
Dale Johannesen140be2d2008-08-19 18:47:28 +00009808 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009809 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009810 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009811 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009812 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009813 tt = t1;
9814
Dale Johannesen140be2d2008-08-19 18:47:28 +00009815 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009816 assert((argOpers[valArgIndx]->isReg() ||
9817 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009818 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009819 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009820 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009821 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009822 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009823 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009824 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009825
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009826 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009827 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009828
Dale Johannesene4d209d2009-02-03 20:21:25 +00009829 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009830 for (int i=0; i <= lastAddrIndx; ++i)
9831 (*MIB).addOperand(*argOpers[i]);
9832 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009833 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009834 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9835 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009836
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009837 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009838 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009839
Mon P Wang63307c32008-05-05 19:05:59 +00009840 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009841 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009842
Dan Gohman14152b42010-07-06 20:24:04 +00009843 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009844 return nextMBB;
9845}
9846
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009847// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009848MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009849X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9850 MachineBasicBlock *MBB,
9851 unsigned regOpcL,
9852 unsigned regOpcH,
9853 unsigned immOpcL,
9854 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009855 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009856 // For the atomic bitwise operator, we generate
9857 // thisMBB (instructions are in pairs, except cmpxchg8b)
9858 // ld t1,t2 = [bitinstr.addr]
9859 // newMBB:
9860 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9861 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009862 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009863 // mov ECX, EBX <- t5, t6
9864 // mov EAX, EDX <- t1, t2
9865 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9866 // mov t3, t4 <- EAX, EDX
9867 // bz newMBB
9868 // result in out1, out2
9869 // fallthrough -->nextMBB
9870
9871 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9872 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009873 const unsigned NotOpc = X86::NOT32r;
9874 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9875 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9876 MachineFunction::iterator MBBIter = MBB;
9877 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009878
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009879 /// First build the CFG
9880 MachineFunction *F = MBB->getParent();
9881 MachineBasicBlock *thisMBB = MBB;
9882 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9883 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9884 F->insert(MBBIter, newMBB);
9885 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009886
Dan Gohman14152b42010-07-06 20:24:04 +00009887 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9888 nextMBB->splice(nextMBB->begin(), thisMBB,
9889 llvm::next(MachineBasicBlock::iterator(bInstr)),
9890 thisMBB->end());
9891 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009892
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009893 // Update thisMBB to fall through to newMBB
9894 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009895
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009896 // newMBB jumps to itself and fall through to nextMBB
9897 newMBB->addSuccessor(nextMBB);
9898 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009899
Dale Johannesene4d209d2009-02-03 20:21:25 +00009900 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009901 // Insert instructions into newMBB based on incoming instruction
9902 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009903 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009904 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009905 MachineOperand& dest1Oper = bInstr->getOperand(0);
9906 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009907 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9908 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009909 argOpers[i] = &bInstr->getOperand(i+2);
9910
Dan Gohman71ea4e52010-05-14 21:01:44 +00009911 // We use some of the operands multiple times, so conservatively just
9912 // clear any kill flags that might be present.
9913 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9914 argOpers[i]->setIsKill(false);
9915 }
9916
Evan Chengad5b52f2010-01-08 19:14:57 +00009917 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009918 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009919
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009920 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009921 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009922 for (int i=0; i <= lastAddrIndx; ++i)
9923 (*MIB).addOperand(*argOpers[i]);
9924 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009925 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009926 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009927 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009928 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009929 MachineOperand newOp3 = *(argOpers[3]);
9930 if (newOp3.isImm())
9931 newOp3.setImm(newOp3.getImm()+4);
9932 else
9933 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009934 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009935 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009936
9937 // t3/4 are defined later, at the bottom of the loop
9938 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9939 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009940 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009941 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009942 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009943 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9944
Evan Cheng306b4ca2010-01-08 23:41:50 +00009945 // The subsequent operations should be using the destination registers of
9946 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009947 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009948 t1 = F->getRegInfo().createVirtualRegister(RC);
9949 t2 = F->getRegInfo().createVirtualRegister(RC);
9950 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9951 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009952 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009953 t1 = dest1Oper.getReg();
9954 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009955 }
9956
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009957 int valArgIndx = lastAddrIndx + 1;
9958 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009959 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009960 "invalid operand");
9961 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9962 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009963 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009964 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009965 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009966 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009967 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009968 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009969 (*MIB).addOperand(*argOpers[valArgIndx]);
9970 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009971 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009972 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009973 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009974 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009975 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009976 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009977 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009978 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009979 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009980 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009981
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009982 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009983 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009984 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009985 MIB.addReg(t2);
9986
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009987 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009988 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009989 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009990 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009991
Dale Johannesene4d209d2009-02-03 20:21:25 +00009992 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009993 for (int i=0; i <= lastAddrIndx; ++i)
9994 (*MIB).addOperand(*argOpers[i]);
9995
9996 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009997 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9998 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009999
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010000 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010001 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010002 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010003 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010004
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010005 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010006 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010007
Dan Gohman14152b42010-07-06 20:24:04 +000010008 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010009 return nextMBB;
10010}
10011
10012// private utility function
10013MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000010014X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
10015 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010016 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010017 // For the atomic min/max operator, we generate
10018 // thisMBB:
10019 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010020 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000010021 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000010022 // cmp t1, t2
10023 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000010024 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010025 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10026 // bz newMBB
10027 // fallthrough -->nextMBB
10028 //
10029 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10030 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010031 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010032 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010033
Mon P Wang63307c32008-05-05 19:05:59 +000010034 /// First build the CFG
10035 MachineFunction *F = MBB->getParent();
10036 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010037 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10038 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10039 F->insert(MBBIter, newMBB);
10040 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010041
Dan Gohman14152b42010-07-06 20:24:04 +000010042 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10043 nextMBB->splice(nextMBB->begin(), thisMBB,
10044 llvm::next(MachineBasicBlock::iterator(mInstr)),
10045 thisMBB->end());
10046 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010047
Mon P Wang63307c32008-05-05 19:05:59 +000010048 // Update thisMBB to fall through to newMBB
10049 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010050
Mon P Wang63307c32008-05-05 19:05:59 +000010051 // newMBB jumps to newMBB and fall through to nextMBB
10052 newMBB->addSuccessor(nextMBB);
10053 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010054
Dale Johannesene4d209d2009-02-03 20:21:25 +000010055 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010056 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010057 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010058 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010059 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010060 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010061 int numArgs = mInstr->getNumOperands() - 1;
10062 for (int i=0; i < numArgs; ++i)
10063 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010064
Mon P Wang63307c32008-05-05 19:05:59 +000010065 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010066 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010067 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010068
Mon P Wangab3e7472008-05-05 22:56:23 +000010069 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010070 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010071 for (int i=0; i <= lastAddrIndx; ++i)
10072 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010073
Mon P Wang63307c32008-05-05 19:05:59 +000010074 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010075 assert((argOpers[valArgIndx]->isReg() ||
10076 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010077 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010078
10079 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010080 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010081 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010082 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010083 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010084 (*MIB).addOperand(*argOpers[valArgIndx]);
10085
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010086 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010087 MIB.addReg(t1);
10088
Dale Johannesene4d209d2009-02-03 20:21:25 +000010089 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010090 MIB.addReg(t1);
10091 MIB.addReg(t2);
10092
10093 // Generate movc
10094 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010095 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010096 MIB.addReg(t2);
10097 MIB.addReg(t1);
10098
10099 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010100 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010101 for (int i=0; i <= lastAddrIndx; ++i)
10102 (*MIB).addOperand(*argOpers[i]);
10103 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010104 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010105 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10106 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010107
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010108 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010109 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010110
Mon P Wang63307c32008-05-05 19:05:59 +000010111 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010112 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010113
Dan Gohman14152b42010-07-06 20:24:04 +000010114 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010115 return nextMBB;
10116}
10117
Eric Christopherf83a5de2009-08-27 18:08:16 +000010118// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010119// or XMM0_V32I8 in AVX all of this code can be replaced with that
10120// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010121MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010122X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010123 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010124 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10125 "Target must have SSE4.2 or AVX features enabled");
10126
Eric Christopherb120ab42009-08-18 22:50:32 +000010127 DebugLoc dl = MI->getDebugLoc();
10128 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010129 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010130 if (!Subtarget->hasAVX()) {
10131 if (memArg)
10132 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10133 else
10134 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10135 } else {
10136 if (memArg)
10137 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10138 else
10139 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10140 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010141
Eric Christopher41c902f2010-11-30 08:20:21 +000010142 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010143 for (unsigned i = 0; i < numArgs; ++i) {
10144 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010145 if (!(Op.isReg() && Op.isImplicit()))
10146 MIB.addOperand(Op);
10147 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010148 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010149 .addReg(X86::XMM0);
10150
Dan Gohman14152b42010-07-06 20:24:04 +000010151 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010152 return BB;
10153}
10154
10155MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010156X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010157 DebugLoc dl = MI->getDebugLoc();
10158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010159
Eric Christopher228232b2010-11-30 07:20:12 +000010160 // Address into RAX/EAX, other two args into ECX, EDX.
10161 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10162 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10163 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10164 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010165 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010166
Eric Christopher228232b2010-11-30 07:20:12 +000010167 unsigned ValOps = X86::AddrNumOperands;
10168 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10169 .addReg(MI->getOperand(ValOps).getReg());
10170 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10171 .addReg(MI->getOperand(ValOps+1).getReg());
10172
10173 // The instruction doesn't actually take any operands though.
10174 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010175
Eric Christopher228232b2010-11-30 07:20:12 +000010176 MI->eraseFromParent(); // The pseudo is gone now.
10177 return BB;
10178}
10179
10180MachineBasicBlock *
10181X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010182 DebugLoc dl = MI->getDebugLoc();
10183 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010184
Eric Christopher228232b2010-11-30 07:20:12 +000010185 // First arg in ECX, the second in EAX.
10186 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10187 .addReg(MI->getOperand(0).getReg());
10188 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10189 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010190
Eric Christopher228232b2010-11-30 07:20:12 +000010191 // The instruction doesn't actually take any operands though.
10192 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010193
Eric Christopher228232b2010-11-30 07:20:12 +000010194 MI->eraseFromParent(); // The pseudo is gone now.
10195 return BB;
10196}
10197
10198MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010199X86TargetLowering::EmitVAARG64WithCustomInserter(
10200 MachineInstr *MI,
10201 MachineBasicBlock *MBB) const {
10202 // Emit va_arg instruction on X86-64.
10203
10204 // Operands to this pseudo-instruction:
10205 // 0 ) Output : destination address (reg)
10206 // 1-5) Input : va_list address (addr, i64mem)
10207 // 6 ) ArgSize : Size (in bytes) of vararg type
10208 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10209 // 8 ) Align : Alignment of type
10210 // 9 ) EFLAGS (implicit-def)
10211
10212 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10213 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10214
10215 unsigned DestReg = MI->getOperand(0).getReg();
10216 MachineOperand &Base = MI->getOperand(1);
10217 MachineOperand &Scale = MI->getOperand(2);
10218 MachineOperand &Index = MI->getOperand(3);
10219 MachineOperand &Disp = MI->getOperand(4);
10220 MachineOperand &Segment = MI->getOperand(5);
10221 unsigned ArgSize = MI->getOperand(6).getImm();
10222 unsigned ArgMode = MI->getOperand(7).getImm();
10223 unsigned Align = MI->getOperand(8).getImm();
10224
10225 // Memory Reference
10226 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10227 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10228 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10229
10230 // Machine Information
10231 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10232 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10233 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10234 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10235 DebugLoc DL = MI->getDebugLoc();
10236
10237 // struct va_list {
10238 // i32 gp_offset
10239 // i32 fp_offset
10240 // i64 overflow_area (address)
10241 // i64 reg_save_area (address)
10242 // }
10243 // sizeof(va_list) = 24
10244 // alignment(va_list) = 8
10245
10246 unsigned TotalNumIntRegs = 6;
10247 unsigned TotalNumXMMRegs = 8;
10248 bool UseGPOffset = (ArgMode == 1);
10249 bool UseFPOffset = (ArgMode == 2);
10250 unsigned MaxOffset = TotalNumIntRegs * 8 +
10251 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10252
10253 /* Align ArgSize to a multiple of 8 */
10254 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10255 bool NeedsAlign = (Align > 8);
10256
10257 MachineBasicBlock *thisMBB = MBB;
10258 MachineBasicBlock *overflowMBB;
10259 MachineBasicBlock *offsetMBB;
10260 MachineBasicBlock *endMBB;
10261
10262 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10263 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10264 unsigned OffsetReg = 0;
10265
10266 if (!UseGPOffset && !UseFPOffset) {
10267 // If we only pull from the overflow region, we don't create a branch.
10268 // We don't need to alter control flow.
10269 OffsetDestReg = 0; // unused
10270 OverflowDestReg = DestReg;
10271
10272 offsetMBB = NULL;
10273 overflowMBB = thisMBB;
10274 endMBB = thisMBB;
10275 } else {
10276 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10277 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10278 // If not, pull from overflow_area. (branch to overflowMBB)
10279 //
10280 // thisMBB
10281 // | .
10282 // | .
10283 // offsetMBB overflowMBB
10284 // | .
10285 // | .
10286 // endMBB
10287
10288 // Registers for the PHI in endMBB
10289 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10290 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10291
10292 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10293 MachineFunction *MF = MBB->getParent();
10294 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10295 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10296 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10297
10298 MachineFunction::iterator MBBIter = MBB;
10299 ++MBBIter;
10300
10301 // Insert the new basic blocks
10302 MF->insert(MBBIter, offsetMBB);
10303 MF->insert(MBBIter, overflowMBB);
10304 MF->insert(MBBIter, endMBB);
10305
10306 // Transfer the remainder of MBB and its successor edges to endMBB.
10307 endMBB->splice(endMBB->begin(), thisMBB,
10308 llvm::next(MachineBasicBlock::iterator(MI)),
10309 thisMBB->end());
10310 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10311
10312 // Make offsetMBB and overflowMBB successors of thisMBB
10313 thisMBB->addSuccessor(offsetMBB);
10314 thisMBB->addSuccessor(overflowMBB);
10315
10316 // endMBB is a successor of both offsetMBB and overflowMBB
10317 offsetMBB->addSuccessor(endMBB);
10318 overflowMBB->addSuccessor(endMBB);
10319
10320 // Load the offset value into a register
10321 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10322 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10323 .addOperand(Base)
10324 .addOperand(Scale)
10325 .addOperand(Index)
10326 .addDisp(Disp, UseFPOffset ? 4 : 0)
10327 .addOperand(Segment)
10328 .setMemRefs(MMOBegin, MMOEnd);
10329
10330 // Check if there is enough room left to pull this argument.
10331 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10332 .addReg(OffsetReg)
10333 .addImm(MaxOffset + 8 - ArgSizeA8);
10334
10335 // Branch to "overflowMBB" if offset >= max
10336 // Fall through to "offsetMBB" otherwise
10337 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10338 .addMBB(overflowMBB);
10339 }
10340
10341 // In offsetMBB, emit code to use the reg_save_area.
10342 if (offsetMBB) {
10343 assert(OffsetReg != 0);
10344
10345 // Read the reg_save_area address.
10346 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10347 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10348 .addOperand(Base)
10349 .addOperand(Scale)
10350 .addOperand(Index)
10351 .addDisp(Disp, 16)
10352 .addOperand(Segment)
10353 .setMemRefs(MMOBegin, MMOEnd);
10354
10355 // Zero-extend the offset
10356 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10357 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10358 .addImm(0)
10359 .addReg(OffsetReg)
10360 .addImm(X86::sub_32bit);
10361
10362 // Add the offset to the reg_save_area to get the final address.
10363 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10364 .addReg(OffsetReg64)
10365 .addReg(RegSaveReg);
10366
10367 // Compute the offset for the next argument
10368 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10369 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10370 .addReg(OffsetReg)
10371 .addImm(UseFPOffset ? 16 : 8);
10372
10373 // Store it back into the va_list.
10374 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10375 .addOperand(Base)
10376 .addOperand(Scale)
10377 .addOperand(Index)
10378 .addDisp(Disp, UseFPOffset ? 4 : 0)
10379 .addOperand(Segment)
10380 .addReg(NextOffsetReg)
10381 .setMemRefs(MMOBegin, MMOEnd);
10382
10383 // Jump to endMBB
10384 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10385 .addMBB(endMBB);
10386 }
10387
10388 //
10389 // Emit code to use overflow area
10390 //
10391
10392 // Load the overflow_area address into a register.
10393 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10394 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10395 .addOperand(Base)
10396 .addOperand(Scale)
10397 .addOperand(Index)
10398 .addDisp(Disp, 8)
10399 .addOperand(Segment)
10400 .setMemRefs(MMOBegin, MMOEnd);
10401
10402 // If we need to align it, do so. Otherwise, just copy the address
10403 // to OverflowDestReg.
10404 if (NeedsAlign) {
10405 // Align the overflow address
10406 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10407 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10408
10409 // aligned_addr = (addr + (align-1)) & ~(align-1)
10410 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10411 .addReg(OverflowAddrReg)
10412 .addImm(Align-1);
10413
10414 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10415 .addReg(TmpReg)
10416 .addImm(~(uint64_t)(Align-1));
10417 } else {
10418 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10419 .addReg(OverflowAddrReg);
10420 }
10421
10422 // Compute the next overflow address after this argument.
10423 // (the overflow address should be kept 8-byte aligned)
10424 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10425 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10426 .addReg(OverflowDestReg)
10427 .addImm(ArgSizeA8);
10428
10429 // Store the new overflow address.
10430 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10431 .addOperand(Base)
10432 .addOperand(Scale)
10433 .addOperand(Index)
10434 .addDisp(Disp, 8)
10435 .addOperand(Segment)
10436 .addReg(NextAddrReg)
10437 .setMemRefs(MMOBegin, MMOEnd);
10438
10439 // If we branched, emit the PHI to the front of endMBB.
10440 if (offsetMBB) {
10441 BuildMI(*endMBB, endMBB->begin(), DL,
10442 TII->get(X86::PHI), DestReg)
10443 .addReg(OffsetDestReg).addMBB(offsetMBB)
10444 .addReg(OverflowDestReg).addMBB(overflowMBB);
10445 }
10446
10447 // Erase the pseudo instruction
10448 MI->eraseFromParent();
10449
10450 return endMBB;
10451}
10452
10453MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010454X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10455 MachineInstr *MI,
10456 MachineBasicBlock *MBB) const {
10457 // Emit code to save XMM registers to the stack. The ABI says that the
10458 // number of registers to save is given in %al, so it's theoretically
10459 // possible to do an indirect jump trick to avoid saving all of them,
10460 // however this code takes a simpler approach and just executes all
10461 // of the stores if %al is non-zero. It's less code, and it's probably
10462 // easier on the hardware branch predictor, and stores aren't all that
10463 // expensive anyway.
10464
10465 // Create the new basic blocks. One block contains all the XMM stores,
10466 // and one block is the final destination regardless of whether any
10467 // stores were performed.
10468 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10469 MachineFunction *F = MBB->getParent();
10470 MachineFunction::iterator MBBIter = MBB;
10471 ++MBBIter;
10472 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10473 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10474 F->insert(MBBIter, XMMSaveMBB);
10475 F->insert(MBBIter, EndMBB);
10476
Dan Gohman14152b42010-07-06 20:24:04 +000010477 // Transfer the remainder of MBB and its successor edges to EndMBB.
10478 EndMBB->splice(EndMBB->begin(), MBB,
10479 llvm::next(MachineBasicBlock::iterator(MI)),
10480 MBB->end());
10481 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10482
Dan Gohmand6708ea2009-08-15 01:38:56 +000010483 // The original block will now fall through to the XMM save block.
10484 MBB->addSuccessor(XMMSaveMBB);
10485 // The XMMSaveMBB will fall through to the end block.
10486 XMMSaveMBB->addSuccessor(EndMBB);
10487
10488 // Now add the instructions.
10489 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10490 DebugLoc DL = MI->getDebugLoc();
10491
10492 unsigned CountReg = MI->getOperand(0).getReg();
10493 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10494 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10495
10496 if (!Subtarget->isTargetWin64()) {
10497 // If %al is 0, branch around the XMM save block.
10498 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010499 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010500 MBB->addSuccessor(EndMBB);
10501 }
10502
10503 // In the XMM save block, save all the XMM argument registers.
10504 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10505 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010506 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010507 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010508 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010509 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010510 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010511 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10512 .addFrameIndex(RegSaveFrameIndex)
10513 .addImm(/*Scale=*/1)
10514 .addReg(/*IndexReg=*/0)
10515 .addImm(/*Disp=*/Offset)
10516 .addReg(/*Segment=*/0)
10517 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010518 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010519 }
10520
Dan Gohman14152b42010-07-06 20:24:04 +000010521 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010522
10523 return EndMBB;
10524}
Mon P Wang63307c32008-05-05 19:05:59 +000010525
Evan Cheng60c07e12006-07-05 22:17:51 +000010526MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010527X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010528 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010529 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10530 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010531
Chris Lattner52600972009-09-02 05:57:00 +000010532 // To "insert" a SELECT_CC instruction, we actually have to insert the
10533 // diamond control-flow pattern. The incoming instruction knows the
10534 // destination vreg to set, the condition code register to branch on, the
10535 // true/false values to select between, and a branch opcode to use.
10536 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10537 MachineFunction::iterator It = BB;
10538 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010539
Chris Lattner52600972009-09-02 05:57:00 +000010540 // thisMBB:
10541 // ...
10542 // TrueVal = ...
10543 // cmpTY ccX, r1, r2
10544 // bCC copy1MBB
10545 // fallthrough --> copy0MBB
10546 MachineBasicBlock *thisMBB = BB;
10547 MachineFunction *F = BB->getParent();
10548 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10549 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010550 F->insert(It, copy0MBB);
10551 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010552
Bill Wendling730c07e2010-06-25 20:48:10 +000010553 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10554 // live into the sink and copy blocks.
10555 const MachineFunction *MF = BB->getParent();
10556 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10557 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010558
Dan Gohman14152b42010-07-06 20:24:04 +000010559 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10560 const MachineOperand &MO = MI->getOperand(I);
10561 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010562 unsigned Reg = MO.getReg();
10563 if (Reg != X86::EFLAGS) continue;
10564 copy0MBB->addLiveIn(Reg);
10565 sinkMBB->addLiveIn(Reg);
10566 }
10567
Dan Gohman14152b42010-07-06 20:24:04 +000010568 // Transfer the remainder of BB and its successor edges to sinkMBB.
10569 sinkMBB->splice(sinkMBB->begin(), BB,
10570 llvm::next(MachineBasicBlock::iterator(MI)),
10571 BB->end());
10572 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10573
10574 // Add the true and fallthrough blocks as its successors.
10575 BB->addSuccessor(copy0MBB);
10576 BB->addSuccessor(sinkMBB);
10577
10578 // Create the conditional branch instruction.
10579 unsigned Opc =
10580 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10581 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10582
Chris Lattner52600972009-09-02 05:57:00 +000010583 // copy0MBB:
10584 // %FalseValue = ...
10585 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010586 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010587
Chris Lattner52600972009-09-02 05:57:00 +000010588 // sinkMBB:
10589 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10590 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010591 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10592 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010593 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10594 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10595
Dan Gohman14152b42010-07-06 20:24:04 +000010596 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010597 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010598}
10599
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010600MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010601X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010602 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010603 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10604 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010605
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010606 assert(!Subtarget->isTargetEnvMacho());
10607
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010608 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10609 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010610
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010611 if (Subtarget->isTargetWin64()) {
10612 if (Subtarget->isTargetCygMing()) {
10613 // ___chkstk(Mingw64):
10614 // Clobbers R10, R11, RAX and EFLAGS.
10615 // Updates RSP.
10616 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10617 .addExternalSymbol("___chkstk")
10618 .addReg(X86::RAX, RegState::Implicit)
10619 .addReg(X86::RSP, RegState::Implicit)
10620 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10621 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10622 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10623 } else {
10624 // __chkstk(MSVCRT): does not update stack pointer.
10625 // Clobbers R10, R11 and EFLAGS.
10626 // FIXME: RAX(allocated size) might be reused and not killed.
10627 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10628 .addExternalSymbol("__chkstk")
10629 .addReg(X86::RAX, RegState::Implicit)
10630 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10631 // RAX has the offset to subtracted from RSP.
10632 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10633 .addReg(X86::RSP)
10634 .addReg(X86::RAX);
10635 }
10636 } else {
10637 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010638 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10639
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010640 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10641 .addExternalSymbol(StackProbeSymbol)
10642 .addReg(X86::EAX, RegState::Implicit)
10643 .addReg(X86::ESP, RegState::Implicit)
10644 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10645 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10646 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10647 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010648
Dan Gohman14152b42010-07-06 20:24:04 +000010649 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010650 return BB;
10651}
Chris Lattner52600972009-09-02 05:57:00 +000010652
10653MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010654X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10655 MachineBasicBlock *BB) const {
10656 // This is pretty easy. We're taking the value that we received from
10657 // our load from the relocation, sticking it in either RDI (x86-64)
10658 // or EAX and doing an indirect call. The return value will then
10659 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010660 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010661 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010662 DebugLoc DL = MI->getDebugLoc();
10663 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010664
10665 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010666 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010667
Eric Christopher30ef0e52010-06-03 04:07:48 +000010668 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010669 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10670 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010671 .addReg(X86::RIP)
10672 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010673 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010674 MI->getOperand(3).getTargetFlags())
10675 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010676 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010677 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010678 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010679 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10680 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010681 .addReg(0)
10682 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010683 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010684 MI->getOperand(3).getTargetFlags())
10685 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010686 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010687 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010688 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010689 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10690 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010691 .addReg(TII->getGlobalBaseReg(F))
10692 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010693 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010694 MI->getOperand(3).getTargetFlags())
10695 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010696 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010697 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010698 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010699
Dan Gohman14152b42010-07-06 20:24:04 +000010700 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010701 return BB;
10702}
10703
10704MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010705X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010706 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010707 switch (MI->getOpcode()) {
10708 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010709 case X86::TAILJMPd64:
10710 case X86::TAILJMPr64:
10711 case X86::TAILJMPm64:
10712 assert(!"TAILJMP64 would not be touched here.");
10713 case X86::TCRETURNdi64:
10714 case X86::TCRETURNri64:
10715 case X86::TCRETURNmi64:
10716 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10717 // On AMD64, additional defs should be added before register allocation.
10718 if (!Subtarget->isTargetWin64()) {
10719 MI->addRegisterDefined(X86::RSI);
10720 MI->addRegisterDefined(X86::RDI);
10721 MI->addRegisterDefined(X86::XMM6);
10722 MI->addRegisterDefined(X86::XMM7);
10723 MI->addRegisterDefined(X86::XMM8);
10724 MI->addRegisterDefined(X86::XMM9);
10725 MI->addRegisterDefined(X86::XMM10);
10726 MI->addRegisterDefined(X86::XMM11);
10727 MI->addRegisterDefined(X86::XMM12);
10728 MI->addRegisterDefined(X86::XMM13);
10729 MI->addRegisterDefined(X86::XMM14);
10730 MI->addRegisterDefined(X86::XMM15);
10731 }
10732 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010733 case X86::WIN_ALLOCA:
10734 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010735 case X86::TLSCall_32:
10736 case X86::TLSCall_64:
10737 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010738 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010739 case X86::CMOV_FR32:
10740 case X86::CMOV_FR64:
10741 case X86::CMOV_V4F32:
10742 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010743 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010744 case X86::CMOV_GR16:
10745 case X86::CMOV_GR32:
10746 case X86::CMOV_RFP32:
10747 case X86::CMOV_RFP64:
10748 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010749 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010750
Dale Johannesen849f2142007-07-03 00:53:03 +000010751 case X86::FP32_TO_INT16_IN_MEM:
10752 case X86::FP32_TO_INT32_IN_MEM:
10753 case X86::FP32_TO_INT64_IN_MEM:
10754 case X86::FP64_TO_INT16_IN_MEM:
10755 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010756 case X86::FP64_TO_INT64_IN_MEM:
10757 case X86::FP80_TO_INT16_IN_MEM:
10758 case X86::FP80_TO_INT32_IN_MEM:
10759 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010760 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10761 DebugLoc DL = MI->getDebugLoc();
10762
Evan Cheng60c07e12006-07-05 22:17:51 +000010763 // Change the floating point control register to use "round towards zero"
10764 // mode when truncating to an integer value.
10765 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010766 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010767 addFrameReference(BuildMI(*BB, MI, DL,
10768 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010769
10770 // Load the old value of the high byte of the control word...
10771 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010772 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010773 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010774 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010775
10776 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010777 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010778 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010779
10780 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010781 addFrameReference(BuildMI(*BB, MI, DL,
10782 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010783
10784 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010785 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010786 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010787
10788 // Get the X86 opcode to use.
10789 unsigned Opc;
10790 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010791 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010792 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10793 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10794 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10795 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10796 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10797 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010798 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10799 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10800 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010801 }
10802
10803 X86AddressMode AM;
10804 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010805 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010806 AM.BaseType = X86AddressMode::RegBase;
10807 AM.Base.Reg = Op.getReg();
10808 } else {
10809 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010810 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010811 }
10812 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010813 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010814 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010815 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010816 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010817 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010818 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010819 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010820 AM.GV = Op.getGlobal();
10821 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010822 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010823 }
Dan Gohman14152b42010-07-06 20:24:04 +000010824 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010825 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010826
10827 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010828 addFrameReference(BuildMI(*BB, MI, DL,
10829 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010830
Dan Gohman14152b42010-07-06 20:24:04 +000010831 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010832 return BB;
10833 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010834 // String/text processing lowering.
10835 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010836 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010837 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10838 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010839 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010840 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10841 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010842 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010843 return EmitPCMP(MI, BB, 5, false /* in mem */);
10844 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010845 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010846 return EmitPCMP(MI, BB, 5, true /* in mem */);
10847
Eric Christopher228232b2010-11-30 07:20:12 +000010848 // Thread synchronization.
10849 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010850 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010851 case X86::MWAIT:
10852 return EmitMwait(MI, BB);
10853
Eric Christopherb120ab42009-08-18 22:50:32 +000010854 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010855 case X86::ATOMAND32:
10856 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010857 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010858 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010859 X86::NOT32r, X86::EAX,
10860 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010861 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010862 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10863 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010864 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010865 X86::NOT32r, X86::EAX,
10866 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010867 case X86::ATOMXOR32:
10868 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010869 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010870 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010871 X86::NOT32r, X86::EAX,
10872 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010873 case X86::ATOMNAND32:
10874 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010875 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010876 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010877 X86::NOT32r, X86::EAX,
10878 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010879 case X86::ATOMMIN32:
10880 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10881 case X86::ATOMMAX32:
10882 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10883 case X86::ATOMUMIN32:
10884 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10885 case X86::ATOMUMAX32:
10886 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010887
10888 case X86::ATOMAND16:
10889 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10890 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010891 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010892 X86::NOT16r, X86::AX,
10893 X86::GR16RegisterClass);
10894 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010895 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010896 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010897 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010898 X86::NOT16r, X86::AX,
10899 X86::GR16RegisterClass);
10900 case X86::ATOMXOR16:
10901 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10902 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010903 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010904 X86::NOT16r, X86::AX,
10905 X86::GR16RegisterClass);
10906 case X86::ATOMNAND16:
10907 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10908 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010909 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010910 X86::NOT16r, X86::AX,
10911 X86::GR16RegisterClass, true);
10912 case X86::ATOMMIN16:
10913 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10914 case X86::ATOMMAX16:
10915 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10916 case X86::ATOMUMIN16:
10917 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10918 case X86::ATOMUMAX16:
10919 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10920
10921 case X86::ATOMAND8:
10922 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10923 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010924 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010925 X86::NOT8r, X86::AL,
10926 X86::GR8RegisterClass);
10927 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010928 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010929 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010930 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010931 X86::NOT8r, X86::AL,
10932 X86::GR8RegisterClass);
10933 case X86::ATOMXOR8:
10934 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10935 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010936 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010937 X86::NOT8r, X86::AL,
10938 X86::GR8RegisterClass);
10939 case X86::ATOMNAND8:
10940 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10941 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010942 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010943 X86::NOT8r, X86::AL,
10944 X86::GR8RegisterClass, true);
10945 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010946 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010947 case X86::ATOMAND64:
10948 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010949 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010950 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010951 X86::NOT64r, X86::RAX,
10952 X86::GR64RegisterClass);
10953 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010954 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10955 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010956 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010957 X86::NOT64r, X86::RAX,
10958 X86::GR64RegisterClass);
10959 case X86::ATOMXOR64:
10960 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010961 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010962 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010963 X86::NOT64r, X86::RAX,
10964 X86::GR64RegisterClass);
10965 case X86::ATOMNAND64:
10966 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10967 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010968 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010969 X86::NOT64r, X86::RAX,
10970 X86::GR64RegisterClass, true);
10971 case X86::ATOMMIN64:
10972 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10973 case X86::ATOMMAX64:
10974 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10975 case X86::ATOMUMIN64:
10976 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10977 case X86::ATOMUMAX64:
10978 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010979
10980 // This group does 64-bit operations on a 32-bit host.
10981 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010982 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010983 X86::AND32rr, X86::AND32rr,
10984 X86::AND32ri, X86::AND32ri,
10985 false);
10986 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010987 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010988 X86::OR32rr, X86::OR32rr,
10989 X86::OR32ri, X86::OR32ri,
10990 false);
10991 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010992 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010993 X86::XOR32rr, X86::XOR32rr,
10994 X86::XOR32ri, X86::XOR32ri,
10995 false);
10996 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010997 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010998 X86::AND32rr, X86::AND32rr,
10999 X86::AND32ri, X86::AND32ri,
11000 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011001 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011002 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011003 X86::ADD32rr, X86::ADC32rr,
11004 X86::ADD32ri, X86::ADC32ri,
11005 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011006 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011007 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011008 X86::SUB32rr, X86::SBB32rr,
11009 X86::SUB32ri, X86::SBB32ri,
11010 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000011011 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011012 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000011013 X86::MOV32rr, X86::MOV32rr,
11014 X86::MOV32ri, X86::MOV32ri,
11015 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011016 case X86::VASTART_SAVE_XMM_REGS:
11017 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000011018
11019 case X86::VAARG_64:
11020 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011021 }
11022}
11023
11024//===----------------------------------------------------------------------===//
11025// X86 Optimization Hooks
11026//===----------------------------------------------------------------------===//
11027
Dan Gohman475871a2008-07-27 21:46:04 +000011028void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000011029 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011030 APInt &KnownZero,
11031 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000011032 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000011033 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011034 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000011035 assert((Opc >= ISD::BUILTIN_OP_END ||
11036 Opc == ISD::INTRINSIC_WO_CHAIN ||
11037 Opc == ISD::INTRINSIC_W_CHAIN ||
11038 Opc == ISD::INTRINSIC_VOID) &&
11039 "Should use MaskedValueIsZero if you don't know whether Op"
11040 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011041
Dan Gohmanf4f92f52008-02-13 23:07:24 +000011042 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011043 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000011044 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011045 case X86ISD::ADD:
11046 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000011047 case X86ISD::ADC:
11048 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011049 case X86ISD::SMUL:
11050 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011051 case X86ISD::INC:
11052 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011053 case X86ISD::OR:
11054 case X86ISD::XOR:
11055 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011056 // These nodes' second result is a boolean.
11057 if (Op.getResNo() == 0)
11058 break;
11059 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011060 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011061 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11062 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011063 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011064 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011065}
Chris Lattner259e97c2006-01-31 19:43:35 +000011066
Owen Andersonbc146b02010-09-21 20:42:50 +000011067unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11068 unsigned Depth) const {
11069 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11070 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11071 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011072
Owen Andersonbc146b02010-09-21 20:42:50 +000011073 // Fallback case.
11074 return 1;
11075}
11076
Evan Cheng206ee9d2006-07-07 08:33:52 +000011077/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011078/// node is a GlobalAddress + offset.
11079bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011080 const GlobalValue* &GA,
11081 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011082 if (N->getOpcode() == X86ISD::Wrapper) {
11083 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011084 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011085 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011086 return true;
11087 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011088 }
Evan Chengad4196b2008-05-12 19:56:52 +000011089 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011090}
11091
Evan Cheng206ee9d2006-07-07 08:33:52 +000011092/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11093/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11094/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011095/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011096static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011097 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011098 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011099 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011100
Eli Friedman7a5e5552009-06-07 06:52:44 +000011101 if (VT.getSizeInBits() != 128)
11102 return SDValue();
11103
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011104 // Don't create instructions with illegal types after legalize types has run.
11105 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11106 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11107 return SDValue();
11108
Nate Begemanfdea31a2010-03-24 20:49:50 +000011109 SmallVector<SDValue, 16> Elts;
11110 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011111 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011112
Nate Begemanfdea31a2010-03-24 20:49:50 +000011113 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011114}
Evan Chengd880b972008-05-09 21:53:03 +000011115
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011116/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11117/// generation and convert it from being a bunch of shuffles and extracts
11118/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011119static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11120 const TargetLowering &TLI) {
11121 SDValue InputVector = N->getOperand(0);
11122
11123 // Only operate on vectors of 4 elements, where the alternative shuffling
11124 // gets to be more expensive.
11125 if (InputVector.getValueType() != MVT::v4i32)
11126 return SDValue();
11127
11128 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11129 // single use which is a sign-extend or zero-extend, and all elements are
11130 // used.
11131 SmallVector<SDNode *, 4> Uses;
11132 unsigned ExtractedElements = 0;
11133 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11134 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11135 if (UI.getUse().getResNo() != InputVector.getResNo())
11136 return SDValue();
11137
11138 SDNode *Extract = *UI;
11139 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11140 return SDValue();
11141
11142 if (Extract->getValueType(0) != MVT::i32)
11143 return SDValue();
11144 if (!Extract->hasOneUse())
11145 return SDValue();
11146 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11147 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11148 return SDValue();
11149 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11150 return SDValue();
11151
11152 // Record which element was extracted.
11153 ExtractedElements |=
11154 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11155
11156 Uses.push_back(Extract);
11157 }
11158
11159 // If not all the elements were used, this may not be worthwhile.
11160 if (ExtractedElements != 15)
11161 return SDValue();
11162
11163 // Ok, we've now decided to do the transformation.
11164 DebugLoc dl = InputVector.getDebugLoc();
11165
11166 // Store the value to a temporary stack slot.
11167 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011168 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11169 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011170
11171 // Replace each use (extract) with a load of the appropriate element.
11172 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11173 UE = Uses.end(); UI != UE; ++UI) {
11174 SDNode *Extract = *UI;
11175
Nadav Rotem86694292011-05-17 08:31:57 +000011176 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011177 SDValue Idx = Extract->getOperand(1);
11178 unsigned EltSize =
11179 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11180 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11181 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11182
Nadav Rotem86694292011-05-17 08:31:57 +000011183 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011184 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011185
11186 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011187 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011188 ScalarAddr, MachinePointerInfo(),
11189 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011190
11191 // Replace the exact with the load.
11192 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11193 }
11194
11195 // The replacement was made in place; don't return anything.
11196 return SDValue();
11197}
11198
Chris Lattner83e6c992006-10-04 06:57:07 +000011199/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011200static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011201 const X86Subtarget *Subtarget) {
11202 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011203 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011204 // Get the LHS/RHS of the select.
11205 SDValue LHS = N->getOperand(1);
11206 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011207
Dan Gohman670e5392009-09-21 18:03:22 +000011208 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011209 // instructions match the semantics of the common C idiom x<y?x:y but not
11210 // x<=y?x:y, because of how they handle negative zero (which can be
11211 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011212 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011213 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011214 Cond.getOpcode() == ISD::SETCC) {
11215 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011216
Chris Lattner47b4ce82009-03-11 05:48:52 +000011217 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011218 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011219 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11220 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011221 switch (CC) {
11222 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011223 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011224 // Converting this to a min would handle NaNs incorrectly, and swapping
11225 // the operands would cause it to handle comparisons between positive
11226 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011227 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011228 if (!UnsafeFPMath &&
11229 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11230 break;
11231 std::swap(LHS, RHS);
11232 }
Dan Gohman670e5392009-09-21 18:03:22 +000011233 Opcode = X86ISD::FMIN;
11234 break;
11235 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011236 // Converting this to a min would handle comparisons between positive
11237 // and negative zero incorrectly.
11238 if (!UnsafeFPMath &&
11239 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11240 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011241 Opcode = X86ISD::FMIN;
11242 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011243 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011244 // Converting this to a min would handle both negative zeros and NaNs
11245 // incorrectly, but we can swap the operands to fix both.
11246 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011247 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011248 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011249 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011250 Opcode = X86ISD::FMIN;
11251 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011252
Dan Gohman670e5392009-09-21 18:03:22 +000011253 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011254 // Converting this to a max would handle comparisons between positive
11255 // and negative zero incorrectly.
11256 if (!UnsafeFPMath &&
11257 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11258 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011259 Opcode = X86ISD::FMAX;
11260 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011261 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011262 // Converting this to a max would handle NaNs incorrectly, and swapping
11263 // the operands would cause it to handle comparisons between positive
11264 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011265 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011266 if (!UnsafeFPMath &&
11267 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11268 break;
11269 std::swap(LHS, RHS);
11270 }
Dan Gohman670e5392009-09-21 18:03:22 +000011271 Opcode = X86ISD::FMAX;
11272 break;
11273 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011274 // Converting this to a max would handle both negative zeros and NaNs
11275 // incorrectly, but we can swap the operands to fix both.
11276 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011277 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011278 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011279 case ISD::SETGE:
11280 Opcode = X86ISD::FMAX;
11281 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011282 }
Dan Gohman670e5392009-09-21 18:03:22 +000011283 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011284 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11285 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011286 switch (CC) {
11287 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011288 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011289 // Converting this to a min would handle comparisons between positive
11290 // and negative zero incorrectly, and swapping the operands would
11291 // cause it to handle NaNs incorrectly.
11292 if (!UnsafeFPMath &&
11293 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011294 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011295 break;
11296 std::swap(LHS, RHS);
11297 }
Dan Gohman670e5392009-09-21 18:03:22 +000011298 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011299 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011300 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011301 // Converting this to a min would handle NaNs incorrectly.
11302 if (!UnsafeFPMath &&
11303 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11304 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011305 Opcode = X86ISD::FMIN;
11306 break;
11307 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011308 // Converting this to a min would handle both negative zeros and NaNs
11309 // incorrectly, but we can swap the operands to fix both.
11310 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011311 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011312 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011313 case ISD::SETGE:
11314 Opcode = X86ISD::FMIN;
11315 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011316
Dan Gohman670e5392009-09-21 18:03:22 +000011317 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011318 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011319 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011320 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011321 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011322 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011323 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011324 // Converting this to a max would handle comparisons between positive
11325 // and negative zero incorrectly, and swapping the operands would
11326 // cause it to handle NaNs incorrectly.
11327 if (!UnsafeFPMath &&
11328 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011329 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011330 break;
11331 std::swap(LHS, RHS);
11332 }
Dan Gohman670e5392009-09-21 18:03:22 +000011333 Opcode = X86ISD::FMAX;
11334 break;
11335 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011336 // Converting this to a max would handle both negative zeros and NaNs
11337 // incorrectly, but we can swap the operands to fix both.
11338 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011339 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011340 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011341 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011342 Opcode = X86ISD::FMAX;
11343 break;
11344 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011345 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011346
Chris Lattner47b4ce82009-03-11 05:48:52 +000011347 if (Opcode)
11348 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011349 }
Eric Christopherfd179292009-08-27 18:07:15 +000011350
Chris Lattnerd1980a52009-03-12 06:52:53 +000011351 // If this is a select between two integer constants, try to do some
11352 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011353 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11354 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011355 // Don't do this for crazy integer types.
11356 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11357 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011358 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011359 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011360
Chris Lattnercee56e72009-03-13 05:53:31 +000011361 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011362 // Efficiently invertible.
11363 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11364 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11365 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11366 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011367 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011368 }
Eric Christopherfd179292009-08-27 18:07:15 +000011369
Chris Lattnerd1980a52009-03-12 06:52:53 +000011370 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011371 if (FalseC->getAPIntValue() == 0 &&
11372 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011373 if (NeedsCondInvert) // Invert the condition if needed.
11374 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11375 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011376
Chris Lattnerd1980a52009-03-12 06:52:53 +000011377 // Zero extend the condition if needed.
11378 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011379
Chris Lattnercee56e72009-03-13 05:53:31 +000011380 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011381 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011382 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011383 }
Eric Christopherfd179292009-08-27 18:07:15 +000011384
Chris Lattner97a29a52009-03-13 05:22:11 +000011385 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011386 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011387 if (NeedsCondInvert) // Invert the condition if needed.
11388 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11389 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011390
Chris Lattner97a29a52009-03-13 05:22:11 +000011391 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011392 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11393 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011394 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011395 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011396 }
Eric Christopherfd179292009-08-27 18:07:15 +000011397
Chris Lattnercee56e72009-03-13 05:53:31 +000011398 // Optimize cases that will turn into an LEA instruction. This requires
11399 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011400 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011401 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011402 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011403
Chris Lattnercee56e72009-03-13 05:53:31 +000011404 bool isFastMultiplier = false;
11405 if (Diff < 10) {
11406 switch ((unsigned char)Diff) {
11407 default: break;
11408 case 1: // result = add base, cond
11409 case 2: // result = lea base( , cond*2)
11410 case 3: // result = lea base(cond, cond*2)
11411 case 4: // result = lea base( , cond*4)
11412 case 5: // result = lea base(cond, cond*4)
11413 case 8: // result = lea base( , cond*8)
11414 case 9: // result = lea base(cond, cond*8)
11415 isFastMultiplier = true;
11416 break;
11417 }
11418 }
Eric Christopherfd179292009-08-27 18:07:15 +000011419
Chris Lattnercee56e72009-03-13 05:53:31 +000011420 if (isFastMultiplier) {
11421 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11422 if (NeedsCondInvert) // Invert the condition if needed.
11423 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11424 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011425
Chris Lattnercee56e72009-03-13 05:53:31 +000011426 // Zero extend the condition if needed.
11427 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11428 Cond);
11429 // Scale the condition by the difference.
11430 if (Diff != 1)
11431 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11432 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011433
Chris Lattnercee56e72009-03-13 05:53:31 +000011434 // Add the base if non-zero.
11435 if (FalseC->getAPIntValue() != 0)
11436 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11437 SDValue(FalseC, 0));
11438 return Cond;
11439 }
Eric Christopherfd179292009-08-27 18:07:15 +000011440 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011441 }
11442 }
Eric Christopherfd179292009-08-27 18:07:15 +000011443
Dan Gohman475871a2008-07-27 21:46:04 +000011444 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011445}
11446
Chris Lattnerd1980a52009-03-12 06:52:53 +000011447/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11448static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11449 TargetLowering::DAGCombinerInfo &DCI) {
11450 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011451
Chris Lattnerd1980a52009-03-12 06:52:53 +000011452 // If the flag operand isn't dead, don't touch this CMOV.
11453 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11454 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011455
Evan Chengb5a55d92011-05-24 01:48:22 +000011456 SDValue FalseOp = N->getOperand(0);
11457 SDValue TrueOp = N->getOperand(1);
11458 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11459 SDValue Cond = N->getOperand(3);
11460 if (CC == X86::COND_E || CC == X86::COND_NE) {
11461 switch (Cond.getOpcode()) {
11462 default: break;
11463 case X86ISD::BSR:
11464 case X86ISD::BSF:
11465 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11466 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11467 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11468 }
11469 }
11470
Chris Lattnerd1980a52009-03-12 06:52:53 +000011471 // If this is a select between two integer constants, try to do some
11472 // optimizations. Note that the operands are ordered the opposite of SELECT
11473 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011474 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11475 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011476 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11477 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011478 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11479 CC = X86::GetOppositeBranchCondition(CC);
11480 std::swap(TrueC, FalseC);
11481 }
Eric Christopherfd179292009-08-27 18:07:15 +000011482
Chris Lattnerd1980a52009-03-12 06:52:53 +000011483 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011484 // This is efficient for any integer data type (including i8/i16) and
11485 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011486 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011487 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11488 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011489
Chris Lattnerd1980a52009-03-12 06:52:53 +000011490 // Zero extend the condition if needed.
11491 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011492
Chris Lattnerd1980a52009-03-12 06:52:53 +000011493 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11494 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011495 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011496 if (N->getNumValues() == 2) // Dead flag value?
11497 return DCI.CombineTo(N, Cond, SDValue());
11498 return Cond;
11499 }
Eric Christopherfd179292009-08-27 18:07:15 +000011500
Chris Lattnercee56e72009-03-13 05:53:31 +000011501 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11502 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011503 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011504 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11505 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011506
Chris Lattner97a29a52009-03-13 05:22:11 +000011507 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011508 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11509 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011510 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11511 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011512
Chris Lattner97a29a52009-03-13 05:22:11 +000011513 if (N->getNumValues() == 2) // Dead flag value?
11514 return DCI.CombineTo(N, Cond, SDValue());
11515 return Cond;
11516 }
Eric Christopherfd179292009-08-27 18:07:15 +000011517
Chris Lattnercee56e72009-03-13 05:53:31 +000011518 // Optimize cases that will turn into an LEA instruction. This requires
11519 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011520 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011521 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011522 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011523
Chris Lattnercee56e72009-03-13 05:53:31 +000011524 bool isFastMultiplier = false;
11525 if (Diff < 10) {
11526 switch ((unsigned char)Diff) {
11527 default: break;
11528 case 1: // result = add base, cond
11529 case 2: // result = lea base( , cond*2)
11530 case 3: // result = lea base(cond, cond*2)
11531 case 4: // result = lea base( , cond*4)
11532 case 5: // result = lea base(cond, cond*4)
11533 case 8: // result = lea base( , cond*8)
11534 case 9: // result = lea base(cond, cond*8)
11535 isFastMultiplier = true;
11536 break;
11537 }
11538 }
Eric Christopherfd179292009-08-27 18:07:15 +000011539
Chris Lattnercee56e72009-03-13 05:53:31 +000011540 if (isFastMultiplier) {
11541 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011542 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11543 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011544 // Zero extend the condition if needed.
11545 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11546 Cond);
11547 // Scale the condition by the difference.
11548 if (Diff != 1)
11549 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11550 DAG.getConstant(Diff, Cond.getValueType()));
11551
11552 // Add the base if non-zero.
11553 if (FalseC->getAPIntValue() != 0)
11554 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11555 SDValue(FalseC, 0));
11556 if (N->getNumValues() == 2) // Dead flag value?
11557 return DCI.CombineTo(N, Cond, SDValue());
11558 return Cond;
11559 }
Eric Christopherfd179292009-08-27 18:07:15 +000011560 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011561 }
11562 }
11563 return SDValue();
11564}
11565
11566
Evan Cheng0b0cd912009-03-28 05:57:29 +000011567/// PerformMulCombine - Optimize a single multiply with constant into two
11568/// in order to implement it with two cheaper instructions, e.g.
11569/// LEA + SHL, LEA + LEA.
11570static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11571 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011572 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11573 return SDValue();
11574
Owen Andersone50ed302009-08-10 22:56:29 +000011575 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011576 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011577 return SDValue();
11578
11579 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11580 if (!C)
11581 return SDValue();
11582 uint64_t MulAmt = C->getZExtValue();
11583 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11584 return SDValue();
11585
11586 uint64_t MulAmt1 = 0;
11587 uint64_t MulAmt2 = 0;
11588 if ((MulAmt % 9) == 0) {
11589 MulAmt1 = 9;
11590 MulAmt2 = MulAmt / 9;
11591 } else if ((MulAmt % 5) == 0) {
11592 MulAmt1 = 5;
11593 MulAmt2 = MulAmt / 5;
11594 } else if ((MulAmt % 3) == 0) {
11595 MulAmt1 = 3;
11596 MulAmt2 = MulAmt / 3;
11597 }
11598 if (MulAmt2 &&
11599 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11600 DebugLoc DL = N->getDebugLoc();
11601
11602 if (isPowerOf2_64(MulAmt2) &&
11603 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11604 // If second multiplifer is pow2, issue it first. We want the multiply by
11605 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11606 // is an add.
11607 std::swap(MulAmt1, MulAmt2);
11608
11609 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011610 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011611 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011612 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011613 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011614 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011615 DAG.getConstant(MulAmt1, VT));
11616
Eric Christopherfd179292009-08-27 18:07:15 +000011617 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011618 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011619 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011620 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011621 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011622 DAG.getConstant(MulAmt2, VT));
11623
11624 // Do not add new nodes to DAG combiner worklist.
11625 DCI.CombineTo(N, NewMul, false);
11626 }
11627 return SDValue();
11628}
11629
Evan Chengad9c0a32009-12-15 00:53:42 +000011630static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11631 SDValue N0 = N->getOperand(0);
11632 SDValue N1 = N->getOperand(1);
11633 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11634 EVT VT = N0.getValueType();
11635
11636 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11637 // since the result of setcc_c is all zero's or all ones.
11638 if (N1C && N0.getOpcode() == ISD::AND &&
11639 N0.getOperand(1).getOpcode() == ISD::Constant) {
11640 SDValue N00 = N0.getOperand(0);
11641 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11642 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11643 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11644 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11645 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11646 APInt ShAmt = N1C->getAPIntValue();
11647 Mask = Mask.shl(ShAmt);
11648 if (Mask != 0)
11649 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11650 N00, DAG.getConstant(Mask, VT));
11651 }
11652 }
11653
11654 return SDValue();
11655}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011656
Nate Begeman740ab032009-01-26 00:52:55 +000011657/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11658/// when possible.
11659static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11660 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011661 EVT VT = N->getValueType(0);
11662 if (!VT.isVector() && VT.isInteger() &&
11663 N->getOpcode() == ISD::SHL)
11664 return PerformSHLCombine(N, DAG);
11665
Nate Begeman740ab032009-01-26 00:52:55 +000011666 // On X86 with SSE2 support, we can transform this to a vector shift if
11667 // all elements are shifted by the same amount. We can't do this in legalize
11668 // because the a constant vector is typically transformed to a constant pool
11669 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011670 if (!Subtarget->hasSSE2())
11671 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011672
Owen Anderson825b72b2009-08-11 20:47:22 +000011673 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011674 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011675
Mon P Wang3becd092009-01-28 08:12:05 +000011676 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011677 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011678 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011679 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011680 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11681 unsigned NumElts = VT.getVectorNumElements();
11682 unsigned i = 0;
11683 for (; i != NumElts; ++i) {
11684 SDValue Arg = ShAmtOp.getOperand(i);
11685 if (Arg.getOpcode() == ISD::UNDEF) continue;
11686 BaseShAmt = Arg;
11687 break;
11688 }
11689 for (; i != NumElts; ++i) {
11690 SDValue Arg = ShAmtOp.getOperand(i);
11691 if (Arg.getOpcode() == ISD::UNDEF) continue;
11692 if (Arg != BaseShAmt) {
11693 return SDValue();
11694 }
11695 }
11696 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011697 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011698 SDValue InVec = ShAmtOp.getOperand(0);
11699 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11700 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11701 unsigned i = 0;
11702 for (; i != NumElts; ++i) {
11703 SDValue Arg = InVec.getOperand(i);
11704 if (Arg.getOpcode() == ISD::UNDEF) continue;
11705 BaseShAmt = Arg;
11706 break;
11707 }
11708 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11709 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011710 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011711 if (C->getZExtValue() == SplatIdx)
11712 BaseShAmt = InVec.getOperand(1);
11713 }
11714 }
11715 if (BaseShAmt.getNode() == 0)
11716 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11717 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011718 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011719 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011720
Mon P Wangefa42202009-09-03 19:56:25 +000011721 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011722 if (EltVT.bitsGT(MVT::i32))
11723 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11724 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011725 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011726
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011727 // The shift amount is identical so we can do a vector shift.
11728 SDValue ValOp = N->getOperand(0);
11729 switch (N->getOpcode()) {
11730 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011731 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011732 break;
11733 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011734 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011735 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011736 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011737 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011738 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011739 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011740 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011741 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011742 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011743 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011744 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011745 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011746 break;
11747 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011748 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011749 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011750 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011751 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011752 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011753 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011754 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011755 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011756 break;
11757 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011758 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011759 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011760 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011761 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011762 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011763 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011764 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011765 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011766 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011767 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011768 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011769 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011770 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011771 }
11772 return SDValue();
11773}
11774
Nate Begemanb65c1752010-12-17 22:55:37 +000011775
Stuart Hastings865f0932011-06-03 23:53:54 +000011776// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11777// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11778// and friends. Likewise for OR -> CMPNEQSS.
11779static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11780 TargetLowering::DAGCombinerInfo &DCI,
11781 const X86Subtarget *Subtarget) {
11782 unsigned opcode;
11783
11784 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11785 // we're requiring SSE2 for both.
11786 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11787 SDValue N0 = N->getOperand(0);
11788 SDValue N1 = N->getOperand(1);
11789 SDValue CMP0 = N0->getOperand(1);
11790 SDValue CMP1 = N1->getOperand(1);
11791 DebugLoc DL = N->getDebugLoc();
11792
11793 // The SETCCs should both refer to the same CMP.
11794 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11795 return SDValue();
11796
11797 SDValue CMP00 = CMP0->getOperand(0);
11798 SDValue CMP01 = CMP0->getOperand(1);
11799 EVT VT = CMP00.getValueType();
11800
11801 if (VT == MVT::f32 || VT == MVT::f64) {
11802 bool ExpectingFlags = false;
11803 // Check for any users that want flags:
11804 for (SDNode::use_iterator UI = N->use_begin(),
11805 UE = N->use_end();
11806 !ExpectingFlags && UI != UE; ++UI)
11807 switch (UI->getOpcode()) {
11808 default:
11809 case ISD::BR_CC:
11810 case ISD::BRCOND:
11811 case ISD::SELECT:
11812 ExpectingFlags = true;
11813 break;
11814 case ISD::CopyToReg:
11815 case ISD::SIGN_EXTEND:
11816 case ISD::ZERO_EXTEND:
11817 case ISD::ANY_EXTEND:
11818 break;
11819 }
11820
11821 if (!ExpectingFlags) {
11822 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11823 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11824
11825 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11826 X86::CondCode tmp = cc0;
11827 cc0 = cc1;
11828 cc1 = tmp;
11829 }
11830
11831 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11832 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11833 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11834 X86ISD::NodeType NTOperator = is64BitFP ?
11835 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11836 // FIXME: need symbolic constants for these magic numbers.
11837 // See X86ATTInstPrinter.cpp:printSSECC().
11838 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11839 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11840 DAG.getConstant(x86cc, MVT::i8));
11841 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11842 OnesOrZeroesF);
11843 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11844 DAG.getConstant(1, MVT::i32));
11845 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11846 return OneBitOfTruth;
11847 }
11848 }
11849 }
11850 }
11851 return SDValue();
11852}
11853
Nate Begemanb65c1752010-12-17 22:55:37 +000011854static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11855 TargetLowering::DAGCombinerInfo &DCI,
11856 const X86Subtarget *Subtarget) {
11857 if (DCI.isBeforeLegalizeOps())
11858 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011859
Stuart Hastings865f0932011-06-03 23:53:54 +000011860 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11861 if (R.getNode())
11862 return R;
11863
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011864 // Want to form ANDNP nodes:
11865 // 1) In the hopes of then easily combining them with OR and AND nodes
11866 // to form PBLEND/PSIGN.
11867 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000011868 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011869 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000011870 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011871
Nate Begemanb65c1752010-12-17 22:55:37 +000011872 SDValue N0 = N->getOperand(0);
11873 SDValue N1 = N->getOperand(1);
11874 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011875
Nate Begemanb65c1752010-12-17 22:55:37 +000011876 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011877 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011878 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011879 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000011880
11881 // Check RHS for vnot
11882 if (N1.getOpcode() == ISD::XOR &&
11883 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011884 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011885
Nate Begemanb65c1752010-12-17 22:55:37 +000011886 return SDValue();
11887}
11888
Evan Cheng760d1942010-01-04 21:22:48 +000011889static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011890 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011891 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011892 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011893 return SDValue();
11894
Stuart Hastings865f0932011-06-03 23:53:54 +000011895 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11896 if (R.getNode())
11897 return R;
11898
Evan Cheng760d1942010-01-04 21:22:48 +000011899 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011900 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011901 return SDValue();
11902
Evan Cheng760d1942010-01-04 21:22:48 +000011903 SDValue N0 = N->getOperand(0);
11904 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011905
Nate Begemanb65c1752010-12-17 22:55:37 +000011906 // look for psign/blend
11907 if (Subtarget->hasSSSE3()) {
11908 if (VT == MVT::v2i64) {
11909 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011910 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000011911 std::swap(N0, N1);
11912 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011913 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000011914 SDValue Mask = N1.getOperand(0);
11915 SDValue X = N1.getOperand(1);
11916 SDValue Y;
11917 if (N0.getOperand(0) == Mask)
11918 Y = N0.getOperand(1);
11919 if (N0.getOperand(1) == Mask)
11920 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011921
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011922 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000011923 if (!Y.getNode())
11924 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011925
Nate Begemanb65c1752010-12-17 22:55:37 +000011926 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11927 if (Mask.getOpcode() != ISD::BITCAST ||
11928 X.getOpcode() != ISD::BITCAST ||
11929 Y.getOpcode() != ISD::BITCAST)
11930 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011931
Nate Begemanb65c1752010-12-17 22:55:37 +000011932 // Look through mask bitcast.
11933 Mask = Mask.getOperand(0);
11934 EVT MaskVT = Mask.getValueType();
11935
11936 // Validate that the Mask operand is a vector sra node. The sra node
11937 // will be an intrinsic.
11938 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11939 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011940
Nate Begemanb65c1752010-12-17 22:55:37 +000011941 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11942 // there is no psrai.b
11943 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11944 case Intrinsic::x86_sse2_psrai_w:
11945 case Intrinsic::x86_sse2_psrai_d:
11946 break;
11947 default: return SDValue();
11948 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011949
Nate Begemanb65c1752010-12-17 22:55:37 +000011950 // Check that the SRA is all signbits.
11951 SDValue SraC = Mask.getOperand(2);
11952 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11953 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11954 if ((SraAmt + 1) != EltBits)
11955 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011956
Nate Begemanb65c1752010-12-17 22:55:37 +000011957 DebugLoc DL = N->getDebugLoc();
11958
11959 // Now we know we at least have a plendvb with the mask val. See if
11960 // we can form a psignb/w/d.
11961 // psign = x.type == y.type == mask.type && y = sub(0, x);
11962 X = X.getOperand(0);
11963 Y = Y.getOperand(0);
11964 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11965 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11966 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11967 unsigned Opc = 0;
11968 switch (EltBits) {
11969 case 8: Opc = X86ISD::PSIGNB; break;
11970 case 16: Opc = X86ISD::PSIGNW; break;
11971 case 32: Opc = X86ISD::PSIGND; break;
11972 default: break;
11973 }
11974 if (Opc) {
11975 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11976 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11977 }
11978 }
11979 // PBLENDVB only available on SSE 4.1
11980 if (!Subtarget->hasSSE41())
11981 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011982
Nate Begemanb65c1752010-12-17 22:55:37 +000011983 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11984 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11985 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011986 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011987 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11988 }
11989 }
11990 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011991
Nate Begemanb65c1752010-12-17 22:55:37 +000011992 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011993 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11994 std::swap(N0, N1);
11995 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11996 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011997 if (!N0.hasOneUse() || !N1.hasOneUse())
11998 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011999
12000 SDValue ShAmt0 = N0.getOperand(1);
12001 if (ShAmt0.getValueType() != MVT::i8)
12002 return SDValue();
12003 SDValue ShAmt1 = N1.getOperand(1);
12004 if (ShAmt1.getValueType() != MVT::i8)
12005 return SDValue();
12006 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
12007 ShAmt0 = ShAmt0.getOperand(0);
12008 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
12009 ShAmt1 = ShAmt1.getOperand(0);
12010
12011 DebugLoc DL = N->getDebugLoc();
12012 unsigned Opc = X86ISD::SHLD;
12013 SDValue Op0 = N0.getOperand(0);
12014 SDValue Op1 = N1.getOperand(0);
12015 if (ShAmt0.getOpcode() == ISD::SUB) {
12016 Opc = X86ISD::SHRD;
12017 std::swap(Op0, Op1);
12018 std::swap(ShAmt0, ShAmt1);
12019 }
12020
Evan Cheng8b1190a2010-04-28 01:18:01 +000012021 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000012022 if (ShAmt1.getOpcode() == ISD::SUB) {
12023 SDValue Sum = ShAmt1.getOperand(0);
12024 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000012025 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
12026 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
12027 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
12028 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000012029 return DAG.getNode(Opc, DL, VT,
12030 Op0, Op1,
12031 DAG.getNode(ISD::TRUNCATE, DL,
12032 MVT::i8, ShAmt0));
12033 }
12034 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
12035 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
12036 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000012037 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000012038 return DAG.getNode(Opc, DL, VT,
12039 N0.getOperand(0), N1.getOperand(0),
12040 DAG.getNode(ISD::TRUNCATE, DL,
12041 MVT::i8, ShAmt0));
12042 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012043
Evan Cheng760d1942010-01-04 21:22:48 +000012044 return SDValue();
12045}
12046
Chris Lattner149a4e52008-02-22 02:09:43 +000012047/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012048static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000012049 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000012050 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
12051 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000012052 // A preferable solution to the general problem is to figure out the right
12053 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000012054
12055 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012056 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012057 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012058 if (VT.getSizeInBits() != 64)
12059 return SDValue();
12060
Devang Patel578efa92009-06-05 21:57:13 +000012061 const Function *F = DAG.getMachineFunction().getFunction();
12062 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012063 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012064 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012065 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012066 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012067 isa<LoadSDNode>(St->getValue()) &&
12068 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12069 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012070 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012071 LoadSDNode *Ld = 0;
12072 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012073 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012074 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012075 // Must be a store of a load. We currently handle two cases: the load
12076 // is a direct child, and it's under an intervening TokenFactor. It is
12077 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012078 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012079 Ld = cast<LoadSDNode>(St->getChain());
12080 else if (St->getValue().hasOneUse() &&
12081 ChainVal->getOpcode() == ISD::TokenFactor) {
12082 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012083 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012084 TokenFactorIndex = i;
12085 Ld = cast<LoadSDNode>(St->getValue());
12086 } else
12087 Ops.push_back(ChainVal->getOperand(i));
12088 }
12089 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012090
Evan Cheng536e6672009-03-12 05:59:15 +000012091 if (!Ld || !ISD::isNormalLoad(Ld))
12092 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012093
Evan Cheng536e6672009-03-12 05:59:15 +000012094 // If this is not the MMX case, i.e. we are just turning i64 load/store
12095 // into f64 load/store, avoid the transformation if there are multiple
12096 // uses of the loaded value.
12097 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12098 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012099
Evan Cheng536e6672009-03-12 05:59:15 +000012100 DebugLoc LdDL = Ld->getDebugLoc();
12101 DebugLoc StDL = N->getDebugLoc();
12102 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12103 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12104 // pair instead.
12105 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012106 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012107 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12108 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012109 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012110 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012111 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012112 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012113 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012114 Ops.size());
12115 }
Evan Cheng536e6672009-03-12 05:59:15 +000012116 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012117 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012118 St->isVolatile(), St->isNonTemporal(),
12119 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012120 }
Evan Cheng536e6672009-03-12 05:59:15 +000012121
12122 // Otherwise, lower to two pairs of 32-bit loads / stores.
12123 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012124 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12125 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012126
Owen Anderson825b72b2009-08-11 20:47:22 +000012127 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012128 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012129 Ld->isVolatile(), Ld->isNonTemporal(),
12130 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012131 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012132 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012133 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012134 MinAlign(Ld->getAlignment(), 4));
12135
12136 SDValue NewChain = LoLd.getValue(1);
12137 if (TokenFactorIndex != -1) {
12138 Ops.push_back(LoLd);
12139 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012140 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012141 Ops.size());
12142 }
12143
12144 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012145 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12146 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012147
12148 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012149 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012150 St->isVolatile(), St->isNonTemporal(),
12151 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012152 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012153 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012154 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012155 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012156 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012157 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012158 }
Dan Gohman475871a2008-07-27 21:46:04 +000012159 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012160}
12161
Chris Lattner6cf73262008-01-25 06:14:17 +000012162/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12163/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012164static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012165 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12166 // F[X]OR(0.0, x) -> x
12167 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012168 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12169 if (C->getValueAPF().isPosZero())
12170 return N->getOperand(1);
12171 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12172 if (C->getValueAPF().isPosZero())
12173 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012174 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012175}
12176
12177/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012178static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012179 // FAND(0.0, x) -> 0.0
12180 // FAND(x, 0.0) -> 0.0
12181 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12182 if (C->getValueAPF().isPosZero())
12183 return N->getOperand(0);
12184 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12185 if (C->getValueAPF().isPosZero())
12186 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012187 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012188}
12189
Dan Gohmane5af2d32009-01-29 01:59:02 +000012190static SDValue PerformBTCombine(SDNode *N,
12191 SelectionDAG &DAG,
12192 TargetLowering::DAGCombinerInfo &DCI) {
12193 // BT ignores high bits in the bit index operand.
12194 SDValue Op1 = N->getOperand(1);
12195 if (Op1.hasOneUse()) {
12196 unsigned BitWidth = Op1.getValueSizeInBits();
12197 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12198 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012199 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12200 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012201 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012202 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12203 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12204 DCI.CommitTargetLoweringOpt(TLO);
12205 }
12206 return SDValue();
12207}
Chris Lattner83e6c992006-10-04 06:57:07 +000012208
Eli Friedman7a5e5552009-06-07 06:52:44 +000012209static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12210 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012211 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012212 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012213 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012214 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012215 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012216 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012217 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012218 }
12219 return SDValue();
12220}
12221
Evan Cheng2e489c42009-12-16 00:53:11 +000012222static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12223 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12224 // (and (i32 x86isd::setcc_carry), 1)
12225 // This eliminates the zext. This transformation is necessary because
12226 // ISD::SETCC is always legalized to i8.
12227 DebugLoc dl = N->getDebugLoc();
12228 SDValue N0 = N->getOperand(0);
12229 EVT VT = N->getValueType(0);
12230 if (N0.getOpcode() == ISD::AND &&
12231 N0.hasOneUse() &&
12232 N0.getOperand(0).hasOneUse()) {
12233 SDValue N00 = N0.getOperand(0);
12234 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12235 return SDValue();
12236 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12237 if (!C || C->getZExtValue() != 1)
12238 return SDValue();
12239 return DAG.getNode(ISD::AND, dl, VT,
12240 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12241 N00.getOperand(0), N00.getOperand(1)),
12242 DAG.getConstant(1, VT));
12243 }
12244
12245 return SDValue();
12246}
12247
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012248// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12249static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12250 unsigned X86CC = N->getConstantOperandVal(0);
12251 SDValue EFLAG = N->getOperand(1);
12252 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012253
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012254 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12255 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12256 // cases.
12257 if (X86CC == X86::COND_B)
12258 return DAG.getNode(ISD::AND, DL, MVT::i8,
12259 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12260 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12261 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012262
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012263 return SDValue();
12264}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012265
Benjamin Kramer1396c402011-06-18 11:09:41 +000012266static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12267 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012268 SDValue Op0 = N->getOperand(0);
12269 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12270 // a 32-bit target where SSE doesn't support i64->FP operations.
12271 if (Op0.getOpcode() == ISD::LOAD) {
12272 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12273 EVT VT = Ld->getValueType(0);
12274 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12275 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12276 !XTLI->getSubtarget()->is64Bit() &&
12277 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012278 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12279 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012280 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12281 return FILDChain;
12282 }
12283 }
12284 return SDValue();
12285}
12286
Chris Lattner23a01992010-12-20 01:37:09 +000012287// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12288static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12289 X86TargetLowering::DAGCombinerInfo &DCI) {
12290 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12291 // the result is either zero or one (depending on the input carry bit).
12292 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12293 if (X86::isZeroNode(N->getOperand(0)) &&
12294 X86::isZeroNode(N->getOperand(1)) &&
12295 // We don't have a good way to replace an EFLAGS use, so only do this when
12296 // dead right now.
12297 SDValue(N, 1).use_empty()) {
12298 DebugLoc DL = N->getDebugLoc();
12299 EVT VT = N->getValueType(0);
12300 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12301 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12302 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12303 DAG.getConstant(X86::COND_B,MVT::i8),
12304 N->getOperand(2)),
12305 DAG.getConstant(1, VT));
12306 return DCI.CombineTo(N, Res1, CarryOut);
12307 }
12308
12309 return SDValue();
12310}
12311
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012312// fold (add Y, (sete X, 0)) -> adc 0, Y
12313// (add Y, (setne X, 0)) -> sbb -1, Y
12314// (sub (sete X, 0), Y) -> sbb 0, Y
12315// (sub (setne X, 0), Y) -> adc -1, Y
12316static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12317 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012318
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012319 // Look through ZExts.
12320 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12321 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12322 return SDValue();
12323
12324 SDValue SetCC = Ext.getOperand(0);
12325 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12326 return SDValue();
12327
12328 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12329 if (CC != X86::COND_E && CC != X86::COND_NE)
12330 return SDValue();
12331
12332 SDValue Cmp = SetCC.getOperand(1);
12333 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012334 !X86::isZeroNode(Cmp.getOperand(1)) ||
12335 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012336 return SDValue();
12337
12338 SDValue CmpOp0 = Cmp.getOperand(0);
12339 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12340 DAG.getConstant(1, CmpOp0.getValueType()));
12341
12342 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12343 if (CC == X86::COND_NE)
12344 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12345 DL, OtherVal.getValueType(), OtherVal,
12346 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12347 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12348 DL, OtherVal.getValueType(), OtherVal,
12349 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12350}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012351
Dan Gohman475871a2008-07-27 21:46:04 +000012352SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012353 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012354 SelectionDAG &DAG = DCI.DAG;
12355 switch (N->getOpcode()) {
12356 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012357 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012358 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012359 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012360 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012361 case ISD::ADD:
12362 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012363 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012364 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012365 case ISD::SHL:
12366 case ISD::SRA:
12367 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012368 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012369 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012370 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012371 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012372 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012373 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12374 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012375 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012376 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012377 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012378 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012379 case X86ISD::SHUFPS: // Handle all target specific shuffles
12380 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012381 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012382 case X86ISD::PUNPCKHBW:
12383 case X86ISD::PUNPCKHWD:
12384 case X86ISD::PUNPCKHDQ:
12385 case X86ISD::PUNPCKHQDQ:
12386 case X86ISD::UNPCKHPS:
12387 case X86ISD::UNPCKHPD:
12388 case X86ISD::PUNPCKLBW:
12389 case X86ISD::PUNPCKLWD:
12390 case X86ISD::PUNPCKLDQ:
12391 case X86ISD::PUNPCKLQDQ:
12392 case X86ISD::UNPCKLPS:
12393 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012394 case X86ISD::VUNPCKLPS:
12395 case X86ISD::VUNPCKLPD:
12396 case X86ISD::VUNPCKLPSY:
12397 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012398 case X86ISD::MOVHLPS:
12399 case X86ISD::MOVLHPS:
12400 case X86ISD::PSHUFD:
12401 case X86ISD::PSHUFHW:
12402 case X86ISD::PSHUFLW:
12403 case X86ISD::MOVSS:
12404 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012405 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012406 }
12407
Dan Gohman475871a2008-07-27 21:46:04 +000012408 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012409}
12410
Evan Chenge5b51ac2010-04-17 06:13:15 +000012411/// isTypeDesirableForOp - Return true if the target has native support for
12412/// the specified value type and it is 'desirable' to use the type for the
12413/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12414/// instruction encodings are longer and some i16 instructions are slow.
12415bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12416 if (!isTypeLegal(VT))
12417 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012418 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012419 return true;
12420
12421 switch (Opc) {
12422 default:
12423 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012424 case ISD::LOAD:
12425 case ISD::SIGN_EXTEND:
12426 case ISD::ZERO_EXTEND:
12427 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012428 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012429 case ISD::SRL:
12430 case ISD::SUB:
12431 case ISD::ADD:
12432 case ISD::MUL:
12433 case ISD::AND:
12434 case ISD::OR:
12435 case ISD::XOR:
12436 return false;
12437 }
12438}
12439
12440/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012441/// beneficial for dag combiner to promote the specified node. If true, it
12442/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012443bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012444 EVT VT = Op.getValueType();
12445 if (VT != MVT::i16)
12446 return false;
12447
Evan Cheng4c26e932010-04-19 19:29:22 +000012448 bool Promote = false;
12449 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012450 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012451 default: break;
12452 case ISD::LOAD: {
12453 LoadSDNode *LD = cast<LoadSDNode>(Op);
12454 // If the non-extending load has a single use and it's not live out, then it
12455 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012456 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12457 Op.hasOneUse()*/) {
12458 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12459 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12460 // The only case where we'd want to promote LOAD (rather then it being
12461 // promoted as an operand is when it's only use is liveout.
12462 if (UI->getOpcode() != ISD::CopyToReg)
12463 return false;
12464 }
12465 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012466 Promote = true;
12467 break;
12468 }
12469 case ISD::SIGN_EXTEND:
12470 case ISD::ZERO_EXTEND:
12471 case ISD::ANY_EXTEND:
12472 Promote = true;
12473 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012474 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012475 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012476 SDValue N0 = Op.getOperand(0);
12477 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012478 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012479 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012480 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012481 break;
12482 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012483 case ISD::ADD:
12484 case ISD::MUL:
12485 case ISD::AND:
12486 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012487 case ISD::XOR:
12488 Commute = true;
12489 // fallthrough
12490 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012491 SDValue N0 = Op.getOperand(0);
12492 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012493 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012494 return false;
12495 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012496 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012497 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012498 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012499 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012500 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012501 }
12502 }
12503
12504 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012505 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012506}
12507
Evan Cheng60c07e12006-07-05 22:17:51 +000012508//===----------------------------------------------------------------------===//
12509// X86 Inline Assembly Support
12510//===----------------------------------------------------------------------===//
12511
Chris Lattnerb8105652009-07-20 17:51:36 +000012512bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12513 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012514
12515 std::string AsmStr = IA->getAsmString();
12516
12517 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012518 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012519 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012520
12521 switch (AsmPieces.size()) {
12522 default: return false;
12523 case 1:
12524 AsmStr = AsmPieces[0];
12525 AsmPieces.clear();
12526 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12527
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012528 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012529 // we will turn this bswap into something that will be lowered to logical ops
12530 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12531 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012532 // bswap $0
12533 if (AsmPieces.size() == 2 &&
12534 (AsmPieces[0] == "bswap" ||
12535 AsmPieces[0] == "bswapq" ||
12536 AsmPieces[0] == "bswapl") &&
12537 (AsmPieces[1] == "$0" ||
12538 AsmPieces[1] == "${0:q}")) {
12539 // No need to check constraints, nothing other than the equivalent of
12540 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012541 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012542 if (!Ty || Ty->getBitWidth() % 16 != 0)
12543 return false;
12544 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012545 }
12546 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012547 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012548 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012549 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012550 AsmPieces[1] == "$$8," &&
12551 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012552 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12553 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012554 const std::string &ConstraintsStr = IA->getConstraintString();
12555 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012556 std::sort(AsmPieces.begin(), AsmPieces.end());
12557 if (AsmPieces.size() == 4 &&
12558 AsmPieces[0] == "~{cc}" &&
12559 AsmPieces[1] == "~{dirflag}" &&
12560 AsmPieces[2] == "~{flags}" &&
12561 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012562 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012563 if (!Ty || Ty->getBitWidth() % 16 != 0)
12564 return false;
12565 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012566 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012567 }
12568 break;
12569 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012570 if (CI->getType()->isIntegerTy(32) &&
12571 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12572 SmallVector<StringRef, 4> Words;
12573 SplitString(AsmPieces[0], Words, " \t,");
12574 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12575 Words[2] == "${0:w}") {
12576 Words.clear();
12577 SplitString(AsmPieces[1], Words, " \t,");
12578 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12579 Words[2] == "$0") {
12580 Words.clear();
12581 SplitString(AsmPieces[2], Words, " \t,");
12582 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12583 Words[2] == "${0:w}") {
12584 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012585 const std::string &ConstraintsStr = IA->getConstraintString();
12586 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012587 std::sort(AsmPieces.begin(), AsmPieces.end());
12588 if (AsmPieces.size() == 4 &&
12589 AsmPieces[0] == "~{cc}" &&
12590 AsmPieces[1] == "~{dirflag}" &&
12591 AsmPieces[2] == "~{flags}" &&
12592 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012593 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012594 if (!Ty || Ty->getBitWidth() % 16 != 0)
12595 return false;
12596 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012597 }
12598 }
12599 }
12600 }
12601 }
Evan Cheng55d42002011-01-08 01:24:27 +000012602
12603 if (CI->getType()->isIntegerTy(64)) {
12604 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12605 if (Constraints.size() >= 2 &&
12606 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12607 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12608 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12609 SmallVector<StringRef, 4> Words;
12610 SplitString(AsmPieces[0], Words, " \t");
12611 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012612 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012613 SplitString(AsmPieces[1], Words, " \t");
12614 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12615 Words.clear();
12616 SplitString(AsmPieces[2], Words, " \t,");
12617 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12618 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012619 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012620 if (!Ty || Ty->getBitWidth() % 16 != 0)
12621 return false;
12622 return IntrinsicLowering::LowerToByteSwap(CI);
12623 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012624 }
12625 }
12626 }
12627 }
12628 break;
12629 }
12630 return false;
12631}
12632
12633
12634
Chris Lattnerf4dff842006-07-11 02:54:03 +000012635/// getConstraintType - Given a constraint letter, return the type of
12636/// constraint it is for this target.
12637X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012638X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12639 if (Constraint.size() == 1) {
12640 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012641 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012642 case 'q':
12643 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012644 case 'f':
12645 case 't':
12646 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012647 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012648 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012649 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000012650 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000012651 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012652 case 'a':
12653 case 'b':
12654 case 'c':
12655 case 'd':
12656 case 'S':
12657 case 'D':
12658 case 'A':
12659 return C_Register;
12660 case 'I':
12661 case 'J':
12662 case 'K':
12663 case 'L':
12664 case 'M':
12665 case 'N':
12666 case 'G':
12667 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012668 case 'e':
12669 case 'Z':
12670 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012671 default:
12672 break;
12673 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012674 }
Chris Lattner4234f572007-03-25 02:14:49 +000012675 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012676}
12677
John Thompson44ab89e2010-10-29 17:29:13 +000012678/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012679/// This object must already have been set up with the operand type
12680/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012681TargetLowering::ConstraintWeight
12682 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012683 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012684 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012685 Value *CallOperandVal = info.CallOperandVal;
12686 // If we don't have a value, we can't do a match,
12687 // but allow it at the lowest weight.
12688 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012689 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012690 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012691 // Look at the constraint type.
12692 switch (*constraint) {
12693 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012694 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12695 case 'R':
12696 case 'q':
12697 case 'Q':
12698 case 'a':
12699 case 'b':
12700 case 'c':
12701 case 'd':
12702 case 'S':
12703 case 'D':
12704 case 'A':
12705 if (CallOperandVal->getType()->isIntegerTy())
12706 weight = CW_SpecificReg;
12707 break;
12708 case 'f':
12709 case 't':
12710 case 'u':
12711 if (type->isFloatingPointTy())
12712 weight = CW_SpecificReg;
12713 break;
12714 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012715 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012716 weight = CW_SpecificReg;
12717 break;
12718 case 'x':
12719 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012720 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012721 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012722 break;
12723 case 'I':
12724 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12725 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012726 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012727 }
12728 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012729 case 'J':
12730 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12731 if (C->getZExtValue() <= 63)
12732 weight = CW_Constant;
12733 }
12734 break;
12735 case 'K':
12736 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12737 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12738 weight = CW_Constant;
12739 }
12740 break;
12741 case 'L':
12742 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12743 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12744 weight = CW_Constant;
12745 }
12746 break;
12747 case 'M':
12748 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12749 if (C->getZExtValue() <= 3)
12750 weight = CW_Constant;
12751 }
12752 break;
12753 case 'N':
12754 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12755 if (C->getZExtValue() <= 0xff)
12756 weight = CW_Constant;
12757 }
12758 break;
12759 case 'G':
12760 case 'C':
12761 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12762 weight = CW_Constant;
12763 }
12764 break;
12765 case 'e':
12766 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12767 if ((C->getSExtValue() >= -0x80000000LL) &&
12768 (C->getSExtValue() <= 0x7fffffffLL))
12769 weight = CW_Constant;
12770 }
12771 break;
12772 case 'Z':
12773 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12774 if (C->getZExtValue() <= 0xffffffff)
12775 weight = CW_Constant;
12776 }
12777 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012778 }
12779 return weight;
12780}
12781
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012782/// LowerXConstraint - try to replace an X constraint, which matches anything,
12783/// with another that has more specific requirements based on the type of the
12784/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012785const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012786LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012787 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12788 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012789 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012790 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012791 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012792 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012793 return "x";
12794 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012795
Chris Lattner5e764232008-04-26 23:02:14 +000012796 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012797}
12798
Chris Lattner48884cd2007-08-25 00:47:38 +000012799/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12800/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012801void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012802 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012803 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012804 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012805 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012806
Eric Christopher100c8332011-06-02 23:16:42 +000012807 // Only support length 1 constraints for now.
12808 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012809
Eric Christopher100c8332011-06-02 23:16:42 +000012810 char ConstraintLetter = Constraint[0];
12811 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012812 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012813 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012814 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012815 if (C->getZExtValue() <= 31) {
12816 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012817 break;
12818 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012819 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012820 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012821 case 'J':
12822 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012823 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012824 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12825 break;
12826 }
12827 }
12828 return;
12829 case 'K':
12830 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012831 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012832 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12833 break;
12834 }
12835 }
12836 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012837 case 'N':
12838 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012839 if (C->getZExtValue() <= 255) {
12840 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012841 break;
12842 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012843 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012844 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012845 case 'e': {
12846 // 32-bit signed value
12847 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012848 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12849 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012850 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012851 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012852 break;
12853 }
12854 // FIXME gcc accepts some relocatable values here too, but only in certain
12855 // memory models; it's complicated.
12856 }
12857 return;
12858 }
12859 case 'Z': {
12860 // 32-bit unsigned value
12861 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012862 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12863 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012864 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12865 break;
12866 }
12867 }
12868 // FIXME gcc accepts some relocatable values here too, but only in certain
12869 // memory models; it's complicated.
12870 return;
12871 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012872 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012873 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012874 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012875 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012876 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012877 break;
12878 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012879
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012880 // In any sort of PIC mode addresses need to be computed at runtime by
12881 // adding in a register or some sort of table lookup. These can't
12882 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012883 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012884 return;
12885
Chris Lattnerdc43a882007-05-03 16:52:29 +000012886 // If we are in non-pic codegen mode, we allow the address of a global (with
12887 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012888 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012889 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012890
Chris Lattner49921962009-05-08 18:23:14 +000012891 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12892 while (1) {
12893 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12894 Offset += GA->getOffset();
12895 break;
12896 } else if (Op.getOpcode() == ISD::ADD) {
12897 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12898 Offset += C->getZExtValue();
12899 Op = Op.getOperand(0);
12900 continue;
12901 }
12902 } else if (Op.getOpcode() == ISD::SUB) {
12903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12904 Offset += -C->getZExtValue();
12905 Op = Op.getOperand(0);
12906 continue;
12907 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012908 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012909
Chris Lattner49921962009-05-08 18:23:14 +000012910 // Otherwise, this isn't something we can handle, reject it.
12911 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012912 }
Eric Christopherfd179292009-08-27 18:07:15 +000012913
Dan Gohman46510a72010-04-15 01:51:59 +000012914 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012915 // If we require an extra load to get this address, as in PIC mode, we
12916 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012917 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12918 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012919 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012920
Devang Patel0d881da2010-07-06 22:08:15 +000012921 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12922 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012923 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012924 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012925 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012926
Gabor Greifba36cb52008-08-28 21:40:38 +000012927 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012928 Ops.push_back(Result);
12929 return;
12930 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012931 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012932}
12933
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012934std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012935X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012936 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012937 // First, see if this is a constraint that directly corresponds to an LLVM
12938 // register class.
12939 if (Constraint.size() == 1) {
12940 // GCC Constraint Letters
12941 switch (Constraint[0]) {
12942 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000012943 // TODO: Slight differences here in allocation order and leaving
12944 // RIP in the class. Do they matter any more here than they do
12945 // in the normal allocation?
12946 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12947 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012948 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012949 return std::make_pair(0U, X86::GR32RegisterClass);
12950 else if (VT == MVT::i16)
12951 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000012952 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000012953 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012954 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000012955 return std::make_pair(0U, X86::GR64RegisterClass);
12956 break;
12957 }
12958 // 32-bit fallthrough
12959 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012960 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012961 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
12962 else if (VT == MVT::i16)
12963 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000012964 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000012965 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
12966 else if (VT == MVT::i64)
12967 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
12968 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012969 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012970 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000012971 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012972 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012973 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012974 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000012975 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012976 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012977 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012978 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000012979 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012980 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12981 if (VT == MVT::i16)
12982 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12983 if (VT == MVT::i32 || !Subtarget->is64Bit())
12984 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12985 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012986 case 'f': // FP Stack registers.
12987 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12988 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012989 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012990 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012991 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012992 return std::make_pair(0U, X86::RFP64RegisterClass);
12993 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012994 case 'y': // MMX_REGS if MMX allowed.
12995 if (!Subtarget->hasMMX()) break;
12996 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012997 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012998 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012999 // FALL THROUGH.
13000 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013001 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000013002
Owen Anderson825b72b2009-08-11 20:47:22 +000013003 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000013004 default: break;
13005 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013006 case MVT::f32:
13007 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000013008 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013009 case MVT::f64:
13010 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000013011 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013012 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013013 case MVT::v16i8:
13014 case MVT::v8i16:
13015 case MVT::v4i32:
13016 case MVT::v2i64:
13017 case MVT::v4f32:
13018 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000013019 return std::make_pair(0U, X86::VR128RegisterClass);
13020 }
Chris Lattnerad043e82007-04-09 05:11:28 +000013021 break;
13022 }
13023 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013024
Chris Lattnerf76d1802006-07-31 23:26:50 +000013025 // Use the default implementation in TargetLowering to convert the register
13026 // constraint into a member of a register class.
13027 std::pair<unsigned, const TargetRegisterClass*> Res;
13028 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000013029
13030 // Not found as a standard register?
13031 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013032 // Map st(0) -> st(7) -> ST0
13033 if (Constraint.size() == 7 && Constraint[0] == '{' &&
13034 tolower(Constraint[1]) == 's' &&
13035 tolower(Constraint[2]) == 't' &&
13036 Constraint[3] == '(' &&
13037 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
13038 Constraint[5] == ')' &&
13039 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000013040
Chris Lattner56d77c72009-09-13 22:41:48 +000013041 Res.first = X86::ST0+Constraint[4]-'0';
13042 Res.second = X86::RFP80RegisterClass;
13043 return Res;
13044 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013045
Chris Lattner56d77c72009-09-13 22:41:48 +000013046 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013047 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000013048 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000013049 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013050 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013051 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013052
13053 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013054 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013055 Res.first = X86::EFLAGS;
13056 Res.second = X86::CCRRegisterClass;
13057 return Res;
13058 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013059
Dale Johannesen330169f2008-11-13 21:52:36 +000013060 // 'A' means EAX + EDX.
13061 if (Constraint == "A") {
13062 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013063 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013064 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013065 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013066 return Res;
13067 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013068
Chris Lattnerf76d1802006-07-31 23:26:50 +000013069 // Otherwise, check to see if this is a register class of the wrong value
13070 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13071 // turn into {ax},{dx}.
13072 if (Res.second->hasType(VT))
13073 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013074
Chris Lattnerf76d1802006-07-31 23:26:50 +000013075 // All of the single-register GCC register classes map their values onto
13076 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13077 // really want an 8-bit or 32-bit register, map to the appropriate register
13078 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013079 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013080 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013081 unsigned DestReg = 0;
13082 switch (Res.first) {
13083 default: break;
13084 case X86::AX: DestReg = X86::AL; break;
13085 case X86::DX: DestReg = X86::DL; break;
13086 case X86::CX: DestReg = X86::CL; break;
13087 case X86::BX: DestReg = X86::BL; break;
13088 }
13089 if (DestReg) {
13090 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013091 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013092 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013093 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013094 unsigned DestReg = 0;
13095 switch (Res.first) {
13096 default: break;
13097 case X86::AX: DestReg = X86::EAX; break;
13098 case X86::DX: DestReg = X86::EDX; break;
13099 case X86::CX: DestReg = X86::ECX; break;
13100 case X86::BX: DestReg = X86::EBX; break;
13101 case X86::SI: DestReg = X86::ESI; break;
13102 case X86::DI: DestReg = X86::EDI; break;
13103 case X86::BP: DestReg = X86::EBP; break;
13104 case X86::SP: DestReg = X86::ESP; break;
13105 }
13106 if (DestReg) {
13107 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013108 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013109 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013110 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013111 unsigned DestReg = 0;
13112 switch (Res.first) {
13113 default: break;
13114 case X86::AX: DestReg = X86::RAX; break;
13115 case X86::DX: DestReg = X86::RDX; break;
13116 case X86::CX: DestReg = X86::RCX; break;
13117 case X86::BX: DestReg = X86::RBX; break;
13118 case X86::SI: DestReg = X86::RSI; break;
13119 case X86::DI: DestReg = X86::RDI; break;
13120 case X86::BP: DestReg = X86::RBP; break;
13121 case X86::SP: DestReg = X86::RSP; break;
13122 }
13123 if (DestReg) {
13124 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013125 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013126 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013127 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013128 } else if (Res.second == X86::FR32RegisterClass ||
13129 Res.second == X86::FR64RegisterClass ||
13130 Res.second == X86::VR128RegisterClass) {
13131 // Handle references to XMM physical registers that got mapped into the
13132 // wrong class. This can happen with constraints like {xmm0} where the
13133 // target independent register mapper will just pick the first match it can
13134 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013135 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013136 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013137 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013138 Res.second = X86::FR64RegisterClass;
13139 else if (X86::VR128RegisterClass->hasType(VT))
13140 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013141 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013142
Chris Lattnerf76d1802006-07-31 23:26:50 +000013143 return Res;
13144}