blob: 6ce531a960a1ee2c87468ee03a0a92eb0b9e86dd [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
David Greenef125a292011-02-08 19:04:41 +000074static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
75
76
David Greenea5f26012011-02-07 19:36:54 +000077/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
78/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000079/// simple subregister reference. Idx is an index in the 128 bits we
80/// want. It need not be aligned to a 128-bit bounday. That makes
81/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000082static SDValue Extract128BitVector(SDValue Vec,
83 SDValue Idx,
84 SelectionDAG &DAG,
85 DebugLoc dl) {
86 EVT VT = Vec.getValueType();
87 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000088 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000089 int Factor = VT.getSizeInBits()/128;
90 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
91 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000092
93 // Extract from UNDEF is UNDEF.
94 if (Vec.getOpcode() == ISD::UNDEF)
95 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
96
97 if (isa<ConstantSDNode>(Idx)) {
98 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
99
100 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
101 // we can match to VEXTRACTF128.
102 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
103
104 // This is the index of the first element of the 128-bit chunk
105 // we want.
106 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
107 * ElemsPerChunk);
108
109 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000110 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
111 VecIdx);
112
113 return Result;
114 }
115
116 return SDValue();
117}
118
119/// Generate a DAG to put 128-bits into a vector > 128 bits. This
120/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000121/// simple superregister reference. Idx is an index in the 128 bits
122/// we want. It need not be aligned to a 128-bit bounday. That makes
123/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000124static SDValue Insert128BitVector(SDValue Result,
125 SDValue Vec,
126 SDValue Idx,
127 SelectionDAG &DAG,
128 DebugLoc dl) {
129 if (isa<ConstantSDNode>(Idx)) {
130 EVT VT = Vec.getValueType();
131 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
132
133 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000134 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000135 EVT ResultVT = Result.getValueType();
136
137 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000138 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000139
140 // This is the index of the first element of the 128-bit chunk
141 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000142 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000143 * ElemsPerChunk);
144
145 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000146 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
147 VecIdx);
148 return Result;
149 }
150
151 return SDValue();
152}
153
David Greenef125a292011-02-08 19:04:41 +0000154/// Given two vectors, concat them.
155static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
156 DebugLoc dl = Lower.getDebugLoc();
157
158 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
159
160 EVT VT = EVT::getVectorVT(*DAG.getContext(),
161 Lower.getValueType().getVectorElementType(),
162 Lower.getValueType().getVectorNumElements() * 2);
163
164 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
165 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
166
167 // Insert the upper subvector.
168 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
169 DAG.getConstant(
170 // This is half the length of the result
171 // vector. Start inserting the upper 128
172 // bits here.
173 Lower.getValueType().getVectorNumElements(),
174 MVT::i32),
175 DAG, dl);
176
177 // Insert the lower subvector.
178 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
179 return Vec;
180}
181
Chris Lattnerf0144122009-07-28 03:13:23 +0000182static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000183 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
184 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000185
Evan Cheng2bffee22011-02-01 01:14:13 +0000186 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000187 if (is64Bit)
188 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000189 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000190 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000191
Evan Cheng203576a2011-07-20 19:50:42 +0000192 if (Subtarget->isTargetELF())
193 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000194 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000195 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000196 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000197}
198
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000199X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000200 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000201 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000202 X86ScalarSSEf64 = Subtarget->hasXMMInt();
203 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000204 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000205
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000206 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000207 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000208
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000209 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000210 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000211
212 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000213 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000214
Eric Christopherde5e1012011-03-11 01:05:58 +0000215 // For 64-bit since we have so many registers use the ILP scheduler, for
216 // 32-bit code use the register pressure specific scheduling.
217 if (Subtarget->is64Bit())
218 setSchedulingPreference(Sched::ILP);
219 else
220 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000221 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000222
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000223 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000224 // Setup Windows compiler runtime calls.
225 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000226 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000227 setLibcallName(RTLIB::SREM_I64, "_allrem");
228 setLibcallName(RTLIB::UREM_I64, "_aullrem");
229 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000230 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000231 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000232 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000233 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000234 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
235 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
236 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000237 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
238 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000239 }
240
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000241 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000242 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000243 setUseUnderscoreSetJmp(false);
244 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000245 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000246 // MS runtime is weird: it exports _setjmp, but longjmp!
247 setUseUnderscoreSetJmp(true);
248 setUseUnderscoreLongJmp(false);
249 } else {
250 setUseUnderscoreSetJmp(true);
251 setUseUnderscoreLongJmp(true);
252 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000253
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000254 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000256 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000258 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000260
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000262
Scott Michelfdc40a02009-02-17 22:15:04 +0000263 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000265 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000267 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
269 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000270
271 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
273 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
274 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
275 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
276 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
277 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000278
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000279 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
280 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
282 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
283 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000284
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000288 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000289 // We have an algorithm for SSE2->double, and we turn this into a
290 // 64-bit FILD followed by conditional FADD for other targets.
291 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000292 // We have an algorithm for SSE2, and we turn this into a 64-bit
293 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000294 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000295 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000296
297 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
298 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
300 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000301
Devang Patel6a784892009-06-05 18:48:29 +0000302 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000303 // SSE has no i16 to fp conversion, only i32
304 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000306 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000308 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
310 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000311 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000312 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
314 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000315 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000316
Dale Johannesen73328d12007-09-19 23:55:34 +0000317 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
318 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
320 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000321
Evan Cheng02568ff2006-01-30 22:13:22 +0000322 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
323 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
325 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000326
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000327 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000329 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000331 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
333 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000334 }
335
336 // Handle FP_TO_UINT by promoting the destination to a larger signed
337 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
339 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
340 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000341
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000345 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000346 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 // Expand FP_TO_UINT into a select.
348 // FIXME: We would like to use a Custom expander here eventually to do
349 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000351 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000352 // With SSE3 we can use fisttpll to convert to a signed i64; without
353 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000355 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000356
Chris Lattner399610a2006-12-05 18:22:22 +0000357 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000358 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000359 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
360 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000361 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000362 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000363 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000365 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000366 }
Chris Lattner21f66852005-12-23 05:15:23 +0000367
Dan Gohmanb00ee212008-02-18 19:34:53 +0000368 // Scalar integer divide and remainder are lowered to use operations that
369 // produce two results, to match the available instructions. This exposes
370 // the two-result form to trivial CSE, which is able to combine x/y and x%y
371 // into a single instruction.
372 //
373 // Scalar integer multiply-high is also lowered to use two-result
374 // operations, to match the available instructions. However, plain multiply
375 // (low) operations are left as Legal, as there are single-result
376 // instructions for this in x86. Using the two-result multiply instructions
377 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000378 for (unsigned i = 0, e = 4; i != e; ++i) {
379 MVT VT = IntVTs[i];
380 setOperationAction(ISD::MULHS, VT, Expand);
381 setOperationAction(ISD::MULHU, VT, Expand);
382 setOperationAction(ISD::SDIV, VT, Expand);
383 setOperationAction(ISD::UDIV, VT, Expand);
384 setOperationAction(ISD::SREM, VT, Expand);
385 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000386
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000387 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000388 setOperationAction(ISD::ADDC, VT, Custom);
389 setOperationAction(ISD::ADDE, VT, Custom);
390 setOperationAction(ISD::SUBC, VT, Custom);
391 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000392 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000393
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
395 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
396 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
397 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000398 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
400 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
401 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
402 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
403 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
404 setOperationAction(ISD::FREM , MVT::f32 , Expand);
405 setOperationAction(ISD::FREM , MVT::f64 , Expand);
406 setOperationAction(ISD::FREM , MVT::f80 , Expand);
407 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000408
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
410 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000411 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
412 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
414 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000415 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000418 }
419
Benjamin Kramer1292c222010-12-04 20:32:23 +0000420 if (Subtarget->hasPOPCNT()) {
421 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
422 } else {
423 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
424 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
425 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
426 if (Subtarget->is64Bit())
427 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
428 }
429
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
431 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000432
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000434 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000435 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000436 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000437 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
439 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
440 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
441 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
442 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000443 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
445 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
446 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
447 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000448 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000450 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000451 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000453
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000454 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
456 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
457 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
458 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000459 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
461 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000462 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000463 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
465 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
466 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
467 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000468 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000469 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000470 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
472 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
473 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000474 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
476 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
477 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000478 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000479
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000480 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000482
Eric Christopher9a9d2752010-07-22 02:48:34 +0000483 // We may not have a libcall for MEMBARRIER so we should lower this.
484 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000485
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000486 // On X86 and X86-64, atomic operations are lowered to locked instructions.
487 // Locked instructions, in turn, have implicit fence semantics (all memory
488 // operations are flushed before issuing the locked instruction, and they
489 // are not buffered), so we can fold away the common pattern of
490 // fence-atomic-fence.
491 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000492
Mon P Wang63307c32008-05-05 19:05:59 +0000493 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000494 for (unsigned i = 0, e = 4; i != e; ++i) {
495 MVT VT = IntVTs[i];
496 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
497 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
498 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000499
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000500 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
503 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
505 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
506 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000508 }
509
Evan Cheng3c992d22006-03-07 02:02:57 +0000510 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000511 if (!Subtarget->isTargetDarwin() &&
512 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000513 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000515 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000516
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
518 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
519 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
520 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000521 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000522 setExceptionPointerRegister(X86::RAX);
523 setExceptionSelectorRegister(X86::RDX);
524 } else {
525 setExceptionPointerRegister(X86::EAX);
526 setExceptionSelectorRegister(X86::EDX);
527 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
529 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000530
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000532
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000534
Nate Begemanacc398c2006-01-25 18:21:52 +0000535 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::VASTART , MVT::Other, Custom);
537 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000538 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::VAARG , MVT::Other, Custom);
540 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000541 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::VAARG , MVT::Other, Expand);
543 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000544 }
Evan Chengae642192007-03-02 23:16:35 +0000545
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
547 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
NAKAMURA Takumia2e07622011-03-24 07:07:00 +0000548 setOperationAction(ISD::DYNAMIC_STACKALLOC,
549 (Subtarget->is64Bit() ? MVT::i64 : MVT::i32),
550 (Subtarget->isTargetCOFF()
551 && !Subtarget->isTargetEnvMacho()
552 ? Custom : Expand));
Chris Lattnerb99329e2006-01-13 02:42:53 +0000553
Evan Chengc7ce29b2009-02-13 22:36:38 +0000554 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000555 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000556 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
558 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000559
Evan Cheng223547a2006-01-31 22:28:30 +0000560 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::FABS , MVT::f64, Custom);
562 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000563
564 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::FNEG , MVT::f64, Custom);
566 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000567
Evan Cheng68c47cb2007-01-05 07:55:56 +0000568 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
570 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000571
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000572 // Lower this to FGETSIGNx86 plus an AND.
573 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
574 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
575
Evan Chengd25e9e82006-02-02 00:28:23 +0000576 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FSIN , MVT::f64, Expand);
578 setOperationAction(ISD::FCOS , MVT::f64, Expand);
579 setOperationAction(ISD::FSIN , MVT::f32, Expand);
580 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000581
Chris Lattnera54aa942006-01-29 06:26:08 +0000582 // Expand FP immediates into loads from the stack, except for the special
583 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584 addLegalFPImmediate(APFloat(+0.0)); // xorpd
585 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000586 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587 // Use SSE for f32, x87 for f64.
588 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
590 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
592 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000594
595 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000597
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599
600 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
602 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603
604 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000605 setOperationAction(ISD::FSIN , MVT::f32, Expand);
606 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000607
Nate Begemane1795842008-02-14 08:57:00 +0000608 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000609 addLegalFPImmediate(APFloat(+0.0f)); // xorps
610 addLegalFPImmediate(APFloat(+0.0)); // FLD0
611 addLegalFPImmediate(APFloat(+1.0)); // FLD1
612 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
613 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
614
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
617 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000619 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000621 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
623 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000624
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
626 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
627 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
628 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000629
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000630 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
632 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000633 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000634 addLegalFPImmediate(APFloat(+0.0)); // FLD0
635 addLegalFPImmediate(APFloat(+1.0)); // FLD1
636 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
637 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
639 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
640 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
641 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000642 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000643
Cameron Zwarich33390842011-07-08 21:39:21 +0000644 // We don't support FMA.
645 setOperationAction(ISD::FMA, MVT::f64, Expand);
646 setOperationAction(ISD::FMA, MVT::f32, Expand);
647
Dale Johannesen59a58732007-08-05 18:49:15 +0000648 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000649 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
651 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
652 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000654 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000655 addLegalFPImmediate(TmpFlt); // FLD0
656 TmpFlt.changeSign();
657 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000658
659 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000660 APFloat TmpFlt2(+1.0);
661 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
662 &ignored);
663 addLegalFPImmediate(TmpFlt2); // FLD1
664 TmpFlt2.changeSign();
665 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
666 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000667
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
670 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000671 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000672
673 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000674 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000675
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000676 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
678 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
679 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::FLOG, MVT::f80, Expand);
682 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
683 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
684 setOperationAction(ISD::FEXP, MVT::f80, Expand);
685 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000686
Mon P Wangf007a8b2008-11-06 05:31:54 +0000687 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000688 // (for widening) or expand (for scalarization). Then we will selectively
689 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
691 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
692 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000708 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
709 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000741 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000742 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
746 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
747 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
748 setTruncStoreAction((MVT::SimpleValueType)VT,
749 (MVT::SimpleValueType)InnerVT, Expand);
750 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
751 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
752 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000753 }
754
Evan Chengc7ce29b2009-02-13 22:36:38 +0000755 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
756 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000757 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000758 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000759 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000760 }
761
Dale Johannesen0488fb62010-09-30 23:57:10 +0000762 // MMX-sized vectors (other than x86mmx) are expected to be expanded
763 // into smaller operations.
764 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
765 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
766 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
767 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
768 setOperationAction(ISD::AND, MVT::v8i8, Expand);
769 setOperationAction(ISD::AND, MVT::v4i16, Expand);
770 setOperationAction(ISD::AND, MVT::v2i32, Expand);
771 setOperationAction(ISD::AND, MVT::v1i64, Expand);
772 setOperationAction(ISD::OR, MVT::v8i8, Expand);
773 setOperationAction(ISD::OR, MVT::v4i16, Expand);
774 setOperationAction(ISD::OR, MVT::v2i32, Expand);
775 setOperationAction(ISD::OR, MVT::v1i64, Expand);
776 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
777 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
778 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
779 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
780 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
781 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
782 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
783 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
784 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
785 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
786 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
787 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
788 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000789 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
790 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
791 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
792 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000793
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000794 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000796
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
798 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
799 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
800 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
801 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
802 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
803 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
804 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
805 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
806 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
807 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
808 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000809 }
810
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000811 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000812 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000813
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000814 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
815 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
817 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
818 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
819 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000820
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
822 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
823 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
824 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
826 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
827 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
828 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
829 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
830 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
831 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
832 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
833 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
834 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
835 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
836 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
839 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
840 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
841 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000842
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
844 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
847 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000848
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000849 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
850 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
851 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
852 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
853 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
854
Evan Cheng2c3ae372006-04-12 21:21:57 +0000855 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
857 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000858 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000859 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000860 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000861 // Do not attempt to custom lower non-128-bit vectors
862 if (!VT.is128BitVector())
863 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 setOperationAction(ISD::BUILD_VECTOR,
865 VT.getSimpleVT().SimpleTy, Custom);
866 setOperationAction(ISD::VECTOR_SHUFFLE,
867 VT.getSimpleVT().SimpleTy, Custom);
868 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
869 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000870 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000871
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
873 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
875 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
876 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
877 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000878
Nate Begemancdd1eec2008-02-12 22:51:28 +0000879 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
881 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000882 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000883
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000884 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
886 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000887 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000888
889 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000890 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000891 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000892
Owen Andersond6662ad2009-08-10 20:46:15 +0000893 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000895 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000897 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000898 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000899 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000901 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000903 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000904
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000906
Evan Cheng2c3ae372006-04-12 21:21:57 +0000907 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
909 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
910 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
911 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
914 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000915 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000916
Nate Begeman14d12ca2008-02-11 04:19:36 +0000917 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000918 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
921 setOperationAction(ISD::FRINT, MVT::f32, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
923 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
924 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
925 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
926 setOperationAction(ISD::FRINT, MVT::f64, Legal);
927 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
928
Nate Begeman14d12ca2008-02-11 04:19:36 +0000929 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000931
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000932 // Can turn SHL into an integer multiply.
933 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000934 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000935
Nate Begeman14d12ca2008-02-11 04:19:36 +0000936 // i8 and i16 vectors are custom , because the source register and source
937 // source memory operand types are not the same width. f32 vectors are
938 // custom since the immediate controlling the insert encodes additional
939 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
948 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000949
950 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
952 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000953 }
954 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000955
Nadav Rotem43012222011-05-11 08:12:09 +0000956 if (Subtarget->hasSSE2()) {
957 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
958 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
959 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
960
961 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
962 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
963 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
964
965 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
966 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
967 }
968
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000969 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000970 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000971
David Greene9b9838d2009-06-29 16:47:10 +0000972 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000973 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
974 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
975 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
976 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
977 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
978 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000979
Owen Anderson825b72b2009-08-11 20:47:22 +0000980 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
982 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000983
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
985 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
986 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
987 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
988 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
989 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000990
Owen Anderson825b72b2009-08-11 20:47:22 +0000991 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
992 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
993 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
994 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
995 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
996 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000997
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +0000998 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +0000999 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001000 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1001 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1002 EVT VT = SVT;
1003
1004 // Extract subvector is special because the value type
1005 // (result) is 128-bit but the source is 256-bit wide.
1006 if (VT.is128BitVector())
1007 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1008
1009 // Do not attempt to custom lower other non-256-bit vectors
1010 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001011 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001012
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001013 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1014 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1015 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1016 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
1017 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
1018 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001019 }
1020
David Greene54d8eba2011-01-27 22:38:56 +00001021 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001022 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1023 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1024 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001025
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001026 // Do not attempt to promote non-256-bit vectors
1027 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001028 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001029
1030 setOperationAction(ISD::AND, SVT, Promote);
1031 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1032 setOperationAction(ISD::OR, SVT, Promote);
1033 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1034 setOperationAction(ISD::XOR, SVT, Promote);
1035 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1036 setOperationAction(ISD::LOAD, SVT, Promote);
1037 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1038 setOperationAction(ISD::SELECT, SVT, Promote);
1039 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001040 }
David Greene9b9838d2009-06-29 16:47:10 +00001041 }
1042
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001043 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1044 // of this type with custom code.
1045 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1046 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1047 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1048 }
1049
Evan Cheng6be2c582006-04-05 23:38:46 +00001050 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001051 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001052
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001053
Eli Friedman962f5492010-06-02 19:35:46 +00001054 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1055 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001056 //
Eli Friedman962f5492010-06-02 19:35:46 +00001057 // FIXME: We really should do custom legalization for addition and
1058 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1059 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001060 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1061 // Add/Sub/Mul with overflow operations are custom lowered.
1062 MVT VT = IntVTs[i];
1063 setOperationAction(ISD::SADDO, VT, Custom);
1064 setOperationAction(ISD::UADDO, VT, Custom);
1065 setOperationAction(ISD::SSUBO, VT, Custom);
1066 setOperationAction(ISD::USUBO, VT, Custom);
1067 setOperationAction(ISD::SMULO, VT, Custom);
1068 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001069 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001070
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001071 // There are no 8-bit 3-address imul/mul instructions
1072 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1073 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001074
Evan Chengd54f2d52009-03-31 19:38:51 +00001075 if (!Subtarget->is64Bit()) {
1076 // These libcalls are not available in 32-bit.
1077 setLibcallName(RTLIB::SHL_I128, 0);
1078 setLibcallName(RTLIB::SRL_I128, 0);
1079 setLibcallName(RTLIB::SRA_I128, 0);
1080 }
1081
Evan Cheng206ee9d2006-07-07 08:33:52 +00001082 // We have target-specific dag combine patterns for the following nodes:
1083 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001084 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001085 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001086 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001087 setTargetDAGCombine(ISD::SHL);
1088 setTargetDAGCombine(ISD::SRA);
1089 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001090 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001091 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001092 setTargetDAGCombine(ISD::ADD);
1093 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001094 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001095 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001096 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001097 if (Subtarget->is64Bit())
1098 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001099
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001100 computeRegisterProperties();
1101
Evan Cheng05219282011-01-06 06:52:41 +00001102 // On Darwin, -Os means optimize for size without hurting performance,
1103 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001104 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001105 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001106 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001107 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1108 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1109 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001110 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001111 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001112
1113 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001114}
1115
Scott Michel5b8f82e2008-03-10 15:42:14 +00001116
Owen Anderson825b72b2009-08-11 20:47:22 +00001117MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1118 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001119}
1120
1121
Evan Cheng29286502008-01-23 23:17:41 +00001122/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1123/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001124static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001125 if (MaxAlign == 16)
1126 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001127 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001128 if (VTy->getBitWidth() == 128)
1129 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001130 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001131 unsigned EltAlign = 0;
1132 getMaxByValAlign(ATy->getElementType(), EltAlign);
1133 if (EltAlign > MaxAlign)
1134 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001135 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001136 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1137 unsigned EltAlign = 0;
1138 getMaxByValAlign(STy->getElementType(i), EltAlign);
1139 if (EltAlign > MaxAlign)
1140 MaxAlign = EltAlign;
1141 if (MaxAlign == 16)
1142 break;
1143 }
1144 }
1145 return;
1146}
1147
1148/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1149/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001150/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1151/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001152unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001153 if (Subtarget->is64Bit()) {
1154 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001155 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001156 if (TyAlign > 8)
1157 return TyAlign;
1158 return 8;
1159 }
1160
Evan Cheng29286502008-01-23 23:17:41 +00001161 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001162 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001163 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001164 return Align;
1165}
Chris Lattner2b02a442007-02-25 08:29:00 +00001166
Evan Chengf0df0312008-05-15 08:39:06 +00001167/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001168/// and store operations as a result of memset, memcpy, and memmove
1169/// lowering. If DstAlign is zero that means it's safe to destination
1170/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1171/// means there isn't a need to check it against alignment requirement,
1172/// probably because the source does not need to be loaded. If
1173/// 'NonScalarIntSafe' is true, that means it's safe to return a
1174/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1175/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1176/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001177/// It returns EVT::Other if the type should be determined using generic
1178/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001179EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001180X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1181 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001182 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001183 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001184 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001185 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1186 // linux. This is because the stack realignment code can't handle certain
1187 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001188 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001189 if (NonScalarIntSafe &&
1190 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001191 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001192 (Subtarget->isUnalignedMemAccessFast() ||
1193 ((DstAlign == 0 || DstAlign >= 16) &&
1194 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001195 Subtarget->getStackAlignment() >= 16) {
1196 if (Subtarget->hasSSE2())
1197 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001198 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001199 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001200 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001201 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001202 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001203 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001204 // Do not use f64 to lower memcpy if source is string constant. It's
1205 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001206 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001207 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001208 }
Evan Chengf0df0312008-05-15 08:39:06 +00001209 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001210 return MVT::i64;
1211 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001212}
1213
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001214/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1215/// current function. The returned value is a member of the
1216/// MachineJumpTableInfo::JTEntryKind enum.
1217unsigned X86TargetLowering::getJumpTableEncoding() const {
1218 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1219 // symbol.
1220 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1221 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001222 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001223
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001224 // Otherwise, use the normal jump table encoding heuristics.
1225 return TargetLowering::getJumpTableEncoding();
1226}
1227
Chris Lattnerc64daab2010-01-26 05:02:42 +00001228const MCExpr *
1229X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1230 const MachineBasicBlock *MBB,
1231 unsigned uid,MCContext &Ctx) const{
1232 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1233 Subtarget->isPICStyleGOT());
1234 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1235 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001236 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1237 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001238}
1239
Evan Chengcc415862007-11-09 01:32:10 +00001240/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1241/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001242SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001243 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001244 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001245 // This doesn't have DebugLoc associated with it, but is not really the
1246 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001247 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001248 return Table;
1249}
1250
Chris Lattner589c6f62010-01-26 06:28:43 +00001251/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1252/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1253/// MCExpr.
1254const MCExpr *X86TargetLowering::
1255getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1256 MCContext &Ctx) const {
1257 // X86-64 uses RIP relative addressing based on the jump table label.
1258 if (Subtarget->isPICStyleRIPRel())
1259 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1260
1261 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001262 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001263}
1264
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001265// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001266std::pair<const TargetRegisterClass*, uint8_t>
1267X86TargetLowering::findRepresentativeClass(EVT VT) const{
1268 const TargetRegisterClass *RRC = 0;
1269 uint8_t Cost = 1;
1270 switch (VT.getSimpleVT().SimpleTy) {
1271 default:
1272 return TargetLowering::findRepresentativeClass(VT);
1273 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1274 RRC = (Subtarget->is64Bit()
1275 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1276 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001277 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001278 RRC = X86::VR64RegisterClass;
1279 break;
1280 case MVT::f32: case MVT::f64:
1281 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1282 case MVT::v4f32: case MVT::v2f64:
1283 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1284 case MVT::v4f64:
1285 RRC = X86::VR128RegisterClass;
1286 break;
1287 }
1288 return std::make_pair(RRC, Cost);
1289}
1290
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001291bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1292 unsigned &Offset) const {
1293 if (!Subtarget->isTargetLinux())
1294 return false;
1295
1296 if (Subtarget->is64Bit()) {
1297 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1298 Offset = 0x28;
1299 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1300 AddressSpace = 256;
1301 else
1302 AddressSpace = 257;
1303 } else {
1304 // %gs:0x14 on i386
1305 Offset = 0x14;
1306 AddressSpace = 256;
1307 }
1308 return true;
1309}
1310
1311
Chris Lattner2b02a442007-02-25 08:29:00 +00001312//===----------------------------------------------------------------------===//
1313// Return Value Calling Convention Implementation
1314//===----------------------------------------------------------------------===//
1315
Chris Lattner59ed56b2007-02-28 04:55:35 +00001316#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001317
Michael J. Spencerec38de22010-10-10 22:04:20 +00001318bool
Eric Christopher471e4222011-06-08 23:55:35 +00001319X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1320 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001321 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001322 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001323 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001324 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001325 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001326 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001327}
1328
Dan Gohman98ca4f22009-08-05 01:29:28 +00001329SDValue
1330X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001331 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001332 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001333 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001334 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001335 MachineFunction &MF = DAG.getMachineFunction();
1336 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001337
Chris Lattner9774c912007-02-27 05:28:59 +00001338 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001339 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001340 RVLocs, *DAG.getContext());
1341 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001342
Evan Chengdcea1632010-02-04 02:40:39 +00001343 // Add the regs to the liveout set for the function.
1344 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1345 for (unsigned i = 0; i != RVLocs.size(); ++i)
1346 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1347 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001348
Dan Gohman475871a2008-07-27 21:46:04 +00001349 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001350
Dan Gohman475871a2008-07-27 21:46:04 +00001351 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001352 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1353 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001354 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1355 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001356
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001357 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001358 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1359 CCValAssign &VA = RVLocs[i];
1360 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001361 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001362 EVT ValVT = ValToCopy.getValueType();
1363
Dale Johannesenc4510512010-09-24 19:05:48 +00001364 // If this is x86-64, and we disabled SSE, we can't return FP values,
1365 // or SSE or MMX vectors.
1366 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1367 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001368 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001369 report_fatal_error("SSE register return with SSE disabled");
1370 }
1371 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1372 // llvm-gcc has never done it right and no one has noticed, so this
1373 // should be OK for now.
1374 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001375 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001376 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001377
Chris Lattner447ff682008-03-11 03:23:40 +00001378 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1379 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001380 if (VA.getLocReg() == X86::ST0 ||
1381 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001382 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1383 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001384 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001385 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001386 RetOps.push_back(ValToCopy);
1387 // Don't emit a copytoreg.
1388 continue;
1389 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001390
Evan Cheng242b38b2009-02-23 09:03:22 +00001391 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1392 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001393 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001394 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001395 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001396 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001397 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1398 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001399 // If we don't have SSE2 available, convert to v4f32 so the generated
1400 // register is legal.
1401 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001402 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001403 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001404 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001405 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001406
Dale Johannesendd64c412009-02-04 00:33:20 +00001407 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001408 Flag = Chain.getValue(1);
1409 }
Dan Gohman61a92132008-04-21 23:59:07 +00001410
1411 // The x86-64 ABI for returning structs by value requires that we copy
1412 // the sret argument into %rax for the return. We saved the argument into
1413 // a virtual register in the entry block, so now we copy the value out
1414 // and into %rax.
1415 if (Subtarget->is64Bit() &&
1416 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1417 MachineFunction &MF = DAG.getMachineFunction();
1418 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1419 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001420 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001421 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001422 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001423
Dale Johannesendd64c412009-02-04 00:33:20 +00001424 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001425 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001426
1427 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001428 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001429 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001430
Chris Lattner447ff682008-03-11 03:23:40 +00001431 RetOps[0] = Chain; // Update chain.
1432
1433 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001434 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001435 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001436
1437 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001438 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001439}
1440
Evan Cheng3d2125c2010-11-30 23:55:39 +00001441bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1442 if (N->getNumValues() != 1)
1443 return false;
1444 if (!N->hasNUsesOfValue(1, 0))
1445 return false;
1446
1447 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001448 if (Copy->getOpcode() != ISD::CopyToReg &&
1449 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001450 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001451
1452 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001453 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001454 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001455 if (UI->getOpcode() != X86ISD::RET_FLAG)
1456 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001457 HasRet = true;
1458 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001459
Evan Cheng1bf891a2010-12-01 22:59:46 +00001460 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001461}
1462
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001463EVT
1464X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001465 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001466 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001467 // TODO: Is this also valid on 32-bit?
1468 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001469 ReturnMVT = MVT::i8;
1470 else
1471 ReturnMVT = MVT::i32;
1472
1473 EVT MinVT = getRegisterType(Context, ReturnMVT);
1474 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001475}
1476
Dan Gohman98ca4f22009-08-05 01:29:28 +00001477/// LowerCallResult - Lower the result values of a call into the
1478/// appropriate copies out of appropriate physical registers.
1479///
1480SDValue
1481X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001482 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 const SmallVectorImpl<ISD::InputArg> &Ins,
1484 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001485 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001486
Chris Lattnere32bbf62007-02-28 07:09:55 +00001487 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001488 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001489 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001490 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1491 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001492 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001493
Chris Lattner3085e152007-02-25 08:59:22 +00001494 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001495 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001496 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001497 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001498
Torok Edwin3f142c32009-02-01 18:15:56 +00001499 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001500 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001501 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001502 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001503 }
1504
Evan Cheng79fb3b42009-02-20 20:43:02 +00001505 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001506
1507 // If this is a call to a function that returns an fp value on the floating
1508 // point stack, we must guarantee the the value is popped from the stack, so
1509 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001510 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001511 // instead.
1512 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1513 // If we prefer to use the value in xmm registers, copy it out as f80 and
1514 // use a truncate to move it from fp stack reg to xmm reg.
1515 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001516 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001517 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1518 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001519 Val = Chain.getValue(0);
1520
1521 // Round the f80 to the right size, which also moves it to the appropriate
1522 // xmm register.
1523 if (CopyVT != VA.getValVT())
1524 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1525 // This truncation won't change the value.
1526 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001527 } else {
1528 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1529 CopyVT, InFlag).getValue(1);
1530 Val = Chain.getValue(0);
1531 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001532 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001533 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001534 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001535
Dan Gohman98ca4f22009-08-05 01:29:28 +00001536 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001537}
1538
1539
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001540//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001541// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001542//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001543// StdCall calling convention seems to be standard for many Windows' API
1544// routines and around. It differs from C calling convention just a little:
1545// callee should clean up the stack, not caller. Symbols should be also
1546// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001547// For info on fast calling convention see Fast Calling Convention (tail call)
1548// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001549
Dan Gohman98ca4f22009-08-05 01:29:28 +00001550/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001551/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001552static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1553 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001554 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001555
Dan Gohman98ca4f22009-08-05 01:29:28 +00001556 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001557}
1558
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001559/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001560/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561static bool
1562ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1563 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001564 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001565
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001567}
1568
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001569/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1570/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001571/// the specific parameter attribute. The copy will be passed as a byval
1572/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001573static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001574CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001575 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1576 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001577 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001578
Dale Johannesendd64c412009-02-04 00:33:20 +00001579 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001580 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001581 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001582}
1583
Chris Lattner29689432010-03-11 00:22:57 +00001584/// IsTailCallConvention - Return true if the calling convention is one that
1585/// supports tail call optimization.
1586static bool IsTailCallConvention(CallingConv::ID CC) {
1587 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1588}
1589
Evan Cheng485fafc2011-03-21 01:19:09 +00001590bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1591 if (!CI->isTailCall())
1592 return false;
1593
1594 CallSite CS(CI);
1595 CallingConv::ID CalleeCC = CS.getCallingConv();
1596 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1597 return false;
1598
1599 return true;
1600}
1601
Evan Cheng0c439eb2010-01-27 00:07:07 +00001602/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1603/// a tailcall target by changing its ABI.
1604static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001605 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001606}
1607
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608SDValue
1609X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001610 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611 const SmallVectorImpl<ISD::InputArg> &Ins,
1612 DebugLoc dl, SelectionDAG &DAG,
1613 const CCValAssign &VA,
1614 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001615 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001616 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001617 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001618 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001619 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001620 EVT ValVT;
1621
1622 // If value is passed by pointer we have address passed instead of the value
1623 // itself.
1624 if (VA.getLocInfo() == CCValAssign::Indirect)
1625 ValVT = VA.getLocVT();
1626 else
1627 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001628
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001629 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001630 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001631 // In case of tail call optimization mark all arguments mutable. Since they
1632 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001633 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001634 unsigned Bytes = Flags.getByValSize();
1635 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1636 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001637 return DAG.getFrameIndex(FI, getPointerTy());
1638 } else {
1639 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001640 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001641 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1642 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001643 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001644 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001645 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001646}
1647
Dan Gohman475871a2008-07-27 21:46:04 +00001648SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001650 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001651 bool isVarArg,
1652 const SmallVectorImpl<ISD::InputArg> &Ins,
1653 DebugLoc dl,
1654 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001655 SmallVectorImpl<SDValue> &InVals)
1656 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001657 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001658 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001659
Gordon Henriksen86737662008-01-05 16:56:59 +00001660 const Function* Fn = MF.getFunction();
1661 if (Fn->hasExternalLinkage() &&
1662 Subtarget->isTargetCygMing() &&
1663 Fn->getName() == "main")
1664 FuncInfo->setForceFramePointer(true);
1665
Evan Cheng1bc78042006-04-26 01:20:17 +00001666 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001668 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001669
Chris Lattner29689432010-03-11 00:22:57 +00001670 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1671 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001672
Chris Lattner638402b2007-02-28 07:00:42 +00001673 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001674 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001675 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001676 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001677
1678 // Allocate shadow area for Win64
1679 if (IsWin64) {
1680 CCInfo.AllocateStack(32, 8);
1681 }
1682
Duncan Sands45907662010-10-31 13:21:44 +00001683 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001684
Chris Lattnerf39f7712007-02-28 05:46:49 +00001685 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001686 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001687 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1688 CCValAssign &VA = ArgLocs[i];
1689 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1690 // places.
1691 assert(VA.getValNo() != LastVal &&
1692 "Don't support value assigned to multiple locs yet");
1693 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001694
Chris Lattnerf39f7712007-02-28 05:46:49 +00001695 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001696 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001697 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001699 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001700 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001701 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001702 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001703 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001704 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001705 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001706 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1707 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001708 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001709 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001710 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001711 RC = X86::VR64RegisterClass;
1712 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001713 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001714
Devang Patel68e6bee2011-02-21 23:21:26 +00001715 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001717
Chris Lattnerf39f7712007-02-28 05:46:49 +00001718 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1719 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1720 // right size.
1721 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001722 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001723 DAG.getValueType(VA.getValVT()));
1724 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001725 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001726 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001727 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001728 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001729
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001730 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001731 // Handle MMX values passed in XMM regs.
1732 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001733 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1734 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001735 } else
1736 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001737 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001738 } else {
1739 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001741 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001742
1743 // If value is passed via pointer - do a load.
1744 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001745 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1746 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001747
Dan Gohman98ca4f22009-08-05 01:29:28 +00001748 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001749 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001750
Dan Gohman61a92132008-04-21 23:59:07 +00001751 // The x86-64 ABI for returning structs by value requires that we copy
1752 // the sret argument into %rax for the return. Save the argument into
1753 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001754 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001755 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1756 unsigned Reg = FuncInfo->getSRetReturnReg();
1757 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001759 FuncInfo->setSRetReturnReg(Reg);
1760 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001763 }
1764
Chris Lattnerf39f7712007-02-28 05:46:49 +00001765 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001766 // Align stack specially for tail calls.
1767 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001768 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001769
Evan Cheng1bc78042006-04-26 01:20:17 +00001770 // If the function takes variable number of arguments, make a frame index for
1771 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001772 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001773 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1774 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001775 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001776 }
1777 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001778 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1779
1780 // FIXME: We should really autogenerate these arrays
1781 static const unsigned GPR64ArgRegsWin64[] = {
1782 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001784 static const unsigned GPR64ArgRegs64Bit[] = {
1785 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1786 };
1787 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001788 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1789 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1790 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001791 const unsigned *GPR64ArgRegs;
1792 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001793
1794 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001795 // The XMM registers which might contain var arg parameters are shadowed
1796 // in their paired GPR. So we only need to save the GPR to their home
1797 // slots.
1798 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001799 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001800 } else {
1801 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1802 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001803
1804 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001805 }
1806 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1807 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001808
Devang Patel578efa92009-06-05 21:57:13 +00001809 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001810 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001811 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001812 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001813 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001814 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001815 // Kernel mode asks for SSE to be disabled, so don't push them
1816 // on the stack.
1817 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001818
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001819 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001820 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001821 // Get to the caller-allocated home save location. Add 8 to account
1822 // for the return address.
1823 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001824 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001825 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001826 // Fixup to set vararg frame on shadow area (4 x i64).
1827 if (NumIntRegs < 4)
1828 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001829 } else {
1830 // For X86-64, if there are vararg parameters that are passed via
1831 // registers, then we must store them to their spots on the stack so they
1832 // may be loaded by deferencing the result of va_next.
1833 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1834 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1835 FuncInfo->setRegSaveFrameIndex(
1836 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001837 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001838 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001839
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001841 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001842 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1843 getPointerTy());
1844 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001845 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001846 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1847 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001848 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001849 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001851 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001852 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001853 MachinePointerInfo::getFixedStack(
1854 FuncInfo->getRegSaveFrameIndex(), Offset),
1855 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001856 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001857 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001858 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001859
Dan Gohmanface41a2009-08-16 21:24:25 +00001860 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1861 // Now store the XMM (fp + vector) parameter registers.
1862 SmallVector<SDValue, 11> SaveXMMOps;
1863 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001864
Devang Patel68e6bee2011-02-21 23:21:26 +00001865 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001866 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1867 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001868
Dan Gohman1e93df62010-04-17 14:41:14 +00001869 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1870 FuncInfo->getRegSaveFrameIndex()));
1871 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1872 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001873
Dan Gohmanface41a2009-08-16 21:24:25 +00001874 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001875 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001876 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001877 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1878 SaveXMMOps.push_back(Val);
1879 }
1880 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1881 MVT::Other,
1882 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001883 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001884
1885 if (!MemOps.empty())
1886 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1887 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001889 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001890
Gordon Henriksen86737662008-01-05 16:56:59 +00001891 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001892 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001893 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001894 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001895 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001896 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001897 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001898 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001899 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001900
Gordon Henriksen86737662008-01-05 16:56:59 +00001901 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001902 // RegSaveFrameIndex is X86-64 only.
1903 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001904 if (CallConv == CallingConv::X86_FastCall ||
1905 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001906 // fastcc functions can't have varargs.
1907 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001908 }
Evan Cheng25caf632006-05-23 21:06:34 +00001909
Dan Gohman98ca4f22009-08-05 01:29:28 +00001910 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001911}
1912
Dan Gohman475871a2008-07-27 21:46:04 +00001913SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1915 SDValue StackPtr, SDValue Arg,
1916 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001917 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001918 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001919 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001920 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001921 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001922 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001923 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001924
1925 return DAG.getStore(Chain, dl, Arg, PtrOff,
1926 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001927 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001928}
1929
Bill Wendling64e87322009-01-16 19:25:27 +00001930/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001931/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001932SDValue
1933X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001934 SDValue &OutRetAddr, SDValue Chain,
1935 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001936 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001937 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001938 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001939 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001940
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001941 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001942 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1943 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001944 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001945}
1946
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001947/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001948/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001949static SDValue
1950EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001951 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001952 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001953 // Store the return address to the appropriate stack slot.
1954 if (!FPDiff) return Chain;
1955 // Calculate the new stack slot for the return address.
1956 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001957 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001958 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001961 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001962 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001963 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001964 return Chain;
1965}
1966
Dan Gohman98ca4f22009-08-05 01:29:28 +00001967SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001968X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001969 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001970 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001971 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001972 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001973 const SmallVectorImpl<ISD::InputArg> &Ins,
1974 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001975 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976 MachineFunction &MF = DAG.getMachineFunction();
1977 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001978 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001979 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001980 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001981
Evan Cheng5f941932010-02-05 02:21:12 +00001982 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001983 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001984 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1985 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001986 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001987
1988 // Sibcalls are automatically detected tailcalls which do not require
1989 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001990 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001991 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001992
1993 if (isTailCall)
1994 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001995 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001996
Chris Lattner29689432010-03-11 00:22:57 +00001997 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1998 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001999
Chris Lattner638402b2007-02-28 07:00:42 +00002000 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002001 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002002 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002003 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002004
2005 // Allocate shadow area for Win64
2006 if (IsWin64) {
2007 CCInfo.AllocateStack(32, 8);
2008 }
2009
Duncan Sands45907662010-10-31 13:21:44 +00002010 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002011
Chris Lattner423c5f42007-02-28 05:31:48 +00002012 // Get a count of how many bytes are to be pushed on the stack.
2013 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002014 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002015 // This is a sibcall. The memory operands are available in caller's
2016 // own caller's stack.
2017 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002018 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002019 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002020
Gordon Henriksen86737662008-01-05 16:56:59 +00002021 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002022 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002023 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002024 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002025 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2026 FPDiff = NumBytesCallerPushed - NumBytes;
2027
2028 // Set the delta of movement of the returnaddr stackslot.
2029 // But only set if delta is greater than previous delta.
2030 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2031 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2032 }
2033
Evan Chengf22f9b32010-02-06 03:28:46 +00002034 if (!IsSibcall)
2035 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002036
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002038 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002039 if (isTailCall && FPDiff)
2040 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2041 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002042
Dan Gohman475871a2008-07-27 21:46:04 +00002043 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2044 SmallVector<SDValue, 8> MemOpChains;
2045 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002046
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002047 // Walk the register/memloc assignments, inserting copies/loads. In the case
2048 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002049 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2050 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002051 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002052 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002053 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002054 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002055
Chris Lattner423c5f42007-02-28 05:31:48 +00002056 // Promote the value if needed.
2057 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002058 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002059 case CCValAssign::Full: break;
2060 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002061 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002062 break;
2063 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002064 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002065 break;
2066 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002067 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2068 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002069 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2071 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002072 } else
2073 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2074 break;
2075 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002076 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002077 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002078 case CCValAssign::Indirect: {
2079 // Store the argument.
2080 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002081 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002082 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002083 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002084 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002085 Arg = SpillSlot;
2086 break;
2087 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002088 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002089
Chris Lattner423c5f42007-02-28 05:31:48 +00002090 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002091 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2092 if (isVarArg && IsWin64) {
2093 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2094 // shadow reg if callee is a varargs function.
2095 unsigned ShadowReg = 0;
2096 switch (VA.getLocReg()) {
2097 case X86::XMM0: ShadowReg = X86::RCX; break;
2098 case X86::XMM1: ShadowReg = X86::RDX; break;
2099 case X86::XMM2: ShadowReg = X86::R8; break;
2100 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002101 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002102 if (ShadowReg)
2103 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002104 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002105 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002106 assert(VA.isMemLoc());
2107 if (StackPtr.getNode() == 0)
2108 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2109 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2110 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002111 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002112 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002113
Evan Cheng32fe1032006-05-25 00:59:30 +00002114 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002116 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002117
Evan Cheng347d5f72006-04-28 21:29:37 +00002118 // Build a sequence of copy-to-reg nodes chained together with token chain
2119 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002120 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002121 // Tail call byval lowering might overwrite argument registers so in case of
2122 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002123 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002124 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002125 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002126 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002127 InFlag = Chain.getValue(1);
2128 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002129
Chris Lattner88e1fd52009-07-09 04:24:46 +00002130 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002131 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2132 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002133 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002134 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2135 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002136 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002137 InFlag);
2138 InFlag = Chain.getValue(1);
2139 } else {
2140 // If we are tail calling and generating PIC/GOT style code load the
2141 // address of the callee into ECX. The value in ecx is used as target of
2142 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2143 // for tail calls on PIC/GOT architectures. Normally we would just put the
2144 // address of GOT into ebx and then call target@PLT. But for tail calls
2145 // ebx would be restored (since ebx is callee saved) before jumping to the
2146 // target@PLT.
2147
2148 // Note: The actual moving to ECX is done further down.
2149 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2150 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2151 !G->getGlobal()->hasProtectedVisibility())
2152 Callee = LowerGlobalAddress(Callee, DAG);
2153 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002154 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002155 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002156 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002157
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002158 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002159 // From AMD64 ABI document:
2160 // For calls that may call functions that use varargs or stdargs
2161 // (prototype-less calls or calls to functions containing ellipsis (...) in
2162 // the declaration) %al is used as hidden argument to specify the number
2163 // of SSE registers used. The contents of %al do not need to match exactly
2164 // the number of registers, but must be an ubound on the number of SSE
2165 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002166
Gordon Henriksen86737662008-01-05 16:56:59 +00002167 // Count the number of XMM registers allocated.
2168 static const unsigned XMMArgRegs[] = {
2169 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2170 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2171 };
2172 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002173 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002174 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002175
Dale Johannesendd64c412009-02-04 00:33:20 +00002176 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002177 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002178 InFlag = Chain.getValue(1);
2179 }
2180
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002181
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002182 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002183 if (isTailCall) {
2184 // Force all the incoming stack arguments to be loaded from the stack
2185 // before any new outgoing arguments are stored to the stack, because the
2186 // outgoing stack slots may alias the incoming argument stack slots, and
2187 // the alias isn't otherwise explicit. This is slightly more conservative
2188 // than necessary, because it means that each store effectively depends
2189 // on every argument instead of just those arguments it would clobber.
2190 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2191
Dan Gohman475871a2008-07-27 21:46:04 +00002192 SmallVector<SDValue, 8> MemOpChains2;
2193 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002194 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002195 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002196 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002197 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002198 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2199 CCValAssign &VA = ArgLocs[i];
2200 if (VA.isRegLoc())
2201 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002202 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002203 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002204 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002205 // Create frame index.
2206 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002207 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002208 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002209 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002210
Duncan Sands276dcbd2008-03-21 09:14:45 +00002211 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002212 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002213 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002214 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002215 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002216 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002217 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002218
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2220 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002221 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002222 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002223 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002224 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002225 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002226 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002227 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002228 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002229 }
2230 }
2231
2232 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002233 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002234 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002235
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002236 // Copy arguments to their registers.
2237 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002238 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002239 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002240 InFlag = Chain.getValue(1);
2241 }
Dan Gohman475871a2008-07-27 21:46:04 +00002242 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002243
Gordon Henriksen86737662008-01-05 16:56:59 +00002244 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002245 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002246 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002247 }
2248
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002249 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2250 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2251 // In the 64-bit large code model, we have to make all calls
2252 // through a register, since the call instruction's 32-bit
2253 // pc-relative offset may not be large enough to hold the whole
2254 // address.
2255 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002256 // If the callee is a GlobalAddress node (quite common, every direct call
2257 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2258 // it.
2259
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002260 // We should use extra load for direct calls to dllimported functions in
2261 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002262 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002263 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002264 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002265 bool ExtraLoad = false;
2266 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002267
Chris Lattner48a7d022009-07-09 05:02:21 +00002268 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2269 // external symbols most go through the PLT in PIC mode. If the symbol
2270 // has hidden or protected visibility, or if it is static or local, then
2271 // we don't need to use the PLT - we can directly call it.
2272 if (Subtarget->isTargetELF() &&
2273 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002274 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002275 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002276 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002277 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002278 (!Subtarget->getTargetTriple().isMacOSX() ||
2279 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002280 // PC-relative references to external symbols should go through $stub,
2281 // unless we're building with the leopard linker or later, which
2282 // automatically synthesizes these stubs.
2283 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002284 } else if (Subtarget->isPICStyleRIPRel() &&
2285 isa<Function>(GV) &&
2286 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2287 // If the function is marked as non-lazy, generate an indirect call
2288 // which loads from the GOT directly. This avoids runtime overhead
2289 // at the cost of eager binding (and one extra byte of encoding).
2290 OpFlags = X86II::MO_GOTPCREL;
2291 WrapperKind = X86ISD::WrapperRIP;
2292 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002293 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002294
Devang Patel0d881da2010-07-06 22:08:15 +00002295 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002296 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002297
2298 // Add a wrapper if needed.
2299 if (WrapperKind != ISD::DELETED_NODE)
2300 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2301 // Add extra indirection if needed.
2302 if (ExtraLoad)
2303 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2304 MachinePointerInfo::getGOT(),
2305 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002306 }
Bill Wendling056292f2008-09-16 21:48:12 +00002307 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002308 unsigned char OpFlags = 0;
2309
Evan Cheng1bf891a2010-12-01 22:59:46 +00002310 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2311 // external symbols should go through the PLT.
2312 if (Subtarget->isTargetELF() &&
2313 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2314 OpFlags = X86II::MO_PLT;
2315 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002316 (!Subtarget->getTargetTriple().isMacOSX() ||
2317 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002318 // PC-relative references to external symbols should go through $stub,
2319 // unless we're building with the leopard linker or later, which
2320 // automatically synthesizes these stubs.
2321 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002322 }
Eric Christopherfd179292009-08-27 18:07:15 +00002323
Chris Lattner48a7d022009-07-09 05:02:21 +00002324 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2325 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002326 }
2327
Chris Lattnerd96d0722007-02-25 06:40:16 +00002328 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002329 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002330 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002331
Evan Chengf22f9b32010-02-06 03:28:46 +00002332 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002333 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2334 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002335 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002337
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002338 Ops.push_back(Chain);
2339 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002340
Dan Gohman98ca4f22009-08-05 01:29:28 +00002341 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002343
Gordon Henriksen86737662008-01-05 16:56:59 +00002344 // Add argument registers to the end of the list so that they are known live
2345 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002346 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2347 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2348 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002349
Evan Cheng586ccac2008-03-18 23:36:35 +00002350 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002351 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002352 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2353
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002354 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002355 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002357
Gabor Greifba36cb52008-08-28 21:40:38 +00002358 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002359 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002360
Dan Gohman98ca4f22009-08-05 01:29:28 +00002361 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002362 // We used to do:
2363 //// If this is the first return lowered for this function, add the regs
2364 //// to the liveout set for the function.
2365 // This isn't right, although it's probably harmless on x86; liveouts
2366 // should be computed from returns not tail calls. Consider a void
2367 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368 return DAG.getNode(X86ISD::TC_RETURN, dl,
2369 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002370 }
2371
Dale Johannesenace16102009-02-03 19:33:06 +00002372 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002373 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002374
Chris Lattner2d297092006-05-23 18:50:38 +00002375 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002376 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002377 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002378 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002379 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002380 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002381 // pops the hidden struct pointer, so we have to push it back.
2382 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002383 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002384 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002385 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002386
Gordon Henriksenae636f82008-01-03 16:47:34 +00002387 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002388 if (!IsSibcall) {
2389 Chain = DAG.getCALLSEQ_END(Chain,
2390 DAG.getIntPtrConstant(NumBytes, true),
2391 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2392 true),
2393 InFlag);
2394 InFlag = Chain.getValue(1);
2395 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002396
Chris Lattner3085e152007-02-25 08:59:22 +00002397 // Handle result values, copying them out of physregs into vregs that we
2398 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002399 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2400 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002401}
2402
Evan Cheng25ab6902006-09-08 06:48:29 +00002403
2404//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002405// Fast Calling Convention (tail call) implementation
2406//===----------------------------------------------------------------------===//
2407
2408// Like std call, callee cleans arguments, convention except that ECX is
2409// reserved for storing the tail called function address. Only 2 registers are
2410// free for argument passing (inreg). Tail call optimization is performed
2411// provided:
2412// * tailcallopt is enabled
2413// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002414// On X86_64 architecture with GOT-style position independent code only local
2415// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002416// To keep the stack aligned according to platform abi the function
2417// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2418// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002419// If a tail called function callee has more arguments than the caller the
2420// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002421// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002422// original REtADDR, but before the saved framepointer or the spilled registers
2423// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2424// stack layout:
2425// arg1
2426// arg2
2427// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002428// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002429// move area ]
2430// (possible EBP)
2431// ESI
2432// EDI
2433// local1 ..
2434
2435/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2436/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002437unsigned
2438X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2439 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002440 MachineFunction &MF = DAG.getMachineFunction();
2441 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002442 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002443 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002444 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002445 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002446 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002447 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2448 // Number smaller than 12 so just add the difference.
2449 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2450 } else {
2451 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002452 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002453 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002454 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002455 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002456}
2457
Evan Cheng5f941932010-02-05 02:21:12 +00002458/// MatchingStackOffset - Return true if the given stack call argument is
2459/// already available in the same position (relatively) of the caller's
2460/// incoming argument stack.
2461static
2462bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2463 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2464 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002465 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2466 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002467 if (Arg.getOpcode() == ISD::CopyFromReg) {
2468 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002469 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002470 return false;
2471 MachineInstr *Def = MRI->getVRegDef(VR);
2472 if (!Def)
2473 return false;
2474 if (!Flags.isByVal()) {
2475 if (!TII->isLoadFromStackSlot(Def, FI))
2476 return false;
2477 } else {
2478 unsigned Opcode = Def->getOpcode();
2479 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2480 Def->getOperand(1).isFI()) {
2481 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002482 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002483 } else
2484 return false;
2485 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002486 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2487 if (Flags.isByVal())
2488 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002489 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002490 // define @foo(%struct.X* %A) {
2491 // tail call @bar(%struct.X* byval %A)
2492 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002493 return false;
2494 SDValue Ptr = Ld->getBasePtr();
2495 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2496 if (!FINode)
2497 return false;
2498 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002499 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002500 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002501 FI = FINode->getIndex();
2502 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002503 } else
2504 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002505
Evan Cheng4cae1332010-03-05 08:38:04 +00002506 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002507 if (!MFI->isFixedObjectIndex(FI))
2508 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002509 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002510}
2511
Dan Gohman98ca4f22009-08-05 01:29:28 +00002512/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2513/// for tail call optimization. Targets which want to do tail call
2514/// optimization should implement this function.
2515bool
2516X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002517 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002518 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002519 bool isCalleeStructRet,
2520 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002521 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002522 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002523 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002525 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002526 CalleeCC != CallingConv::C)
2527 return false;
2528
Evan Cheng7096ae42010-01-29 06:45:59 +00002529 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002530 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002531 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002532 CallingConv::ID CallerCC = CallerF->getCallingConv();
2533 bool CCMatch = CallerCC == CalleeCC;
2534
Dan Gohman1797ed52010-02-08 20:27:50 +00002535 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002536 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002537 return true;
2538 return false;
2539 }
2540
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002541 // Look for obvious safe cases to perform tail call optimization that do not
2542 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002543
Evan Cheng2c12cb42010-03-26 16:26:03 +00002544 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2545 // emit a special epilogue.
2546 if (RegInfo->needsStackRealignment(MF))
2547 return false;
2548
Evan Chenga375d472010-03-15 18:54:48 +00002549 // Also avoid sibcall optimization if either caller or callee uses struct
2550 // return semantics.
2551 if (isCalleeStructRet || isCallerStructRet)
2552 return false;
2553
Chad Rosier2416da32011-06-24 21:15:36 +00002554 // An stdcall caller is expected to clean up its arguments; the callee
2555 // isn't going to do that.
2556 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2557 return false;
2558
Chad Rosier871f6642011-05-18 19:59:50 +00002559 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002560 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002561 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002562
2563 // Optimizing for varargs on Win64 is unlikely to be safe without
2564 // additional testing.
2565 if (Subtarget->isTargetWin64())
2566 return false;
2567
Chad Rosier871f6642011-05-18 19:59:50 +00002568 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002569 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2570 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002571
Chad Rosier871f6642011-05-18 19:59:50 +00002572 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2573 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2574 if (!ArgLocs[i].isRegLoc())
2575 return false;
2576 }
2577
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002578 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2579 // Therefore if it's not used by the call it is not safe to optimize this into
2580 // a sibcall.
2581 bool Unused = false;
2582 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2583 if (!Ins[i].Used) {
2584 Unused = true;
2585 break;
2586 }
2587 }
2588 if (Unused) {
2589 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002590 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2591 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002592 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002593 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002594 CCValAssign &VA = RVLocs[i];
2595 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2596 return false;
2597 }
2598 }
2599
Evan Cheng13617962010-04-30 01:12:32 +00002600 // If the calling conventions do not match, then we'd better make sure the
2601 // results are returned in the same way as what the caller expects.
2602 if (!CCMatch) {
2603 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002604 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2605 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002606 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2607
2608 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002609 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2610 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002611 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2612
2613 if (RVLocs1.size() != RVLocs2.size())
2614 return false;
2615 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2616 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2617 return false;
2618 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2619 return false;
2620 if (RVLocs1[i].isRegLoc()) {
2621 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2622 return false;
2623 } else {
2624 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2625 return false;
2626 }
2627 }
2628 }
2629
Evan Chenga6bff982010-01-30 01:22:00 +00002630 // If the callee takes no arguments then go on to check the results of the
2631 // call.
2632 if (!Outs.empty()) {
2633 // Check if stack adjustment is needed. For now, do not do this if any
2634 // argument is passed on the stack.
2635 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002636 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2637 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002638
2639 // Allocate shadow area for Win64
2640 if (Subtarget->isTargetWin64()) {
2641 CCInfo.AllocateStack(32, 8);
2642 }
2643
Duncan Sands45907662010-10-31 13:21:44 +00002644 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002645 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002646 MachineFunction &MF = DAG.getMachineFunction();
2647 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2648 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002649
2650 // Check if the arguments are already laid out in the right way as
2651 // the caller's fixed stack objects.
2652 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002653 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2654 const X86InstrInfo *TII =
2655 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002656 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2657 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002658 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002659 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002660 if (VA.getLocInfo() == CCValAssign::Indirect)
2661 return false;
2662 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002663 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2664 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002665 return false;
2666 }
2667 }
2668 }
Evan Cheng9c044672010-05-29 01:35:22 +00002669
2670 // If the tailcall address may be in a register, then make sure it's
2671 // possible to register allocate for it. In 32-bit, the call address can
2672 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002673 // callee-saved registers are restored. These happen to be the same
2674 // registers used to pass 'inreg' arguments so watch out for those.
2675 if (!Subtarget->is64Bit() &&
2676 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002677 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002678 unsigned NumInRegs = 0;
2679 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2680 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002681 if (!VA.isRegLoc())
2682 continue;
2683 unsigned Reg = VA.getLocReg();
2684 switch (Reg) {
2685 default: break;
2686 case X86::EAX: case X86::EDX: case X86::ECX:
2687 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002688 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002689 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002690 }
2691 }
2692 }
Evan Chenga6bff982010-01-30 01:22:00 +00002693 }
Evan Chengb1712452010-01-27 06:25:16 +00002694
Evan Cheng86809cc2010-02-03 03:28:02 +00002695 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002696}
2697
Dan Gohman3df24e62008-09-03 23:12:08 +00002698FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002699X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2700 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002701}
2702
2703
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002704//===----------------------------------------------------------------------===//
2705// Other Lowering Hooks
2706//===----------------------------------------------------------------------===//
2707
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002708static bool MayFoldLoad(SDValue Op) {
2709 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2710}
2711
2712static bool MayFoldIntoStore(SDValue Op) {
2713 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2714}
2715
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002716static bool isTargetShuffle(unsigned Opcode) {
2717 switch(Opcode) {
2718 default: return false;
2719 case X86ISD::PSHUFD:
2720 case X86ISD::PSHUFHW:
2721 case X86ISD::PSHUFLW:
2722 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002723 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002724 case X86ISD::SHUFPS:
2725 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002726 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002727 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002728 case X86ISD::MOVLPS:
2729 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002730 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002731 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002732 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002733 case X86ISD::MOVSS:
2734 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002735 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002736 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002737 case X86ISD::VUNPCKLPS:
2738 case X86ISD::VUNPCKLPD:
2739 case X86ISD::VUNPCKLPSY:
2740 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002741 case X86ISD::PUNPCKLWD:
2742 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002743 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002744 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002745 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002746 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002747 case X86ISD::PUNPCKHWD:
2748 case X86ISD::PUNPCKHBW:
2749 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002750 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00002751 case X86ISD::VPERMIL:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002752 return true;
2753 }
2754 return false;
2755}
2756
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002757static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002758 SDValue V1, SelectionDAG &DAG) {
2759 switch(Opc) {
2760 default: llvm_unreachable("Unknown x86 shuffle node");
2761 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002762 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002763 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002764 return DAG.getNode(Opc, dl, VT, V1);
2765 }
2766
2767 return SDValue();
2768}
2769
2770static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002771 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002772 switch(Opc) {
2773 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002774 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002775 case X86ISD::PSHUFHW:
2776 case X86ISD::PSHUFLW:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00002777 case X86ISD::VPERMIL:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002778 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2779 }
2780
2781 return SDValue();
2782}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002783
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002784static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2785 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2786 switch(Opc) {
2787 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002788 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002789 case X86ISD::SHUFPD:
2790 case X86ISD::SHUFPS:
2791 return DAG.getNode(Opc, dl, VT, V1, V2,
2792 DAG.getConstant(TargetMask, MVT::i8));
2793 }
2794 return SDValue();
2795}
2796
2797static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2798 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2799 switch(Opc) {
2800 default: llvm_unreachable("Unknown x86 shuffle node");
2801 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002802 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002803 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002804 case X86ISD::MOVLPS:
2805 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002806 case X86ISD::MOVSS:
2807 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002808 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002809 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002810 case X86ISD::VUNPCKLPS:
2811 case X86ISD::VUNPCKLPD:
2812 case X86ISD::VUNPCKLPSY:
2813 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002814 case X86ISD::PUNPCKLWD:
2815 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002816 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002817 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002818 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002819 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002820 case X86ISD::PUNPCKHWD:
2821 case X86ISD::PUNPCKHBW:
2822 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002823 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002824 return DAG.getNode(Opc, dl, VT, V1, V2);
2825 }
2826 return SDValue();
2827}
2828
Dan Gohmand858e902010-04-17 15:26:15 +00002829SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002830 MachineFunction &MF = DAG.getMachineFunction();
2831 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2832 int ReturnAddrIndex = FuncInfo->getRAIndex();
2833
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002834 if (ReturnAddrIndex == 0) {
2835 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002836 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002837 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002838 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002839 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002840 }
2841
Evan Cheng25ab6902006-09-08 06:48:29 +00002842 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002843}
2844
2845
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002846bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2847 bool hasSymbolicDisplacement) {
2848 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002849 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002850 return false;
2851
2852 // If we don't have a symbolic displacement - we don't have any extra
2853 // restrictions.
2854 if (!hasSymbolicDisplacement)
2855 return true;
2856
2857 // FIXME: Some tweaks might be needed for medium code model.
2858 if (M != CodeModel::Small && M != CodeModel::Kernel)
2859 return false;
2860
2861 // For small code model we assume that latest object is 16MB before end of 31
2862 // bits boundary. We may also accept pretty large negative constants knowing
2863 // that all objects are in the positive half of address space.
2864 if (M == CodeModel::Small && Offset < 16*1024*1024)
2865 return true;
2866
2867 // For kernel code model we know that all object resist in the negative half
2868 // of 32bits address space. We may not accept negative offsets, since they may
2869 // be just off and we may accept pretty large positive ones.
2870 if (M == CodeModel::Kernel && Offset > 0)
2871 return true;
2872
2873 return false;
2874}
2875
Evan Chengef41ff62011-06-23 17:54:54 +00002876/// isCalleePop - Determines whether the callee is required to pop its
2877/// own arguments. Callee pop is necessary to support tail calls.
2878bool X86::isCalleePop(CallingConv::ID CallingConv,
2879 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2880 if (IsVarArg)
2881 return false;
2882
2883 switch (CallingConv) {
2884 default:
2885 return false;
2886 case CallingConv::X86_StdCall:
2887 return !is64Bit;
2888 case CallingConv::X86_FastCall:
2889 return !is64Bit;
2890 case CallingConv::X86_ThisCall:
2891 return !is64Bit;
2892 case CallingConv::Fast:
2893 return TailCallOpt;
2894 case CallingConv::GHC:
2895 return TailCallOpt;
2896 }
2897}
2898
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002899/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2900/// specific condition code, returning the condition code and the LHS/RHS of the
2901/// comparison to make.
2902static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2903 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002904 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002905 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2906 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2907 // X > -1 -> X == 0, jump !sign.
2908 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002909 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002910 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2911 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002912 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002913 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002914 // X < 1 -> X <= 0
2915 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002916 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002917 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002918 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002919
Evan Chengd9558e02006-01-06 00:43:03 +00002920 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002921 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002922 case ISD::SETEQ: return X86::COND_E;
2923 case ISD::SETGT: return X86::COND_G;
2924 case ISD::SETGE: return X86::COND_GE;
2925 case ISD::SETLT: return X86::COND_L;
2926 case ISD::SETLE: return X86::COND_LE;
2927 case ISD::SETNE: return X86::COND_NE;
2928 case ISD::SETULT: return X86::COND_B;
2929 case ISD::SETUGT: return X86::COND_A;
2930 case ISD::SETULE: return X86::COND_BE;
2931 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002932 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002933 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002934
Chris Lattner4c78e022008-12-23 23:42:27 +00002935 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002936
Chris Lattner4c78e022008-12-23 23:42:27 +00002937 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002938 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2939 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002940 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2941 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002942 }
2943
Chris Lattner4c78e022008-12-23 23:42:27 +00002944 switch (SetCCOpcode) {
2945 default: break;
2946 case ISD::SETOLT:
2947 case ISD::SETOLE:
2948 case ISD::SETUGT:
2949 case ISD::SETUGE:
2950 std::swap(LHS, RHS);
2951 break;
2952 }
2953
2954 // On a floating point condition, the flags are set as follows:
2955 // ZF PF CF op
2956 // 0 | 0 | 0 | X > Y
2957 // 0 | 0 | 1 | X < Y
2958 // 1 | 0 | 0 | X == Y
2959 // 1 | 1 | 1 | unordered
2960 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002961 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002962 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002963 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002964 case ISD::SETOLT: // flipped
2965 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002966 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002967 case ISD::SETOLE: // flipped
2968 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002969 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002970 case ISD::SETUGT: // flipped
2971 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002972 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002973 case ISD::SETUGE: // flipped
2974 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002975 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002976 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002977 case ISD::SETNE: return X86::COND_NE;
2978 case ISD::SETUO: return X86::COND_P;
2979 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002980 case ISD::SETOEQ:
2981 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002982 }
Evan Chengd9558e02006-01-06 00:43:03 +00002983}
2984
Evan Cheng4a460802006-01-11 00:33:36 +00002985/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2986/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002987/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002988static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002989 switch (X86CC) {
2990 default:
2991 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002992 case X86::COND_B:
2993 case X86::COND_BE:
2994 case X86::COND_E:
2995 case X86::COND_P:
2996 case X86::COND_A:
2997 case X86::COND_AE:
2998 case X86::COND_NE:
2999 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003000 return true;
3001 }
3002}
3003
Evan Chengeb2f9692009-10-27 19:56:55 +00003004/// isFPImmLegal - Returns true if the target can instruction select the
3005/// specified FP immediate natively. If false, the legalizer will
3006/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003007bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003008 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3009 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3010 return true;
3011 }
3012 return false;
3013}
3014
Nate Begeman9008ca62009-04-27 18:41:29 +00003015/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3016/// the specified range (L, H].
3017static bool isUndefOrInRange(int Val, int Low, int Hi) {
3018 return (Val < 0) || (Val >= Low && Val < Hi);
3019}
3020
3021/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3022/// specified value.
3023static bool isUndefOrEqual(int Val, int CmpVal) {
3024 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003026 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003027}
3028
Nate Begeman9008ca62009-04-27 18:41:29 +00003029/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3030/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3031/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003032static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003033 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003034 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003035 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003036 return (Mask[0] < 2 && Mask[1] < 2);
3037 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003038}
3039
Nate Begeman9008ca62009-04-27 18:41:29 +00003040bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003041 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003042 N->getMask(M);
3043 return ::isPSHUFDMask(M, N->getValueType(0));
3044}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003045
Nate Begeman9008ca62009-04-27 18:41:29 +00003046/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3047/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003048static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003050 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003051
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 // Lower quadword copied in order or undef.
3053 for (int i = 0; i != 4; ++i)
3054 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003055 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003056
Evan Cheng506d3df2006-03-29 23:07:14 +00003057 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 for (int i = 4; i != 8; ++i)
3059 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003060 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003061
Evan Cheng506d3df2006-03-29 23:07:14 +00003062 return true;
3063}
3064
Nate Begeman9008ca62009-04-27 18:41:29 +00003065bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003066 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003067 N->getMask(M);
3068 return ::isPSHUFHWMask(M, N->getValueType(0));
3069}
Evan Cheng506d3df2006-03-29 23:07:14 +00003070
Nate Begeman9008ca62009-04-27 18:41:29 +00003071/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3072/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003073static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003074 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003075 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003076
Rafael Espindola15684b22009-04-24 12:40:33 +00003077 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003078 for (int i = 4; i != 8; ++i)
3079 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003080 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003081
Rafael Espindola15684b22009-04-24 12:40:33 +00003082 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003083 for (int i = 0; i != 4; ++i)
3084 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003085 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003086
Rafael Espindola15684b22009-04-24 12:40:33 +00003087 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003088}
3089
Nate Begeman9008ca62009-04-27 18:41:29 +00003090bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003091 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003092 N->getMask(M);
3093 return ::isPSHUFLWMask(M, N->getValueType(0));
3094}
3095
Nate Begemana09008b2009-10-19 02:17:23 +00003096/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3097/// is suitable for input to PALIGNR.
3098static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3099 bool hasSSSE3) {
3100 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003101
Nate Begemana09008b2009-10-19 02:17:23 +00003102 // Do not handle v2i64 / v2f64 shuffles with palignr.
3103 if (e < 4 || !hasSSSE3)
3104 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003105
Nate Begemana09008b2009-10-19 02:17:23 +00003106 for (i = 0; i != e; ++i)
3107 if (Mask[i] >= 0)
3108 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003109
Nate Begemana09008b2009-10-19 02:17:23 +00003110 // All undef, not a palignr.
3111 if (i == e)
3112 return false;
3113
3114 // Determine if it's ok to perform a palignr with only the LHS, since we
3115 // don't have access to the actual shuffle elements to see if RHS is undef.
3116 bool Unary = Mask[i] < (int)e;
3117 bool NeedsUnary = false;
3118
3119 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003120
Nate Begemana09008b2009-10-19 02:17:23 +00003121 // Check the rest of the elements to see if they are consecutive.
3122 for (++i; i != e; ++i) {
3123 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003124 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003125 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003126
Nate Begemana09008b2009-10-19 02:17:23 +00003127 Unary = Unary && (m < (int)e);
3128 NeedsUnary = NeedsUnary || (m < s);
3129
3130 if (NeedsUnary && !Unary)
3131 return false;
3132 if (Unary && m != ((s+i) & (e-1)))
3133 return false;
3134 if (!Unary && m != (s+i))
3135 return false;
3136 }
3137 return true;
3138}
3139
3140bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3141 SmallVector<int, 8> M;
3142 N->getMask(M);
3143 return ::isPALIGNRMask(M, N->getValueType(0), true);
3144}
3145
Evan Cheng14aed5e2006-03-24 01:18:28 +00003146/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3147/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003148static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003149 int NumElems = VT.getVectorNumElements();
3150 if (NumElems != 2 && NumElems != 4)
3151 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003152
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 int Half = NumElems / 2;
3154 for (int i = 0; i < Half; ++i)
3155 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003156 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 for (int i = Half; i < NumElems; ++i)
3158 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Evan Cheng14aed5e2006-03-24 01:18:28 +00003161 return true;
3162}
3163
Nate Begeman9008ca62009-04-27 18:41:29 +00003164bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3165 SmallVector<int, 8> M;
3166 N->getMask(M);
3167 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003168}
3169
Evan Cheng213d2cf2007-05-17 18:45:50 +00003170/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003171/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3172/// half elements to come from vector 1 (which would equal the dest.) and
3173/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003174static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003176
3177 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003179
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 int Half = NumElems / 2;
3181 for (int i = 0; i < Half; ++i)
3182 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003183 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 for (int i = Half; i < NumElems; ++i)
3185 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003186 return false;
3187 return true;
3188}
3189
Nate Begeman9008ca62009-04-27 18:41:29 +00003190static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3191 SmallVector<int, 8> M;
3192 N->getMask(M);
3193 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003194}
3195
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003196/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3197/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003198bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3199 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003200 return false;
3201
Evan Cheng2064a2b2006-03-28 06:50:32 +00003202 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3204 isUndefOrEqual(N->getMaskElt(1), 7) &&
3205 isUndefOrEqual(N->getMaskElt(2), 2) &&
3206 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003207}
3208
Nate Begeman0b10b912009-11-07 23:17:15 +00003209/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3210/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3211/// <2, 3, 2, 3>
3212bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3213 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003214
Nate Begeman0b10b912009-11-07 23:17:15 +00003215 if (NumElems != 4)
3216 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003217
Nate Begeman0b10b912009-11-07 23:17:15 +00003218 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3219 isUndefOrEqual(N->getMaskElt(1), 3) &&
3220 isUndefOrEqual(N->getMaskElt(2), 2) &&
3221 isUndefOrEqual(N->getMaskElt(3), 3);
3222}
3223
Evan Cheng5ced1d82006-04-06 23:23:56 +00003224/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3225/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003226bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3227 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003228
Evan Cheng5ced1d82006-04-06 23:23:56 +00003229 if (NumElems != 2 && NumElems != 4)
3230 return false;
3231
Evan Chengc5cdff22006-04-07 21:53:05 +00003232 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003234 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003235
Evan Chengc5cdff22006-04-07 21:53:05 +00003236 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003238 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003239
3240 return true;
3241}
3242
Nate Begeman0b10b912009-11-07 23:17:15 +00003243/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3244/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3245bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003246 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003247
David Greenea20244d2011-03-02 17:23:43 +00003248 if ((NumElems != 2 && NumElems != 4)
3249 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003250 return false;
3251
Evan Chengc5cdff22006-04-07 21:53:05 +00003252 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003254 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003255
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 for (unsigned i = 0; i < NumElems/2; ++i)
3257 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003258 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003259
3260 return true;
3261}
3262
Evan Cheng0038e592006-03-28 00:39:58 +00003263/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3264/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003265static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003266 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003268 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003269 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003270
David Greenea20244d2011-03-02 17:23:43 +00003271 // Handle vector lengths > 128 bits. Define a "section" as a set of
3272 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3273 // sections.
3274 unsigned NumSections = VT.getSizeInBits() / 128;
3275 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3276 unsigned NumSectionElts = NumElts / NumSections;
3277
3278 unsigned Start = 0;
3279 unsigned End = NumSectionElts;
3280 for (unsigned s = 0; s < NumSections; ++s) {
3281 for (unsigned i = Start, j = s * NumSectionElts;
3282 i != End;
3283 i += 2, ++j) {
3284 int BitI = Mask[i];
3285 int BitI1 = Mask[i+1];
3286 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003287 return false;
David Greenea20244d2011-03-02 17:23:43 +00003288 if (V2IsSplat) {
3289 if (!isUndefOrEqual(BitI1, NumElts))
3290 return false;
3291 } else {
3292 if (!isUndefOrEqual(BitI1, j + NumElts))
3293 return false;
3294 }
Evan Cheng39623da2006-04-20 08:58:49 +00003295 }
David Greenea20244d2011-03-02 17:23:43 +00003296 // Process the next 128 bits.
3297 Start += NumSectionElts;
3298 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003299 }
David Greenea20244d2011-03-02 17:23:43 +00003300
Evan Cheng0038e592006-03-28 00:39:58 +00003301 return true;
3302}
3303
Nate Begeman9008ca62009-04-27 18:41:29 +00003304bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3305 SmallVector<int, 8> M;
3306 N->getMask(M);
3307 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003308}
3309
Evan Cheng4fcb9222006-03-28 02:43:26 +00003310/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3311/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003312static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003313 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003314 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003315 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003316 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003317
Nate Begeman9008ca62009-04-27 18:41:29 +00003318 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3319 int BitI = Mask[i];
3320 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003321 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003322 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003323 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003324 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003325 return false;
3326 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003327 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003328 return false;
3329 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003330 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003331 return true;
3332}
3333
Nate Begeman9008ca62009-04-27 18:41:29 +00003334bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3335 SmallVector<int, 8> M;
3336 N->getMask(M);
3337 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003338}
3339
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003340/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3341/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3342/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003343static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003345 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003346 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003347
David Greenea20244d2011-03-02 17:23:43 +00003348 // Handle vector lengths > 128 bits. Define a "section" as a set of
3349 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3350 // sections.
3351 unsigned NumSections = VT.getSizeInBits() / 128;
3352 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3353 unsigned NumSectionElts = NumElems / NumSections;
3354
3355 for (unsigned s = 0; s < NumSections; ++s) {
3356 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3357 i != NumSectionElts * (s + 1);
3358 i += 2, ++j) {
3359 int BitI = Mask[i];
3360 int BitI1 = Mask[i+1];
3361
3362 if (!isUndefOrEqual(BitI, j))
3363 return false;
3364 if (!isUndefOrEqual(BitI1, j))
3365 return false;
3366 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003367 }
David Greenea20244d2011-03-02 17:23:43 +00003368
Rafael Espindola15684b22009-04-24 12:40:33 +00003369 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003370}
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3373 SmallVector<int, 8> M;
3374 N->getMask(M);
3375 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3376}
3377
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003378/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3379/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3380/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003381static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003383 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3384 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003385
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3387 int BitI = Mask[i];
3388 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003389 if (!isUndefOrEqual(BitI, j))
3390 return false;
3391 if (!isUndefOrEqual(BitI1, j))
3392 return false;
3393 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003394 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003395}
3396
Nate Begeman9008ca62009-04-27 18:41:29 +00003397bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3398 SmallVector<int, 8> M;
3399 N->getMask(M);
3400 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3401}
3402
Evan Cheng017dcc62006-04-21 01:05:10 +00003403/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3404/// specifies a shuffle of elements that is suitable for input to MOVSS,
3405/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003406static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003407 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003408 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003409
3410 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003411
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003413 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003414
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 for (int i = 1; i < NumElts; ++i)
3416 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003417 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003418
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003419 return true;
3420}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003421
Nate Begeman9008ca62009-04-27 18:41:29 +00003422bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3423 SmallVector<int, 8> M;
3424 N->getMask(M);
3425 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003426}
3427
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003428/// isVPERMILMask - Return true if the specified VECTOR_SHUFFLE operand
3429/// specifies a shuffle of elements that is suitable for input to VPERMIL*.
3430static bool isVPERMILMask(const SmallVectorImpl<int> &Mask, EVT VT) {
3431 unsigned NumElts = VT.getVectorNumElements();
3432 unsigned NumLanes = VT.getSizeInBits()/128;
3433
3434 // Match any permutation of 128-bit vector with 32/64-bit types
3435 if (NumLanes == 1) {
3436 if (NumElts == 4 || NumElts == 2)
3437 return true;
3438 return false;
3439 }
3440
3441 // Only match 256-bit with 32/64-bit types
3442 if (NumElts != 8 && NumElts != 4)
3443 return false;
3444
3445 // The mask on the high lane should be the same as the low. Actually,
3446 // they can differ if any of the corresponding index in a lane is undef.
3447 int LaneSize = NumElts/NumLanes;
3448 for (int i = 0; i < LaneSize; ++i) {
3449 int HighElt = i+LaneSize;
3450 if (Mask[i] < 0 || Mask[HighElt] < 0)
3451 continue;
3452
3453 if (Mask[HighElt]-Mask[i] != LaneSize)
3454 return false;
3455 }
3456
3457 return true;
3458}
3459
3460/// getShuffleVPERMILImmediateediate - Return the appropriate immediate to shuffle
3461/// the specified VECTOR_MASK mask with VPERMIL* instructions.
3462static unsigned getShuffleVPERMILImmediate(SDNode *N) {
3463 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3464 EVT VT = SVOp->getValueType(0);
3465
3466 int NumElts = VT.getVectorNumElements();
3467 int NumLanes = VT.getSizeInBits()/128;
3468
3469 unsigned Mask = 0;
3470 for (int i = 0; i < NumElts/NumLanes /* lane size */; ++i)
3471 Mask |= SVOp->getMaskElt(i) << (i*2);
3472
3473 return Mask;
3474}
3475
Evan Cheng017dcc62006-04-21 01:05:10 +00003476/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3477/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003478/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003479static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 bool V2IsSplat = false, bool V2IsUndef = false) {
3481 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003482 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003483 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003484
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003486 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003487
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 for (int i = 1; i < NumOps; ++i)
3489 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3490 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3491 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003492 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003493
Evan Cheng39623da2006-04-20 08:58:49 +00003494 return true;
3495}
3496
Nate Begeman9008ca62009-04-27 18:41:29 +00003497static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003498 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003499 SmallVector<int, 8> M;
3500 N->getMask(M);
3501 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003502}
3503
Evan Chengd9539472006-04-14 21:59:03 +00003504/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3505/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003506bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3507 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003508 return false;
3509
3510 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003511 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003512 int Elt = N->getMaskElt(i);
3513 if (Elt >= 0 && Elt != 1)
3514 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003515 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003516
3517 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003518 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003519 int Elt = N->getMaskElt(i);
3520 if (Elt >= 0 && Elt != 3)
3521 return false;
3522 if (Elt == 3)
3523 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003524 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003525 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003526 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003527 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003528}
3529
3530/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3531/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003532bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3533 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003534 return false;
3535
3536 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003537 for (unsigned i = 0; i < 2; ++i)
3538 if (N->getMaskElt(i) > 0)
3539 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003540
3541 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003542 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003543 int Elt = N->getMaskElt(i);
3544 if (Elt >= 0 && Elt != 2)
3545 return false;
3546 if (Elt == 2)
3547 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003548 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003549 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003550 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003551}
3552
Evan Cheng0b457f02008-09-25 20:50:48 +00003553/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3554/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003555bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3556 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003557
Nate Begeman9008ca62009-04-27 18:41:29 +00003558 for (int i = 0; i < e; ++i)
3559 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003560 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003561 for (int i = 0; i < e; ++i)
3562 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003563 return false;
3564 return true;
3565}
3566
David Greenec38a03e2011-02-03 15:50:00 +00003567/// isVEXTRACTF128Index - Return true if the specified
3568/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3569/// suitable for input to VEXTRACTF128.
3570bool X86::isVEXTRACTF128Index(SDNode *N) {
3571 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3572 return false;
3573
3574 // The index should be aligned on a 128-bit boundary.
3575 uint64_t Index =
3576 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3577
3578 unsigned VL = N->getValueType(0).getVectorNumElements();
3579 unsigned VBits = N->getValueType(0).getSizeInBits();
3580 unsigned ElSize = VBits / VL;
3581 bool Result = (Index * ElSize) % 128 == 0;
3582
3583 return Result;
3584}
3585
David Greeneccacdc12011-02-04 16:08:29 +00003586/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3587/// operand specifies a subvector insert that is suitable for input to
3588/// VINSERTF128.
3589bool X86::isVINSERTF128Index(SDNode *N) {
3590 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3591 return false;
3592
3593 // The index should be aligned on a 128-bit boundary.
3594 uint64_t Index =
3595 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3596
3597 unsigned VL = N->getValueType(0).getVectorNumElements();
3598 unsigned VBits = N->getValueType(0).getSizeInBits();
3599 unsigned ElSize = VBits / VL;
3600 bool Result = (Index * ElSize) % 128 == 0;
3601
3602 return Result;
3603}
3604
Evan Cheng63d33002006-03-22 08:01:21 +00003605/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003606/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003607unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003608 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3609 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3610
Evan Chengb9df0ca2006-03-22 02:53:00 +00003611 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3612 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003613 for (int i = 0; i < NumOperands; ++i) {
3614 int Val = SVOp->getMaskElt(NumOperands-i-1);
3615 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003616 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003617 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003618 if (i != NumOperands - 1)
3619 Mask <<= Shift;
3620 }
Evan Cheng63d33002006-03-22 08:01:21 +00003621 return Mask;
3622}
3623
Evan Cheng506d3df2006-03-29 23:07:14 +00003624/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003625/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003626unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003627 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003628 unsigned Mask = 0;
3629 // 8 nodes, but we only care about the last 4.
3630 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003631 int Val = SVOp->getMaskElt(i);
3632 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003633 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003634 if (i != 4)
3635 Mask <<= 2;
3636 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003637 return Mask;
3638}
3639
3640/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003641/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003642unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003643 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003644 unsigned Mask = 0;
3645 // 8 nodes, but we only care about the first 4.
3646 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003647 int Val = SVOp->getMaskElt(i);
3648 if (Val >= 0)
3649 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003650 if (i != 0)
3651 Mask <<= 2;
3652 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003653 return Mask;
3654}
3655
Nate Begemana09008b2009-10-19 02:17:23 +00003656/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3657/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3658unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3659 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3660 EVT VVT = N->getValueType(0);
3661 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3662 int Val = 0;
3663
3664 unsigned i, e;
3665 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3666 Val = SVOp->getMaskElt(i);
3667 if (Val >= 0)
3668 break;
3669 }
3670 return (Val - i) * EltSize;
3671}
3672
David Greenec38a03e2011-02-03 15:50:00 +00003673/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3674/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3675/// instructions.
3676unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3677 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3678 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3679
3680 uint64_t Index =
3681 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3682
3683 EVT VecVT = N->getOperand(0).getValueType();
3684 EVT ElVT = VecVT.getVectorElementType();
3685
3686 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00003687 return Index / NumElemsPerChunk;
3688}
3689
David Greeneccacdc12011-02-04 16:08:29 +00003690/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3691/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3692/// instructions.
3693unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3694 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3695 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3696
3697 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003698 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003699
3700 EVT VecVT = N->getValueType(0);
3701 EVT ElVT = VecVT.getVectorElementType();
3702
3703 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00003704 return Index / NumElemsPerChunk;
3705}
3706
Evan Cheng37b73872009-07-30 08:33:02 +00003707/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3708/// constant +0.0.
3709bool X86::isZeroNode(SDValue Elt) {
3710 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003711 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003712 (isa<ConstantFPSDNode>(Elt) &&
3713 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3714}
3715
Nate Begeman9008ca62009-04-27 18:41:29 +00003716/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3717/// their permute mask.
3718static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3719 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003720 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003721 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003722 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003723
Nate Begeman5a5ca152009-04-29 05:20:52 +00003724 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003725 int idx = SVOp->getMaskElt(i);
3726 if (idx < 0)
3727 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003728 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003729 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003730 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003731 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003732 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003733 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3734 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003735}
3736
Evan Cheng779ccea2007-12-07 21:30:01 +00003737/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3738/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003739static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003740 unsigned NumElems = VT.getVectorNumElements();
3741 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003742 int idx = Mask[i];
3743 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003744 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003745 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003746 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003747 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003748 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003749 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003750}
3751
Evan Cheng533a0aa2006-04-19 20:35:22 +00003752/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3753/// match movhlps. The lower half elements should come from upper half of
3754/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003755/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003756static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3757 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003758 return false;
3759 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003760 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003761 return false;
3762 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003763 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003764 return false;
3765 return true;
3766}
3767
Evan Cheng5ced1d82006-04-06 23:23:56 +00003768/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003769/// is promoted to a vector. It also returns the LoadSDNode by reference if
3770/// required.
3771static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003772 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3773 return false;
3774 N = N->getOperand(0).getNode();
3775 if (!ISD::isNON_EXTLoad(N))
3776 return false;
3777 if (LD)
3778 *LD = cast<LoadSDNode>(N);
3779 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003780}
3781
Evan Cheng533a0aa2006-04-19 20:35:22 +00003782/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3783/// match movlp{s|d}. The lower half elements should come from lower half of
3784/// V1 (and in order), and the upper half elements should come from the upper
3785/// half of V2 (and in order). And since V1 will become the source of the
3786/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003787static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3788 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003789 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003790 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003791 // Is V2 is a vector load, don't do this transformation. We will try to use
3792 // load folding shufps op.
3793 if (ISD::isNON_EXTLoad(V2))
3794 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003795
Nate Begeman5a5ca152009-04-29 05:20:52 +00003796 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003797
Evan Cheng533a0aa2006-04-19 20:35:22 +00003798 if (NumElems != 2 && NumElems != 4)
3799 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003800 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003801 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003802 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003803 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003804 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003805 return false;
3806 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003807}
3808
Evan Cheng39623da2006-04-20 08:58:49 +00003809/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3810/// all the same.
3811static bool isSplatVector(SDNode *N) {
3812 if (N->getOpcode() != ISD::BUILD_VECTOR)
3813 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003814
Dan Gohman475871a2008-07-27 21:46:04 +00003815 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003816 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3817 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003818 return false;
3819 return true;
3820}
3821
Evan Cheng213d2cf2007-05-17 18:45:50 +00003822/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003823/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003824/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003825static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003826 SDValue V1 = N->getOperand(0);
3827 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003828 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3829 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003830 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003831 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003832 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003833 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3834 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003835 if (Opc != ISD::BUILD_VECTOR ||
3836 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003837 return false;
3838 } else if (Idx >= 0) {
3839 unsigned Opc = V1.getOpcode();
3840 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3841 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003842 if (Opc != ISD::BUILD_VECTOR ||
3843 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003844 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003845 }
3846 }
3847 return true;
3848}
3849
3850/// getZeroVector - Returns a vector of specified type with all zero elements.
3851///
Owen Andersone50ed302009-08-10 22:56:29 +00003852static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003853 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003854 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003855
Dale Johannesen0488fb62010-09-30 23:57:10 +00003856 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003857 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003858 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003859 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003860 if (HasSSE2) { // SSE2
3861 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3862 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3863 } else { // SSE1
3864 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3865 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3866 }
3867 } else if (VT.getSizeInBits() == 256) { // AVX
3868 // 256-bit logic and arithmetic instructions in AVX are
3869 // all floating-point, no support for integer ops. Default
3870 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003872 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3873 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003874 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003875 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003876}
3877
Chris Lattner8a594482007-11-25 00:24:49 +00003878/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003879/// Always build ones vectors as <4 x i32> or <8 x i32> bitcasted to
3880/// their original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00003881static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003882 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003883 assert((VT.is128BitVector() || VT.is256BitVector())
3884 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003885
Owen Anderson825b72b2009-08-11 20:47:22 +00003886 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003887
Dan Gohman475871a2008-07-27 21:46:04 +00003888 SDValue Vec;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003889 if (VT.is256BitVector()) {
3890 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3891 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
3892 } else
3893 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003894 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003895}
3896
Evan Cheng39623da2006-04-20 08:58:49 +00003897/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3898/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003899static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003900 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003901 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003902
Evan Cheng39623da2006-04-20 08:58:49 +00003903 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 SmallVector<int, 8> MaskVec;
3905 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003906
Nate Begeman5a5ca152009-04-29 05:20:52 +00003907 for (unsigned i = 0; i != NumElems; ++i) {
3908 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 MaskVec[i] = NumElems;
3910 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003911 }
Evan Cheng39623da2006-04-20 08:58:49 +00003912 }
Evan Cheng39623da2006-04-20 08:58:49 +00003913 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003914 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3915 SVOp->getOperand(1), &MaskVec[0]);
3916 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003917}
3918
Evan Cheng017dcc62006-04-21 01:05:10 +00003919/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3920/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003921static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003922 SDValue V2) {
3923 unsigned NumElems = VT.getVectorNumElements();
3924 SmallVector<int, 8> Mask;
3925 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003926 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 Mask.push_back(i);
3928 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003929}
3930
Nate Begeman9008ca62009-04-27 18:41:29 +00003931/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003932static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003933 SDValue V2) {
3934 unsigned NumElems = VT.getVectorNumElements();
3935 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003936 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003937 Mask.push_back(i);
3938 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003939 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003940 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003941}
3942
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003943/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003944static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003945 SDValue V2) {
3946 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003947 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003949 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 Mask.push_back(i + Half);
3951 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003952 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003953 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003954}
3955
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003956// PromoteSplatv8v16 - All i16 and i8 vector types can't be used directly by
3957// a generic shuffle instruction because the target has no such instructions.
3958// Generate shuffles which repeat i16 and i8 several times until they can be
3959// represented by v4f32 and then be manipulated by target suported shuffles.
3960static SDValue PromoteSplatv8v16(SDValue V, SelectionDAG &DAG, int &EltNo) {
3961 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003963 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00003964
Nate Begeman9008ca62009-04-27 18:41:29 +00003965 while (NumElems > 4) {
3966 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003967 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003969 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00003970 EltNo -= NumElems/2;
3971 }
3972 NumElems >>= 1;
3973 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003974 return V;
3975}
Eric Christopherfd179292009-08-27 18:07:15 +00003976
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00003977/// getLegalSplat - Generate a legal splat with supported x86 shuffles
3978static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
3979 EVT VT = V.getValueType();
3980 DebugLoc dl = V.getDebugLoc();
3981 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
3982 && "Vector size not supported");
3983
3984 bool Is128 = VT.getSizeInBits() == 128;
3985 EVT NVT = Is128 ? MVT::v4f32 : MVT::v8f32;
3986 V = DAG.getNode(ISD::BITCAST, dl, NVT, V);
3987
3988 if (Is128) {
3989 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
3990 V = DAG.getVectorShuffle(NVT, dl, V, DAG.getUNDEF(NVT), &SplatMask[0]);
3991 } else {
3992 // The second half of indicies refer to the higher part, which is a
3993 // duplication of the lower one. This makes this shuffle a perfect match
3994 // for the VPERM instruction.
3995 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
3996 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
3997 V = DAG.getVectorShuffle(NVT, dl, V, DAG.getUNDEF(NVT), &SplatMask[0]);
3998 }
3999
4000 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4001}
4002
4003/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32 and
4004/// v8i32, v16i16 or v32i8 to v8f32.
4005static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4006 EVT SrcVT = SV->getValueType(0);
4007 SDValue V1 = SV->getOperand(0);
4008 DebugLoc dl = SV->getDebugLoc();
4009
4010 int EltNo = SV->getSplatIndex();
4011 int NumElems = SrcVT.getVectorNumElements();
4012 unsigned Size = SrcVT.getSizeInBits();
4013
4014 // Extract the 128-bit part containing the splat element and update
4015 // the splat element index when it refers to the higher register.
4016 if (Size == 256) {
4017 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4018 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4019 if (Idx > 0)
4020 EltNo -= NumElems/2;
4021 }
4022
4023 // Make this 128-bit vector duplicate i8 and i16 elements
4024 if (NumElems > 4)
4025 V1 = PromoteSplatv8v16(V1, DAG, EltNo);
4026
4027 // Recreate the 256-bit vector and place the same 128-bit vector
4028 // into the low and high part. This is necessary because we want
4029 // to use VPERM to shuffle the v8f32 vector, and VPERM only shuffles
4030 // inside each separate v4f32 lane.
4031 if (Size == 256) {
4032 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4033 DAG.getConstant(0, MVT::i32), DAG, dl);
4034 V1 = Insert128BitVector(InsV, V1,
4035 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4036 }
4037
4038 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004039}
4040
Evan Chengba05f722006-04-21 23:03:30 +00004041/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004042/// vector of zero or undef vector. This produces a shuffle where the low
4043/// element of V2 is swizzled into the zero/undef vector, landing at element
4044/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00004045static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00004046 bool isZero, bool HasSSE2,
4047 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004048 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004049 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00004050 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4051 unsigned NumElems = VT.getVectorNumElements();
4052 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004053 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004054 // If this is the insertion idx, put the low elt of V2 here.
4055 MaskVec.push_back(i == Idx ? NumElems : i);
4056 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004057}
4058
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004059/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4060/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004061static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4062 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004063 if (Depth == 6)
4064 return SDValue(); // Limit search depth.
4065
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004066 SDValue V = SDValue(N, 0);
4067 EVT VT = V.getValueType();
4068 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004069
4070 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4071 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4072 Index = SV->getMaskElt(Index);
4073
4074 if (Index < 0)
4075 return DAG.getUNDEF(VT.getVectorElementType());
4076
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004077 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004078 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004079 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004080 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004081
4082 // Recurse into target specific vector shuffles to find scalars.
4083 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004084 int NumElems = VT.getVectorNumElements();
4085 SmallVector<unsigned, 16> ShuffleMask;
4086 SDValue ImmN;
4087
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004088 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004089 case X86ISD::SHUFPS:
4090 case X86ISD::SHUFPD:
4091 ImmN = N->getOperand(N->getNumOperands()-1);
4092 DecodeSHUFPSMask(NumElems,
4093 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4094 ShuffleMask);
4095 break;
4096 case X86ISD::PUNPCKHBW:
4097 case X86ISD::PUNPCKHWD:
4098 case X86ISD::PUNPCKHDQ:
4099 case X86ISD::PUNPCKHQDQ:
4100 DecodePUNPCKHMask(NumElems, ShuffleMask);
4101 break;
4102 case X86ISD::UNPCKHPS:
4103 case X86ISD::UNPCKHPD:
4104 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4105 break;
4106 case X86ISD::PUNPCKLBW:
4107 case X86ISD::PUNPCKLWD:
4108 case X86ISD::PUNPCKLDQ:
4109 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004110 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004111 break;
4112 case X86ISD::UNPCKLPS:
4113 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004114 case X86ISD::VUNPCKLPS:
4115 case X86ISD::VUNPCKLPD:
4116 case X86ISD::VUNPCKLPSY:
4117 case X86ISD::VUNPCKLPDY:
4118 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004119 break;
4120 case X86ISD::MOVHLPS:
4121 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4122 break;
4123 case X86ISD::MOVLHPS:
4124 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4125 break;
4126 case X86ISD::PSHUFD:
4127 ImmN = N->getOperand(N->getNumOperands()-1);
4128 DecodePSHUFMask(NumElems,
4129 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4130 ShuffleMask);
4131 break;
4132 case X86ISD::PSHUFHW:
4133 ImmN = N->getOperand(N->getNumOperands()-1);
4134 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4135 ShuffleMask);
4136 break;
4137 case X86ISD::PSHUFLW:
4138 ImmN = N->getOperand(N->getNumOperands()-1);
4139 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4140 ShuffleMask);
4141 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004142 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004143 case X86ISD::MOVSD: {
4144 // The index 0 always comes from the first element of the second source,
4145 // this is why MOVSS and MOVSD are used in the first place. The other
4146 // elements come from the other positions of the first source vector.
4147 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004148 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4149 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004150 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004151 case X86ISD::VPERMIL:
4152 ImmN = N->getOperand(N->getNumOperands()-1);
4153 DecodeVPERMILMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4154 ShuffleMask);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004155 default:
4156 assert("not implemented for target shuffle node");
4157 return SDValue();
4158 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004159
4160 Index = ShuffleMask[Index];
4161 if (Index < 0)
4162 return DAG.getUNDEF(VT.getVectorElementType());
4163
4164 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4165 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4166 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004167 }
4168
4169 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004170 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004171 V = V.getOperand(0);
4172 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004173 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004174
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004175 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004176 return SDValue();
4177 }
4178
4179 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4180 return (Index == 0) ? V.getOperand(0)
4181 : DAG.getUNDEF(VT.getVectorElementType());
4182
4183 if (V.getOpcode() == ISD::BUILD_VECTOR)
4184 return V.getOperand(Index);
4185
4186 return SDValue();
4187}
4188
4189/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4190/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004191/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004192static
4193unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4194 bool ZerosFromLeft, SelectionDAG &DAG) {
4195 int i = 0;
4196
4197 while (i < NumElems) {
4198 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004199 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004200 if (!(Elt.getNode() &&
4201 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4202 break;
4203 ++i;
4204 }
4205
4206 return i;
4207}
4208
4209/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4210/// MaskE correspond consecutively to elements from one of the vector operands,
4211/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4212static
4213bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4214 int OpIdx, int NumElems, unsigned &OpNum) {
4215 bool SeenV1 = false;
4216 bool SeenV2 = false;
4217
4218 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4219 int Idx = SVOp->getMaskElt(i);
4220 // Ignore undef indicies
4221 if (Idx < 0)
4222 continue;
4223
4224 if (Idx < NumElems)
4225 SeenV1 = true;
4226 else
4227 SeenV2 = true;
4228
4229 // Only accept consecutive elements from the same vector
4230 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4231 return false;
4232 }
4233
4234 OpNum = SeenV1 ? 0 : 1;
4235 return true;
4236}
4237
4238/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4239/// logical left shift of a vector.
4240static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4241 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4242 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4243 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4244 false /* check zeros from right */, DAG);
4245 unsigned OpSrc;
4246
4247 if (!NumZeros)
4248 return false;
4249
4250 // Considering the elements in the mask that are not consecutive zeros,
4251 // check if they consecutively come from only one of the source vectors.
4252 //
4253 // V1 = {X, A, B, C} 0
4254 // \ \ \ /
4255 // vector_shuffle V1, V2 <1, 2, 3, X>
4256 //
4257 if (!isShuffleMaskConsecutive(SVOp,
4258 0, // Mask Start Index
4259 NumElems-NumZeros-1, // Mask End Index
4260 NumZeros, // Where to start looking in the src vector
4261 NumElems, // Number of elements in vector
4262 OpSrc)) // Which source operand ?
4263 return false;
4264
4265 isLeft = false;
4266 ShAmt = NumZeros;
4267 ShVal = SVOp->getOperand(OpSrc);
4268 return true;
4269}
4270
4271/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4272/// logical left shift of a vector.
4273static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4274 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4275 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4276 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4277 true /* check zeros from left */, DAG);
4278 unsigned OpSrc;
4279
4280 if (!NumZeros)
4281 return false;
4282
4283 // Considering the elements in the mask that are not consecutive zeros,
4284 // check if they consecutively come from only one of the source vectors.
4285 //
4286 // 0 { A, B, X, X } = V2
4287 // / \ / /
4288 // vector_shuffle V1, V2 <X, X, 4, 5>
4289 //
4290 if (!isShuffleMaskConsecutive(SVOp,
4291 NumZeros, // Mask Start Index
4292 NumElems-1, // Mask End Index
4293 0, // Where to start looking in the src vector
4294 NumElems, // Number of elements in vector
4295 OpSrc)) // Which source operand ?
4296 return false;
4297
4298 isLeft = true;
4299 ShAmt = NumZeros;
4300 ShVal = SVOp->getOperand(OpSrc);
4301 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004302}
4303
4304/// isVectorShift - Returns true if the shuffle can be implemented as a
4305/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004306static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004307 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004308 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4309 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4310 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004311
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004312 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004313}
4314
Evan Chengc78d3b42006-04-24 18:01:45 +00004315/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4316///
Dan Gohman475871a2008-07-27 21:46:04 +00004317static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004318 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004319 SelectionDAG &DAG,
4320 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004321 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004322 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004323
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004324 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004325 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004326 bool First = true;
4327 for (unsigned i = 0; i < 16; ++i) {
4328 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4329 if (ThisIsNonZero && First) {
4330 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004331 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004332 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004333 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004334 First = false;
4335 }
4336
4337 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004338 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004339 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4340 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004341 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004343 }
4344 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004345 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4346 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4347 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004348 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004349 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004350 } else
4351 ThisElt = LastElt;
4352
Gabor Greifba36cb52008-08-28 21:40:38 +00004353 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004354 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004355 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004356 }
4357 }
4358
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004359 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004360}
4361
Bill Wendlinga348c562007-03-22 18:42:45 +00004362/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004363///
Dan Gohman475871a2008-07-27 21:46:04 +00004364static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004365 unsigned NumNonZero, unsigned NumZero,
4366 SelectionDAG &DAG,
4367 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004368 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004369 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004370
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004371 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004372 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004373 bool First = true;
4374 for (unsigned i = 0; i < 8; ++i) {
4375 bool isNonZero = (NonZeros & (1 << i)) != 0;
4376 if (isNonZero) {
4377 if (First) {
4378 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004379 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004380 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004381 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004382 First = false;
4383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004384 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004385 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004386 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004387 }
4388 }
4389
4390 return V;
4391}
4392
Evan Chengf26ffe92008-05-29 08:22:04 +00004393/// getVShift - Return a vector logical shift node.
4394///
Owen Andersone50ed302009-08-10 22:56:29 +00004395static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 unsigned NumBits, SelectionDAG &DAG,
4397 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004398 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004399 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004400 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4401 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004402 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004403 DAG.getConstant(NumBits,
4404 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004405}
4406
Dan Gohman475871a2008-07-27 21:46:04 +00004407SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004408X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004409 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004410
Evan Chengc3630942009-12-09 21:00:30 +00004411 // Check if the scalar load can be widened into a vector load. And if
4412 // the address is "base + cst" see if the cst can be "absorbed" into
4413 // the shuffle mask.
4414 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4415 SDValue Ptr = LD->getBasePtr();
4416 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4417 return SDValue();
4418 EVT PVT = LD->getValueType(0);
4419 if (PVT != MVT::i32 && PVT != MVT::f32)
4420 return SDValue();
4421
4422 int FI = -1;
4423 int64_t Offset = 0;
4424 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4425 FI = FINode->getIndex();
4426 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004427 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004428 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4429 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4430 Offset = Ptr.getConstantOperandVal(1);
4431 Ptr = Ptr.getOperand(0);
4432 } else {
4433 return SDValue();
4434 }
4435
4436 SDValue Chain = LD->getChain();
4437 // Make sure the stack object alignment is at least 16.
4438 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4439 if (DAG.InferPtrAlignment(Ptr) < 16) {
4440 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004441 // Can't change the alignment. FIXME: It's possible to compute
4442 // the exact stack offset and reference FI + adjust offset instead.
4443 // If someone *really* cares about this. That's the way to implement it.
4444 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004445 } else {
4446 MFI->setObjectAlignment(FI, 16);
4447 }
4448 }
4449
4450 // (Offset % 16) must be multiple of 4. Then address is then
4451 // Ptr + (Offset & ~15).
4452 if (Offset < 0)
4453 return SDValue();
4454 if ((Offset % 16) & 3)
4455 return SDValue();
4456 int64_t StartOffset = Offset & ~15;
4457 if (StartOffset)
4458 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4459 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4460
4461 int EltNo = (Offset - StartOffset) >> 2;
4462 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4463 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004464 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4465 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004466 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004467 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004468 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4469 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004470 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004471 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004472 }
4473
4474 return SDValue();
4475}
4476
Michael J. Spencerec38de22010-10-10 22:04:20 +00004477/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4478/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004479/// load which has the same value as a build_vector whose operands are 'elts'.
4480///
4481/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004482///
Nate Begeman1449f292010-03-24 22:19:06 +00004483/// FIXME: we'd also like to handle the case where the last elements are zero
4484/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4485/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004486static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004487 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004488 EVT EltVT = VT.getVectorElementType();
4489 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004490
Nate Begemanfdea31a2010-03-24 20:49:50 +00004491 LoadSDNode *LDBase = NULL;
4492 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004493
Nate Begeman1449f292010-03-24 22:19:06 +00004494 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004495 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004496 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004497 for (unsigned i = 0; i < NumElems; ++i) {
4498 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004499
Nate Begemanfdea31a2010-03-24 20:49:50 +00004500 if (!Elt.getNode() ||
4501 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4502 return SDValue();
4503 if (!LDBase) {
4504 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4505 return SDValue();
4506 LDBase = cast<LoadSDNode>(Elt.getNode());
4507 LastLoadedElt = i;
4508 continue;
4509 }
4510 if (Elt.getOpcode() == ISD::UNDEF)
4511 continue;
4512
4513 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4514 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4515 return SDValue();
4516 LastLoadedElt = i;
4517 }
Nate Begeman1449f292010-03-24 22:19:06 +00004518
4519 // If we have found an entire vector of loads and undefs, then return a large
4520 // load of the entire vector width starting at the base pointer. If we found
4521 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004522 if (LastLoadedElt == NumElems - 1) {
4523 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004524 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004525 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004526 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004527 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004528 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004529 LDBase->isVolatile(), LDBase->isNonTemporal(),
4530 LDBase->getAlignment());
4531 } else if (NumElems == 4 && LastLoadedElt == 1) {
4532 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4533 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004534 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4535 Ops, 2, MVT::i32,
4536 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004537 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004538 }
4539 return SDValue();
4540}
4541
Evan Chengc3630942009-12-09 21:00:30 +00004542SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004543X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004544 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004545
David Greenef125a292011-02-08 19:04:41 +00004546 EVT VT = Op.getValueType();
4547 EVT ExtVT = VT.getVectorElementType();
4548
4549 unsigned NumElems = Op.getNumOperands();
4550
4551 // For AVX-length vectors, build the individual 128-bit pieces and
4552 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004553 if (VT.getSizeInBits() > 256 &&
4554 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004555 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4556 SmallVector<SDValue, 8> V;
4557 V.resize(NumElems);
4558 for (unsigned i = 0; i < NumElems; ++i) {
4559 V[i] = Op.getOperand(i);
4560 }
Owen Anderson95771af2011-02-25 21:41:48 +00004561
David Greenef125a292011-02-08 19:04:41 +00004562 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4563
4564 // Build the lower subvector.
4565 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4566 // Build the upper subvector.
4567 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4568 NumElems/2);
4569
4570 return ConcatVectors(Lower, Upper, DAG);
4571 }
4572
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004573 // All zero's:
4574 // - pxor (SSE2), xorps (SSE1), vpxor (128 AVX), xorp[s|d] (256 AVX)
4575 // All one's:
4576 // - pcmpeqd (SSE2 and 128 AVX), fallback to constant pools (256 AVX)
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004577 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004578 ISD::isBuildVectorAllOnes(Op.getNode())) {
4579 // Canonicalize this to <4 x i32> or <8 x 32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004580 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4581 // eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004582 if (Op.getValueType() == MVT::v4i32 ||
4583 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004584 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004585
Gabor Greifba36cb52008-08-28 21:40:38 +00004586 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004587 return getOnesVector(Op.getValueType(), DAG, dl);
4588 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004589 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004590
Owen Andersone50ed302009-08-10 22:56:29 +00004591 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592
Evan Cheng0db9fe62006-04-25 20:13:52 +00004593 unsigned NumZero = 0;
4594 unsigned NumNonZero = 0;
4595 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004596 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004597 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004599 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004600 if (Elt.getOpcode() == ISD::UNDEF)
4601 continue;
4602 Values.insert(Elt);
4603 if (Elt.getOpcode() != ISD::Constant &&
4604 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004605 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004606 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004607 NumZero++;
4608 else {
4609 NonZeros |= (1 << i);
4610 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004611 }
4612 }
4613
Chris Lattner97a2a562010-08-26 05:24:29 +00004614 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4615 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004616 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004617
Chris Lattner67f453a2008-03-09 05:42:06 +00004618 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004619 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004620 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004621 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004622
Chris Lattner62098042008-03-09 01:05:04 +00004623 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4624 // the value are obviously zero, truncate the value to i32 and do the
4625 // insertion that way. Only do this if the value is non-constant or if the
4626 // value is a constant being inserted into element 0. It is cheaper to do
4627 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004628 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004629 (!IsAllConstants || Idx == 0)) {
4630 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004631 // Handle SSE only.
4632 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4633 EVT VecVT = MVT::v4i32;
4634 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004635
Chris Lattner62098042008-03-09 01:05:04 +00004636 // Truncate the value (which may itself be a constant) to i32, and
4637 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004638 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004639 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004640 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4641 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004642
Chris Lattner62098042008-03-09 01:05:04 +00004643 // Now we have our 32-bit value zero extended in the low element of
4644 // a vector. If Idx != 0, swizzle it into place.
4645 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 SmallVector<int, 4> Mask;
4647 Mask.push_back(Idx);
4648 for (unsigned i = 1; i != VecElts; ++i)
4649 Mask.push_back(i);
4650 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004651 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004652 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004653 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004654 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004655 }
4656 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004657
Chris Lattner19f79692008-03-08 22:59:52 +00004658 // If we have a constant or non-constant insertion into the low element of
4659 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4660 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004661 // depending on what the source datatype is.
4662 if (Idx == 0) {
4663 if (NumZero == 0) {
4664 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004665 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4666 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004667 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4668 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4669 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4670 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004671 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4672 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004673 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4674 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004675 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4676 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4677 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004678 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004679 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004680 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004681
4682 // Is it a vector logical left shift?
4683 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004684 X86::isZeroNode(Op.getOperand(0)) &&
4685 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004686 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004687 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004688 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004689 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004690 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004691 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004692
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004693 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004694 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004695
Chris Lattner19f79692008-03-08 22:59:52 +00004696 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4697 // is a non-constant being inserted into an element other than the low one,
4698 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4699 // movd/movss) to move this into the low element, then shuffle it into
4700 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004701 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004702 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004703
Evan Cheng0db9fe62006-04-25 20:13:52 +00004704 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004705 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4706 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004707 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004708 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004709 MaskVec.push_back(i == Idx ? 0 : 1);
4710 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004711 }
4712 }
4713
Chris Lattner67f453a2008-03-09 05:42:06 +00004714 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004715 if (Values.size() == 1) {
4716 if (EVTBits == 32) {
4717 // Instead of a shuffle like this:
4718 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4719 // Check if it's possible to issue this instead.
4720 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4721 unsigned Idx = CountTrailingZeros_32(NonZeros);
4722 SDValue Item = Op.getOperand(Idx);
4723 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4724 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4725 }
Dan Gohman475871a2008-07-27 21:46:04 +00004726 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004727 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004728
Dan Gohmana3941172007-07-24 22:55:08 +00004729 // A vector full of immediates; various special cases are already
4730 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004731 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004732 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004733
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004734 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004735 if (EVTBits == 64) {
4736 if (NumNonZero == 1) {
4737 // One half is zero or undef.
4738 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004739 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004740 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004741 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4742 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004743 }
Dan Gohman475871a2008-07-27 21:46:04 +00004744 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004745 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746
4747 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004748 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004749 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004750 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004751 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004752 }
4753
Bill Wendling826f36f2007-03-28 00:57:11 +00004754 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004755 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004756 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004757 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004758 }
4759
4760 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004761 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004762 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004763 if (NumElems == 4 && NumZero > 0) {
4764 for (unsigned i = 0; i < 4; ++i) {
4765 bool isZero = !(NonZeros & (1 << i));
4766 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004767 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004768 else
Dale Johannesenace16102009-02-03 19:33:06 +00004769 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004770 }
4771
4772 for (unsigned i = 0; i < 2; ++i) {
4773 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4774 default: break;
4775 case 0:
4776 V[i] = V[i*2]; // Must be a zero vector.
4777 break;
4778 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004779 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004780 break;
4781 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004782 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004783 break;
4784 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004785 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004786 break;
4787 }
4788 }
4789
Nate Begeman9008ca62009-04-27 18:41:29 +00004790 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004791 bool Reverse = (NonZeros & 0x3) == 2;
4792 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004793 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004794 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4795 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004796 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4797 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004798 }
4799
Nate Begemanfdea31a2010-03-24 20:49:50 +00004800 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4801 // Check for a build vector of consecutive loads.
4802 for (unsigned i = 0; i < NumElems; ++i)
4803 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004804
Nate Begemanfdea31a2010-03-24 20:49:50 +00004805 // Check for elements which are consecutive loads.
4806 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4807 if (LD.getNode())
4808 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004809
4810 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004811 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004812 SDValue Result;
4813 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4814 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4815 else
4816 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004817
Chris Lattner24faf612010-08-28 17:59:08 +00004818 for (unsigned i = 1; i < NumElems; ++i) {
4819 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4820 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004821 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004822 }
4823 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004824 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004825
Chris Lattner6e80e442010-08-28 17:15:43 +00004826 // Otherwise, expand into a number of unpckl*, start by extending each of
4827 // our (non-undef) elements to the full vector width with the element in the
4828 // bottom slot of the vector (which generates no code for SSE).
4829 for (unsigned i = 0; i < NumElems; ++i) {
4830 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4831 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4832 else
4833 V[i] = DAG.getUNDEF(VT);
4834 }
4835
4836 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004837 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4838 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4839 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004840 unsigned EltStride = NumElems >> 1;
4841 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004842 for (unsigned i = 0; i < EltStride; ++i) {
4843 // If V[i+EltStride] is undef and this is the first round of mixing,
4844 // then it is safe to just drop this shuffle: V[i] is already in the
4845 // right place, the one element (since it's the first round) being
4846 // inserted as undef can be dropped. This isn't safe for successive
4847 // rounds because they will permute elements within both vectors.
4848 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4849 EltStride == NumElems/2)
4850 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004851
Chris Lattner6e80e442010-08-28 17:15:43 +00004852 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004853 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004854 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004855 }
4856 return V[0];
4857 }
Dan Gohman475871a2008-07-27 21:46:04 +00004858 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004859}
4860
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004861SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004862X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004863 // We support concatenate two MMX registers and place them in a MMX
4864 // register. This is better than doing a stack convert.
4865 DebugLoc dl = Op.getDebugLoc();
4866 EVT ResVT = Op.getValueType();
4867 assert(Op.getNumOperands() == 2);
4868 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4869 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4870 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004871 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004872 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4873 InVec = Op.getOperand(1);
4874 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4875 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004876 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004877 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4878 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4879 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004880 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004881 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4882 Mask[0] = 0; Mask[1] = 2;
4883 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4884 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004885 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004886}
4887
Nate Begemanb9a47b82009-02-23 08:49:38 +00004888// v8i16 shuffles - Prefer shuffles in the following order:
4889// 1. [all] pshuflw, pshufhw, optional move
4890// 2. [ssse3] 1 x pshufb
4891// 3. [ssse3] 2 x pshufb + 1 x por
4892// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004893SDValue
4894X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4895 SelectionDAG &DAG) const {
4896 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004897 SDValue V1 = SVOp->getOperand(0);
4898 SDValue V2 = SVOp->getOperand(1);
4899 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004900 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004901
Nate Begemanb9a47b82009-02-23 08:49:38 +00004902 // Determine if more than 1 of the words in each of the low and high quadwords
4903 // of the result come from the same quadword of one of the two inputs. Undef
4904 // mask values count as coming from any quadword, for better codegen.
4905 SmallVector<unsigned, 4> LoQuad(4);
4906 SmallVector<unsigned, 4> HiQuad(4);
4907 BitVector InputQuads(4);
4908 for (unsigned i = 0; i < 8; ++i) {
4909 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004910 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004911 MaskVals.push_back(EltIdx);
4912 if (EltIdx < 0) {
4913 ++Quad[0];
4914 ++Quad[1];
4915 ++Quad[2];
4916 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004917 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004918 }
4919 ++Quad[EltIdx / 4];
4920 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004921 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004922
Nate Begemanb9a47b82009-02-23 08:49:38 +00004923 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004924 unsigned MaxQuad = 1;
4925 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004926 if (LoQuad[i] > MaxQuad) {
4927 BestLoQuad = i;
4928 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004929 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004930 }
4931
Nate Begemanb9a47b82009-02-23 08:49:38 +00004932 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004933 MaxQuad = 1;
4934 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004935 if (HiQuad[i] > MaxQuad) {
4936 BestHiQuad = i;
4937 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004938 }
4939 }
4940
Nate Begemanb9a47b82009-02-23 08:49:38 +00004941 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004942 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004943 // single pshufb instruction is necessary. If There are more than 2 input
4944 // quads, disable the next transformation since it does not help SSSE3.
4945 bool V1Used = InputQuads[0] || InputQuads[1];
4946 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004947 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004948 if (InputQuads.count() == 2 && V1Used && V2Used) {
4949 BestLoQuad = InputQuads.find_first();
4950 BestHiQuad = InputQuads.find_next(BestLoQuad);
4951 }
4952 if (InputQuads.count() > 2) {
4953 BestLoQuad = -1;
4954 BestHiQuad = -1;
4955 }
4956 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004957
Nate Begemanb9a47b82009-02-23 08:49:38 +00004958 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4959 // the shuffle mask. If a quad is scored as -1, that means that it contains
4960 // words from all 4 input quadwords.
4961 SDValue NewV;
4962 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004963 SmallVector<int, 8> MaskV;
4964 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4965 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004966 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004967 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4968 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4969 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004970
Nate Begemanb9a47b82009-02-23 08:49:38 +00004971 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4972 // source words for the shuffle, to aid later transformations.
4973 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004974 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004975 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004976 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004977 if (idx != (int)i)
4978 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004979 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004980 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004981 AllWordsInNewV = false;
4982 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004983 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004984
Nate Begemanb9a47b82009-02-23 08:49:38 +00004985 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4986 if (AllWordsInNewV) {
4987 for (int i = 0; i != 8; ++i) {
4988 int idx = MaskVals[i];
4989 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004990 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004991 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004992 if ((idx != i) && idx < 4)
4993 pshufhw = false;
4994 if ((idx != i) && idx > 3)
4995 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004996 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004997 V1 = NewV;
4998 V2Used = false;
4999 BestLoQuad = 0;
5000 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005001 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005002
Nate Begemanb9a47b82009-02-23 08:49:38 +00005003 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5004 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005005 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005006 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5007 unsigned TargetMask = 0;
5008 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005009 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005010 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5011 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5012 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005013 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005014 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005015 }
Eric Christopherfd179292009-08-27 18:07:15 +00005016
Nate Begemanb9a47b82009-02-23 08:49:38 +00005017 // If we have SSSE3, and all words of the result are from 1 input vector,
5018 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5019 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005020 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005021 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005022
Nate Begemanb9a47b82009-02-23 08:49:38 +00005023 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005024 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005025 // mask, and elements that come from V1 in the V2 mask, so that the two
5026 // results can be OR'd together.
5027 bool TwoInputs = V1Used && V2Used;
5028 for (unsigned i = 0; i != 8; ++i) {
5029 int EltIdx = MaskVals[i] * 2;
5030 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005031 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5032 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 continue;
5034 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005035 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5036 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005037 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005038 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005039 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005040 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005041 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005042 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005043 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005044
Nate Begemanb9a47b82009-02-23 08:49:38 +00005045 // Calculate the shuffle mask for the second input, shuffle it, and
5046 // OR it with the first shuffled input.
5047 pshufbMask.clear();
5048 for (unsigned i = 0; i != 8; ++i) {
5049 int EltIdx = MaskVals[i] * 2;
5050 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005051 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5052 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005053 continue;
5054 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005055 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5056 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005057 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005058 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005059 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005060 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005061 MVT::v16i8, &pshufbMask[0], 16));
5062 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005063 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005064 }
5065
5066 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5067 // and update MaskVals with new element order.
5068 BitVector InOrder(8);
5069 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005070 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005071 for (int i = 0; i != 4; ++i) {
5072 int idx = MaskVals[i];
5073 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005074 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005075 InOrder.set(i);
5076 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005077 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005078 InOrder.set(i);
5079 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005080 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005081 }
5082 }
5083 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005084 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005085 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005086 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005087
5088 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5089 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5090 NewV.getOperand(0),
5091 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5092 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005093 }
Eric Christopherfd179292009-08-27 18:07:15 +00005094
Nate Begemanb9a47b82009-02-23 08:49:38 +00005095 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5096 // and update MaskVals with the new element order.
5097 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005098 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005099 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005100 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005101 for (unsigned i = 4; i != 8; ++i) {
5102 int idx = MaskVals[i];
5103 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005104 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005105 InOrder.set(i);
5106 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005107 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005108 InOrder.set(i);
5109 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005110 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005111 }
5112 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005113 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005114 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005115
5116 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5117 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5118 NewV.getOperand(0),
5119 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5120 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005121 }
Eric Christopherfd179292009-08-27 18:07:15 +00005122
Nate Begemanb9a47b82009-02-23 08:49:38 +00005123 // In case BestHi & BestLo were both -1, which means each quadword has a word
5124 // from each of the four input quadwords, calculate the InOrder bitvector now
5125 // before falling through to the insert/extract cleanup.
5126 if (BestLoQuad == -1 && BestHiQuad == -1) {
5127 NewV = V1;
5128 for (int i = 0; i != 8; ++i)
5129 if (MaskVals[i] < 0 || MaskVals[i] == i)
5130 InOrder.set(i);
5131 }
Eric Christopherfd179292009-08-27 18:07:15 +00005132
Nate Begemanb9a47b82009-02-23 08:49:38 +00005133 // The other elements are put in the right place using pextrw and pinsrw.
5134 for (unsigned i = 0; i != 8; ++i) {
5135 if (InOrder[i])
5136 continue;
5137 int EltIdx = MaskVals[i];
5138 if (EltIdx < 0)
5139 continue;
5140 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005141 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005142 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005144 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005145 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005146 DAG.getIntPtrConstant(i));
5147 }
5148 return NewV;
5149}
5150
5151// v16i8 shuffles - Prefer shuffles in the following order:
5152// 1. [ssse3] 1 x pshufb
5153// 2. [ssse3] 2 x pshufb + 1 x por
5154// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5155static
Nate Begeman9008ca62009-04-27 18:41:29 +00005156SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005157 SelectionDAG &DAG,
5158 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005159 SDValue V1 = SVOp->getOperand(0);
5160 SDValue V2 = SVOp->getOperand(1);
5161 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005162 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005163 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005164
Nate Begemanb9a47b82009-02-23 08:49:38 +00005165 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005166 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005167 // present, fall back to case 3.
5168 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5169 bool V1Only = true;
5170 bool V2Only = true;
5171 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005172 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005173 if (EltIdx < 0)
5174 continue;
5175 if (EltIdx < 16)
5176 V2Only = false;
5177 else
5178 V1Only = false;
5179 }
Eric Christopherfd179292009-08-27 18:07:15 +00005180
Nate Begemanb9a47b82009-02-23 08:49:38 +00005181 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5182 if (TLI.getSubtarget()->hasSSSE3()) {
5183 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005184
Nate Begemanb9a47b82009-02-23 08:49:38 +00005185 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005186 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005187 //
5188 // Otherwise, we have elements from both input vectors, and must zero out
5189 // elements that come from V2 in the first mask, and V1 in the second mask
5190 // so that we can OR them together.
5191 bool TwoInputs = !(V1Only || V2Only);
5192 for (unsigned i = 0; i != 16; ++i) {
5193 int EltIdx = MaskVals[i];
5194 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005195 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005196 continue;
5197 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005198 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005199 }
5200 // If all the elements are from V2, assign it to V1 and return after
5201 // building the first pshufb.
5202 if (V2Only)
5203 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005204 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005205 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005206 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005207 if (!TwoInputs)
5208 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005209
Nate Begemanb9a47b82009-02-23 08:49:38 +00005210 // Calculate the shuffle mask for the second input, shuffle it, and
5211 // OR it with the first shuffled input.
5212 pshufbMask.clear();
5213 for (unsigned i = 0; i != 16; ++i) {
5214 int EltIdx = MaskVals[i];
5215 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005216 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005217 continue;
5218 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005219 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005220 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005221 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005222 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005223 MVT::v16i8, &pshufbMask[0], 16));
5224 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005225 }
Eric Christopherfd179292009-08-27 18:07:15 +00005226
Nate Begemanb9a47b82009-02-23 08:49:38 +00005227 // No SSSE3 - Calculate in place words and then fix all out of place words
5228 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5229 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005230 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5231 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005232 SDValue NewV = V2Only ? V2 : V1;
5233 for (int i = 0; i != 8; ++i) {
5234 int Elt0 = MaskVals[i*2];
5235 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005236
Nate Begemanb9a47b82009-02-23 08:49:38 +00005237 // This word of the result is all undef, skip it.
5238 if (Elt0 < 0 && Elt1 < 0)
5239 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005240
Nate Begemanb9a47b82009-02-23 08:49:38 +00005241 // This word of the result is already in the correct place, skip it.
5242 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5243 continue;
5244 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5245 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005246
Nate Begemanb9a47b82009-02-23 08:49:38 +00005247 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5248 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5249 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005250
5251 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5252 // using a single extract together, load it and store it.
5253 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005254 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005255 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005256 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005257 DAG.getIntPtrConstant(i));
5258 continue;
5259 }
5260
Nate Begemanb9a47b82009-02-23 08:49:38 +00005261 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005262 // source byte is not also odd, shift the extracted word left 8 bits
5263 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005264 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005265 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005266 DAG.getIntPtrConstant(Elt1 / 2));
5267 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005268 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005269 DAG.getConstant(8,
5270 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005271 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005272 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5273 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005274 }
5275 // If Elt0 is defined, extract it from the appropriate source. If the
5276 // source byte is not also even, shift the extracted word right 8 bits. If
5277 // Elt1 was also defined, OR the extracted values together before
5278 // inserting them in the result.
5279 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005280 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005281 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5282 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005283 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005284 DAG.getConstant(8,
5285 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005286 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005287 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5288 DAG.getConstant(0x00FF, MVT::i16));
5289 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005290 : InsElt0;
5291 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005292 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005293 DAG.getIntPtrConstant(i));
5294 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005295 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005296}
5297
Evan Cheng7a831ce2007-12-15 03:00:47 +00005298/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005299/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005300/// done when every pair / quad of shuffle mask elements point to elements in
5301/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005302/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005303static
Nate Begeman9008ca62009-04-27 18:41:29 +00005304SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005305 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005306 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005307 SDValue V1 = SVOp->getOperand(0);
5308 SDValue V2 = SVOp->getOperand(1);
5309 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005310 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005311 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005312 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005313 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005314 case MVT::v4f32: NewVT = MVT::v2f64; break;
5315 case MVT::v4i32: NewVT = MVT::v2i64; break;
5316 case MVT::v8i16: NewVT = MVT::v4i32; break;
5317 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005318 }
5319
Nate Begeman9008ca62009-04-27 18:41:29 +00005320 int Scale = NumElems / NewWidth;
5321 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005322 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005323 int StartIdx = -1;
5324 for (int j = 0; j < Scale; ++j) {
5325 int EltIdx = SVOp->getMaskElt(i+j);
5326 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005327 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005328 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005329 StartIdx = EltIdx - (EltIdx % Scale);
5330 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005331 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005332 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005333 if (StartIdx == -1)
5334 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005335 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005336 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005337 }
5338
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005339 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5340 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005341 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005342}
5343
Evan Chengd880b972008-05-09 21:53:03 +00005344/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005345///
Owen Andersone50ed302009-08-10 22:56:29 +00005346static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005347 SDValue SrcOp, SelectionDAG &DAG,
5348 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005349 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005350 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005351 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005352 LD = dyn_cast<LoadSDNode>(SrcOp);
5353 if (!LD) {
5354 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5355 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005356 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005357 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005358 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005359 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005360 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005361 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005362 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005363 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005364 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5365 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5366 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005367 SrcOp.getOperand(0)
5368 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005369 }
5370 }
5371 }
5372
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005373 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005374 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005375 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005376 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005377}
5378
Evan Chengace3c172008-07-22 21:13:36 +00005379/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5380/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005381static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005382LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5383 SDValue V1 = SVOp->getOperand(0);
5384 SDValue V2 = SVOp->getOperand(1);
5385 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005386 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005387
Evan Chengace3c172008-07-22 21:13:36 +00005388 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005389 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005390 SmallVector<int, 8> Mask1(4U, -1);
5391 SmallVector<int, 8> PermMask;
5392 SVOp->getMask(PermMask);
5393
Evan Chengace3c172008-07-22 21:13:36 +00005394 unsigned NumHi = 0;
5395 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005396 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005397 int Idx = PermMask[i];
5398 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005399 Locs[i] = std::make_pair(-1, -1);
5400 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005401 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5402 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005403 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005404 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005405 NumLo++;
5406 } else {
5407 Locs[i] = std::make_pair(1, NumHi);
5408 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005409 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005410 NumHi++;
5411 }
5412 }
5413 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005414
Evan Chengace3c172008-07-22 21:13:36 +00005415 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005416 // If no more than two elements come from either vector. This can be
5417 // implemented with two shuffles. First shuffle gather the elements.
5418 // The second shuffle, which takes the first shuffle as both of its
5419 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005420 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005421
Nate Begeman9008ca62009-04-27 18:41:29 +00005422 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005423
Evan Chengace3c172008-07-22 21:13:36 +00005424 for (unsigned i = 0; i != 4; ++i) {
5425 if (Locs[i].first == -1)
5426 continue;
5427 else {
5428 unsigned Idx = (i < 2) ? 0 : 4;
5429 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005430 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005431 }
5432 }
5433
Nate Begeman9008ca62009-04-27 18:41:29 +00005434 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005435 } else if (NumLo == 3 || NumHi == 3) {
5436 // Otherwise, we must have three elements from one vector, call it X, and
5437 // one element from the other, call it Y. First, use a shufps to build an
5438 // intermediate vector with the one element from Y and the element from X
5439 // that will be in the same half in the final destination (the indexes don't
5440 // matter). Then, use a shufps to build the final vector, taking the half
5441 // containing the element from Y from the intermediate, and the other half
5442 // from X.
5443 if (NumHi == 3) {
5444 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005445 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005446 std::swap(V1, V2);
5447 }
5448
5449 // Find the element from V2.
5450 unsigned HiIndex;
5451 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005452 int Val = PermMask[HiIndex];
5453 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005454 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005455 if (Val >= 4)
5456 break;
5457 }
5458
Nate Begeman9008ca62009-04-27 18:41:29 +00005459 Mask1[0] = PermMask[HiIndex];
5460 Mask1[1] = -1;
5461 Mask1[2] = PermMask[HiIndex^1];
5462 Mask1[3] = -1;
5463 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005464
5465 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005466 Mask1[0] = PermMask[0];
5467 Mask1[1] = PermMask[1];
5468 Mask1[2] = HiIndex & 1 ? 6 : 4;
5469 Mask1[3] = HiIndex & 1 ? 4 : 6;
5470 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005471 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005472 Mask1[0] = HiIndex & 1 ? 2 : 0;
5473 Mask1[1] = HiIndex & 1 ? 0 : 2;
5474 Mask1[2] = PermMask[2];
5475 Mask1[3] = PermMask[3];
5476 if (Mask1[2] >= 0)
5477 Mask1[2] += 4;
5478 if (Mask1[3] >= 0)
5479 Mask1[3] += 4;
5480 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005481 }
Evan Chengace3c172008-07-22 21:13:36 +00005482 }
5483
5484 // Break it into (shuffle shuffle_hi, shuffle_lo).
5485 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005486 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005487 SmallVector<int,8> LoMask(4U, -1);
5488 SmallVector<int,8> HiMask(4U, -1);
5489
5490 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005491 unsigned MaskIdx = 0;
5492 unsigned LoIdx = 0;
5493 unsigned HiIdx = 2;
5494 for (unsigned i = 0; i != 4; ++i) {
5495 if (i == 2) {
5496 MaskPtr = &HiMask;
5497 MaskIdx = 1;
5498 LoIdx = 0;
5499 HiIdx = 2;
5500 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005501 int Idx = PermMask[i];
5502 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005503 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005504 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005505 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005506 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005507 LoIdx++;
5508 } else {
5509 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005510 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005511 HiIdx++;
5512 }
5513 }
5514
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5516 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5517 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005518 for (unsigned i = 0; i != 4; ++i) {
5519 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005520 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005521 } else {
5522 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005523 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005524 }
5525 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005526 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005527}
5528
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005529static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005530 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005531 V = V.getOperand(0);
5532 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5533 V = V.getOperand(0);
5534 if (MayFoldLoad(V))
5535 return true;
5536 return false;
5537}
5538
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005539// FIXME: the version above should always be used. Since there's
5540// a bug where several vector shuffles can't be folded because the
5541// DAG is not updated during lowering and a node claims to have two
5542// uses while it only has one, use this version, and let isel match
5543// another instruction if the load really happens to have more than
5544// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005545// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005546static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005547 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005548 V = V.getOperand(0);
5549 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5550 V = V.getOperand(0);
5551 if (ISD::isNormalLoad(V.getNode()))
5552 return true;
5553 return false;
5554}
5555
5556/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5557/// a vector extract, and if both can be later optimized into a single load.
5558/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5559/// here because otherwise a target specific shuffle node is going to be
5560/// emitted for this shuffle, and the optimization not done.
5561/// FIXME: This is probably not the best approach, but fix the problem
5562/// until the right path is decided.
5563static
5564bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5565 const TargetLowering &TLI) {
5566 EVT VT = V.getValueType();
5567 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5568
5569 // Be sure that the vector shuffle is present in a pattern like this:
5570 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5571 if (!V.hasOneUse())
5572 return false;
5573
5574 SDNode *N = *V.getNode()->use_begin();
5575 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5576 return false;
5577
5578 SDValue EltNo = N->getOperand(1);
5579 if (!isa<ConstantSDNode>(EltNo))
5580 return false;
5581
5582 // If the bit convert changed the number of elements, it is unsafe
5583 // to examine the mask.
5584 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005585 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005586 EVT SrcVT = V.getOperand(0).getValueType();
5587 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5588 return false;
5589 V = V.getOperand(0);
5590 HasShuffleIntoBitcast = true;
5591 }
5592
5593 // Select the input vector, guarding against out of range extract vector.
5594 unsigned NumElems = VT.getVectorNumElements();
5595 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5596 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5597 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5598
5599 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005600 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005601 V = V.getOperand(0);
5602
5603 if (ISD::isNormalLoad(V.getNode())) {
5604 // Is the original load suitable?
5605 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5606
5607 // FIXME: avoid the multi-use bug that is preventing lots of
5608 // of foldings to be detected, this is still wrong of course, but
5609 // give the temporary desired behavior, and if it happens that
5610 // the load has real more uses, during isel it will not fold, and
5611 // will generate poor code.
5612 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5613 return false;
5614
5615 if (!HasShuffleIntoBitcast)
5616 return true;
5617
5618 // If there's a bitcast before the shuffle, check if the load type and
5619 // alignment is valid.
5620 unsigned Align = LN0->getAlignment();
5621 unsigned NewAlign =
5622 TLI.getTargetData()->getABITypeAlignment(
5623 VT.getTypeForEVT(*DAG.getContext()));
5624
5625 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5626 return false;
5627 }
5628
5629 return true;
5630}
5631
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005632static
Evan Cheng835580f2010-10-07 20:50:20 +00005633SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5634 EVT VT = Op.getValueType();
5635
5636 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005637 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5638 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005639 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5640 V1, DAG));
5641}
5642
5643static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005644SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5645 bool HasSSE2) {
5646 SDValue V1 = Op.getOperand(0);
5647 SDValue V2 = Op.getOperand(1);
5648 EVT VT = Op.getValueType();
5649
5650 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5651
5652 if (HasSSE2 && VT == MVT::v2f64)
5653 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5654
5655 // v4f32 or v4i32
5656 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5657}
5658
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005659static
5660SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5661 SDValue V1 = Op.getOperand(0);
5662 SDValue V2 = Op.getOperand(1);
5663 EVT VT = Op.getValueType();
5664
5665 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5666 "unsupported shuffle type");
5667
5668 if (V2.getOpcode() == ISD::UNDEF)
5669 V2 = V1;
5670
5671 // v4i32 or v4f32
5672 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5673}
5674
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005675static
5676SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5677 SDValue V1 = Op.getOperand(0);
5678 SDValue V2 = Op.getOperand(1);
5679 EVT VT = Op.getValueType();
5680 unsigned NumElems = VT.getVectorNumElements();
5681
5682 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5683 // operand of these instructions is only memory, so check if there's a
5684 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5685 // same masks.
5686 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005687
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005688 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005689 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005690 CanFoldLoad = true;
5691
5692 // When V1 is a load, it can be folded later into a store in isel, example:
5693 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5694 // turns into:
5695 // (MOVLPSmr addr:$src1, VR128:$src2)
5696 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005697 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005698 CanFoldLoad = true;
5699
Eric Christopher893a8822011-02-20 05:04:42 +00005700 // Both of them can't be memory operations though.
5701 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5702 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005703
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005704 if (CanFoldLoad) {
5705 if (HasSSE2 && NumElems == 2)
5706 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5707
5708 if (NumElems == 4)
5709 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5710 }
5711
5712 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5713 // movl and movlp will both match v2i64, but v2i64 is never matched by
5714 // movl earlier because we make it strict to avoid messing with the movlp load
5715 // folding logic (see the code above getMOVLP call). Match it here then,
5716 // this is horrible, but will stay like this until we move all shuffle
5717 // matching to x86 specific nodes. Note that for the 1st condition all
5718 // types are matched with movsd.
5719 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5720 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5721 else if (HasSSE2)
5722 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5723
5724
5725 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5726
5727 // Invert the operand order and use SHUFPS to match it.
5728 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5729 X86::getShuffleSHUFImmediate(SVOp), DAG);
5730}
5731
David Greenec4db4e52011-02-28 19:06:56 +00005732static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005733 switch(VT.getSimpleVT().SimpleTy) {
5734 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5735 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005736 case MVT::v4f32:
5737 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5738 case MVT::v2f64:
5739 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5740 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5741 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005742 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5743 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5744 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005745 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005746 }
5747 return 0;
5748}
5749
5750static inline unsigned getUNPCKHOpcode(EVT VT) {
5751 switch(VT.getSimpleVT().SimpleTy) {
5752 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5753 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5754 case MVT::v4f32: return X86ISD::UNPCKHPS;
5755 case MVT::v2f64: return X86ISD::UNPCKHPD;
5756 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5757 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5758 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005759 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005760 }
5761 return 0;
5762}
5763
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005764static
5765SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005766 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005767 const X86Subtarget *Subtarget) {
5768 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5769 EVT VT = Op.getValueType();
5770 DebugLoc dl = Op.getDebugLoc();
5771 SDValue V1 = Op.getOperand(0);
5772 SDValue V2 = Op.getOperand(1);
5773
5774 if (isZeroShuffle(SVOp))
5775 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5776
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005777 // Handle splat operations
5778 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00005779 unsigned NumElem = VT.getVectorNumElements();
5780 // Special case, this is the only place now where it's allowed to return
5781 // a vector_shuffle operation without using a target specific node, because
5782 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
5783 // this be moved to DAGCombine instead?
5784 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005785 return Op;
5786
5787 // Handle splats by matching through known masks
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00005788 if ((VT.is128BitVector() && NumElem <= 4) ||
5789 (VT.is256BitVector() && NumElem <= 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005790 return SDValue();
5791
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00005792 // All i16 and i8 vector types can't be used directly by a generic shuffle
5793 // instruction because the target has no such instruction. Generate shuffles
5794 // which repeat i16 and i8 several times until they fit in i32, and then can
5795 // be manipulated by target suported shuffles. After the insertion of the
5796 // necessary shuffles, the result is bitcasted back to v4f32 or v8f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005797 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005798 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005799
5800 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5801 // do it!
5802 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5803 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5804 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005805 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005806 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5807 // FIXME: Figure out a cleaner way to do this.
5808 // Try to make use of movq to zero out the top part.
5809 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5810 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5811 if (NewOp.getNode()) {
5812 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5813 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5814 DAG, Subtarget, dl);
5815 }
5816 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5817 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5818 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5819 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5820 DAG, Subtarget, dl);
5821 }
5822 }
5823 return SDValue();
5824}
5825
Dan Gohman475871a2008-07-27 21:46:04 +00005826SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005827X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005828 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005829 SDValue V1 = Op.getOperand(0);
5830 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005831 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005832 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005833 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005834 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005835 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5836 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005837 bool V1IsSplat = false;
5838 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005839 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005840 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005841 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005842 MachineFunction &MF = DAG.getMachineFunction();
5843 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005844
Dale Johannesen0488fb62010-09-30 23:57:10 +00005845 // Shuffle operations on MMX not supported.
5846 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005847 return Op;
5848
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005849 // Vector shuffle lowering takes 3 steps:
5850 //
5851 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5852 // narrowing and commutation of operands should be handled.
5853 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5854 // shuffle nodes.
5855 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5856 // so the shuffle can be broken into other shuffles and the legalizer can
5857 // try the lowering again.
5858 //
5859 // The general ideia is that no vector_shuffle operation should be left to
5860 // be matched during isel, all of them must be converted to a target specific
5861 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005862
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005863 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5864 // narrowing and commutation of operands should be handled. The actual code
5865 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005866 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005867 if (NewOp.getNode())
5868 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005869
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005870 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5871 // unpckh_undef). Only use pshufd if speed is more important than size.
5872 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5873 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005874 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005875 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5876 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5877 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005878
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005879 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005880 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005881 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005882
Dale Johannesen0488fb62010-09-30 23:57:10 +00005883 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005884 return getMOVHighToLow(Op, dl, DAG);
5885
5886 // Use to match splats
5887 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5888 (VT == MVT::v2f64 || VT == MVT::v2i64))
5889 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5890
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005891 if (X86::isPSHUFDMask(SVOp)) {
5892 // The actual implementation will match the mask in the if above and then
5893 // during isel it can match several different instructions, not only pshufd
5894 // as its name says, sad but true, emulate the behavior for now...
5895 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5896 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5897
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005898 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5899
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005900 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005901 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5902
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005903 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005904 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5905 TargetMask, DAG);
5906
5907 if (VT == MVT::v4f32)
5908 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5909 TargetMask, DAG);
5910 }
Eric Christopherfd179292009-08-27 18:07:15 +00005911
Evan Chengf26ffe92008-05-29 08:22:04 +00005912 // Check if this can be converted into a logical shift.
5913 bool isLeft = false;
5914 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005915 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005916 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005917 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005918 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005919 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005920 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005921 EVT EltVT = VT.getVectorElementType();
5922 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005923 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005924 }
Eric Christopherfd179292009-08-27 18:07:15 +00005925
Nate Begeman9008ca62009-04-27 18:41:29 +00005926 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005927 if (V1IsUndef)
5928 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005929 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005930 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005931 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005932 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005933 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5934
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005935 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005936 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5937 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005938 }
Eric Christopherfd179292009-08-27 18:07:15 +00005939
Nate Begeman9008ca62009-04-27 18:41:29 +00005940 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005941 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5942 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005943
Dale Johannesen0488fb62010-09-30 23:57:10 +00005944 if (X86::isMOVHLPSMask(SVOp))
5945 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005946
Dale Johannesen0488fb62010-09-30 23:57:10 +00005947 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5948 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005949
Dale Johannesen0488fb62010-09-30 23:57:10 +00005950 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5951 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005952
Dale Johannesen0488fb62010-09-30 23:57:10 +00005953 if (X86::isMOVLPMask(SVOp))
5954 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005955
Nate Begeman9008ca62009-04-27 18:41:29 +00005956 if (ShouldXformToMOVHLPS(SVOp) ||
5957 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5958 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005959
Evan Chengf26ffe92008-05-29 08:22:04 +00005960 if (isShift) {
5961 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005962 EVT EltVT = VT.getVectorElementType();
5963 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005964 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005965 }
Eric Christopherfd179292009-08-27 18:07:15 +00005966
Evan Cheng9eca5e82006-10-25 21:49:50 +00005967 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005968 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5969 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005970 V1IsSplat = isSplatVector(V1.getNode());
5971 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005972
Chris Lattner8a594482007-11-25 00:24:49 +00005973 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005974 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005975 Op = CommuteVectorShuffle(SVOp, DAG);
5976 SVOp = cast<ShuffleVectorSDNode>(Op);
5977 V1 = SVOp->getOperand(0);
5978 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005979 std::swap(V1IsSplat, V2IsSplat);
5980 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005981 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005982 }
5983
Nate Begeman9008ca62009-04-27 18:41:29 +00005984 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5985 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005986 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005987 return V1;
5988 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5989 // the instruction selector will not match, so get a canonical MOVL with
5990 // swapped operands to undo the commute.
5991 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005992 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005993
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005994 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005995 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5996 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005997
5998 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005999 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006000
Evan Cheng9bbbb982006-10-25 20:48:19 +00006001 if (V2IsSplat) {
6002 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006003 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006004 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006005 SDValue NewMask = NormalizeMask(SVOp, DAG);
6006 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6007 if (NSVOp != SVOp) {
6008 if (X86::isUNPCKLMask(NSVOp, true)) {
6009 return NewMask;
6010 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6011 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006012 }
6013 }
6014 }
6015
Evan Cheng9eca5e82006-10-25 21:49:50 +00006016 if (Commuted) {
6017 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006018 // FIXME: this seems wrong.
6019 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6020 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006021
6022 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00006023 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
6024 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006025
6026 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006027 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006028 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006029
Nate Begeman9008ca62009-04-27 18:41:29 +00006030 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006031 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006032 return CommuteVectorShuffle(SVOp, DAG);
6033
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006034 // The checks below are all present in isShuffleMaskLegal, but they are
6035 // inlined here right now to enable us to directly emit target specific
6036 // nodes, and remove one by one until they don't return Op anymore.
6037 SmallVector<int, 16> M;
6038 SVOp->getMask(M);
6039
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006040 if (isPALIGNRMask(M, VT, HasSSSE3))
6041 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6042 X86::getShufflePALIGNRImmediate(SVOp),
6043 DAG);
6044
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006045 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6046 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00006047 if (VT == MVT::v2f64) {
6048 X86ISD::NodeType Opcode =
6049 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
6050 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
6051 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006052 if (VT == MVT::v2i64)
6053 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6054 }
6055
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006056 if (isPSHUFHWMask(M, VT))
6057 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6058 X86::getShufflePSHUFHWImmediate(SVOp),
6059 DAG);
6060
6061 if (isPSHUFLWMask(M, VT))
6062 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6063 X86::getShufflePSHUFLWImmediate(SVOp),
6064 DAG);
6065
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006066 if (isSHUFPMask(M, VT)) {
6067 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6068 if (VT == MVT::v4f32 || VT == MVT::v4i32)
6069 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
6070 TargetMask, DAG);
6071 if (VT == MVT::v2f64 || VT == MVT::v2i64)
6072 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
6073 TargetMask, DAG);
6074 }
6075
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006076 if (X86::isUNPCKL_v_undef_Mask(SVOp))
6077 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00006078 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
6079 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006080 if (X86::isUNPCKH_v_undef_Mask(SVOp))
6081 if (VT != MVT::v2i64 && VT != MVT::v2f64)
6082 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6083
Evan Cheng14b32e12007-12-11 01:46:18 +00006084 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00006085 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00006086 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006087 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00006088 return NewOp;
6089 }
6090
Owen Anderson825b72b2009-08-11 20:47:22 +00006091 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006092 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006093 if (NewOp.getNode())
6094 return NewOp;
6095 }
Eric Christopherfd179292009-08-27 18:07:15 +00006096
Dale Johannesen0488fb62010-09-30 23:57:10 +00006097 // Handle all 4 wide cases with a number of shuffles.
6098 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006100
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006101 // Handle VPERMIL permutations
6102 if (isVPERMILMask(M, VT)) {
6103 unsigned TargetMask = getShuffleVPERMILImmediate(SVOp);
6104 if (VT == MVT::v8f32)
6105 return getTargetShuffleNode(X86ISD::VPERMIL, dl, VT, V1, TargetMask, DAG);
6106 }
6107
Dan Gohman475871a2008-07-27 21:46:04 +00006108 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006109}
6110
Dan Gohman475871a2008-07-27 21:46:04 +00006111SDValue
6112X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006113 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006114 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006115 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006116 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006117 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006118 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006119 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006120 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006121 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006122 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006123 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6124 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6125 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006126 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6127 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006128 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006129 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006130 Op.getOperand(0)),
6131 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006132 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006133 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006134 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006135 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006136 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006137 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006138 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6139 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006140 // result has a single use which is a store or a bitcast to i32. And in
6141 // the case of a store, it's not worth it if the index is a constant 0,
6142 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006143 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006144 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006145 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006146 if ((User->getOpcode() != ISD::STORE ||
6147 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6148 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006149 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006150 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006151 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006152 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006153 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006154 Op.getOperand(0)),
6155 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006156 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006157 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006158 // ExtractPS works with constant index.
6159 if (isa<ConstantSDNode>(Op.getOperand(1)))
6160 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006161 }
Dan Gohman475871a2008-07-27 21:46:04 +00006162 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006163}
6164
6165
Dan Gohman475871a2008-07-27 21:46:04 +00006166SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006167X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6168 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006169 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006170 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006171
David Greene74a579d2011-02-10 16:57:36 +00006172 SDValue Vec = Op.getOperand(0);
6173 EVT VecVT = Vec.getValueType();
6174
6175 // If this is a 256-bit vector result, first extract the 128-bit
6176 // vector and then extract from the 128-bit vector.
6177 if (VecVT.getSizeInBits() > 128) {
6178 DebugLoc dl = Op.getNode()->getDebugLoc();
6179 unsigned NumElems = VecVT.getVectorNumElements();
6180 SDValue Idx = Op.getOperand(1);
6181
6182 if (!isa<ConstantSDNode>(Idx))
6183 return SDValue();
6184
6185 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6186 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6187
6188 // Get the 128-bit vector.
6189 bool Upper = IdxVal >= ExtractNumElems;
6190 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6191
6192 // Extract from it.
6193 SDValue ScaledIdx = Idx;
6194 if (Upper)
6195 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6196 DAG.getConstant(ExtractNumElems,
6197 Idx.getValueType()));
6198 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6199 ScaledIdx);
6200 }
6201
6202 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6203
Evan Cheng62a3f152008-03-24 21:52:23 +00006204 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006205 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006206 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006207 return Res;
6208 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006209
Owen Andersone50ed302009-08-10 22:56:29 +00006210 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006211 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006212 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006213 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006214 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006215 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006216 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006217 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6218 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006219 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006220 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006221 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006222 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006223 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006224 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006225 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006226 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006227 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006228 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006229 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006230 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006231 if (Idx == 0)
6232 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006233
Evan Cheng0db9fe62006-04-25 20:13:52 +00006234 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006235 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006236 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006237 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006238 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006239 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006240 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006241 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006242 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6243 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6244 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006245 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006246 if (Idx == 0)
6247 return Op;
6248
6249 // UNPCKHPD the element to the lowest double word, then movsd.
6250 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6251 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006252 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006253 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006254 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006255 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006256 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006257 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006258 }
6259
Dan Gohman475871a2008-07-27 21:46:04 +00006260 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006261}
6262
Dan Gohman475871a2008-07-27 21:46:04 +00006263SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006264X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6265 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006266 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006267 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006268 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006269
Dan Gohman475871a2008-07-27 21:46:04 +00006270 SDValue N0 = Op.getOperand(0);
6271 SDValue N1 = Op.getOperand(1);
6272 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006273
Dan Gohman8a55ce42009-09-23 21:02:20 +00006274 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006275 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006276 unsigned Opc;
6277 if (VT == MVT::v8i16)
6278 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006279 else if (VT == MVT::v16i8)
6280 Opc = X86ISD::PINSRB;
6281 else
6282 Opc = X86ISD::PINSRB;
6283
Nate Begeman14d12ca2008-02-11 04:19:36 +00006284 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6285 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006286 if (N1.getValueType() != MVT::i32)
6287 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6288 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006289 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006290 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006291 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006292 // Bits [7:6] of the constant are the source select. This will always be
6293 // zero here. The DAG Combiner may combine an extract_elt index into these
6294 // bits. For example (insert (extract, 3), 2) could be matched by putting
6295 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006296 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006297 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006298 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006299 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006300 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006301 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006302 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006303 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006304 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006305 // PINSR* works with constant index.
6306 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006307 }
Dan Gohman475871a2008-07-27 21:46:04 +00006308 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006309}
6310
Dan Gohman475871a2008-07-27 21:46:04 +00006311SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006312X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006313 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006314 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006315
David Greene6b381262011-02-09 15:32:06 +00006316 DebugLoc dl = Op.getDebugLoc();
6317 SDValue N0 = Op.getOperand(0);
6318 SDValue N1 = Op.getOperand(1);
6319 SDValue N2 = Op.getOperand(2);
6320
6321 // If this is a 256-bit vector result, first insert into a 128-bit
6322 // vector and then insert into the 256-bit vector.
6323 if (VT.getSizeInBits() > 128) {
6324 if (!isa<ConstantSDNode>(N2))
6325 return SDValue();
6326
6327 // Get the 128-bit vector.
6328 unsigned NumElems = VT.getVectorNumElements();
6329 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6330 bool Upper = IdxVal >= NumElems / 2;
6331
6332 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6333
6334 // Insert into it.
6335 SDValue ScaledN2 = N2;
6336 if (Upper)
6337 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006338 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006339 (VT.getSizeInBits() / 128),
6340 N2.getValueType()));
6341 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6342 N1, ScaledN2);
6343
6344 // Insert the 128-bit vector
6345 // FIXME: Why UNDEF?
6346 return Insert128BitVector(N0, Op, N2, DAG, dl);
6347 }
6348
Nate Begeman14d12ca2008-02-11 04:19:36 +00006349 if (Subtarget->hasSSE41())
6350 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6351
Dan Gohman8a55ce42009-09-23 21:02:20 +00006352 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006353 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006354
Dan Gohman8a55ce42009-09-23 21:02:20 +00006355 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006356 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6357 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006358 if (N1.getValueType() != MVT::i32)
6359 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6360 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006361 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006362 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006363 }
Dan Gohman475871a2008-07-27 21:46:04 +00006364 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006365}
6366
Dan Gohman475871a2008-07-27 21:46:04 +00006367SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006368X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006369 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006370 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006371 EVT OpVT = Op.getValueType();
6372
6373 // If this is a 256-bit vector result, first insert into a 128-bit
6374 // vector and then insert into the 256-bit vector.
6375 if (OpVT.getSizeInBits() > 128) {
6376 // Insert into a 128-bit vector.
6377 EVT VT128 = EVT::getVectorVT(*Context,
6378 OpVT.getVectorElementType(),
6379 OpVT.getVectorNumElements() / 2);
6380
6381 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6382
6383 // Insert the 128-bit vector.
6384 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6385 DAG.getConstant(0, MVT::i32),
6386 DAG, dl);
6387 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006388
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006389 if (Op.getValueType() == MVT::v1i64 &&
6390 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006391 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006392
Owen Anderson825b72b2009-08-11 20:47:22 +00006393 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006394 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6395 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006396 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006397 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006398}
6399
David Greene91585092011-01-26 15:38:49 +00006400// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6401// a simple subregister reference or explicit instructions to grab
6402// upper bits of a vector.
6403SDValue
6404X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6405 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006406 DebugLoc dl = Op.getNode()->getDebugLoc();
6407 SDValue Vec = Op.getNode()->getOperand(0);
6408 SDValue Idx = Op.getNode()->getOperand(1);
6409
6410 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6411 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6412 return Extract128BitVector(Vec, Idx, DAG, dl);
6413 }
David Greene91585092011-01-26 15:38:49 +00006414 }
6415 return SDValue();
6416}
6417
David Greenecfe33c42011-01-26 19:13:22 +00006418// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6419// simple superregister reference or explicit instructions to insert
6420// the upper bits of a vector.
6421SDValue
6422X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6423 if (Subtarget->hasAVX()) {
6424 DebugLoc dl = Op.getNode()->getDebugLoc();
6425 SDValue Vec = Op.getNode()->getOperand(0);
6426 SDValue SubVec = Op.getNode()->getOperand(1);
6427 SDValue Idx = Op.getNode()->getOperand(2);
6428
6429 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6430 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006431 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006432 }
6433 }
6434 return SDValue();
6435}
6436
Bill Wendling056292f2008-09-16 21:48:12 +00006437// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6438// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6439// one of the above mentioned nodes. It has to be wrapped because otherwise
6440// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6441// be used to form addressing mode. These wrapped nodes will be selected
6442// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006443SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006444X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006445 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006446
Chris Lattner41621a22009-06-26 19:22:52 +00006447 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6448 // global base reg.
6449 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006450 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006451 CodeModel::Model M = getTargetMachine().getCodeModel();
6452
Chris Lattner4f066492009-07-11 20:29:19 +00006453 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006454 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006455 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006456 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006457 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006458 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006459 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006460
Evan Cheng1606e8e2009-03-13 07:51:59 +00006461 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006462 CP->getAlignment(),
6463 CP->getOffset(), OpFlag);
6464 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006465 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006466 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006467 if (OpFlag) {
6468 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006469 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006470 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006471 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006472 }
6473
6474 return Result;
6475}
6476
Dan Gohmand858e902010-04-17 15:26:15 +00006477SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006478 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006479
Chris Lattner18c59872009-06-27 04:16:01 +00006480 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6481 // global base reg.
6482 unsigned char OpFlag = 0;
6483 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006484 CodeModel::Model M = getTargetMachine().getCodeModel();
6485
Chris Lattner4f066492009-07-11 20:29:19 +00006486 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006487 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006488 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006489 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006490 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006491 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006492 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006493
Chris Lattner18c59872009-06-27 04:16:01 +00006494 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6495 OpFlag);
6496 DebugLoc DL = JT->getDebugLoc();
6497 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006498
Chris Lattner18c59872009-06-27 04:16:01 +00006499 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006500 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006501 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6502 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006503 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006504 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006505
Chris Lattner18c59872009-06-27 04:16:01 +00006506 return Result;
6507}
6508
6509SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006510X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006511 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006512
Chris Lattner18c59872009-06-27 04:16:01 +00006513 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6514 // global base reg.
6515 unsigned char OpFlag = 0;
6516 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006517 CodeModel::Model M = getTargetMachine().getCodeModel();
6518
Chris Lattner4f066492009-07-11 20:29:19 +00006519 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006520 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006521 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006522 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006523 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006524 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006525 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006526
Chris Lattner18c59872009-06-27 04:16:01 +00006527 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006528
Chris Lattner18c59872009-06-27 04:16:01 +00006529 DebugLoc DL = Op.getDebugLoc();
6530 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006531
6532
Chris Lattner18c59872009-06-27 04:16:01 +00006533 // With PIC, the address is actually $g + Offset.
6534 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006535 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006536 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6537 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006538 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006539 Result);
6540 }
Eric Christopherfd179292009-08-27 18:07:15 +00006541
Chris Lattner18c59872009-06-27 04:16:01 +00006542 return Result;
6543}
6544
Dan Gohman475871a2008-07-27 21:46:04 +00006545SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006546X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006547 // Create the TargetBlockAddressAddress node.
6548 unsigned char OpFlags =
6549 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006550 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006551 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006552 DebugLoc dl = Op.getDebugLoc();
6553 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6554 /*isTarget=*/true, OpFlags);
6555
Dan Gohmanf705adb2009-10-30 01:28:02 +00006556 if (Subtarget->isPICStyleRIPRel() &&
6557 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006558 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6559 else
6560 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006561
Dan Gohman29cbade2009-11-20 23:18:13 +00006562 // With PIC, the address is actually $g + Offset.
6563 if (isGlobalRelativeToPICBase(OpFlags)) {
6564 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6565 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6566 Result);
6567 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006568
6569 return Result;
6570}
6571
6572SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006573X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006574 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006575 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006576 // Create the TargetGlobalAddress node, folding in the constant
6577 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006578 unsigned char OpFlags =
6579 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006580 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006581 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006582 if (OpFlags == X86II::MO_NO_FLAG &&
6583 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006584 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006585 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006586 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006587 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006588 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006589 }
Eric Christopherfd179292009-08-27 18:07:15 +00006590
Chris Lattner4f066492009-07-11 20:29:19 +00006591 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006592 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006593 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6594 else
6595 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006596
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006597 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006598 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006599 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6600 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006601 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006602 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006603
Chris Lattner36c25012009-07-10 07:34:39 +00006604 // For globals that require a load from a stub to get the address, emit the
6605 // load.
6606 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006607 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006608 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006609
Dan Gohman6520e202008-10-18 02:06:02 +00006610 // If there was a non-zero offset that we didn't fold, create an explicit
6611 // addition for it.
6612 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006613 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006614 DAG.getConstant(Offset, getPointerTy()));
6615
Evan Cheng0db9fe62006-04-25 20:13:52 +00006616 return Result;
6617}
6618
Evan Chengda43bcf2008-09-24 00:05:32 +00006619SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006620X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006621 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006622 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006623 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006624}
6625
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006626static SDValue
6627GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006628 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006629 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006630 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006631 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006632 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006633 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006634 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006635 GA->getOffset(),
6636 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006637 if (InFlag) {
6638 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006639 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006640 } else {
6641 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006642 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006643 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006644
6645 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006646 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006647
Rafael Espindola15f1b662009-04-24 12:59:40 +00006648 SDValue Flag = Chain.getValue(1);
6649 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006650}
6651
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006652// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006653static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006654LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006655 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006656 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006657 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6658 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006659 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006660 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006661 InFlag = Chain.getValue(1);
6662
Chris Lattnerb903bed2009-06-26 21:20:29 +00006663 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006664}
6665
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006666// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006667static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006668LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006669 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006670 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6671 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006672}
6673
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006674// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6675// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006676static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006677 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006678 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006679 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006680
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006681 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6682 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6683 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006684
Michael J. Spencerec38de22010-10-10 22:04:20 +00006685 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006686 DAG.getIntPtrConstant(0),
6687 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006688
Chris Lattnerb903bed2009-06-26 21:20:29 +00006689 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006690 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6691 // initialexec.
6692 unsigned WrapperKind = X86ISD::Wrapper;
6693 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006694 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006695 } else if (is64Bit) {
6696 assert(model == TLSModel::InitialExec);
6697 OperandFlags = X86II::MO_GOTTPOFF;
6698 WrapperKind = X86ISD::WrapperRIP;
6699 } else {
6700 assert(model == TLSModel::InitialExec);
6701 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006702 }
Eric Christopherfd179292009-08-27 18:07:15 +00006703
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006704 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6705 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006706 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006707 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006708 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006709 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006710
Rafael Espindola9a580232009-02-27 13:37:18 +00006711 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006712 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006713 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006714
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006715 // The address of the thread local variable is the add of the thread
6716 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006717 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006718}
6719
Dan Gohman475871a2008-07-27 21:46:04 +00006720SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006721X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006722
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006723 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006724 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006725
Eric Christopher30ef0e52010-06-03 04:07:48 +00006726 if (Subtarget->isTargetELF()) {
6727 // TODO: implement the "local dynamic" model
6728 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006729
Eric Christopher30ef0e52010-06-03 04:07:48 +00006730 // If GV is an alias then use the aliasee for determining
6731 // thread-localness.
6732 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6733 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006734
6735 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006736 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006737
Eric Christopher30ef0e52010-06-03 04:07:48 +00006738 switch (model) {
6739 case TLSModel::GeneralDynamic:
6740 case TLSModel::LocalDynamic: // not implemented
6741 if (Subtarget->is64Bit())
6742 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6743 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006744
Eric Christopher30ef0e52010-06-03 04:07:48 +00006745 case TLSModel::InitialExec:
6746 case TLSModel::LocalExec:
6747 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6748 Subtarget->is64Bit());
6749 }
6750 } else if (Subtarget->isTargetDarwin()) {
6751 // Darwin only has one model of TLS. Lower to that.
6752 unsigned char OpFlag = 0;
6753 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6754 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006755
Eric Christopher30ef0e52010-06-03 04:07:48 +00006756 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6757 // global base reg.
6758 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6759 !Subtarget->is64Bit();
6760 if (PIC32)
6761 OpFlag = X86II::MO_TLVP_PIC_BASE;
6762 else
6763 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006764 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006765 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006766 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006767 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006768 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006769
Eric Christopher30ef0e52010-06-03 04:07:48 +00006770 // With PIC32, the address is actually $g + Offset.
6771 if (PIC32)
6772 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6773 DAG.getNode(X86ISD::GlobalBaseReg,
6774 DebugLoc(), getPointerTy()),
6775 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006776
Eric Christopher30ef0e52010-06-03 04:07:48 +00006777 // Lowering the machine isd will make sure everything is in the right
6778 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006779 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006780 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006781 SDValue Args[] = { Chain, Offset };
6782 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006783
Eric Christopher30ef0e52010-06-03 04:07:48 +00006784 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6785 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6786 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006787
Eric Christopher30ef0e52010-06-03 04:07:48 +00006788 // And our return value (tls address) is in the standard call return value
6789 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006790 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6791 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006792 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006793
Eric Christopher30ef0e52010-06-03 04:07:48 +00006794 assert(false &&
6795 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006796
Torok Edwinc23197a2009-07-14 16:55:14 +00006797 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006798 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006799}
6800
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801
Nadav Rotem43012222011-05-11 08:12:09 +00006802/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006803/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006804SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006805 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006806 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006807 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006808 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006809 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006810 SDValue ShOpLo = Op.getOperand(0);
6811 SDValue ShOpHi = Op.getOperand(1);
6812 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006813 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006814 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006815 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006816
Dan Gohman475871a2008-07-27 21:46:04 +00006817 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006818 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006819 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6820 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006821 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006822 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6823 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006824 }
Evan Chenge3413162006-01-09 18:33:28 +00006825
Owen Anderson825b72b2009-08-11 20:47:22 +00006826 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6827 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006828 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006829 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006830
Dan Gohman475871a2008-07-27 21:46:04 +00006831 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006832 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006833 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6834 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006835
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006836 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006837 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6838 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006839 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006840 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6841 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006842 }
6843
Dan Gohman475871a2008-07-27 21:46:04 +00006844 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006845 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006846}
Evan Chenga3195e82006-01-12 22:54:21 +00006847
Dan Gohmand858e902010-04-17 15:26:15 +00006848SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6849 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006850 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006851
Dale Johannesen0488fb62010-09-30 23:57:10 +00006852 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006853 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006854
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006856 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006857
Eli Friedman36df4992009-05-27 00:47:34 +00006858 // These are really Legal; return the operand so the caller accepts it as
6859 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006860 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006861 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006863 Subtarget->is64Bit()) {
6864 return Op;
6865 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006866
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006867 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006868 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006869 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006870 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006871 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006872 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006873 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006874 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006875 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006876 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6877}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006878
Owen Andersone50ed302009-08-10 22:56:29 +00006879SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006880 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006881 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006882 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006883 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006884 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006885 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006886 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006887 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006888 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006889 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006890
Chris Lattner492a43e2010-09-22 01:28:21 +00006891 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006892
Stuart Hastings84be9582011-06-02 15:57:11 +00006893 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6894 MachineMemOperand *MMO;
6895 if (FI) {
6896 int SSFI = FI->getIndex();
6897 MMO =
6898 DAG.getMachineFunction()
6899 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6900 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6901 } else {
6902 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6903 StackSlot = StackSlot.getOperand(1);
6904 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006905 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006906 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6907 X86ISD::FILD, DL,
6908 Tys, Ops, array_lengthof(Ops),
6909 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006910
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006911 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006912 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006913 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006914
6915 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6916 // shouldn't be necessary except that RFP cannot be live across
6917 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006918 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006919 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6920 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006921 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006923 SDValue Ops[] = {
6924 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6925 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006926 MachineMemOperand *MMO =
6927 DAG.getMachineFunction()
6928 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006929 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006930
Chris Lattner492a43e2010-09-22 01:28:21 +00006931 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6932 Ops, array_lengthof(Ops),
6933 Op.getValueType(), MMO);
6934 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006935 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006936 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006937 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006938
Evan Cheng0db9fe62006-04-25 20:13:52 +00006939 return Result;
6940}
6941
Bill Wendling8b8a6362009-01-17 03:56:04 +00006942// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006943SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6944 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006945 // This algorithm is not obvious. Here it is in C code, more or less:
6946 /*
6947 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6948 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6949 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006950
Bill Wendling8b8a6362009-01-17 03:56:04 +00006951 // Copy ints to xmm registers.
6952 __m128i xh = _mm_cvtsi32_si128( hi );
6953 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006954
Bill Wendling8b8a6362009-01-17 03:56:04 +00006955 // Combine into low half of a single xmm register.
6956 __m128i x = _mm_unpacklo_epi32( xh, xl );
6957 __m128d d;
6958 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006959
Bill Wendling8b8a6362009-01-17 03:56:04 +00006960 // Merge in appropriate exponents to give the integer bits the right
6961 // magnitude.
6962 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006963
Bill Wendling8b8a6362009-01-17 03:56:04 +00006964 // Subtract away the biases to deal with the IEEE-754 double precision
6965 // implicit 1.
6966 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006967
Bill Wendling8b8a6362009-01-17 03:56:04 +00006968 // All conversions up to here are exact. The correctly rounded result is
6969 // calculated using the current rounding mode using the following
6970 // horizontal add.
6971 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6972 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6973 // store doesn't really need to be here (except
6974 // maybe to zero the other double)
6975 return sd;
6976 }
6977 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006978
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006979 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006980 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006981
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006982 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006983 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006984 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6985 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6986 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6987 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006988 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006989 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006990
Bill Wendling8b8a6362009-01-17 03:56:04 +00006991 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006992 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006993 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006994 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006995 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006996 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006997 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006998
Owen Anderson825b72b2009-08-11 20:47:22 +00006999 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7000 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007001 Op.getOperand(0),
7002 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007003 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7004 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007005 Op.getOperand(0),
7006 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007007 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7008 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007009 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007010 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007011 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007012 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007013 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007014 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007015 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007016 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007017
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007018 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007019 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007020 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7021 DAG.getUNDEF(MVT::v2f64), ShufMask);
7022 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7023 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007024 DAG.getIntPtrConstant(0));
7025}
7026
Bill Wendling8b8a6362009-01-17 03:56:04 +00007027// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007028SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7029 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007030 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007031 // FP constant to bias correct the final result.
7032 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007034
7035 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007036 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7037 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00007038 Op.getOperand(0),
7039 DAG.getIntPtrConstant(0)));
7040
Owen Anderson825b72b2009-08-11 20:47:22 +00007041 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007042 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007043 DAG.getIntPtrConstant(0));
7044
7045 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007046 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007047 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007048 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007050 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007051 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 MVT::v2f64, Bias)));
7053 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007054 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007055 DAG.getIntPtrConstant(0));
7056
7057 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007058 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007059
7060 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007061 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007062
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007064 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007065 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007067 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007068 }
7069
7070 // Handle final rounding.
7071 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007072}
7073
Dan Gohmand858e902010-04-17 15:26:15 +00007074SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7075 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007076 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007077 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007078
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007079 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007080 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7081 // the optimization here.
7082 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007083 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007084
Owen Andersone50ed302009-08-10 22:56:29 +00007085 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007086 EVT DstVT = Op.getValueType();
7087 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007088 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007089 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007090 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007091
7092 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007093 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007094 if (SrcVT == MVT::i32) {
7095 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7096 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7097 getPointerTy(), StackSlot, WordOff);
7098 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007099 StackSlot, MachinePointerInfo(),
7100 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007101 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007102 OffsetSlot, MachinePointerInfo(),
7103 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007104 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7105 return Fild;
7106 }
7107
7108 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7109 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007110 StackSlot, MachinePointerInfo(),
7111 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007112 // For i64 source, we need to add the appropriate power of 2 if the input
7113 // was negative. This is the same as the optimization in
7114 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7115 // we must be careful to do the computation in x87 extended precision, not
7116 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007117 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7118 MachineMemOperand *MMO =
7119 DAG.getMachineFunction()
7120 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7121 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007122
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007123 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7124 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007125 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7126 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007127
7128 APInt FF(32, 0x5F800000ULL);
7129
7130 // Check whether the sign bit is set.
7131 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7132 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7133 ISD::SETLT);
7134
7135 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7136 SDValue FudgePtr = DAG.getConstantPool(
7137 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7138 getPointerTy());
7139
7140 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7141 SDValue Zero = DAG.getIntPtrConstant(0);
7142 SDValue Four = DAG.getIntPtrConstant(4);
7143 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7144 Zero, Four);
7145 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7146
7147 // Load the value out, extending it from f32 to f80.
7148 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007149 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007150 FudgePtr, MachinePointerInfo::getConstantPool(),
7151 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007152 // Extend everything to 80 bits to force it to be done on x87.
7153 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7154 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007155}
7156
Dan Gohman475871a2008-07-27 21:46:04 +00007157std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007158FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007159 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007160
Owen Andersone50ed302009-08-10 22:56:29 +00007161 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007162
7163 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007164 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7165 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007166 }
7167
Owen Anderson825b72b2009-08-11 20:47:22 +00007168 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7169 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007170 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007171
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007172 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007173 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007174 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007175 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007176 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007177 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007178 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007179 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007180
Evan Cheng87c89352007-10-15 20:11:21 +00007181 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7182 // stack slot.
7183 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007184 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007185 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007186 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007187
Michael J. Spencerec38de22010-10-10 22:04:20 +00007188
7189
Evan Cheng0db9fe62006-04-25 20:13:52 +00007190 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007191 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007192 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007193 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7194 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7195 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007196 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007197
Dan Gohman475871a2008-07-27 21:46:04 +00007198 SDValue Chain = DAG.getEntryNode();
7199 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007200 EVT TheVT = Op.getOperand(0).getValueType();
7201 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007202 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007203 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007204 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007205 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007206 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007207 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007208 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007209 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007210
Chris Lattner492a43e2010-09-22 01:28:21 +00007211 MachineMemOperand *MMO =
7212 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7213 MachineMemOperand::MOLoad, MemSize, MemSize);
7214 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7215 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007216 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007217 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007218 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7219 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007220
Chris Lattner07290932010-09-22 01:05:16 +00007221 MachineMemOperand *MMO =
7222 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7223 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007224
Evan Cheng0db9fe62006-04-25 20:13:52 +00007225 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007226 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007227 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7228 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007229
Chris Lattner27a6c732007-11-24 07:07:01 +00007230 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007231}
7232
Dan Gohmand858e902010-04-17 15:26:15 +00007233SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7234 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007235 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007236 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007237
Eli Friedman948e95a2009-05-23 09:59:16 +00007238 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007239 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007240 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7241 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007242
Chris Lattner27a6c732007-11-24 07:07:01 +00007243 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007244 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007245 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007246}
7247
Dan Gohmand858e902010-04-17 15:26:15 +00007248SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7249 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007250 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7251 SDValue FIST = Vals.first, StackSlot = Vals.second;
7252 assert(FIST.getNode() && "Unexpected failure");
7253
7254 // Load the result.
7255 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007256 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007257}
7258
Dan Gohmand858e902010-04-17 15:26:15 +00007259SDValue X86TargetLowering::LowerFABS(SDValue Op,
7260 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007261 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007262 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007263 EVT VT = Op.getValueType();
7264 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007265 if (VT.isVector())
7266 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007267 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007268 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007269 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007270 CV.push_back(C);
7271 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007272 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007273 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007274 CV.push_back(C);
7275 CV.push_back(C);
7276 CV.push_back(C);
7277 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007278 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007279 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007280 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007281 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007282 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007283 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007284 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007285}
7286
Dan Gohmand858e902010-04-17 15:26:15 +00007287SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007288 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007289 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007290 EVT VT = Op.getValueType();
7291 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007292 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007293 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007294 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007295 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007296 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007297 CV.push_back(C);
7298 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007299 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007300 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007301 CV.push_back(C);
7302 CV.push_back(C);
7303 CV.push_back(C);
7304 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007305 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007306 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007307 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007308 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007309 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007310 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007311 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007312 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007313 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007314 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007315 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007316 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007317 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007318 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007319 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007320}
7321
Dan Gohmand858e902010-04-17 15:26:15 +00007322SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007323 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007324 SDValue Op0 = Op.getOperand(0);
7325 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007326 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007327 EVT VT = Op.getValueType();
7328 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007329
7330 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007331 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007332 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007333 SrcVT = VT;
7334 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007335 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007336 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007337 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007338 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007339 }
7340
7341 // At this point the operands and the result should have the same
7342 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007343
Evan Cheng68c47cb2007-01-05 07:55:56 +00007344 // First get the sign bit of second operand.
7345 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007346 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007347 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7348 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007349 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007350 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7351 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7352 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7353 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007354 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007355 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007356 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007357 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007358 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007359 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007360 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007361
7362 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007363 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007364 // Op0 is MVT::f32, Op1 is MVT::f64.
7365 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7366 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7367 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007368 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007369 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007370 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007371 }
7372
Evan Cheng73d6cf12007-01-05 21:37:56 +00007373 // Clear first operand sign bit.
7374 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007375 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007376 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7377 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007378 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007379 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7380 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7381 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7382 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007383 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007384 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007385 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007386 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007387 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007388 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007389 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007390
7391 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007392 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007393}
7394
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007395SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7396 SDValue N0 = Op.getOperand(0);
7397 DebugLoc dl = Op.getDebugLoc();
7398 EVT VT = Op.getValueType();
7399
7400 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7401 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7402 DAG.getConstant(1, VT));
7403 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7404}
7405
Dan Gohman076aee32009-03-04 19:44:21 +00007406/// Emit nodes that will be selected as "test Op0,Op0", or something
7407/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007408SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007409 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007410 DebugLoc dl = Op.getDebugLoc();
7411
Dan Gohman31125812009-03-07 01:58:32 +00007412 // CF and OF aren't always set the way we want. Determine which
7413 // of these we need.
7414 bool NeedCF = false;
7415 bool NeedOF = false;
7416 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007417 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007418 case X86::COND_A: case X86::COND_AE:
7419 case X86::COND_B: case X86::COND_BE:
7420 NeedCF = true;
7421 break;
7422 case X86::COND_G: case X86::COND_GE:
7423 case X86::COND_L: case X86::COND_LE:
7424 case X86::COND_O: case X86::COND_NO:
7425 NeedOF = true;
7426 break;
Dan Gohman31125812009-03-07 01:58:32 +00007427 }
7428
Dan Gohman076aee32009-03-04 19:44:21 +00007429 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007430 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7431 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007432 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7433 // Emit a CMP with 0, which is the TEST pattern.
7434 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7435 DAG.getConstant(0, Op.getValueType()));
7436
7437 unsigned Opcode = 0;
7438 unsigned NumOperands = 0;
7439 switch (Op.getNode()->getOpcode()) {
7440 case ISD::ADD:
7441 // Due to an isel shortcoming, be conservative if this add is likely to be
7442 // selected as part of a load-modify-store instruction. When the root node
7443 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7444 // uses of other nodes in the match, such as the ADD in this case. This
7445 // leads to the ADD being left around and reselected, with the result being
7446 // two adds in the output. Alas, even if none our users are stores, that
7447 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7448 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7449 // climbing the DAG back to the root, and it doesn't seem to be worth the
7450 // effort.
7451 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007452 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007453 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7454 goto default_case;
7455
7456 if (ConstantSDNode *C =
7457 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7458 // An add of one will be selected as an INC.
7459 if (C->getAPIntValue() == 1) {
7460 Opcode = X86ISD::INC;
7461 NumOperands = 1;
7462 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007463 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007464
7465 // An add of negative one (subtract of one) will be selected as a DEC.
7466 if (C->getAPIntValue().isAllOnesValue()) {
7467 Opcode = X86ISD::DEC;
7468 NumOperands = 1;
7469 break;
7470 }
Dan Gohman076aee32009-03-04 19:44:21 +00007471 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007472
7473 // Otherwise use a regular EFLAGS-setting add.
7474 Opcode = X86ISD::ADD;
7475 NumOperands = 2;
7476 break;
7477 case ISD::AND: {
7478 // If the primary and result isn't used, don't bother using X86ISD::AND,
7479 // because a TEST instruction will be better.
7480 bool NonFlagUse = false;
7481 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7482 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7483 SDNode *User = *UI;
7484 unsigned UOpNo = UI.getOperandNo();
7485 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7486 // Look pass truncate.
7487 UOpNo = User->use_begin().getOperandNo();
7488 User = *User->use_begin();
7489 }
7490
7491 if (User->getOpcode() != ISD::BRCOND &&
7492 User->getOpcode() != ISD::SETCC &&
7493 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7494 NonFlagUse = true;
7495 break;
7496 }
Dan Gohman076aee32009-03-04 19:44:21 +00007497 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007498
7499 if (!NonFlagUse)
7500 break;
7501 }
7502 // FALL THROUGH
7503 case ISD::SUB:
7504 case ISD::OR:
7505 case ISD::XOR:
7506 // Due to the ISEL shortcoming noted above, be conservative if this op is
7507 // likely to be selected as part of a load-modify-store instruction.
7508 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7509 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7510 if (UI->getOpcode() == ISD::STORE)
7511 goto default_case;
7512
7513 // Otherwise use a regular EFLAGS-setting instruction.
7514 switch (Op.getNode()->getOpcode()) {
7515 default: llvm_unreachable("unexpected operator!");
7516 case ISD::SUB: Opcode = X86ISD::SUB; break;
7517 case ISD::OR: Opcode = X86ISD::OR; break;
7518 case ISD::XOR: Opcode = X86ISD::XOR; break;
7519 case ISD::AND: Opcode = X86ISD::AND; break;
7520 }
7521
7522 NumOperands = 2;
7523 break;
7524 case X86ISD::ADD:
7525 case X86ISD::SUB:
7526 case X86ISD::INC:
7527 case X86ISD::DEC:
7528 case X86ISD::OR:
7529 case X86ISD::XOR:
7530 case X86ISD::AND:
7531 return SDValue(Op.getNode(), 1);
7532 default:
7533 default_case:
7534 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007535 }
7536
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007537 if (Opcode == 0)
7538 // Emit a CMP with 0, which is the TEST pattern.
7539 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7540 DAG.getConstant(0, Op.getValueType()));
7541
7542 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7543 SmallVector<SDValue, 4> Ops;
7544 for (unsigned i = 0; i != NumOperands; ++i)
7545 Ops.push_back(Op.getOperand(i));
7546
7547 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7548 DAG.ReplaceAllUsesWith(Op, New);
7549 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007550}
7551
7552/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7553/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007554SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007555 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007556 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7557 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007558 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007559
7560 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007561 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007562}
7563
Evan Chengd40d03e2010-01-06 19:38:29 +00007564/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7565/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007566SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7567 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007568 SDValue Op0 = And.getOperand(0);
7569 SDValue Op1 = And.getOperand(1);
7570 if (Op0.getOpcode() == ISD::TRUNCATE)
7571 Op0 = Op0.getOperand(0);
7572 if (Op1.getOpcode() == ISD::TRUNCATE)
7573 Op1 = Op1.getOperand(0);
7574
Evan Chengd40d03e2010-01-06 19:38:29 +00007575 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007576 if (Op1.getOpcode() == ISD::SHL)
7577 std::swap(Op0, Op1);
7578 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007579 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7580 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007581 // If we looked past a truncate, check that it's only truncating away
7582 // known zeros.
7583 unsigned BitWidth = Op0.getValueSizeInBits();
7584 unsigned AndBitWidth = And.getValueSizeInBits();
7585 if (BitWidth > AndBitWidth) {
7586 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7587 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7588 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7589 return SDValue();
7590 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007591 LHS = Op1;
7592 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007593 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007594 } else if (Op1.getOpcode() == ISD::Constant) {
7595 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7596 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007597 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7598 LHS = AndLHS.getOperand(0);
7599 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007600 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007601 }
Evan Cheng0488db92007-09-25 01:57:46 +00007602
Evan Chengd40d03e2010-01-06 19:38:29 +00007603 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007604 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007605 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007606 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007607 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007608 // Also promote i16 to i32 for performance / code size reason.
7609 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007610 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007611 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007612
Evan Chengd40d03e2010-01-06 19:38:29 +00007613 // If the operand types disagree, extend the shift amount to match. Since
7614 // BT ignores high bits (like shifts) we can use anyextend.
7615 if (LHS.getValueType() != RHS.getValueType())
7616 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007617
Evan Chengd40d03e2010-01-06 19:38:29 +00007618 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7619 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7620 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7621 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007622 }
7623
Evan Cheng54de3ea2010-01-05 06:52:31 +00007624 return SDValue();
7625}
7626
Dan Gohmand858e902010-04-17 15:26:15 +00007627SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007628 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7629 SDValue Op0 = Op.getOperand(0);
7630 SDValue Op1 = Op.getOperand(1);
7631 DebugLoc dl = Op.getDebugLoc();
7632 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7633
7634 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007635 // Lower (X & (1 << N)) == 0 to BT(X, N).
7636 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7637 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007638 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007639 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007640 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007641 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7642 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7643 if (NewSetCC.getNode())
7644 return NewSetCC;
7645 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007646
Chris Lattner481eebc2010-12-19 21:23:48 +00007647 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7648 // these.
7649 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007650 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007651 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7652 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007653
Chris Lattner481eebc2010-12-19 21:23:48 +00007654 // If the input is a setcc, then reuse the input setcc or use a new one with
7655 // the inverted condition.
7656 if (Op0.getOpcode() == X86ISD::SETCC) {
7657 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7658 bool Invert = (CC == ISD::SETNE) ^
7659 cast<ConstantSDNode>(Op1)->isNullValue();
7660 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007661
Evan Cheng2c755ba2010-02-27 07:36:59 +00007662 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007663 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7664 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7665 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007666 }
7667
Evan Chenge5b51ac2010-04-17 06:13:15 +00007668 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007669 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007670 if (X86CC == X86::COND_INVALID)
7671 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007672
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007673 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007674 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007675 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007676}
7677
Dan Gohmand858e902010-04-17 15:26:15 +00007678SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007679 SDValue Cond;
7680 SDValue Op0 = Op.getOperand(0);
7681 SDValue Op1 = Op.getOperand(1);
7682 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007683 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007684 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7685 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007686 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007687
7688 if (isFP) {
7689 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007690 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007691 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7692 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007693 bool Swap = false;
7694
7695 switch (SetCCOpcode) {
7696 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007697 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007698 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007699 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007700 case ISD::SETGT: Swap = true; // Fallthrough
7701 case ISD::SETLT:
7702 case ISD::SETOLT: SSECC = 1; break;
7703 case ISD::SETOGE:
7704 case ISD::SETGE: Swap = true; // Fallthrough
7705 case ISD::SETLE:
7706 case ISD::SETOLE: SSECC = 2; break;
7707 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007708 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007709 case ISD::SETNE: SSECC = 4; break;
7710 case ISD::SETULE: Swap = true;
7711 case ISD::SETUGE: SSECC = 5; break;
7712 case ISD::SETULT: Swap = true;
7713 case ISD::SETUGT: SSECC = 6; break;
7714 case ISD::SETO: SSECC = 7; break;
7715 }
7716 if (Swap)
7717 std::swap(Op0, Op1);
7718
Nate Begemanfb8ead02008-07-25 19:05:58 +00007719 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007720 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007721 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007722 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007723 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7724 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007725 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007726 }
7727 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007728 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007729 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7730 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007731 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007732 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007733 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007734 }
7735 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007736 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007737 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007738
Nate Begeman30a0de92008-07-17 16:51:19 +00007739 // We are handling one of the integer comparisons here. Since SSE only has
7740 // GT and EQ comparisons for integer, swapping operands and multiple
7741 // operations may be required for some comparisons.
7742 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7743 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007744
Owen Anderson825b72b2009-08-11 20:47:22 +00007745 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007746 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007747 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007748 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007749 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7750 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007751 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007752
Nate Begeman30a0de92008-07-17 16:51:19 +00007753 switch (SetCCOpcode) {
7754 default: break;
7755 case ISD::SETNE: Invert = true;
7756 case ISD::SETEQ: Opc = EQOpc; break;
7757 case ISD::SETLT: Swap = true;
7758 case ISD::SETGT: Opc = GTOpc; break;
7759 case ISD::SETGE: Swap = true;
7760 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7761 case ISD::SETULT: Swap = true;
7762 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7763 case ISD::SETUGE: Swap = true;
7764 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7765 }
7766 if (Swap)
7767 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007768
Nate Begeman30a0de92008-07-17 16:51:19 +00007769 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7770 // bits of the inputs before performing those operations.
7771 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007772 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007773 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7774 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007775 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007776 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7777 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007778 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7779 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007780 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007781
Dale Johannesenace16102009-02-03 19:33:06 +00007782 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007783
7784 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007785 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007786 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007787
Nate Begeman30a0de92008-07-17 16:51:19 +00007788 return Result;
7789}
Evan Cheng0488db92007-09-25 01:57:46 +00007790
Evan Cheng370e5342008-12-03 08:38:43 +00007791// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007792static bool isX86LogicalCmp(SDValue Op) {
7793 unsigned Opc = Op.getNode()->getOpcode();
7794 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7795 return true;
7796 if (Op.getResNo() == 1 &&
7797 (Opc == X86ISD::ADD ||
7798 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007799 Opc == X86ISD::ADC ||
7800 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007801 Opc == X86ISD::SMUL ||
7802 Opc == X86ISD::UMUL ||
7803 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007804 Opc == X86ISD::DEC ||
7805 Opc == X86ISD::OR ||
7806 Opc == X86ISD::XOR ||
7807 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007808 return true;
7809
Chris Lattner9637d5b2010-12-05 07:49:54 +00007810 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7811 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007812
Dan Gohman076aee32009-03-04 19:44:21 +00007813 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007814}
7815
Chris Lattnera2b56002010-12-05 01:23:24 +00007816static bool isZero(SDValue V) {
7817 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7818 return C && C->isNullValue();
7819}
7820
Chris Lattner96908b12010-12-05 02:00:51 +00007821static bool isAllOnes(SDValue V) {
7822 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7823 return C && C->isAllOnesValue();
7824}
7825
Dan Gohmand858e902010-04-17 15:26:15 +00007826SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007827 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007828 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007829 SDValue Op1 = Op.getOperand(1);
7830 SDValue Op2 = Op.getOperand(2);
7831 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007832 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007833
Dan Gohman1a492952009-10-20 16:22:37 +00007834 if (Cond.getOpcode() == ISD::SETCC) {
7835 SDValue NewCond = LowerSETCC(Cond, DAG);
7836 if (NewCond.getNode())
7837 Cond = NewCond;
7838 }
Evan Cheng734503b2006-09-11 02:19:56 +00007839
Chris Lattnera2b56002010-12-05 01:23:24 +00007840 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007841 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007842 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007843 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007844 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007845 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7846 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007847 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007848
Chris Lattnera2b56002010-12-05 01:23:24 +00007849 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007850
7851 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007852 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7853 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007854
7855 SDValue CmpOp0 = Cmp.getOperand(0);
7856 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7857 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007858
Chris Lattner96908b12010-12-05 02:00:51 +00007859 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007860 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7861 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007862
Chris Lattner96908b12010-12-05 02:00:51 +00007863 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7864 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007865
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007866 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007867 if (N2C == 0 || !N2C->isNullValue())
7868 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7869 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007870 }
7871 }
7872
Chris Lattnera2b56002010-12-05 01:23:24 +00007873 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007874 if (Cond.getOpcode() == ISD::AND &&
7875 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7876 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007877 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007878 Cond = Cond.getOperand(0);
7879 }
7880
Evan Cheng3f41d662007-10-08 22:16:29 +00007881 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7882 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007883 if (Cond.getOpcode() == X86ISD::SETCC ||
7884 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007885 CC = Cond.getOperand(0);
7886
Dan Gohman475871a2008-07-27 21:46:04 +00007887 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007888 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007889 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007890
Evan Cheng3f41d662007-10-08 22:16:29 +00007891 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007892 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007893 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007894 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007895
Chris Lattnerd1980a52009-03-12 06:52:53 +00007896 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7897 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007898 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007899 addTest = false;
7900 }
7901 }
7902
7903 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007904 // Look pass the truncate.
7905 if (Cond.getOpcode() == ISD::TRUNCATE)
7906 Cond = Cond.getOperand(0);
7907
7908 // We know the result of AND is compared against zero. Try to match
7909 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007910 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007911 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007912 if (NewSetCC.getNode()) {
7913 CC = NewSetCC.getOperand(0);
7914 Cond = NewSetCC.getOperand(1);
7915 addTest = false;
7916 }
7917 }
7918 }
7919
7920 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007922 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007923 }
7924
Benjamin Kramere915ff32010-12-22 23:09:28 +00007925 // a < b ? -1 : 0 -> RES = ~setcc_carry
7926 // a < b ? 0 : -1 -> RES = setcc_carry
7927 // a >= b ? -1 : 0 -> RES = setcc_carry
7928 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7929 if (Cond.getOpcode() == X86ISD::CMP) {
7930 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7931
7932 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7933 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7934 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7935 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7936 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7937 return DAG.getNOT(DL, Res, Res.getValueType());
7938 return Res;
7939 }
7940 }
7941
Evan Cheng0488db92007-09-25 01:57:46 +00007942 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7943 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007944 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007945 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007946 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007947}
7948
Evan Cheng370e5342008-12-03 08:38:43 +00007949// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7950// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7951// from the AND / OR.
7952static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7953 Opc = Op.getOpcode();
7954 if (Opc != ISD::OR && Opc != ISD::AND)
7955 return false;
7956 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7957 Op.getOperand(0).hasOneUse() &&
7958 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7959 Op.getOperand(1).hasOneUse());
7960}
7961
Evan Cheng961d6d42009-02-02 08:19:07 +00007962// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7963// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007964static bool isXor1OfSetCC(SDValue Op) {
7965 if (Op.getOpcode() != ISD::XOR)
7966 return false;
7967 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7968 if (N1C && N1C->getAPIntValue() == 1) {
7969 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7970 Op.getOperand(0).hasOneUse();
7971 }
7972 return false;
7973}
7974
Dan Gohmand858e902010-04-17 15:26:15 +00007975SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007976 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007977 SDValue Chain = Op.getOperand(0);
7978 SDValue Cond = Op.getOperand(1);
7979 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007980 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007981 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007982
Dan Gohman1a492952009-10-20 16:22:37 +00007983 if (Cond.getOpcode() == ISD::SETCC) {
7984 SDValue NewCond = LowerSETCC(Cond, DAG);
7985 if (NewCond.getNode())
7986 Cond = NewCond;
7987 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007988#if 0
7989 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007990 else if (Cond.getOpcode() == X86ISD::ADD ||
7991 Cond.getOpcode() == X86ISD::SUB ||
7992 Cond.getOpcode() == X86ISD::SMUL ||
7993 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007994 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007995#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007996
Evan Chengad9c0a32009-12-15 00:53:42 +00007997 // Look pass (and (setcc_carry (cmp ...)), 1).
7998 if (Cond.getOpcode() == ISD::AND &&
7999 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8000 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008001 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008002 Cond = Cond.getOperand(0);
8003 }
8004
Evan Cheng3f41d662007-10-08 22:16:29 +00008005 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8006 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008007 if (Cond.getOpcode() == X86ISD::SETCC ||
8008 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008009 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008010
Dan Gohman475871a2008-07-27 21:46:04 +00008011 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008012 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008013 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008014 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008015 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008016 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008017 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008018 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008019 default: break;
8020 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008021 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008022 // These can only come from an arithmetic instruction with overflow,
8023 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008024 Cond = Cond.getNode()->getOperand(1);
8025 addTest = false;
8026 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008027 }
Evan Cheng0488db92007-09-25 01:57:46 +00008028 }
Evan Cheng370e5342008-12-03 08:38:43 +00008029 } else {
8030 unsigned CondOpc;
8031 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8032 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008033 if (CondOpc == ISD::OR) {
8034 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8035 // two branches instead of an explicit OR instruction with a
8036 // separate test.
8037 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008038 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008039 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008040 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008041 Chain, Dest, CC, Cmp);
8042 CC = Cond.getOperand(1).getOperand(0);
8043 Cond = Cmp;
8044 addTest = false;
8045 }
8046 } else { // ISD::AND
8047 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8048 // two branches instead of an explicit AND instruction with a
8049 // separate test. However, we only do this if this block doesn't
8050 // have a fall-through edge, because this requires an explicit
8051 // jmp when the condition is false.
8052 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008053 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008054 Op.getNode()->hasOneUse()) {
8055 X86::CondCode CCode =
8056 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8057 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008058 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008059 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008060 // Look for an unconditional branch following this conditional branch.
8061 // We need this because we need to reverse the successors in order
8062 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008063 if (User->getOpcode() == ISD::BR) {
8064 SDValue FalseBB = User->getOperand(1);
8065 SDNode *NewBR =
8066 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008067 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008068 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008069 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008070
Dale Johannesene4d209d2009-02-03 20:21:25 +00008071 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008072 Chain, Dest, CC, Cmp);
8073 X86::CondCode CCode =
8074 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8075 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008076 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008077 Cond = Cmp;
8078 addTest = false;
8079 }
8080 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008081 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008082 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8083 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8084 // It should be transformed during dag combiner except when the condition
8085 // is set by a arithmetics with overflow node.
8086 X86::CondCode CCode =
8087 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8088 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008089 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008090 Cond = Cond.getOperand(0).getOperand(1);
8091 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008092 }
Evan Cheng0488db92007-09-25 01:57:46 +00008093 }
8094
8095 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008096 // Look pass the truncate.
8097 if (Cond.getOpcode() == ISD::TRUNCATE)
8098 Cond = Cond.getOperand(0);
8099
8100 // We know the result of AND is compared against zero. Try to match
8101 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008102 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008103 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8104 if (NewSetCC.getNode()) {
8105 CC = NewSetCC.getOperand(0);
8106 Cond = NewSetCC.getOperand(1);
8107 addTest = false;
8108 }
8109 }
8110 }
8111
8112 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008113 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008114 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008115 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008116 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008117 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008118}
8119
Anton Korobeynikove060b532007-04-17 19:34:00 +00008120
8121// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8122// Calls to _alloca is needed to probe the stack when allocating more than 4k
8123// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8124// that the guard pages used by the OS virtual memory manager are allocated in
8125// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008126SDValue
8127X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008128 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008129 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008130 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008131 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008132 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008133
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008134 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008135 SDValue Chain = Op.getOperand(0);
8136 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008137 // FIXME: Ensure alignment here
8138
Dan Gohman475871a2008-07-27 21:46:04 +00008139 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008140
Owen Anderson825b72b2009-08-11 20:47:22 +00008141 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008142 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008143
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008144 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008145 Flag = Chain.getValue(1);
8146
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008147 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008148
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008149 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008150 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008151
Dale Johannesendd64c412009-02-04 00:33:20 +00008152 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008153
Dan Gohman475871a2008-07-27 21:46:04 +00008154 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008155 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008156}
8157
Dan Gohmand858e902010-04-17 15:26:15 +00008158SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008159 MachineFunction &MF = DAG.getMachineFunction();
8160 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8161
Dan Gohman69de1932008-02-06 22:27:42 +00008162 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008163 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008164
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008165 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008166 // vastart just stores the address of the VarArgsFrameIndex slot into the
8167 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008168 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8169 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008170 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8171 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008172 }
8173
8174 // __va_list_tag:
8175 // gp_offset (0 - 6 * 8)
8176 // fp_offset (48 - 48 + 8 * 16)
8177 // overflow_arg_area (point to parameters coming in memory).
8178 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008179 SmallVector<SDValue, 8> MemOps;
8180 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008181 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008182 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008183 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8184 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008185 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008186 MemOps.push_back(Store);
8187
8188 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008189 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008190 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008191 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008192 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8193 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008194 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008195 MemOps.push_back(Store);
8196
8197 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008198 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008199 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008200 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8201 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008202 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8203 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008204 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008205 MemOps.push_back(Store);
8206
8207 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008208 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008209 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008210 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8211 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008212 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8213 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008214 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008215 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008216 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008217}
8218
Dan Gohmand858e902010-04-17 15:26:15 +00008219SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008220 assert(Subtarget->is64Bit() &&
8221 "LowerVAARG only handles 64-bit va_arg!");
8222 assert((Subtarget->isTargetLinux() ||
8223 Subtarget->isTargetDarwin()) &&
8224 "Unhandled target in LowerVAARG");
8225 assert(Op.getNode()->getNumOperands() == 4);
8226 SDValue Chain = Op.getOperand(0);
8227 SDValue SrcPtr = Op.getOperand(1);
8228 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8229 unsigned Align = Op.getConstantOperandVal(3);
8230 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008231
Dan Gohman320afb82010-10-12 18:00:49 +00008232 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008233 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00008234 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8235 uint8_t ArgMode;
8236
8237 // Decide which area this value should be read from.
8238 // TODO: Implement the AMD64 ABI in its entirety. This simple
8239 // selection mechanism works only for the basic types.
8240 if (ArgVT == MVT::f80) {
8241 llvm_unreachable("va_arg for f80 not yet implemented");
8242 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8243 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8244 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8245 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8246 } else {
8247 llvm_unreachable("Unhandled argument type in LowerVAARG");
8248 }
8249
8250 if (ArgMode == 2) {
8251 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008252 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008253 !(DAG.getMachineFunction()
8254 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008255 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008256 }
8257
8258 // Insert VAARG_64 node into the DAG
8259 // VAARG_64 returns two values: Variable Argument Address, Chain
8260 SmallVector<SDValue, 11> InstOps;
8261 InstOps.push_back(Chain);
8262 InstOps.push_back(SrcPtr);
8263 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8264 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8265 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8266 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8267 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8268 VTs, &InstOps[0], InstOps.size(),
8269 MVT::i64,
8270 MachinePointerInfo(SV),
8271 /*Align=*/0,
8272 /*Volatile=*/false,
8273 /*ReadMem=*/true,
8274 /*WriteMem=*/true);
8275 Chain = VAARG.getValue(1);
8276
8277 // Load the next argument and return it
8278 return DAG.getLoad(ArgVT, dl,
8279 Chain,
8280 VAARG,
8281 MachinePointerInfo(),
8282 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008283}
8284
Dan Gohmand858e902010-04-17 15:26:15 +00008285SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008286 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008287 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008288 SDValue Chain = Op.getOperand(0);
8289 SDValue DstPtr = Op.getOperand(1);
8290 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008291 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8292 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008293 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008294
Chris Lattnere72f2022010-09-21 05:40:29 +00008295 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008296 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008297 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008298 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008299}
8300
Dan Gohman475871a2008-07-27 21:46:04 +00008301SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008302X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008303 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008304 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008305 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008306 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008307 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008308 case Intrinsic::x86_sse_comieq_ss:
8309 case Intrinsic::x86_sse_comilt_ss:
8310 case Intrinsic::x86_sse_comile_ss:
8311 case Intrinsic::x86_sse_comigt_ss:
8312 case Intrinsic::x86_sse_comige_ss:
8313 case Intrinsic::x86_sse_comineq_ss:
8314 case Intrinsic::x86_sse_ucomieq_ss:
8315 case Intrinsic::x86_sse_ucomilt_ss:
8316 case Intrinsic::x86_sse_ucomile_ss:
8317 case Intrinsic::x86_sse_ucomigt_ss:
8318 case Intrinsic::x86_sse_ucomige_ss:
8319 case Intrinsic::x86_sse_ucomineq_ss:
8320 case Intrinsic::x86_sse2_comieq_sd:
8321 case Intrinsic::x86_sse2_comilt_sd:
8322 case Intrinsic::x86_sse2_comile_sd:
8323 case Intrinsic::x86_sse2_comigt_sd:
8324 case Intrinsic::x86_sse2_comige_sd:
8325 case Intrinsic::x86_sse2_comineq_sd:
8326 case Intrinsic::x86_sse2_ucomieq_sd:
8327 case Intrinsic::x86_sse2_ucomilt_sd:
8328 case Intrinsic::x86_sse2_ucomile_sd:
8329 case Intrinsic::x86_sse2_ucomigt_sd:
8330 case Intrinsic::x86_sse2_ucomige_sd:
8331 case Intrinsic::x86_sse2_ucomineq_sd: {
8332 unsigned Opc = 0;
8333 ISD::CondCode CC = ISD::SETCC_INVALID;
8334 switch (IntNo) {
8335 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008336 case Intrinsic::x86_sse_comieq_ss:
8337 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008338 Opc = X86ISD::COMI;
8339 CC = ISD::SETEQ;
8340 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008341 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008342 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008343 Opc = X86ISD::COMI;
8344 CC = ISD::SETLT;
8345 break;
8346 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008347 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008348 Opc = X86ISD::COMI;
8349 CC = ISD::SETLE;
8350 break;
8351 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008352 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008353 Opc = X86ISD::COMI;
8354 CC = ISD::SETGT;
8355 break;
8356 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008357 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008358 Opc = X86ISD::COMI;
8359 CC = ISD::SETGE;
8360 break;
8361 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008362 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008363 Opc = X86ISD::COMI;
8364 CC = ISD::SETNE;
8365 break;
8366 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008367 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008368 Opc = X86ISD::UCOMI;
8369 CC = ISD::SETEQ;
8370 break;
8371 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008372 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008373 Opc = X86ISD::UCOMI;
8374 CC = ISD::SETLT;
8375 break;
8376 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008377 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008378 Opc = X86ISD::UCOMI;
8379 CC = ISD::SETLE;
8380 break;
8381 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008382 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008383 Opc = X86ISD::UCOMI;
8384 CC = ISD::SETGT;
8385 break;
8386 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008387 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008388 Opc = X86ISD::UCOMI;
8389 CC = ISD::SETGE;
8390 break;
8391 case Intrinsic::x86_sse_ucomineq_ss:
8392 case Intrinsic::x86_sse2_ucomineq_sd:
8393 Opc = X86ISD::UCOMI;
8394 CC = ISD::SETNE;
8395 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008396 }
Evan Cheng734503b2006-09-11 02:19:56 +00008397
Dan Gohman475871a2008-07-27 21:46:04 +00008398 SDValue LHS = Op.getOperand(1);
8399 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008400 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008401 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008402 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8403 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8404 DAG.getConstant(X86CC, MVT::i8), Cond);
8405 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008406 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008407 // ptest and testp intrinsics. The intrinsic these come from are designed to
8408 // return an integer value, not just an instruction so lower it to the ptest
8409 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008410 case Intrinsic::x86_sse41_ptestz:
8411 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008412 case Intrinsic::x86_sse41_ptestnzc:
8413 case Intrinsic::x86_avx_ptestz_256:
8414 case Intrinsic::x86_avx_ptestc_256:
8415 case Intrinsic::x86_avx_ptestnzc_256:
8416 case Intrinsic::x86_avx_vtestz_ps:
8417 case Intrinsic::x86_avx_vtestc_ps:
8418 case Intrinsic::x86_avx_vtestnzc_ps:
8419 case Intrinsic::x86_avx_vtestz_pd:
8420 case Intrinsic::x86_avx_vtestc_pd:
8421 case Intrinsic::x86_avx_vtestnzc_pd:
8422 case Intrinsic::x86_avx_vtestz_ps_256:
8423 case Intrinsic::x86_avx_vtestc_ps_256:
8424 case Intrinsic::x86_avx_vtestnzc_ps_256:
8425 case Intrinsic::x86_avx_vtestz_pd_256:
8426 case Intrinsic::x86_avx_vtestc_pd_256:
8427 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8428 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008429 unsigned X86CC = 0;
8430 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008431 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008432 case Intrinsic::x86_avx_vtestz_ps:
8433 case Intrinsic::x86_avx_vtestz_pd:
8434 case Intrinsic::x86_avx_vtestz_ps_256:
8435 case Intrinsic::x86_avx_vtestz_pd_256:
8436 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008437 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008438 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008439 // ZF = 1
8440 X86CC = X86::COND_E;
8441 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008442 case Intrinsic::x86_avx_vtestc_ps:
8443 case Intrinsic::x86_avx_vtestc_pd:
8444 case Intrinsic::x86_avx_vtestc_ps_256:
8445 case Intrinsic::x86_avx_vtestc_pd_256:
8446 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008447 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008448 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008449 // CF = 1
8450 X86CC = X86::COND_B;
8451 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008452 case Intrinsic::x86_avx_vtestnzc_ps:
8453 case Intrinsic::x86_avx_vtestnzc_pd:
8454 case Intrinsic::x86_avx_vtestnzc_ps_256:
8455 case Intrinsic::x86_avx_vtestnzc_pd_256:
8456 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008457 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008458 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008459 // ZF and CF = 0
8460 X86CC = X86::COND_A;
8461 break;
8462 }
Eric Christopherfd179292009-08-27 18:07:15 +00008463
Eric Christopher71c67532009-07-29 00:28:05 +00008464 SDValue LHS = Op.getOperand(1);
8465 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008466 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8467 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008468 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8469 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8470 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008471 }
Evan Cheng5759f972008-05-04 09:15:50 +00008472
8473 // Fix vector shift instructions where the last operand is a non-immediate
8474 // i32 value.
8475 case Intrinsic::x86_sse2_pslli_w:
8476 case Intrinsic::x86_sse2_pslli_d:
8477 case Intrinsic::x86_sse2_pslli_q:
8478 case Intrinsic::x86_sse2_psrli_w:
8479 case Intrinsic::x86_sse2_psrli_d:
8480 case Intrinsic::x86_sse2_psrli_q:
8481 case Intrinsic::x86_sse2_psrai_w:
8482 case Intrinsic::x86_sse2_psrai_d:
8483 case Intrinsic::x86_mmx_pslli_w:
8484 case Intrinsic::x86_mmx_pslli_d:
8485 case Intrinsic::x86_mmx_pslli_q:
8486 case Intrinsic::x86_mmx_psrli_w:
8487 case Intrinsic::x86_mmx_psrli_d:
8488 case Intrinsic::x86_mmx_psrli_q:
8489 case Intrinsic::x86_mmx_psrai_w:
8490 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008491 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008492 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008493 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008494
8495 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008496 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008497 switch (IntNo) {
8498 case Intrinsic::x86_sse2_pslli_w:
8499 NewIntNo = Intrinsic::x86_sse2_psll_w;
8500 break;
8501 case Intrinsic::x86_sse2_pslli_d:
8502 NewIntNo = Intrinsic::x86_sse2_psll_d;
8503 break;
8504 case Intrinsic::x86_sse2_pslli_q:
8505 NewIntNo = Intrinsic::x86_sse2_psll_q;
8506 break;
8507 case Intrinsic::x86_sse2_psrli_w:
8508 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8509 break;
8510 case Intrinsic::x86_sse2_psrli_d:
8511 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8512 break;
8513 case Intrinsic::x86_sse2_psrli_q:
8514 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8515 break;
8516 case Intrinsic::x86_sse2_psrai_w:
8517 NewIntNo = Intrinsic::x86_sse2_psra_w;
8518 break;
8519 case Intrinsic::x86_sse2_psrai_d:
8520 NewIntNo = Intrinsic::x86_sse2_psra_d;
8521 break;
8522 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008523 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008524 switch (IntNo) {
8525 case Intrinsic::x86_mmx_pslli_w:
8526 NewIntNo = Intrinsic::x86_mmx_psll_w;
8527 break;
8528 case Intrinsic::x86_mmx_pslli_d:
8529 NewIntNo = Intrinsic::x86_mmx_psll_d;
8530 break;
8531 case Intrinsic::x86_mmx_pslli_q:
8532 NewIntNo = Intrinsic::x86_mmx_psll_q;
8533 break;
8534 case Intrinsic::x86_mmx_psrli_w:
8535 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8536 break;
8537 case Intrinsic::x86_mmx_psrli_d:
8538 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8539 break;
8540 case Intrinsic::x86_mmx_psrli_q:
8541 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8542 break;
8543 case Intrinsic::x86_mmx_psrai_w:
8544 NewIntNo = Intrinsic::x86_mmx_psra_w;
8545 break;
8546 case Intrinsic::x86_mmx_psrai_d:
8547 NewIntNo = Intrinsic::x86_mmx_psra_d;
8548 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008549 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008550 }
8551 break;
8552 }
8553 }
Mon P Wangefa42202009-09-03 19:56:25 +00008554
8555 // The vector shift intrinsics with scalars uses 32b shift amounts but
8556 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8557 // to be zero.
8558 SDValue ShOps[4];
8559 ShOps[0] = ShAmt;
8560 ShOps[1] = DAG.getConstant(0, MVT::i32);
8561 if (ShAmtVT == MVT::v4i32) {
8562 ShOps[2] = DAG.getUNDEF(MVT::i32);
8563 ShOps[3] = DAG.getUNDEF(MVT::i32);
8564 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8565 } else {
8566 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008567// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008568 }
8569
Owen Andersone50ed302009-08-10 22:56:29 +00008570 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008571 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008572 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008573 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008574 Op.getOperand(1), ShAmt);
8575 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008576 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008577}
Evan Cheng72261582005-12-20 06:22:03 +00008578
Dan Gohmand858e902010-04-17 15:26:15 +00008579SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8580 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008581 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8582 MFI->setReturnAddressIsTaken(true);
8583
Bill Wendling64e87322009-01-16 19:25:27 +00008584 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008585 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008586
8587 if (Depth > 0) {
8588 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8589 SDValue Offset =
8590 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008592 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008593 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008594 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008595 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008596 }
8597
8598 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008599 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008600 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008601 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008602}
8603
Dan Gohmand858e902010-04-17 15:26:15 +00008604SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008605 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8606 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008607
Owen Andersone50ed302009-08-10 22:56:29 +00008608 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008609 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008610 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8611 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008612 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008613 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008614 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8615 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008616 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008617 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008618}
8619
Dan Gohman475871a2008-07-27 21:46:04 +00008620SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008621 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008622 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008623}
8624
Dan Gohmand858e902010-04-17 15:26:15 +00008625SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008626 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008627 SDValue Chain = Op.getOperand(0);
8628 SDValue Offset = Op.getOperand(1);
8629 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008630 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008631
Dan Gohmand8816272010-08-11 18:14:00 +00008632 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8633 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8634 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008635 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008636
Dan Gohmand8816272010-08-11 18:14:00 +00008637 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8638 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008639 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008640 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8641 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008642 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008643 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008644
Dale Johannesene4d209d2009-02-03 20:21:25 +00008645 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008646 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008647 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008648}
8649
Dan Gohman475871a2008-07-27 21:46:04 +00008650SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008651 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008652 SDValue Root = Op.getOperand(0);
8653 SDValue Trmp = Op.getOperand(1); // trampoline
8654 SDValue FPtr = Op.getOperand(2); // nested function
8655 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008656 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008657
Dan Gohman69de1932008-02-06 22:27:42 +00008658 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008659
8660 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008661 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008662
8663 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008664 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8665 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008666
Evan Cheng0e6a0522011-07-18 20:57:22 +00008667 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
8668 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008669
8670 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8671
8672 // Load the pointer to the nested function into R11.
8673 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008674 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008675 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008676 Addr, MachinePointerInfo(TrmpAddr),
8677 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008678
Owen Anderson825b72b2009-08-11 20:47:22 +00008679 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8680 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008681 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8682 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008683 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008684
8685 // Load the 'nest' parameter value into R10.
8686 // R10 is specified in X86CallingConv.td
8687 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008688 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8689 DAG.getConstant(10, MVT::i64));
8690 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008691 Addr, MachinePointerInfo(TrmpAddr, 10),
8692 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008693
Owen Anderson825b72b2009-08-11 20:47:22 +00008694 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8695 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008696 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8697 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008698 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008699
8700 // Jump to the nested function.
8701 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008702 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8703 DAG.getConstant(20, MVT::i64));
8704 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008705 Addr, MachinePointerInfo(TrmpAddr, 20),
8706 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008707
8708 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008709 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8710 DAG.getConstant(22, MVT::i64));
8711 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008712 MachinePointerInfo(TrmpAddr, 22),
8713 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008714
Dan Gohman475871a2008-07-27 21:46:04 +00008715 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008716 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008717 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008718 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008719 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008720 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008721 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008722 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008723
8724 switch (CC) {
8725 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008726 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008727 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008728 case CallingConv::X86_StdCall: {
8729 // Pass 'nest' parameter in ECX.
8730 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008731 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008732
8733 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008734 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008735 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008736
Chris Lattner58d74912008-03-12 17:45:29 +00008737 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008738 unsigned InRegCount = 0;
8739 unsigned Idx = 1;
8740
8741 for (FunctionType::param_iterator I = FTy->param_begin(),
8742 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008743 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008744 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008745 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008746
8747 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008748 report_fatal_error("Nest register in use - reduce number of inreg"
8749 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008750 }
8751 }
8752 break;
8753 }
8754 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008755 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008756 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008757 // Pass 'nest' parameter in EAX.
8758 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008759 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008760 break;
8761 }
8762
Dan Gohman475871a2008-07-27 21:46:04 +00008763 SDValue OutChains[4];
8764 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008765
Owen Anderson825b72b2009-08-11 20:47:22 +00008766 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8767 DAG.getConstant(10, MVT::i32));
8768 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008769
Chris Lattnera62fe662010-02-05 19:20:30 +00008770 // This is storing the opcode for MOV32ri.
8771 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00008772 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008773 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008774 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008775 Trmp, MachinePointerInfo(TrmpAddr),
8776 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008777
Owen Anderson825b72b2009-08-11 20:47:22 +00008778 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8779 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008780 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8781 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008782 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008783
Chris Lattnera62fe662010-02-05 19:20:30 +00008784 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008785 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8786 DAG.getConstant(5, MVT::i32));
8787 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008788 MachinePointerInfo(TrmpAddr, 5),
8789 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008790
Owen Anderson825b72b2009-08-11 20:47:22 +00008791 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8792 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008793 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8794 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008795 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008796
Dan Gohman475871a2008-07-27 21:46:04 +00008797 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008798 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008799 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008800 }
8801}
8802
Dan Gohmand858e902010-04-17 15:26:15 +00008803SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8804 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008805 /*
8806 The rounding mode is in bits 11:10 of FPSR, and has the following
8807 settings:
8808 00 Round to nearest
8809 01 Round to -inf
8810 10 Round to +inf
8811 11 Round to 0
8812
8813 FLT_ROUNDS, on the other hand, expects the following:
8814 -1 Undefined
8815 0 Round to 0
8816 1 Round to nearest
8817 2 Round to +inf
8818 3 Round to -inf
8819
8820 To perform the conversion, we do:
8821 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8822 */
8823
8824 MachineFunction &MF = DAG.getMachineFunction();
8825 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008826 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008827 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008828 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008829 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008830
8831 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008832 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008833 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008834
Michael J. Spencerec38de22010-10-10 22:04:20 +00008835
Chris Lattner2156b792010-09-22 01:11:26 +00008836 MachineMemOperand *MMO =
8837 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8838 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008839
Chris Lattner2156b792010-09-22 01:11:26 +00008840 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8841 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8842 DAG.getVTList(MVT::Other),
8843 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008844
8845 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008846 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008847 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008848
8849 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008850 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008851 DAG.getNode(ISD::SRL, DL, MVT::i16,
8852 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008853 CWD, DAG.getConstant(0x800, MVT::i16)),
8854 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008855 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008856 DAG.getNode(ISD::SRL, DL, MVT::i16,
8857 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008858 CWD, DAG.getConstant(0x400, MVT::i16)),
8859 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008860
Dan Gohman475871a2008-07-27 21:46:04 +00008861 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008862 DAG.getNode(ISD::AND, DL, MVT::i16,
8863 DAG.getNode(ISD::ADD, DL, MVT::i16,
8864 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008865 DAG.getConstant(1, MVT::i16)),
8866 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008867
8868
Duncan Sands83ec4b62008-06-06 12:08:01 +00008869 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008870 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008871}
8872
Dan Gohmand858e902010-04-17 15:26:15 +00008873SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008874 EVT VT = Op.getValueType();
8875 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008876 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008877 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008878
8879 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008880 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008881 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008882 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008883 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008884 }
Evan Cheng18efe262007-12-14 02:13:44 +00008885
Evan Cheng152804e2007-12-14 08:30:15 +00008886 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008887 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008888 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008889
8890 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008891 SDValue Ops[] = {
8892 Op,
8893 DAG.getConstant(NumBits+NumBits-1, OpVT),
8894 DAG.getConstant(X86::COND_E, MVT::i8),
8895 Op.getValue(1)
8896 };
8897 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008898
8899 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008900 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008901
Owen Anderson825b72b2009-08-11 20:47:22 +00008902 if (VT == MVT::i8)
8903 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008904 return Op;
8905}
8906
Dan Gohmand858e902010-04-17 15:26:15 +00008907SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008908 EVT VT = Op.getValueType();
8909 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008910 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008911 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008912
8913 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008914 if (VT == MVT::i8) {
8915 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008916 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008917 }
Evan Cheng152804e2007-12-14 08:30:15 +00008918
8919 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008920 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008921 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008922
8923 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008924 SDValue Ops[] = {
8925 Op,
8926 DAG.getConstant(NumBits, OpVT),
8927 DAG.getConstant(X86::COND_E, MVT::i8),
8928 Op.getValue(1)
8929 };
8930 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008931
Owen Anderson825b72b2009-08-11 20:47:22 +00008932 if (VT == MVT::i8)
8933 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008934 return Op;
8935}
8936
Dan Gohmand858e902010-04-17 15:26:15 +00008937SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008938 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008939 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008940 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008941
Mon P Wangaf9b9522008-12-18 21:42:19 +00008942 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8943 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8944 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8945 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8946 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8947 //
8948 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8949 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8950 // return AloBlo + AloBhi + AhiBlo;
8951
8952 SDValue A = Op.getOperand(0);
8953 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008954
Dale Johannesene4d209d2009-02-03 20:21:25 +00008955 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008956 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8957 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008958 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008959 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8960 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008961 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008962 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008963 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008964 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008965 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008966 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008967 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008968 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008969 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008970 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008971 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8972 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008973 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008974 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8975 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008976 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8977 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008978 return Res;
8979}
8980
Nadav Rotem43012222011-05-11 08:12:09 +00008981SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8982
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008983 EVT VT = Op.getValueType();
8984 DebugLoc dl = Op.getDebugLoc();
8985 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008986 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008987
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008988 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008989
Nadav Rotem43012222011-05-11 08:12:09 +00008990 // Must have SSE2.
8991 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008992
Nadav Rotem43012222011-05-11 08:12:09 +00008993 // Optimize shl/srl/sra with constant shift amount.
8994 if (isSplatVector(Amt.getNode())) {
8995 SDValue SclrAmt = Amt->getOperand(0);
8996 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8997 uint64_t ShiftAmt = C->getZExtValue();
8998
8999 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9000 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9001 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9002 R, DAG.getConstant(ShiftAmt, MVT::i32));
9003
9004 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9005 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9006 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9007 R, DAG.getConstant(ShiftAmt, MVT::i32));
9008
9009 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9010 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9011 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9012 R, DAG.getConstant(ShiftAmt, MVT::i32));
9013
9014 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9015 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9016 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9017 R, DAG.getConstant(ShiftAmt, MVT::i32));
9018
9019 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9020 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9021 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9022 R, DAG.getConstant(ShiftAmt, MVT::i32));
9023
9024 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9025 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9026 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9027 R, DAG.getConstant(ShiftAmt, MVT::i32));
9028
9029 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9030 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9031 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9032 R, DAG.getConstant(ShiftAmt, MVT::i32));
9033
9034 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9035 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9036 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9037 R, DAG.getConstant(ShiftAmt, MVT::i32));
9038 }
9039 }
9040
9041 // Lower SHL with variable shift amount.
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009042 // Cannot lower SHL without SSE2 or later.
9043 if (!Subtarget->hasSSE2()) return SDValue();
Nadav Rotem43012222011-05-11 08:12:09 +00009044
9045 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009046 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9047 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9048 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9049
9050 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009051
Nate Begeman51409212010-07-28 00:21:48 +00009052 std::vector<Constant*> CV(4, CI);
9053 Constant *C = ConstantVector::get(CV);
9054 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9055 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009056 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009057 false, false, 16);
9058
9059 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009060 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009061 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9062 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9063 }
Nadav Rotem43012222011-05-11 08:12:09 +00009064 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009065 // a = a << 5;
9066 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9067 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9068 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9069
9070 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9071 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9072
9073 std::vector<Constant*> CVM1(16, CM1);
9074 std::vector<Constant*> CVM2(16, CM2);
9075 Constant *C = ConstantVector::get(CVM1);
9076 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9077 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009078 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009079 false, false, 16);
9080
9081 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9082 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9083 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9084 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9085 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009086 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009087 // a += a
9088 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009089
Nate Begeman51409212010-07-28 00:21:48 +00009090 C = ConstantVector::get(CVM2);
9091 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9092 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009093 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009094 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009095
Nate Begeman51409212010-07-28 00:21:48 +00009096 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9097 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9098 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9099 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9100 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00009101 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009102 // a += a
9103 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009104
Nate Begeman51409212010-07-28 00:21:48 +00009105 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009106 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00009107 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
9108 return R;
9109 }
9110 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009111}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009112
Dan Gohmand858e902010-04-17 15:26:15 +00009113SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009114 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9115 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009116 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9117 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009118 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009119 SDValue LHS = N->getOperand(0);
9120 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009121 unsigned BaseOp = 0;
9122 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009123 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009124 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009125 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009126 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009127 // A subtract of one will be selected as a INC. Note that INC doesn't
9128 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009129 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9130 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009131 BaseOp = X86ISD::INC;
9132 Cond = X86::COND_O;
9133 break;
9134 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009135 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009136 Cond = X86::COND_O;
9137 break;
9138 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009139 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009140 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009141 break;
9142 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009143 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9144 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009145 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9146 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009147 BaseOp = X86ISD::DEC;
9148 Cond = X86::COND_O;
9149 break;
9150 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009151 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009152 Cond = X86::COND_O;
9153 break;
9154 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009155 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009156 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009157 break;
9158 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009159 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009160 Cond = X86::COND_O;
9161 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009162 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9163 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9164 MVT::i32);
9165 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009166
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009167 SDValue SetCC =
9168 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9169 DAG.getConstant(X86::COND_O, MVT::i32),
9170 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009171
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009172 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9173 return Sum;
9174 }
Bill Wendling74c37652008-12-09 22:08:41 +00009175 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009176
Bill Wendling61edeb52008-12-02 01:06:39 +00009177 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009178 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009179 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009180
Bill Wendling61edeb52008-12-02 01:06:39 +00009181 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009182 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9183 DAG.getConstant(Cond, MVT::i32),
9184 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009185
Bill Wendling61edeb52008-12-02 01:06:39 +00009186 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9187 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009188}
9189
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009190SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
9191 DebugLoc dl = Op.getDebugLoc();
9192 SDNode* Node = Op.getNode();
9193 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
9194 EVT VT = Node->getValueType(0);
9195
9196 if (Subtarget->hasSSE2() && VT.isVector()) {
9197 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
9198 ExtraVT.getScalarType().getSizeInBits();
9199 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
9200
9201 unsigned SHLIntrinsicsID = 0;
9202 unsigned SRAIntrinsicsID = 0;
9203 switch (VT.getSimpleVT().SimpleTy) {
9204 default:
9205 return SDValue();
9206 case MVT::v2i64: {
9207 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
9208 SRAIntrinsicsID = 0;
9209 break;
9210 }
9211 case MVT::v4i32: {
9212 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
9213 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
9214 break;
9215 }
9216 case MVT::v8i16: {
9217 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
9218 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
9219 break;
9220 }
9221 }
9222
9223 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9224 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
9225 Node->getOperand(0), ShAmt);
9226
9227 // In case of 1 bit sext, no need to shr
9228 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
9229
9230 if (SRAIntrinsicsID) {
9231 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9232 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
9233 Tmp1, ShAmt);
9234 }
9235 return Tmp1;
9236 }
9237
9238 return SDValue();
9239}
9240
9241
Eric Christopher9a9d2752010-07-22 02:48:34 +00009242SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9243 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009244
Eric Christopher77ed1352011-07-08 00:04:56 +00009245 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
9246 // There isn't any reason to disable it if the target processor supports it.
9247 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009248 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +00009249 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009250 SDValue Ops[] = {
9251 DAG.getRegister(X86::ESP, MVT::i32), // Base
9252 DAG.getTargetConstant(1, MVT::i8), // Scale
9253 DAG.getRegister(0, MVT::i32), // Index
9254 DAG.getTargetConstant(0, MVT::i32), // Disp
9255 DAG.getRegister(0, MVT::i32), // Segment.
9256 Zero,
9257 Chain
9258 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009259 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009260 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9261 array_lengthof(Ops));
9262 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009263 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009264
Eric Christopher9a9d2752010-07-22 02:48:34 +00009265 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009266 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009267 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009268
Chris Lattner132929a2010-08-14 17:26:09 +00009269 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9270 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9271 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9272 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009273
Chris Lattner132929a2010-08-14 17:26:09 +00009274 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9275 if (!Op1 && !Op2 && !Op3 && Op4)
9276 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009277
Chris Lattner132929a2010-08-14 17:26:09 +00009278 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9279 if (Op1 && !Op2 && !Op3 && !Op4)
9280 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009281
9282 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009283 // (MFENCE)>;
9284 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009285}
9286
Dan Gohmand858e902010-04-17 15:26:15 +00009287SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009288 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009289 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009290 unsigned Reg = 0;
9291 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009292 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009293 default:
9294 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009295 case MVT::i8: Reg = X86::AL; size = 1; break;
9296 case MVT::i16: Reg = X86::AX; size = 2; break;
9297 case MVT::i32: Reg = X86::EAX; size = 4; break;
9298 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009299 assert(Subtarget->is64Bit() && "Node not type legal!");
9300 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009301 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009302 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009303 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009304 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009305 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009306 Op.getOperand(1),
9307 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009308 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009309 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009310 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009311 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9312 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9313 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009314 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009315 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009316 return cpOut;
9317}
9318
Duncan Sands1607f052008-12-01 11:39:25 +00009319SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009320 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009321 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009322 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009323 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009324 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009325 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009326 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9327 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009328 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009329 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9330 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009331 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009332 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009333 rdx.getValue(1)
9334 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009335 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009336}
9337
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009338SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009339 SelectionDAG &DAG) const {
9340 EVT SrcVT = Op.getOperand(0).getValueType();
9341 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009342 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9343 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009344 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009345 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009346 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009347 // i64 <=> MMX conversions are Legal.
9348 if (SrcVT==MVT::i64 && DstVT.isVector())
9349 return Op;
9350 if (DstVT==MVT::i64 && SrcVT.isVector())
9351 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009352 // MMX <=> MMX conversions are Legal.
9353 if (SrcVT.isVector() && DstVT.isVector())
9354 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009355 // All other conversions need to be expanded.
9356 return SDValue();
9357}
Chris Lattner5b856542010-12-20 00:59:46 +00009358
Dan Gohmand858e902010-04-17 15:26:15 +00009359SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009360 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009361 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009362 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009363 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009364 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009365 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009366 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009367 Node->getOperand(0),
9368 Node->getOperand(1), negOp,
9369 cast<AtomicSDNode>(Node)->getSrcValue(),
9370 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009371}
9372
Chris Lattner5b856542010-12-20 00:59:46 +00009373static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9374 EVT VT = Op.getNode()->getValueType(0);
9375
9376 // Let legalize expand this if it isn't a legal type yet.
9377 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9378 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009379
Chris Lattner5b856542010-12-20 00:59:46 +00009380 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009381
Chris Lattner5b856542010-12-20 00:59:46 +00009382 unsigned Opc;
9383 bool ExtraOp = false;
9384 switch (Op.getOpcode()) {
9385 default: assert(0 && "Invalid code");
9386 case ISD::ADDC: Opc = X86ISD::ADD; break;
9387 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9388 case ISD::SUBC: Opc = X86ISD::SUB; break;
9389 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9390 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009391
Chris Lattner5b856542010-12-20 00:59:46 +00009392 if (!ExtraOp)
9393 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9394 Op.getOperand(1));
9395 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9396 Op.getOperand(1), Op.getOperand(2));
9397}
9398
Evan Cheng0db9fe62006-04-25 20:13:52 +00009399/// LowerOperation - Provide custom lowering hooks for some operations.
9400///
Dan Gohmand858e902010-04-17 15:26:15 +00009401SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009402 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009403 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009404 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +00009405 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009406 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9407 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009408 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009409 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009410 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9411 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9412 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009413 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009414 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009415 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9416 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9417 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009418 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009419 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009420 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009421 case ISD::SHL_PARTS:
9422 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009423 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009424 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009425 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009426 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009427 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009428 case ISD::FABS: return LowerFABS(Op, DAG);
9429 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009430 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009431 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009432 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009433 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009434 case ISD::SELECT: return LowerSELECT(Op, DAG);
9435 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009436 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009437 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009438 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009439 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009440 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009441 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9442 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009443 case ISD::FRAME_TO_ARGS_OFFSET:
9444 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009445 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009446 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009447 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009448 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009449 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9450 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009451 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009452 case ISD::SRA:
9453 case ISD::SRL:
9454 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009455 case ISD::SADDO:
9456 case ISD::UADDO:
9457 case ISD::SSUBO:
9458 case ISD::USUBO:
9459 case ISD::SMULO:
9460 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009461 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009462 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009463 case ISD::ADDC:
9464 case ISD::ADDE:
9465 case ISD::SUBC:
9466 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009467 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009468}
9469
Duncan Sands1607f052008-12-01 11:39:25 +00009470void X86TargetLowering::
9471ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009472 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009473 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009474 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009475 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009476
9477 SDValue Chain = Node->getOperand(0);
9478 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009479 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009480 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009481 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009482 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009483 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009484 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009485 SDValue Result =
9486 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9487 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009488 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009489 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009490 Results.push_back(Result.getValue(2));
9491}
9492
Duncan Sands126d9072008-07-04 11:47:58 +00009493/// ReplaceNodeResults - Replace a node with an illegal result type
9494/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009495void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9496 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009497 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009498 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009499 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009500 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009501 assert(false && "Do not know how to custom type legalize this operation!");
9502 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009503 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +00009504 case ISD::ADDC:
9505 case ISD::ADDE:
9506 case ISD::SUBC:
9507 case ISD::SUBE:
9508 // We don't want to expand or promote these.
9509 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009510 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009511 std::pair<SDValue,SDValue> Vals =
9512 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009513 SDValue FIST = Vals.first, StackSlot = Vals.second;
9514 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009515 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009516 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009517 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9518 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009519 }
9520 return;
9521 }
9522 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009523 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009524 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009525 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009526 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009527 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009528 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009529 eax.getValue(2));
9530 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9531 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009532 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009533 Results.push_back(edx.getValue(1));
9534 return;
9535 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009536 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009537 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009538 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009539 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009540 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9541 DAG.getConstant(0, MVT::i32));
9542 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9543 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009544 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9545 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009546 cpInL.getValue(1));
9547 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009548 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9549 DAG.getConstant(0, MVT::i32));
9550 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9551 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009552 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009553 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009554 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009555 swapInL.getValue(1));
9556 SDValue Ops[] = { swapInH.getValue(0),
9557 N->getOperand(1),
9558 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009559 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009560 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9561 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9562 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009563 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009564 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009565 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009566 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009567 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009568 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009569 Results.push_back(cpOutH.getValue(1));
9570 return;
9571 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009572 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009573 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9574 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009575 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009576 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9577 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009578 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009579 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9580 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009581 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009582 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9583 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009584 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009585 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9586 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009587 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009588 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9589 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009590 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009591 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9592 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009593 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009594}
9595
Evan Cheng72261582005-12-20 06:22:03 +00009596const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9597 switch (Opcode) {
9598 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009599 case X86ISD::BSF: return "X86ISD::BSF";
9600 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009601 case X86ISD::SHLD: return "X86ISD::SHLD";
9602 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009603 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009604 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009605 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009606 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009607 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009608 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009609 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9610 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9611 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009612 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009613 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009614 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009615 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009616 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009617 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009618 case X86ISD::COMI: return "X86ISD::COMI";
9619 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009620 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009621 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009622 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9623 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009624 case X86ISD::CMOV: return "X86ISD::CMOV";
9625 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009626 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009627 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9628 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009629 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009630 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009631 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009632 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009633 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009634 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9635 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009636 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009637 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +00009638 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +00009639 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9640 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9641 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009642 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009643 case X86ISD::FMAX: return "X86ISD::FMAX";
9644 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009645 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9646 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009647 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009648 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009649 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009650 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009651 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009652 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9653 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009654 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9655 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9656 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9657 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9658 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9659 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009660 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9661 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009662 case X86ISD::VSHL: return "X86ISD::VSHL";
9663 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009664 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9665 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9666 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9667 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9668 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9669 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9670 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9671 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9672 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9673 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009674 case X86ISD::ADD: return "X86ISD::ADD";
9675 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009676 case X86ISD::ADC: return "X86ISD::ADC";
9677 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009678 case X86ISD::SMUL: return "X86ISD::SMUL";
9679 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009680 case X86ISD::INC: return "X86ISD::INC";
9681 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009682 case X86ISD::OR: return "X86ISD::OR";
9683 case X86ISD::XOR: return "X86ISD::XOR";
9684 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009685 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009686 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009687 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009688 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9689 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9690 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9691 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9692 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9693 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9694 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9695 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9696 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009697 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009698 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009699 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009700 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9701 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009702 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9703 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9704 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9705 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9706 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9707 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9708 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9709 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9710 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009711 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9712 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9713 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9714 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009715 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9716 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9717 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9718 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9719 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9720 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9721 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9722 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9723 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9724 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00009725 case X86ISD::VPERMIL: return "X86ISD::VPERMIL";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009726 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009727 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009728 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009729 }
9730}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009731
Chris Lattnerc9addb72007-03-30 23:15:24 +00009732// isLegalAddressingMode - Return true if the addressing mode represented
9733// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009734bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009735 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00009736 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009737 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009738 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009739
Chris Lattnerc9addb72007-03-30 23:15:24 +00009740 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009741 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009742 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009743
Chris Lattnerc9addb72007-03-30 23:15:24 +00009744 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009745 unsigned GVFlags =
9746 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009747
Chris Lattnerdfed4132009-07-10 07:38:24 +00009748 // If a reference to this global requires an extra load, we can't fold it.
9749 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009750 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009751
Chris Lattnerdfed4132009-07-10 07:38:24 +00009752 // If BaseGV requires a register for the PIC base, we cannot also have a
9753 // BaseReg specified.
9754 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009755 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009756
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009757 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009758 if ((M != CodeModel::Small || R != Reloc::Static) &&
9759 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009760 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009761 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009762
Chris Lattnerc9addb72007-03-30 23:15:24 +00009763 switch (AM.Scale) {
9764 case 0:
9765 case 1:
9766 case 2:
9767 case 4:
9768 case 8:
9769 // These scales always work.
9770 break;
9771 case 3:
9772 case 5:
9773 case 9:
9774 // These scales are formed with basereg+scalereg. Only accept if there is
9775 // no basereg yet.
9776 if (AM.HasBaseReg)
9777 return false;
9778 break;
9779 default: // Other stuff never works.
9780 return false;
9781 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009782
Chris Lattnerc9addb72007-03-30 23:15:24 +00009783 return true;
9784}
9785
9786
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009787bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009788 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009789 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009790 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9791 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009792 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009793 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009794 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009795}
9796
Owen Andersone50ed302009-08-10 22:56:29 +00009797bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009798 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009799 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009800 unsigned NumBits1 = VT1.getSizeInBits();
9801 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009802 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009803 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009804 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009805}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009806
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009807bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009808 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009809 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009810}
9811
Owen Andersone50ed302009-08-10 22:56:29 +00009812bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009813 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009814 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009815}
9816
Owen Andersone50ed302009-08-10 22:56:29 +00009817bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009818 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009819 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009820}
9821
Evan Cheng60c07e12006-07-05 22:17:51 +00009822/// isShuffleMaskLegal - Targets can use this to indicate that they only
9823/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9824/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9825/// are assumed to be legal.
9826bool
Eric Christopherfd179292009-08-27 18:07:15 +00009827X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009828 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009829 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009830 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009831 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009832
Nate Begemana09008b2009-10-19 02:17:23 +00009833 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009834 return (VT.getVectorNumElements() == 2 ||
9835 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9836 isMOVLMask(M, VT) ||
9837 isSHUFPMask(M, VT) ||
9838 isPSHUFDMask(M, VT) ||
9839 isPSHUFHWMask(M, VT) ||
9840 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009841 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009842 isUNPCKLMask(M, VT) ||
9843 isUNPCKHMask(M, VT) ||
9844 isUNPCKL_v_undef_Mask(M, VT) ||
9845 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009846}
9847
Dan Gohman7d8143f2008-04-09 20:09:42 +00009848bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009849X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009850 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009851 unsigned NumElts = VT.getVectorNumElements();
9852 // FIXME: This collection of masks seems suspect.
9853 if (NumElts == 2)
9854 return true;
9855 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9856 return (isMOVLMask(Mask, VT) ||
9857 isCommutedMOVLMask(Mask, VT, true) ||
9858 isSHUFPMask(Mask, VT) ||
9859 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009860 }
9861 return false;
9862}
9863
9864//===----------------------------------------------------------------------===//
9865// X86 Scheduler Hooks
9866//===----------------------------------------------------------------------===//
9867
Mon P Wang63307c32008-05-05 19:05:59 +00009868// private utility function
9869MachineBasicBlock *
9870X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9871 MachineBasicBlock *MBB,
9872 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009873 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009874 unsigned LoadOpc,
9875 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009876 unsigned notOpc,
9877 unsigned EAXreg,
9878 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009879 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009880 // For the atomic bitwise operator, we generate
9881 // thisMBB:
9882 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009883 // ld t1 = [bitinstr.addr]
9884 // op t2 = t1, [bitinstr.val]
9885 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009886 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9887 // bz newMBB
9888 // fallthrough -->nextMBB
9889 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9890 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009891 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009892 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009893
Mon P Wang63307c32008-05-05 19:05:59 +00009894 /// First build the CFG
9895 MachineFunction *F = MBB->getParent();
9896 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009897 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9898 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9899 F->insert(MBBIter, newMBB);
9900 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009901
Dan Gohman14152b42010-07-06 20:24:04 +00009902 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9903 nextMBB->splice(nextMBB->begin(), thisMBB,
9904 llvm::next(MachineBasicBlock::iterator(bInstr)),
9905 thisMBB->end());
9906 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009907
Mon P Wang63307c32008-05-05 19:05:59 +00009908 // Update thisMBB to fall through to newMBB
9909 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009910
Mon P Wang63307c32008-05-05 19:05:59 +00009911 // newMBB jumps to itself and fall through to nextMBB
9912 newMBB->addSuccessor(nextMBB);
9913 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009914
Mon P Wang63307c32008-05-05 19:05:59 +00009915 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009916 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009917 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009918 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009919 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009920 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009921 int numArgs = bInstr->getNumOperands() - 1;
9922 for (int i=0; i < numArgs; ++i)
9923 argOpers[i] = &bInstr->getOperand(i+1);
9924
9925 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009926 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009927 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009928
Dale Johannesen140be2d2008-08-19 18:47:28 +00009929 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009930 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009931 for (int i=0; i <= lastAddrIndx; ++i)
9932 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009933
Dale Johannesen140be2d2008-08-19 18:47:28 +00009934 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009935 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009936 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009938 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009939 tt = t1;
9940
Dale Johannesen140be2d2008-08-19 18:47:28 +00009941 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009942 assert((argOpers[valArgIndx]->isReg() ||
9943 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009944 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009945 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009946 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009947 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009948 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009949 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009950 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009951
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009952 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009953 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009954
Dale Johannesene4d209d2009-02-03 20:21:25 +00009955 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009956 for (int i=0; i <= lastAddrIndx; ++i)
9957 (*MIB).addOperand(*argOpers[i]);
9958 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009959 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009960 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9961 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009962
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009963 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009964 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009965
Mon P Wang63307c32008-05-05 19:05:59 +00009966 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009967 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009968
Dan Gohman14152b42010-07-06 20:24:04 +00009969 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009970 return nextMBB;
9971}
9972
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009973// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009974MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009975X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9976 MachineBasicBlock *MBB,
9977 unsigned regOpcL,
9978 unsigned regOpcH,
9979 unsigned immOpcL,
9980 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009981 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009982 // For the atomic bitwise operator, we generate
9983 // thisMBB (instructions are in pairs, except cmpxchg8b)
9984 // ld t1,t2 = [bitinstr.addr]
9985 // newMBB:
9986 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9987 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009988 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009989 // mov ECX, EBX <- t5, t6
9990 // mov EAX, EDX <- t1, t2
9991 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9992 // mov t3, t4 <- EAX, EDX
9993 // bz newMBB
9994 // result in out1, out2
9995 // fallthrough -->nextMBB
9996
9997 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9998 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009999 const unsigned NotOpc = X86::NOT32r;
10000 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10001 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10002 MachineFunction::iterator MBBIter = MBB;
10003 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010004
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010005 /// First build the CFG
10006 MachineFunction *F = MBB->getParent();
10007 MachineBasicBlock *thisMBB = MBB;
10008 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10009 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10010 F->insert(MBBIter, newMBB);
10011 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010012
Dan Gohman14152b42010-07-06 20:24:04 +000010013 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10014 nextMBB->splice(nextMBB->begin(), thisMBB,
10015 llvm::next(MachineBasicBlock::iterator(bInstr)),
10016 thisMBB->end());
10017 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010018
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010019 // Update thisMBB to fall through to newMBB
10020 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010021
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010022 // newMBB jumps to itself and fall through to nextMBB
10023 newMBB->addSuccessor(nextMBB);
10024 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010025
Dale Johannesene4d209d2009-02-03 20:21:25 +000010026 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010027 // Insert instructions into newMBB based on incoming instruction
10028 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010029 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010030 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010031 MachineOperand& dest1Oper = bInstr->getOperand(0);
10032 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010033 MachineOperand* argOpers[2 + X86::AddrNumOperands];
10034 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010035 argOpers[i] = &bInstr->getOperand(i+2);
10036
Dan Gohman71ea4e52010-05-14 21:01:44 +000010037 // We use some of the operands multiple times, so conservatively just
10038 // clear any kill flags that might be present.
10039 if (argOpers[i]->isReg() && argOpers[i]->isUse())
10040 argOpers[i]->setIsKill(false);
10041 }
10042
Evan Chengad5b52f2010-01-08 19:14:57 +000010043 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010044 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000010045
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010046 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010047 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010048 for (int i=0; i <= lastAddrIndx; ++i)
10049 (*MIB).addOperand(*argOpers[i]);
10050 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010051 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000010052 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000010053 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010054 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000010055 MachineOperand newOp3 = *(argOpers[3]);
10056 if (newOp3.isImm())
10057 newOp3.setImm(newOp3.getImm()+4);
10058 else
10059 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010060 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000010061 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010062
10063 // t3/4 are defined later, at the bottom of the loop
10064 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
10065 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010066 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010067 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010068 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010069 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
10070
Evan Cheng306b4ca2010-01-08 23:41:50 +000010071 // The subsequent operations should be using the destination registers of
10072 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000010073 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000010074 t1 = F->getRegInfo().createVirtualRegister(RC);
10075 t2 = F->getRegInfo().createVirtualRegister(RC);
10076 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
10077 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010078 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000010079 t1 = dest1Oper.getReg();
10080 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010081 }
10082
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010083 int valArgIndx = lastAddrIndx + 1;
10084 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000010085 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010086 "invalid operand");
10087 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
10088 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010089 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010090 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010091 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010092 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000010093 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000010094 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010095 (*MIB).addOperand(*argOpers[valArgIndx]);
10096 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000010097 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010098 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000010099 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010100 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010101 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010102 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010103 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000010104 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000010105 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010106 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010107
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010108 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010109 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010110 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010111 MIB.addReg(t2);
10112
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010113 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010114 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010115 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010116 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000010117
Dale Johannesene4d209d2009-02-03 20:21:25 +000010118 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010119 for (int i=0; i <= lastAddrIndx; ++i)
10120 (*MIB).addOperand(*argOpers[i]);
10121
10122 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010123 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10124 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010125
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010126 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010127 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010128 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010129 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010130
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010131 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010132 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010133
Dan Gohman14152b42010-07-06 20:24:04 +000010134 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010135 return nextMBB;
10136}
10137
10138// private utility function
10139MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000010140X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
10141 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010142 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010143 // For the atomic min/max operator, we generate
10144 // thisMBB:
10145 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010146 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000010147 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000010148 // cmp t1, t2
10149 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000010150 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010151 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10152 // bz newMBB
10153 // fallthrough -->nextMBB
10154 //
10155 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10156 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010157 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010158 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010159
Mon P Wang63307c32008-05-05 19:05:59 +000010160 /// First build the CFG
10161 MachineFunction *F = MBB->getParent();
10162 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010163 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10164 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10165 F->insert(MBBIter, newMBB);
10166 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010167
Dan Gohman14152b42010-07-06 20:24:04 +000010168 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10169 nextMBB->splice(nextMBB->begin(), thisMBB,
10170 llvm::next(MachineBasicBlock::iterator(mInstr)),
10171 thisMBB->end());
10172 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010173
Mon P Wang63307c32008-05-05 19:05:59 +000010174 // Update thisMBB to fall through to newMBB
10175 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010176
Mon P Wang63307c32008-05-05 19:05:59 +000010177 // newMBB jumps to newMBB and fall through to nextMBB
10178 newMBB->addSuccessor(nextMBB);
10179 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010180
Dale Johannesene4d209d2009-02-03 20:21:25 +000010181 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010182 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010183 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010184 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010185 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010186 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010187 int numArgs = mInstr->getNumOperands() - 1;
10188 for (int i=0; i < numArgs; ++i)
10189 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010190
Mon P Wang63307c32008-05-05 19:05:59 +000010191 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010192 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010193 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010194
Mon P Wangab3e7472008-05-05 22:56:23 +000010195 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010196 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010197 for (int i=0; i <= lastAddrIndx; ++i)
10198 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010199
Mon P Wang63307c32008-05-05 19:05:59 +000010200 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010201 assert((argOpers[valArgIndx]->isReg() ||
10202 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010203 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010204
10205 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010206 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010207 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010208 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010209 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010210 (*MIB).addOperand(*argOpers[valArgIndx]);
10211
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010212 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010213 MIB.addReg(t1);
10214
Dale Johannesene4d209d2009-02-03 20:21:25 +000010215 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010216 MIB.addReg(t1);
10217 MIB.addReg(t2);
10218
10219 // Generate movc
10220 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010221 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010222 MIB.addReg(t2);
10223 MIB.addReg(t1);
10224
10225 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010226 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010227 for (int i=0; i <= lastAddrIndx; ++i)
10228 (*MIB).addOperand(*argOpers[i]);
10229 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010230 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010231 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10232 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010233
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010234 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010235 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010236
Mon P Wang63307c32008-05-05 19:05:59 +000010237 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010238 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010239
Dan Gohman14152b42010-07-06 20:24:04 +000010240 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010241 return nextMBB;
10242}
10243
Eric Christopherf83a5de2009-08-27 18:08:16 +000010244// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010245// or XMM0_V32I8 in AVX all of this code can be replaced with that
10246// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010247MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010248X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010249 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010250 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10251 "Target must have SSE4.2 or AVX features enabled");
10252
Eric Christopherb120ab42009-08-18 22:50:32 +000010253 DebugLoc dl = MI->getDebugLoc();
10254 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010255 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010256 if (!Subtarget->hasAVX()) {
10257 if (memArg)
10258 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10259 else
10260 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10261 } else {
10262 if (memArg)
10263 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10264 else
10265 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10266 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010267
Eric Christopher41c902f2010-11-30 08:20:21 +000010268 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010269 for (unsigned i = 0; i < numArgs; ++i) {
10270 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010271 if (!(Op.isReg() && Op.isImplicit()))
10272 MIB.addOperand(Op);
10273 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010274 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010275 .addReg(X86::XMM0);
10276
Dan Gohman14152b42010-07-06 20:24:04 +000010277 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010278 return BB;
10279}
10280
10281MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010282X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010283 DebugLoc dl = MI->getDebugLoc();
10284 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010285
Eric Christopher228232b2010-11-30 07:20:12 +000010286 // Address into RAX/EAX, other two args into ECX, EDX.
10287 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10288 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10289 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10290 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010291 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010292
Eric Christopher228232b2010-11-30 07:20:12 +000010293 unsigned ValOps = X86::AddrNumOperands;
10294 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10295 .addReg(MI->getOperand(ValOps).getReg());
10296 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10297 .addReg(MI->getOperand(ValOps+1).getReg());
10298
10299 // The instruction doesn't actually take any operands though.
10300 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010301
Eric Christopher228232b2010-11-30 07:20:12 +000010302 MI->eraseFromParent(); // The pseudo is gone now.
10303 return BB;
10304}
10305
10306MachineBasicBlock *
10307X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010308 DebugLoc dl = MI->getDebugLoc();
10309 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010310
Eric Christopher228232b2010-11-30 07:20:12 +000010311 // First arg in ECX, the second in EAX.
10312 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10313 .addReg(MI->getOperand(0).getReg());
10314 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10315 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010316
Eric Christopher228232b2010-11-30 07:20:12 +000010317 // The instruction doesn't actually take any operands though.
10318 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010319
Eric Christopher228232b2010-11-30 07:20:12 +000010320 MI->eraseFromParent(); // The pseudo is gone now.
10321 return BB;
10322}
10323
10324MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010325X86TargetLowering::EmitVAARG64WithCustomInserter(
10326 MachineInstr *MI,
10327 MachineBasicBlock *MBB) const {
10328 // Emit va_arg instruction on X86-64.
10329
10330 // Operands to this pseudo-instruction:
10331 // 0 ) Output : destination address (reg)
10332 // 1-5) Input : va_list address (addr, i64mem)
10333 // 6 ) ArgSize : Size (in bytes) of vararg type
10334 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10335 // 8 ) Align : Alignment of type
10336 // 9 ) EFLAGS (implicit-def)
10337
10338 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10339 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10340
10341 unsigned DestReg = MI->getOperand(0).getReg();
10342 MachineOperand &Base = MI->getOperand(1);
10343 MachineOperand &Scale = MI->getOperand(2);
10344 MachineOperand &Index = MI->getOperand(3);
10345 MachineOperand &Disp = MI->getOperand(4);
10346 MachineOperand &Segment = MI->getOperand(5);
10347 unsigned ArgSize = MI->getOperand(6).getImm();
10348 unsigned ArgMode = MI->getOperand(7).getImm();
10349 unsigned Align = MI->getOperand(8).getImm();
10350
10351 // Memory Reference
10352 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10353 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10354 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10355
10356 // Machine Information
10357 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10358 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10359 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10360 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10361 DebugLoc DL = MI->getDebugLoc();
10362
10363 // struct va_list {
10364 // i32 gp_offset
10365 // i32 fp_offset
10366 // i64 overflow_area (address)
10367 // i64 reg_save_area (address)
10368 // }
10369 // sizeof(va_list) = 24
10370 // alignment(va_list) = 8
10371
10372 unsigned TotalNumIntRegs = 6;
10373 unsigned TotalNumXMMRegs = 8;
10374 bool UseGPOffset = (ArgMode == 1);
10375 bool UseFPOffset = (ArgMode == 2);
10376 unsigned MaxOffset = TotalNumIntRegs * 8 +
10377 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10378
10379 /* Align ArgSize to a multiple of 8 */
10380 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10381 bool NeedsAlign = (Align > 8);
10382
10383 MachineBasicBlock *thisMBB = MBB;
10384 MachineBasicBlock *overflowMBB;
10385 MachineBasicBlock *offsetMBB;
10386 MachineBasicBlock *endMBB;
10387
10388 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10389 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10390 unsigned OffsetReg = 0;
10391
10392 if (!UseGPOffset && !UseFPOffset) {
10393 // If we only pull from the overflow region, we don't create a branch.
10394 // We don't need to alter control flow.
10395 OffsetDestReg = 0; // unused
10396 OverflowDestReg = DestReg;
10397
10398 offsetMBB = NULL;
10399 overflowMBB = thisMBB;
10400 endMBB = thisMBB;
10401 } else {
10402 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10403 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10404 // If not, pull from overflow_area. (branch to overflowMBB)
10405 //
10406 // thisMBB
10407 // | .
10408 // | .
10409 // offsetMBB overflowMBB
10410 // | .
10411 // | .
10412 // endMBB
10413
10414 // Registers for the PHI in endMBB
10415 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10416 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10417
10418 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10419 MachineFunction *MF = MBB->getParent();
10420 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10421 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10422 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10423
10424 MachineFunction::iterator MBBIter = MBB;
10425 ++MBBIter;
10426
10427 // Insert the new basic blocks
10428 MF->insert(MBBIter, offsetMBB);
10429 MF->insert(MBBIter, overflowMBB);
10430 MF->insert(MBBIter, endMBB);
10431
10432 // Transfer the remainder of MBB and its successor edges to endMBB.
10433 endMBB->splice(endMBB->begin(), thisMBB,
10434 llvm::next(MachineBasicBlock::iterator(MI)),
10435 thisMBB->end());
10436 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10437
10438 // Make offsetMBB and overflowMBB successors of thisMBB
10439 thisMBB->addSuccessor(offsetMBB);
10440 thisMBB->addSuccessor(overflowMBB);
10441
10442 // endMBB is a successor of both offsetMBB and overflowMBB
10443 offsetMBB->addSuccessor(endMBB);
10444 overflowMBB->addSuccessor(endMBB);
10445
10446 // Load the offset value into a register
10447 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10448 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10449 .addOperand(Base)
10450 .addOperand(Scale)
10451 .addOperand(Index)
10452 .addDisp(Disp, UseFPOffset ? 4 : 0)
10453 .addOperand(Segment)
10454 .setMemRefs(MMOBegin, MMOEnd);
10455
10456 // Check if there is enough room left to pull this argument.
10457 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10458 .addReg(OffsetReg)
10459 .addImm(MaxOffset + 8 - ArgSizeA8);
10460
10461 // Branch to "overflowMBB" if offset >= max
10462 // Fall through to "offsetMBB" otherwise
10463 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10464 .addMBB(overflowMBB);
10465 }
10466
10467 // In offsetMBB, emit code to use the reg_save_area.
10468 if (offsetMBB) {
10469 assert(OffsetReg != 0);
10470
10471 // Read the reg_save_area address.
10472 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10473 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10474 .addOperand(Base)
10475 .addOperand(Scale)
10476 .addOperand(Index)
10477 .addDisp(Disp, 16)
10478 .addOperand(Segment)
10479 .setMemRefs(MMOBegin, MMOEnd);
10480
10481 // Zero-extend the offset
10482 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10483 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10484 .addImm(0)
10485 .addReg(OffsetReg)
10486 .addImm(X86::sub_32bit);
10487
10488 // Add the offset to the reg_save_area to get the final address.
10489 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10490 .addReg(OffsetReg64)
10491 .addReg(RegSaveReg);
10492
10493 // Compute the offset for the next argument
10494 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10495 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10496 .addReg(OffsetReg)
10497 .addImm(UseFPOffset ? 16 : 8);
10498
10499 // Store it back into the va_list.
10500 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10501 .addOperand(Base)
10502 .addOperand(Scale)
10503 .addOperand(Index)
10504 .addDisp(Disp, UseFPOffset ? 4 : 0)
10505 .addOperand(Segment)
10506 .addReg(NextOffsetReg)
10507 .setMemRefs(MMOBegin, MMOEnd);
10508
10509 // Jump to endMBB
10510 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10511 .addMBB(endMBB);
10512 }
10513
10514 //
10515 // Emit code to use overflow area
10516 //
10517
10518 // Load the overflow_area address into a register.
10519 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10520 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10521 .addOperand(Base)
10522 .addOperand(Scale)
10523 .addOperand(Index)
10524 .addDisp(Disp, 8)
10525 .addOperand(Segment)
10526 .setMemRefs(MMOBegin, MMOEnd);
10527
10528 // If we need to align it, do so. Otherwise, just copy the address
10529 // to OverflowDestReg.
10530 if (NeedsAlign) {
10531 // Align the overflow address
10532 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10533 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10534
10535 // aligned_addr = (addr + (align-1)) & ~(align-1)
10536 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10537 .addReg(OverflowAddrReg)
10538 .addImm(Align-1);
10539
10540 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10541 .addReg(TmpReg)
10542 .addImm(~(uint64_t)(Align-1));
10543 } else {
10544 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10545 .addReg(OverflowAddrReg);
10546 }
10547
10548 // Compute the next overflow address after this argument.
10549 // (the overflow address should be kept 8-byte aligned)
10550 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10551 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10552 .addReg(OverflowDestReg)
10553 .addImm(ArgSizeA8);
10554
10555 // Store the new overflow address.
10556 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10557 .addOperand(Base)
10558 .addOperand(Scale)
10559 .addOperand(Index)
10560 .addDisp(Disp, 8)
10561 .addOperand(Segment)
10562 .addReg(NextAddrReg)
10563 .setMemRefs(MMOBegin, MMOEnd);
10564
10565 // If we branched, emit the PHI to the front of endMBB.
10566 if (offsetMBB) {
10567 BuildMI(*endMBB, endMBB->begin(), DL,
10568 TII->get(X86::PHI), DestReg)
10569 .addReg(OffsetDestReg).addMBB(offsetMBB)
10570 .addReg(OverflowDestReg).addMBB(overflowMBB);
10571 }
10572
10573 // Erase the pseudo instruction
10574 MI->eraseFromParent();
10575
10576 return endMBB;
10577}
10578
10579MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010580X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10581 MachineInstr *MI,
10582 MachineBasicBlock *MBB) const {
10583 // Emit code to save XMM registers to the stack. The ABI says that the
10584 // number of registers to save is given in %al, so it's theoretically
10585 // possible to do an indirect jump trick to avoid saving all of them,
10586 // however this code takes a simpler approach and just executes all
10587 // of the stores if %al is non-zero. It's less code, and it's probably
10588 // easier on the hardware branch predictor, and stores aren't all that
10589 // expensive anyway.
10590
10591 // Create the new basic blocks. One block contains all the XMM stores,
10592 // and one block is the final destination regardless of whether any
10593 // stores were performed.
10594 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10595 MachineFunction *F = MBB->getParent();
10596 MachineFunction::iterator MBBIter = MBB;
10597 ++MBBIter;
10598 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10599 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10600 F->insert(MBBIter, XMMSaveMBB);
10601 F->insert(MBBIter, EndMBB);
10602
Dan Gohman14152b42010-07-06 20:24:04 +000010603 // Transfer the remainder of MBB and its successor edges to EndMBB.
10604 EndMBB->splice(EndMBB->begin(), MBB,
10605 llvm::next(MachineBasicBlock::iterator(MI)),
10606 MBB->end());
10607 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10608
Dan Gohmand6708ea2009-08-15 01:38:56 +000010609 // The original block will now fall through to the XMM save block.
10610 MBB->addSuccessor(XMMSaveMBB);
10611 // The XMMSaveMBB will fall through to the end block.
10612 XMMSaveMBB->addSuccessor(EndMBB);
10613
10614 // Now add the instructions.
10615 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10616 DebugLoc DL = MI->getDebugLoc();
10617
10618 unsigned CountReg = MI->getOperand(0).getReg();
10619 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10620 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10621
10622 if (!Subtarget->isTargetWin64()) {
10623 // If %al is 0, branch around the XMM save block.
10624 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010625 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010626 MBB->addSuccessor(EndMBB);
10627 }
10628
10629 // In the XMM save block, save all the XMM argument registers.
10630 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10631 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010632 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010633 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010634 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010635 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010636 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010637 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10638 .addFrameIndex(RegSaveFrameIndex)
10639 .addImm(/*Scale=*/1)
10640 .addReg(/*IndexReg=*/0)
10641 .addImm(/*Disp=*/Offset)
10642 .addReg(/*Segment=*/0)
10643 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010644 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010645 }
10646
Dan Gohman14152b42010-07-06 20:24:04 +000010647 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010648
10649 return EndMBB;
10650}
Mon P Wang63307c32008-05-05 19:05:59 +000010651
Evan Cheng60c07e12006-07-05 22:17:51 +000010652MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010653X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010654 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010655 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10656 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010657
Chris Lattner52600972009-09-02 05:57:00 +000010658 // To "insert" a SELECT_CC instruction, we actually have to insert the
10659 // diamond control-flow pattern. The incoming instruction knows the
10660 // destination vreg to set, the condition code register to branch on, the
10661 // true/false values to select between, and a branch opcode to use.
10662 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10663 MachineFunction::iterator It = BB;
10664 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010665
Chris Lattner52600972009-09-02 05:57:00 +000010666 // thisMBB:
10667 // ...
10668 // TrueVal = ...
10669 // cmpTY ccX, r1, r2
10670 // bCC copy1MBB
10671 // fallthrough --> copy0MBB
10672 MachineBasicBlock *thisMBB = BB;
10673 MachineFunction *F = BB->getParent();
10674 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10675 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010676 F->insert(It, copy0MBB);
10677 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010678
Bill Wendling730c07e2010-06-25 20:48:10 +000010679 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10680 // live into the sink and copy blocks.
10681 const MachineFunction *MF = BB->getParent();
10682 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10683 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010684
Dan Gohman14152b42010-07-06 20:24:04 +000010685 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10686 const MachineOperand &MO = MI->getOperand(I);
10687 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010688 unsigned Reg = MO.getReg();
10689 if (Reg != X86::EFLAGS) continue;
10690 copy0MBB->addLiveIn(Reg);
10691 sinkMBB->addLiveIn(Reg);
10692 }
10693
Dan Gohman14152b42010-07-06 20:24:04 +000010694 // Transfer the remainder of BB and its successor edges to sinkMBB.
10695 sinkMBB->splice(sinkMBB->begin(), BB,
10696 llvm::next(MachineBasicBlock::iterator(MI)),
10697 BB->end());
10698 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10699
10700 // Add the true and fallthrough blocks as its successors.
10701 BB->addSuccessor(copy0MBB);
10702 BB->addSuccessor(sinkMBB);
10703
10704 // Create the conditional branch instruction.
10705 unsigned Opc =
10706 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10707 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10708
Chris Lattner52600972009-09-02 05:57:00 +000010709 // copy0MBB:
10710 // %FalseValue = ...
10711 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010712 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010713
Chris Lattner52600972009-09-02 05:57:00 +000010714 // sinkMBB:
10715 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10716 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010717 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10718 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010719 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10720 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10721
Dan Gohman14152b42010-07-06 20:24:04 +000010722 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010723 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010724}
10725
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010726MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010727X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010728 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010729 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10730 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010731
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010732 assert(!Subtarget->isTargetEnvMacho());
10733
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010734 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10735 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010736
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010737 if (Subtarget->isTargetWin64()) {
10738 if (Subtarget->isTargetCygMing()) {
10739 // ___chkstk(Mingw64):
10740 // Clobbers R10, R11, RAX and EFLAGS.
10741 // Updates RSP.
10742 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10743 .addExternalSymbol("___chkstk")
10744 .addReg(X86::RAX, RegState::Implicit)
10745 .addReg(X86::RSP, RegState::Implicit)
10746 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10747 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10748 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10749 } else {
10750 // __chkstk(MSVCRT): does not update stack pointer.
10751 // Clobbers R10, R11 and EFLAGS.
10752 // FIXME: RAX(allocated size) might be reused and not killed.
10753 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10754 .addExternalSymbol("__chkstk")
10755 .addReg(X86::RAX, RegState::Implicit)
10756 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10757 // RAX has the offset to subtracted from RSP.
10758 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10759 .addReg(X86::RSP)
10760 .addReg(X86::RAX);
10761 }
10762 } else {
10763 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010764 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10765
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010766 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10767 .addExternalSymbol(StackProbeSymbol)
10768 .addReg(X86::EAX, RegState::Implicit)
10769 .addReg(X86::ESP, RegState::Implicit)
10770 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10771 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10772 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10773 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010774
Dan Gohman14152b42010-07-06 20:24:04 +000010775 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010776 return BB;
10777}
Chris Lattner52600972009-09-02 05:57:00 +000010778
10779MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010780X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10781 MachineBasicBlock *BB) const {
10782 // This is pretty easy. We're taking the value that we received from
10783 // our load from the relocation, sticking it in either RDI (x86-64)
10784 // or EAX and doing an indirect call. The return value will then
10785 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010786 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010787 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010788 DebugLoc DL = MI->getDebugLoc();
10789 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010790
10791 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010792 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010793
Eric Christopher30ef0e52010-06-03 04:07:48 +000010794 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010795 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10796 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010797 .addReg(X86::RIP)
10798 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010799 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010800 MI->getOperand(3).getTargetFlags())
10801 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010802 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010803 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010804 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010805 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10806 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010807 .addReg(0)
10808 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010809 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010810 MI->getOperand(3).getTargetFlags())
10811 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010812 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010813 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010814 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010815 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10816 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010817 .addReg(TII->getGlobalBaseReg(F))
10818 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010819 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010820 MI->getOperand(3).getTargetFlags())
10821 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010822 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010823 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010824 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010825
Dan Gohman14152b42010-07-06 20:24:04 +000010826 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010827 return BB;
10828}
10829
10830MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010831X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010832 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010833 switch (MI->getOpcode()) {
10834 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010835 case X86::TAILJMPd64:
10836 case X86::TAILJMPr64:
10837 case X86::TAILJMPm64:
10838 assert(!"TAILJMP64 would not be touched here.");
10839 case X86::TCRETURNdi64:
10840 case X86::TCRETURNri64:
10841 case X86::TCRETURNmi64:
10842 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10843 // On AMD64, additional defs should be added before register allocation.
10844 if (!Subtarget->isTargetWin64()) {
10845 MI->addRegisterDefined(X86::RSI);
10846 MI->addRegisterDefined(X86::RDI);
10847 MI->addRegisterDefined(X86::XMM6);
10848 MI->addRegisterDefined(X86::XMM7);
10849 MI->addRegisterDefined(X86::XMM8);
10850 MI->addRegisterDefined(X86::XMM9);
10851 MI->addRegisterDefined(X86::XMM10);
10852 MI->addRegisterDefined(X86::XMM11);
10853 MI->addRegisterDefined(X86::XMM12);
10854 MI->addRegisterDefined(X86::XMM13);
10855 MI->addRegisterDefined(X86::XMM14);
10856 MI->addRegisterDefined(X86::XMM15);
10857 }
10858 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010859 case X86::WIN_ALLOCA:
10860 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010861 case X86::TLSCall_32:
10862 case X86::TLSCall_64:
10863 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010864 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010865 case X86::CMOV_FR32:
10866 case X86::CMOV_FR64:
10867 case X86::CMOV_V4F32:
10868 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010869 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010870 case X86::CMOV_GR16:
10871 case X86::CMOV_GR32:
10872 case X86::CMOV_RFP32:
10873 case X86::CMOV_RFP64:
10874 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010875 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010876
Dale Johannesen849f2142007-07-03 00:53:03 +000010877 case X86::FP32_TO_INT16_IN_MEM:
10878 case X86::FP32_TO_INT32_IN_MEM:
10879 case X86::FP32_TO_INT64_IN_MEM:
10880 case X86::FP64_TO_INT16_IN_MEM:
10881 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010882 case X86::FP64_TO_INT64_IN_MEM:
10883 case X86::FP80_TO_INT16_IN_MEM:
10884 case X86::FP80_TO_INT32_IN_MEM:
10885 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010886 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10887 DebugLoc DL = MI->getDebugLoc();
10888
Evan Cheng60c07e12006-07-05 22:17:51 +000010889 // Change the floating point control register to use "round towards zero"
10890 // mode when truncating to an integer value.
10891 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010892 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010893 addFrameReference(BuildMI(*BB, MI, DL,
10894 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010895
10896 // Load the old value of the high byte of the control word...
10897 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010898 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010899 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010900 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010901
10902 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010903 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010904 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010905
10906 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010907 addFrameReference(BuildMI(*BB, MI, DL,
10908 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010909
10910 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010911 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010912 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010913
10914 // Get the X86 opcode to use.
10915 unsigned Opc;
10916 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010917 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010918 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10919 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10920 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10921 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10922 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10923 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010924 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10925 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10926 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010927 }
10928
10929 X86AddressMode AM;
10930 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010931 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010932 AM.BaseType = X86AddressMode::RegBase;
10933 AM.Base.Reg = Op.getReg();
10934 } else {
10935 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010936 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010937 }
10938 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010939 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010940 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010941 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010942 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010943 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010944 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010945 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010946 AM.GV = Op.getGlobal();
10947 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010948 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010949 }
Dan Gohman14152b42010-07-06 20:24:04 +000010950 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010951 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010952
10953 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010954 addFrameReference(BuildMI(*BB, MI, DL,
10955 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010956
Dan Gohman14152b42010-07-06 20:24:04 +000010957 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010958 return BB;
10959 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010960 // String/text processing lowering.
10961 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010962 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010963 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10964 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010965 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010966 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10967 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010968 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010969 return EmitPCMP(MI, BB, 5, false /* in mem */);
10970 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010971 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010972 return EmitPCMP(MI, BB, 5, true /* in mem */);
10973
Eric Christopher228232b2010-11-30 07:20:12 +000010974 // Thread synchronization.
10975 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010976 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010977 case X86::MWAIT:
10978 return EmitMwait(MI, BB);
10979
Eric Christopherb120ab42009-08-18 22:50:32 +000010980 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010981 case X86::ATOMAND32:
10982 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010983 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010984 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010985 X86::NOT32r, X86::EAX,
10986 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010987 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010988 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10989 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010990 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010991 X86::NOT32r, X86::EAX,
10992 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010993 case X86::ATOMXOR32:
10994 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010995 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010996 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010997 X86::NOT32r, X86::EAX,
10998 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010999 case X86::ATOMNAND32:
11000 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011001 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011002 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011003 X86::NOT32r, X86::EAX,
11004 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000011005 case X86::ATOMMIN32:
11006 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
11007 case X86::ATOMMAX32:
11008 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
11009 case X86::ATOMUMIN32:
11010 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
11011 case X86::ATOMUMAX32:
11012 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000011013
11014 case X86::ATOMAND16:
11015 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
11016 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011017 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011018 X86::NOT16r, X86::AX,
11019 X86::GR16RegisterClass);
11020 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000011021 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011022 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011023 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011024 X86::NOT16r, X86::AX,
11025 X86::GR16RegisterClass);
11026 case X86::ATOMXOR16:
11027 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
11028 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011029 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011030 X86::NOT16r, X86::AX,
11031 X86::GR16RegisterClass);
11032 case X86::ATOMNAND16:
11033 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
11034 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011035 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011036 X86::NOT16r, X86::AX,
11037 X86::GR16RegisterClass, true);
11038 case X86::ATOMMIN16:
11039 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
11040 case X86::ATOMMAX16:
11041 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
11042 case X86::ATOMUMIN16:
11043 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
11044 case X86::ATOMUMAX16:
11045 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
11046
11047 case X86::ATOMAND8:
11048 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
11049 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011050 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011051 X86::NOT8r, X86::AL,
11052 X86::GR8RegisterClass);
11053 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000011054 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011055 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011056 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011057 X86::NOT8r, X86::AL,
11058 X86::GR8RegisterClass);
11059 case X86::ATOMXOR8:
11060 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
11061 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011062 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011063 X86::NOT8r, X86::AL,
11064 X86::GR8RegisterClass);
11065 case X86::ATOMNAND8:
11066 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
11067 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011068 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011069 X86::NOT8r, X86::AL,
11070 X86::GR8RegisterClass, true);
11071 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011072 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000011073 case X86::ATOMAND64:
11074 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011075 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011076 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011077 X86::NOT64r, X86::RAX,
11078 X86::GR64RegisterClass);
11079 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000011080 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
11081 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011082 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011083 X86::NOT64r, X86::RAX,
11084 X86::GR64RegisterClass);
11085 case X86::ATOMXOR64:
11086 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000011087 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011088 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011089 X86::NOT64r, X86::RAX,
11090 X86::GR64RegisterClass);
11091 case X86::ATOMNAND64:
11092 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
11093 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011094 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000011095 X86::NOT64r, X86::RAX,
11096 X86::GR64RegisterClass, true);
11097 case X86::ATOMMIN64:
11098 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
11099 case X86::ATOMMAX64:
11100 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
11101 case X86::ATOMUMIN64:
11102 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
11103 case X86::ATOMUMAX64:
11104 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011105
11106 // This group does 64-bit operations on a 32-bit host.
11107 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011108 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011109 X86::AND32rr, X86::AND32rr,
11110 X86::AND32ri, X86::AND32ri,
11111 false);
11112 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011113 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011114 X86::OR32rr, X86::OR32rr,
11115 X86::OR32ri, X86::OR32ri,
11116 false);
11117 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011118 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011119 X86::XOR32rr, X86::XOR32rr,
11120 X86::XOR32ri, X86::XOR32ri,
11121 false);
11122 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011123 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011124 X86::AND32rr, X86::AND32rr,
11125 X86::AND32ri, X86::AND32ri,
11126 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011127 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011128 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011129 X86::ADD32rr, X86::ADC32rr,
11130 X86::ADD32ri, X86::ADC32ri,
11131 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011132 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011133 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011134 X86::SUB32rr, X86::SBB32rr,
11135 X86::SUB32ri, X86::SBB32ri,
11136 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000011137 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011138 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000011139 X86::MOV32rr, X86::MOV32rr,
11140 X86::MOV32ri, X86::MOV32ri,
11141 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011142 case X86::VASTART_SAVE_XMM_REGS:
11143 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000011144
11145 case X86::VAARG_64:
11146 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011147 }
11148}
11149
11150//===----------------------------------------------------------------------===//
11151// X86 Optimization Hooks
11152//===----------------------------------------------------------------------===//
11153
Dan Gohman475871a2008-07-27 21:46:04 +000011154void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000011155 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011156 APInt &KnownZero,
11157 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000011158 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000011159 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011160 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000011161 assert((Opc >= ISD::BUILTIN_OP_END ||
11162 Opc == ISD::INTRINSIC_WO_CHAIN ||
11163 Opc == ISD::INTRINSIC_W_CHAIN ||
11164 Opc == ISD::INTRINSIC_VOID) &&
11165 "Should use MaskedValueIsZero if you don't know whether Op"
11166 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011167
Dan Gohmanf4f92f52008-02-13 23:07:24 +000011168 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011169 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000011170 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011171 case X86ISD::ADD:
11172 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000011173 case X86ISD::ADC:
11174 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011175 case X86ISD::SMUL:
11176 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011177 case X86ISD::INC:
11178 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011179 case X86ISD::OR:
11180 case X86ISD::XOR:
11181 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011182 // These nodes' second result is a boolean.
11183 if (Op.getResNo() == 0)
11184 break;
11185 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011186 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011187 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11188 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011189 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011190 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011191}
Chris Lattner259e97c2006-01-31 19:43:35 +000011192
Owen Andersonbc146b02010-09-21 20:42:50 +000011193unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11194 unsigned Depth) const {
11195 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11196 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11197 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011198
Owen Andersonbc146b02010-09-21 20:42:50 +000011199 // Fallback case.
11200 return 1;
11201}
11202
Evan Cheng206ee9d2006-07-07 08:33:52 +000011203/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011204/// node is a GlobalAddress + offset.
11205bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011206 const GlobalValue* &GA,
11207 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011208 if (N->getOpcode() == X86ISD::Wrapper) {
11209 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011210 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011211 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011212 return true;
11213 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011214 }
Evan Chengad4196b2008-05-12 19:56:52 +000011215 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011216}
11217
Evan Cheng206ee9d2006-07-07 08:33:52 +000011218/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11219/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11220/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011221/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011222static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011223 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011224 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011225 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011226
Eli Friedman7a5e5552009-06-07 06:52:44 +000011227 if (VT.getSizeInBits() != 128)
11228 return SDValue();
11229
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011230 // Don't create instructions with illegal types after legalize types has run.
11231 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11232 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11233 return SDValue();
11234
Nate Begemanfdea31a2010-03-24 20:49:50 +000011235 SmallVector<SDValue, 16> Elts;
11236 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011237 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011238
Nate Begemanfdea31a2010-03-24 20:49:50 +000011239 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011240}
Evan Chengd880b972008-05-09 21:53:03 +000011241
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011242/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11243/// generation and convert it from being a bunch of shuffles and extracts
11244/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011245static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11246 const TargetLowering &TLI) {
11247 SDValue InputVector = N->getOperand(0);
11248
11249 // Only operate on vectors of 4 elements, where the alternative shuffling
11250 // gets to be more expensive.
11251 if (InputVector.getValueType() != MVT::v4i32)
11252 return SDValue();
11253
11254 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11255 // single use which is a sign-extend or zero-extend, and all elements are
11256 // used.
11257 SmallVector<SDNode *, 4> Uses;
11258 unsigned ExtractedElements = 0;
11259 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11260 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11261 if (UI.getUse().getResNo() != InputVector.getResNo())
11262 return SDValue();
11263
11264 SDNode *Extract = *UI;
11265 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11266 return SDValue();
11267
11268 if (Extract->getValueType(0) != MVT::i32)
11269 return SDValue();
11270 if (!Extract->hasOneUse())
11271 return SDValue();
11272 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11273 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11274 return SDValue();
11275 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11276 return SDValue();
11277
11278 // Record which element was extracted.
11279 ExtractedElements |=
11280 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11281
11282 Uses.push_back(Extract);
11283 }
11284
11285 // If not all the elements were used, this may not be worthwhile.
11286 if (ExtractedElements != 15)
11287 return SDValue();
11288
11289 // Ok, we've now decided to do the transformation.
11290 DebugLoc dl = InputVector.getDebugLoc();
11291
11292 // Store the value to a temporary stack slot.
11293 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011294 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11295 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011296
11297 // Replace each use (extract) with a load of the appropriate element.
11298 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11299 UE = Uses.end(); UI != UE; ++UI) {
11300 SDNode *Extract = *UI;
11301
Nadav Rotem86694292011-05-17 08:31:57 +000011302 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011303 SDValue Idx = Extract->getOperand(1);
11304 unsigned EltSize =
11305 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11306 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11307 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11308
Nadav Rotem86694292011-05-17 08:31:57 +000011309 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011310 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011311
11312 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011313 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011314 ScalarAddr, MachinePointerInfo(),
11315 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011316
11317 // Replace the exact with the load.
11318 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11319 }
11320
11321 // The replacement was made in place; don't return anything.
11322 return SDValue();
11323}
11324
Chris Lattner83e6c992006-10-04 06:57:07 +000011325/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011326static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011327 const X86Subtarget *Subtarget) {
11328 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011329 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011330 // Get the LHS/RHS of the select.
11331 SDValue LHS = N->getOperand(1);
11332 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011333
Dan Gohman670e5392009-09-21 18:03:22 +000011334 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011335 // instructions match the semantics of the common C idiom x<y?x:y but not
11336 // x<=y?x:y, because of how they handle negative zero (which can be
11337 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011338 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011339 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011340 Cond.getOpcode() == ISD::SETCC) {
11341 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011342
Chris Lattner47b4ce82009-03-11 05:48:52 +000011343 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011344 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011345 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11346 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011347 switch (CC) {
11348 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011349 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011350 // Converting this to a min would handle NaNs incorrectly, and swapping
11351 // the operands would cause it to handle comparisons between positive
11352 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011353 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011354 if (!UnsafeFPMath &&
11355 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11356 break;
11357 std::swap(LHS, RHS);
11358 }
Dan Gohman670e5392009-09-21 18:03:22 +000011359 Opcode = X86ISD::FMIN;
11360 break;
11361 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011362 // Converting this to a min would handle comparisons between positive
11363 // and negative zero incorrectly.
11364 if (!UnsafeFPMath &&
11365 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11366 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011367 Opcode = X86ISD::FMIN;
11368 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011369 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011370 // Converting this to a min would handle both negative zeros and NaNs
11371 // incorrectly, but we can swap the operands to fix both.
11372 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011373 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011374 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011375 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011376 Opcode = X86ISD::FMIN;
11377 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011378
Dan Gohman670e5392009-09-21 18:03:22 +000011379 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011380 // Converting this to a max would handle comparisons between positive
11381 // and negative zero incorrectly.
11382 if (!UnsafeFPMath &&
11383 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11384 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011385 Opcode = X86ISD::FMAX;
11386 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011387 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011388 // Converting this to a max would handle NaNs incorrectly, and swapping
11389 // the operands would cause it to handle comparisons between positive
11390 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011391 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011392 if (!UnsafeFPMath &&
11393 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11394 break;
11395 std::swap(LHS, RHS);
11396 }
Dan Gohman670e5392009-09-21 18:03:22 +000011397 Opcode = X86ISD::FMAX;
11398 break;
11399 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011400 // Converting this to a max would handle both negative zeros and NaNs
11401 // incorrectly, but we can swap the operands to fix both.
11402 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011403 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011404 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011405 case ISD::SETGE:
11406 Opcode = X86ISD::FMAX;
11407 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011408 }
Dan Gohman670e5392009-09-21 18:03:22 +000011409 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011410 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11411 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011412 switch (CC) {
11413 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011414 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011415 // Converting this to a min would handle comparisons between positive
11416 // and negative zero incorrectly, and swapping the operands would
11417 // cause it to handle NaNs incorrectly.
11418 if (!UnsafeFPMath &&
11419 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011420 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011421 break;
11422 std::swap(LHS, RHS);
11423 }
Dan Gohman670e5392009-09-21 18:03:22 +000011424 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011425 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011426 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011427 // Converting this to a min would handle NaNs incorrectly.
11428 if (!UnsafeFPMath &&
11429 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11430 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011431 Opcode = X86ISD::FMIN;
11432 break;
11433 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011434 // Converting this to a min would handle both negative zeros and NaNs
11435 // incorrectly, but we can swap the operands to fix both.
11436 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011437 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011438 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011439 case ISD::SETGE:
11440 Opcode = X86ISD::FMIN;
11441 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011442
Dan Gohman670e5392009-09-21 18:03:22 +000011443 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011444 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011445 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011446 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011447 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011448 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011449 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011450 // Converting this to a max would handle comparisons between positive
11451 // and negative zero incorrectly, and swapping the operands would
11452 // cause it to handle NaNs incorrectly.
11453 if (!UnsafeFPMath &&
11454 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011455 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011456 break;
11457 std::swap(LHS, RHS);
11458 }
Dan Gohman670e5392009-09-21 18:03:22 +000011459 Opcode = X86ISD::FMAX;
11460 break;
11461 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011462 // Converting this to a max would handle both negative zeros and NaNs
11463 // incorrectly, but we can swap the operands to fix both.
11464 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011465 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011466 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011467 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011468 Opcode = X86ISD::FMAX;
11469 break;
11470 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011471 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011472
Chris Lattner47b4ce82009-03-11 05:48:52 +000011473 if (Opcode)
11474 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011475 }
Eric Christopherfd179292009-08-27 18:07:15 +000011476
Chris Lattnerd1980a52009-03-12 06:52:53 +000011477 // If this is a select between two integer constants, try to do some
11478 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011479 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11480 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011481 // Don't do this for crazy integer types.
11482 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11483 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011484 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011485 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011486
Chris Lattnercee56e72009-03-13 05:53:31 +000011487 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011488 // Efficiently invertible.
11489 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11490 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11491 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11492 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011493 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011494 }
Eric Christopherfd179292009-08-27 18:07:15 +000011495
Chris Lattnerd1980a52009-03-12 06:52:53 +000011496 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011497 if (FalseC->getAPIntValue() == 0 &&
11498 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011499 if (NeedsCondInvert) // Invert the condition if needed.
11500 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11501 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011502
Chris Lattnerd1980a52009-03-12 06:52:53 +000011503 // Zero extend the condition if needed.
11504 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011505
Chris Lattnercee56e72009-03-13 05:53:31 +000011506 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011507 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011508 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011509 }
Eric Christopherfd179292009-08-27 18:07:15 +000011510
Chris Lattner97a29a52009-03-13 05:22:11 +000011511 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011512 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011513 if (NeedsCondInvert) // Invert the condition if needed.
11514 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11515 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011516
Chris Lattner97a29a52009-03-13 05:22:11 +000011517 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011518 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11519 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011520 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011521 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011522 }
Eric Christopherfd179292009-08-27 18:07:15 +000011523
Chris Lattnercee56e72009-03-13 05:53:31 +000011524 // Optimize cases that will turn into an LEA instruction. This requires
11525 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011526 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011527 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011528 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011529
Chris Lattnercee56e72009-03-13 05:53:31 +000011530 bool isFastMultiplier = false;
11531 if (Diff < 10) {
11532 switch ((unsigned char)Diff) {
11533 default: break;
11534 case 1: // result = add base, cond
11535 case 2: // result = lea base( , cond*2)
11536 case 3: // result = lea base(cond, cond*2)
11537 case 4: // result = lea base( , cond*4)
11538 case 5: // result = lea base(cond, cond*4)
11539 case 8: // result = lea base( , cond*8)
11540 case 9: // result = lea base(cond, cond*8)
11541 isFastMultiplier = true;
11542 break;
11543 }
11544 }
Eric Christopherfd179292009-08-27 18:07:15 +000011545
Chris Lattnercee56e72009-03-13 05:53:31 +000011546 if (isFastMultiplier) {
11547 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11548 if (NeedsCondInvert) // Invert the condition if needed.
11549 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11550 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011551
Chris Lattnercee56e72009-03-13 05:53:31 +000011552 // Zero extend the condition if needed.
11553 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11554 Cond);
11555 // Scale the condition by the difference.
11556 if (Diff != 1)
11557 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11558 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011559
Chris Lattnercee56e72009-03-13 05:53:31 +000011560 // Add the base if non-zero.
11561 if (FalseC->getAPIntValue() != 0)
11562 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11563 SDValue(FalseC, 0));
11564 return Cond;
11565 }
Eric Christopherfd179292009-08-27 18:07:15 +000011566 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011567 }
11568 }
Eric Christopherfd179292009-08-27 18:07:15 +000011569
Dan Gohman475871a2008-07-27 21:46:04 +000011570 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011571}
11572
Chris Lattnerd1980a52009-03-12 06:52:53 +000011573/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11574static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11575 TargetLowering::DAGCombinerInfo &DCI) {
11576 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011577
Chris Lattnerd1980a52009-03-12 06:52:53 +000011578 // If the flag operand isn't dead, don't touch this CMOV.
11579 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11580 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011581
Evan Chengb5a55d92011-05-24 01:48:22 +000011582 SDValue FalseOp = N->getOperand(0);
11583 SDValue TrueOp = N->getOperand(1);
11584 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11585 SDValue Cond = N->getOperand(3);
11586 if (CC == X86::COND_E || CC == X86::COND_NE) {
11587 switch (Cond.getOpcode()) {
11588 default: break;
11589 case X86ISD::BSR:
11590 case X86ISD::BSF:
11591 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11592 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11593 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11594 }
11595 }
11596
Chris Lattnerd1980a52009-03-12 06:52:53 +000011597 // If this is a select between two integer constants, try to do some
11598 // optimizations. Note that the operands are ordered the opposite of SELECT
11599 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011600 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11601 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011602 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11603 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011604 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11605 CC = X86::GetOppositeBranchCondition(CC);
11606 std::swap(TrueC, FalseC);
11607 }
Eric Christopherfd179292009-08-27 18:07:15 +000011608
Chris Lattnerd1980a52009-03-12 06:52:53 +000011609 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011610 // This is efficient for any integer data type (including i8/i16) and
11611 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011612 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011613 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11614 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011615
Chris Lattnerd1980a52009-03-12 06:52:53 +000011616 // Zero extend the condition if needed.
11617 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011618
Chris Lattnerd1980a52009-03-12 06:52:53 +000011619 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11620 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011621 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011622 if (N->getNumValues() == 2) // Dead flag value?
11623 return DCI.CombineTo(N, Cond, SDValue());
11624 return Cond;
11625 }
Eric Christopherfd179292009-08-27 18:07:15 +000011626
Chris Lattnercee56e72009-03-13 05:53:31 +000011627 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11628 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011629 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011630 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11631 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011632
Chris Lattner97a29a52009-03-13 05:22:11 +000011633 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011634 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11635 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011636 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11637 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011638
Chris Lattner97a29a52009-03-13 05:22:11 +000011639 if (N->getNumValues() == 2) // Dead flag value?
11640 return DCI.CombineTo(N, Cond, SDValue());
11641 return Cond;
11642 }
Eric Christopherfd179292009-08-27 18:07:15 +000011643
Chris Lattnercee56e72009-03-13 05:53:31 +000011644 // Optimize cases that will turn into an LEA instruction. This requires
11645 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011646 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011647 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011648 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011649
Chris Lattnercee56e72009-03-13 05:53:31 +000011650 bool isFastMultiplier = false;
11651 if (Diff < 10) {
11652 switch ((unsigned char)Diff) {
11653 default: break;
11654 case 1: // result = add base, cond
11655 case 2: // result = lea base( , cond*2)
11656 case 3: // result = lea base(cond, cond*2)
11657 case 4: // result = lea base( , cond*4)
11658 case 5: // result = lea base(cond, cond*4)
11659 case 8: // result = lea base( , cond*8)
11660 case 9: // result = lea base(cond, cond*8)
11661 isFastMultiplier = true;
11662 break;
11663 }
11664 }
Eric Christopherfd179292009-08-27 18:07:15 +000011665
Chris Lattnercee56e72009-03-13 05:53:31 +000011666 if (isFastMultiplier) {
11667 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011668 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11669 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011670 // Zero extend the condition if needed.
11671 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11672 Cond);
11673 // Scale the condition by the difference.
11674 if (Diff != 1)
11675 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11676 DAG.getConstant(Diff, Cond.getValueType()));
11677
11678 // Add the base if non-zero.
11679 if (FalseC->getAPIntValue() != 0)
11680 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11681 SDValue(FalseC, 0));
11682 if (N->getNumValues() == 2) // Dead flag value?
11683 return DCI.CombineTo(N, Cond, SDValue());
11684 return Cond;
11685 }
Eric Christopherfd179292009-08-27 18:07:15 +000011686 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011687 }
11688 }
11689 return SDValue();
11690}
11691
11692
Evan Cheng0b0cd912009-03-28 05:57:29 +000011693/// PerformMulCombine - Optimize a single multiply with constant into two
11694/// in order to implement it with two cheaper instructions, e.g.
11695/// LEA + SHL, LEA + LEA.
11696static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11697 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011698 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11699 return SDValue();
11700
Owen Andersone50ed302009-08-10 22:56:29 +000011701 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011702 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011703 return SDValue();
11704
11705 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11706 if (!C)
11707 return SDValue();
11708 uint64_t MulAmt = C->getZExtValue();
11709 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11710 return SDValue();
11711
11712 uint64_t MulAmt1 = 0;
11713 uint64_t MulAmt2 = 0;
11714 if ((MulAmt % 9) == 0) {
11715 MulAmt1 = 9;
11716 MulAmt2 = MulAmt / 9;
11717 } else if ((MulAmt % 5) == 0) {
11718 MulAmt1 = 5;
11719 MulAmt2 = MulAmt / 5;
11720 } else if ((MulAmt % 3) == 0) {
11721 MulAmt1 = 3;
11722 MulAmt2 = MulAmt / 3;
11723 }
11724 if (MulAmt2 &&
11725 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11726 DebugLoc DL = N->getDebugLoc();
11727
11728 if (isPowerOf2_64(MulAmt2) &&
11729 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11730 // If second multiplifer is pow2, issue it first. We want the multiply by
11731 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11732 // is an add.
11733 std::swap(MulAmt1, MulAmt2);
11734
11735 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011736 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011737 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011738 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011739 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011740 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011741 DAG.getConstant(MulAmt1, VT));
11742
Eric Christopherfd179292009-08-27 18:07:15 +000011743 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011744 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011745 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011746 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011747 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011748 DAG.getConstant(MulAmt2, VT));
11749
11750 // Do not add new nodes to DAG combiner worklist.
11751 DCI.CombineTo(N, NewMul, false);
11752 }
11753 return SDValue();
11754}
11755
Evan Chengad9c0a32009-12-15 00:53:42 +000011756static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11757 SDValue N0 = N->getOperand(0);
11758 SDValue N1 = N->getOperand(1);
11759 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11760 EVT VT = N0.getValueType();
11761
11762 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11763 // since the result of setcc_c is all zero's or all ones.
11764 if (N1C && N0.getOpcode() == ISD::AND &&
11765 N0.getOperand(1).getOpcode() == ISD::Constant) {
11766 SDValue N00 = N0.getOperand(0);
11767 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11768 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11769 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11770 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11771 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11772 APInt ShAmt = N1C->getAPIntValue();
11773 Mask = Mask.shl(ShAmt);
11774 if (Mask != 0)
11775 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11776 N00, DAG.getConstant(Mask, VT));
11777 }
11778 }
11779
11780 return SDValue();
11781}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011782
Nate Begeman740ab032009-01-26 00:52:55 +000011783/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11784/// when possible.
11785static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11786 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011787 EVT VT = N->getValueType(0);
11788 if (!VT.isVector() && VT.isInteger() &&
11789 N->getOpcode() == ISD::SHL)
11790 return PerformSHLCombine(N, DAG);
11791
Nate Begeman740ab032009-01-26 00:52:55 +000011792 // On X86 with SSE2 support, we can transform this to a vector shift if
11793 // all elements are shifted by the same amount. We can't do this in legalize
11794 // because the a constant vector is typically transformed to a constant pool
11795 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011796 if (!Subtarget->hasSSE2())
11797 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011798
Owen Anderson825b72b2009-08-11 20:47:22 +000011799 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011800 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011801
Mon P Wang3becd092009-01-28 08:12:05 +000011802 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011803 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011804 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011805 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011806 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11807 unsigned NumElts = VT.getVectorNumElements();
11808 unsigned i = 0;
11809 for (; i != NumElts; ++i) {
11810 SDValue Arg = ShAmtOp.getOperand(i);
11811 if (Arg.getOpcode() == ISD::UNDEF) continue;
11812 BaseShAmt = Arg;
11813 break;
11814 }
11815 for (; i != NumElts; ++i) {
11816 SDValue Arg = ShAmtOp.getOperand(i);
11817 if (Arg.getOpcode() == ISD::UNDEF) continue;
11818 if (Arg != BaseShAmt) {
11819 return SDValue();
11820 }
11821 }
11822 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011823 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011824 SDValue InVec = ShAmtOp.getOperand(0);
11825 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11826 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11827 unsigned i = 0;
11828 for (; i != NumElts; ++i) {
11829 SDValue Arg = InVec.getOperand(i);
11830 if (Arg.getOpcode() == ISD::UNDEF) continue;
11831 BaseShAmt = Arg;
11832 break;
11833 }
11834 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11835 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011836 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011837 if (C->getZExtValue() == SplatIdx)
11838 BaseShAmt = InVec.getOperand(1);
11839 }
11840 }
11841 if (BaseShAmt.getNode() == 0)
11842 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11843 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011844 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011845 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011846
Mon P Wangefa42202009-09-03 19:56:25 +000011847 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011848 if (EltVT.bitsGT(MVT::i32))
11849 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11850 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011851 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011852
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011853 // The shift amount is identical so we can do a vector shift.
11854 SDValue ValOp = N->getOperand(0);
11855 switch (N->getOpcode()) {
11856 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011857 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011858 break;
11859 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011860 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011861 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011862 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011863 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011864 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011865 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011866 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011867 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011868 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011869 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011870 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011871 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011872 break;
11873 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011874 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011875 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011876 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011877 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011878 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011879 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011880 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011881 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011882 break;
11883 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011884 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011885 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011886 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011887 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011888 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011889 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011890 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011891 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011892 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011893 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011894 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011895 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011896 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011897 }
11898 return SDValue();
11899}
11900
Nate Begemanb65c1752010-12-17 22:55:37 +000011901
Stuart Hastings865f0932011-06-03 23:53:54 +000011902// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11903// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11904// and friends. Likewise for OR -> CMPNEQSS.
11905static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11906 TargetLowering::DAGCombinerInfo &DCI,
11907 const X86Subtarget *Subtarget) {
11908 unsigned opcode;
11909
11910 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11911 // we're requiring SSE2 for both.
11912 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11913 SDValue N0 = N->getOperand(0);
11914 SDValue N1 = N->getOperand(1);
11915 SDValue CMP0 = N0->getOperand(1);
11916 SDValue CMP1 = N1->getOperand(1);
11917 DebugLoc DL = N->getDebugLoc();
11918
11919 // The SETCCs should both refer to the same CMP.
11920 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11921 return SDValue();
11922
11923 SDValue CMP00 = CMP0->getOperand(0);
11924 SDValue CMP01 = CMP0->getOperand(1);
11925 EVT VT = CMP00.getValueType();
11926
11927 if (VT == MVT::f32 || VT == MVT::f64) {
11928 bool ExpectingFlags = false;
11929 // Check for any users that want flags:
11930 for (SDNode::use_iterator UI = N->use_begin(),
11931 UE = N->use_end();
11932 !ExpectingFlags && UI != UE; ++UI)
11933 switch (UI->getOpcode()) {
11934 default:
11935 case ISD::BR_CC:
11936 case ISD::BRCOND:
11937 case ISD::SELECT:
11938 ExpectingFlags = true;
11939 break;
11940 case ISD::CopyToReg:
11941 case ISD::SIGN_EXTEND:
11942 case ISD::ZERO_EXTEND:
11943 case ISD::ANY_EXTEND:
11944 break;
11945 }
11946
11947 if (!ExpectingFlags) {
11948 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11949 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11950
11951 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11952 X86::CondCode tmp = cc0;
11953 cc0 = cc1;
11954 cc1 = tmp;
11955 }
11956
11957 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11958 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11959 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11960 X86ISD::NodeType NTOperator = is64BitFP ?
11961 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11962 // FIXME: need symbolic constants for these magic numbers.
11963 // See X86ATTInstPrinter.cpp:printSSECC().
11964 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11965 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11966 DAG.getConstant(x86cc, MVT::i8));
11967 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11968 OnesOrZeroesF);
11969 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11970 DAG.getConstant(1, MVT::i32));
11971 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11972 return OneBitOfTruth;
11973 }
11974 }
11975 }
11976 }
11977 return SDValue();
11978}
11979
Nate Begemanb65c1752010-12-17 22:55:37 +000011980static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11981 TargetLowering::DAGCombinerInfo &DCI,
11982 const X86Subtarget *Subtarget) {
11983 if (DCI.isBeforeLegalizeOps())
11984 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011985
Stuart Hastings865f0932011-06-03 23:53:54 +000011986 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11987 if (R.getNode())
11988 return R;
11989
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011990 // Want to form ANDNP nodes:
11991 // 1) In the hopes of then easily combining them with OR and AND nodes
11992 // to form PBLEND/PSIGN.
11993 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000011994 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011995 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000011996 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011997
Nate Begemanb65c1752010-12-17 22:55:37 +000011998 SDValue N0 = N->getOperand(0);
11999 SDValue N1 = N->getOperand(1);
12000 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012001
Nate Begemanb65c1752010-12-17 22:55:37 +000012002 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012003 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000012004 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012005 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000012006
12007 // Check RHS for vnot
12008 if (N1.getOpcode() == ISD::XOR &&
12009 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012010 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012011
Nate Begemanb65c1752010-12-17 22:55:37 +000012012 return SDValue();
12013}
12014
Evan Cheng760d1942010-01-04 21:22:48 +000012015static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000012016 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000012017 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000012018 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000012019 return SDValue();
12020
Stuart Hastings865f0932011-06-03 23:53:54 +000012021 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
12022 if (R.getNode())
12023 return R;
12024
Evan Cheng760d1942010-01-04 21:22:48 +000012025 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000012026 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000012027 return SDValue();
12028
Evan Cheng760d1942010-01-04 21:22:48 +000012029 SDValue N0 = N->getOperand(0);
12030 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012031
Nate Begemanb65c1752010-12-17 22:55:37 +000012032 // look for psign/blend
12033 if (Subtarget->hasSSSE3()) {
12034 if (VT == MVT::v2i64) {
12035 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012036 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000012037 std::swap(N0, N1);
12038 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012039 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000012040 SDValue Mask = N1.getOperand(0);
12041 SDValue X = N1.getOperand(1);
12042 SDValue Y;
12043 if (N0.getOperand(0) == Mask)
12044 Y = N0.getOperand(1);
12045 if (N0.getOperand(1) == Mask)
12046 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012047
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012048 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000012049 if (!Y.getNode())
12050 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012051
Nate Begemanb65c1752010-12-17 22:55:37 +000012052 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
12053 if (Mask.getOpcode() != ISD::BITCAST ||
12054 X.getOpcode() != ISD::BITCAST ||
12055 Y.getOpcode() != ISD::BITCAST)
12056 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012057
Nate Begemanb65c1752010-12-17 22:55:37 +000012058 // Look through mask bitcast.
12059 Mask = Mask.getOperand(0);
12060 EVT MaskVT = Mask.getValueType();
12061
12062 // Validate that the Mask operand is a vector sra node. The sra node
12063 // will be an intrinsic.
12064 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
12065 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012066
Nate Begemanb65c1752010-12-17 22:55:37 +000012067 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
12068 // there is no psrai.b
12069 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
12070 case Intrinsic::x86_sse2_psrai_w:
12071 case Intrinsic::x86_sse2_psrai_d:
12072 break;
12073 default: return SDValue();
12074 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012075
Nate Begemanb65c1752010-12-17 22:55:37 +000012076 // Check that the SRA is all signbits.
12077 SDValue SraC = Mask.getOperand(2);
12078 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
12079 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
12080 if ((SraAmt + 1) != EltBits)
12081 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012082
Nate Begemanb65c1752010-12-17 22:55:37 +000012083 DebugLoc DL = N->getDebugLoc();
12084
12085 // Now we know we at least have a plendvb with the mask val. See if
12086 // we can form a psignb/w/d.
12087 // psign = x.type == y.type == mask.type && y = sub(0, x);
12088 X = X.getOperand(0);
12089 Y = Y.getOperand(0);
12090 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
12091 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
12092 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
12093 unsigned Opc = 0;
12094 switch (EltBits) {
12095 case 8: Opc = X86ISD::PSIGNB; break;
12096 case 16: Opc = X86ISD::PSIGNW; break;
12097 case 32: Opc = X86ISD::PSIGND; break;
12098 default: break;
12099 }
12100 if (Opc) {
12101 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
12102 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
12103 }
12104 }
12105 // PBLENDVB only available on SSE 4.1
12106 if (!Subtarget->hasSSE41())
12107 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012108
Nate Begemanb65c1752010-12-17 22:55:37 +000012109 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
12110 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
12111 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000012112 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000012113 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
12114 }
12115 }
12116 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012117
Nate Begemanb65c1752010-12-17 22:55:37 +000012118 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000012119 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
12120 std::swap(N0, N1);
12121 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
12122 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000012123 if (!N0.hasOneUse() || !N1.hasOneUse())
12124 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000012125
12126 SDValue ShAmt0 = N0.getOperand(1);
12127 if (ShAmt0.getValueType() != MVT::i8)
12128 return SDValue();
12129 SDValue ShAmt1 = N1.getOperand(1);
12130 if (ShAmt1.getValueType() != MVT::i8)
12131 return SDValue();
12132 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
12133 ShAmt0 = ShAmt0.getOperand(0);
12134 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
12135 ShAmt1 = ShAmt1.getOperand(0);
12136
12137 DebugLoc DL = N->getDebugLoc();
12138 unsigned Opc = X86ISD::SHLD;
12139 SDValue Op0 = N0.getOperand(0);
12140 SDValue Op1 = N1.getOperand(0);
12141 if (ShAmt0.getOpcode() == ISD::SUB) {
12142 Opc = X86ISD::SHRD;
12143 std::swap(Op0, Op1);
12144 std::swap(ShAmt0, ShAmt1);
12145 }
12146
Evan Cheng8b1190a2010-04-28 01:18:01 +000012147 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000012148 if (ShAmt1.getOpcode() == ISD::SUB) {
12149 SDValue Sum = ShAmt1.getOperand(0);
12150 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000012151 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
12152 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
12153 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
12154 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000012155 return DAG.getNode(Opc, DL, VT,
12156 Op0, Op1,
12157 DAG.getNode(ISD::TRUNCATE, DL,
12158 MVT::i8, ShAmt0));
12159 }
12160 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
12161 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
12162 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000012163 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000012164 return DAG.getNode(Opc, DL, VT,
12165 N0.getOperand(0), N1.getOperand(0),
12166 DAG.getNode(ISD::TRUNCATE, DL,
12167 MVT::i8, ShAmt0));
12168 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012169
Evan Cheng760d1942010-01-04 21:22:48 +000012170 return SDValue();
12171}
12172
Chris Lattner149a4e52008-02-22 02:09:43 +000012173/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012174static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000012175 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000012176 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
12177 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000012178 // A preferable solution to the general problem is to figure out the right
12179 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000012180
12181 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012182 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012183 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012184 if (VT.getSizeInBits() != 64)
12185 return SDValue();
12186
Devang Patel578efa92009-06-05 21:57:13 +000012187 const Function *F = DAG.getMachineFunction().getFunction();
12188 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012189 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012190 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012191 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012192 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012193 isa<LoadSDNode>(St->getValue()) &&
12194 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12195 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012196 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012197 LoadSDNode *Ld = 0;
12198 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012199 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012200 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012201 // Must be a store of a load. We currently handle two cases: the load
12202 // is a direct child, and it's under an intervening TokenFactor. It is
12203 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012204 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012205 Ld = cast<LoadSDNode>(St->getChain());
12206 else if (St->getValue().hasOneUse() &&
12207 ChainVal->getOpcode() == ISD::TokenFactor) {
12208 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012209 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012210 TokenFactorIndex = i;
12211 Ld = cast<LoadSDNode>(St->getValue());
12212 } else
12213 Ops.push_back(ChainVal->getOperand(i));
12214 }
12215 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012216
Evan Cheng536e6672009-03-12 05:59:15 +000012217 if (!Ld || !ISD::isNormalLoad(Ld))
12218 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012219
Evan Cheng536e6672009-03-12 05:59:15 +000012220 // If this is not the MMX case, i.e. we are just turning i64 load/store
12221 // into f64 load/store, avoid the transformation if there are multiple
12222 // uses of the loaded value.
12223 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12224 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012225
Evan Cheng536e6672009-03-12 05:59:15 +000012226 DebugLoc LdDL = Ld->getDebugLoc();
12227 DebugLoc StDL = N->getDebugLoc();
12228 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12229 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12230 // pair instead.
12231 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012232 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012233 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12234 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012235 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012236 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012237 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012238 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012239 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012240 Ops.size());
12241 }
Evan Cheng536e6672009-03-12 05:59:15 +000012242 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012243 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012244 St->isVolatile(), St->isNonTemporal(),
12245 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012246 }
Evan Cheng536e6672009-03-12 05:59:15 +000012247
12248 // Otherwise, lower to two pairs of 32-bit loads / stores.
12249 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012250 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12251 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012252
Owen Anderson825b72b2009-08-11 20:47:22 +000012253 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012254 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012255 Ld->isVolatile(), Ld->isNonTemporal(),
12256 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012257 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012258 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012259 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012260 MinAlign(Ld->getAlignment(), 4));
12261
12262 SDValue NewChain = LoLd.getValue(1);
12263 if (TokenFactorIndex != -1) {
12264 Ops.push_back(LoLd);
12265 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012266 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012267 Ops.size());
12268 }
12269
12270 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012271 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12272 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012273
12274 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012275 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012276 St->isVolatile(), St->isNonTemporal(),
12277 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012278 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012279 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012280 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012281 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012282 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012283 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012284 }
Dan Gohman475871a2008-07-27 21:46:04 +000012285 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012286}
12287
Chris Lattner6cf73262008-01-25 06:14:17 +000012288/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12289/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012290static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012291 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12292 // F[X]OR(0.0, x) -> x
12293 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012294 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12295 if (C->getValueAPF().isPosZero())
12296 return N->getOperand(1);
12297 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12298 if (C->getValueAPF().isPosZero())
12299 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012300 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012301}
12302
12303/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012304static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012305 // FAND(0.0, x) -> 0.0
12306 // FAND(x, 0.0) -> 0.0
12307 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12308 if (C->getValueAPF().isPosZero())
12309 return N->getOperand(0);
12310 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12311 if (C->getValueAPF().isPosZero())
12312 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012313 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012314}
12315
Dan Gohmane5af2d32009-01-29 01:59:02 +000012316static SDValue PerformBTCombine(SDNode *N,
12317 SelectionDAG &DAG,
12318 TargetLowering::DAGCombinerInfo &DCI) {
12319 // BT ignores high bits in the bit index operand.
12320 SDValue Op1 = N->getOperand(1);
12321 if (Op1.hasOneUse()) {
12322 unsigned BitWidth = Op1.getValueSizeInBits();
12323 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12324 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012325 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12326 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012327 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012328 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12329 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12330 DCI.CommitTargetLoweringOpt(TLO);
12331 }
12332 return SDValue();
12333}
Chris Lattner83e6c992006-10-04 06:57:07 +000012334
Eli Friedman7a5e5552009-06-07 06:52:44 +000012335static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12336 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012337 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012338 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012339 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012340 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012341 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012342 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012343 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012344 }
12345 return SDValue();
12346}
12347
Evan Cheng2e489c42009-12-16 00:53:11 +000012348static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12349 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12350 // (and (i32 x86isd::setcc_carry), 1)
12351 // This eliminates the zext. This transformation is necessary because
12352 // ISD::SETCC is always legalized to i8.
12353 DebugLoc dl = N->getDebugLoc();
12354 SDValue N0 = N->getOperand(0);
12355 EVT VT = N->getValueType(0);
12356 if (N0.getOpcode() == ISD::AND &&
12357 N0.hasOneUse() &&
12358 N0.getOperand(0).hasOneUse()) {
12359 SDValue N00 = N0.getOperand(0);
12360 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12361 return SDValue();
12362 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12363 if (!C || C->getZExtValue() != 1)
12364 return SDValue();
12365 return DAG.getNode(ISD::AND, dl, VT,
12366 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12367 N00.getOperand(0), N00.getOperand(1)),
12368 DAG.getConstant(1, VT));
12369 }
12370
12371 return SDValue();
12372}
12373
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012374// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12375static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12376 unsigned X86CC = N->getConstantOperandVal(0);
12377 SDValue EFLAG = N->getOperand(1);
12378 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012379
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012380 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12381 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12382 // cases.
12383 if (X86CC == X86::COND_B)
12384 return DAG.getNode(ISD::AND, DL, MVT::i8,
12385 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12386 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12387 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012388
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012389 return SDValue();
12390}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012391
Benjamin Kramer1396c402011-06-18 11:09:41 +000012392static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12393 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012394 SDValue Op0 = N->getOperand(0);
12395 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12396 // a 32-bit target where SSE doesn't support i64->FP operations.
12397 if (Op0.getOpcode() == ISD::LOAD) {
12398 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12399 EVT VT = Ld->getValueType(0);
12400 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12401 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12402 !XTLI->getSubtarget()->is64Bit() &&
12403 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012404 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12405 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012406 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12407 return FILDChain;
12408 }
12409 }
12410 return SDValue();
12411}
12412
Chris Lattner23a01992010-12-20 01:37:09 +000012413// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12414static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12415 X86TargetLowering::DAGCombinerInfo &DCI) {
12416 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12417 // the result is either zero or one (depending on the input carry bit).
12418 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12419 if (X86::isZeroNode(N->getOperand(0)) &&
12420 X86::isZeroNode(N->getOperand(1)) &&
12421 // We don't have a good way to replace an EFLAGS use, so only do this when
12422 // dead right now.
12423 SDValue(N, 1).use_empty()) {
12424 DebugLoc DL = N->getDebugLoc();
12425 EVT VT = N->getValueType(0);
12426 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12427 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12428 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12429 DAG.getConstant(X86::COND_B,MVT::i8),
12430 N->getOperand(2)),
12431 DAG.getConstant(1, VT));
12432 return DCI.CombineTo(N, Res1, CarryOut);
12433 }
12434
12435 return SDValue();
12436}
12437
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012438// fold (add Y, (sete X, 0)) -> adc 0, Y
12439// (add Y, (setne X, 0)) -> sbb -1, Y
12440// (sub (sete X, 0), Y) -> sbb 0, Y
12441// (sub (setne X, 0), Y) -> adc -1, Y
12442static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12443 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012444
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012445 // Look through ZExts.
12446 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12447 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12448 return SDValue();
12449
12450 SDValue SetCC = Ext.getOperand(0);
12451 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12452 return SDValue();
12453
12454 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12455 if (CC != X86::COND_E && CC != X86::COND_NE)
12456 return SDValue();
12457
12458 SDValue Cmp = SetCC.getOperand(1);
12459 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012460 !X86::isZeroNode(Cmp.getOperand(1)) ||
12461 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012462 return SDValue();
12463
12464 SDValue CmpOp0 = Cmp.getOperand(0);
12465 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12466 DAG.getConstant(1, CmpOp0.getValueType()));
12467
12468 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12469 if (CC == X86::COND_NE)
12470 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12471 DL, OtherVal.getValueType(), OtherVal,
12472 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12473 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12474 DL, OtherVal.getValueType(), OtherVal,
12475 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12476}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012477
Dan Gohman475871a2008-07-27 21:46:04 +000012478SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012479 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012480 SelectionDAG &DAG = DCI.DAG;
12481 switch (N->getOpcode()) {
12482 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012483 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012484 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012485 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012486 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012487 case ISD::ADD:
12488 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012489 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012490 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012491 case ISD::SHL:
12492 case ISD::SRA:
12493 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012494 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012495 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012496 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012497 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012498 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012499 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12500 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012501 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012502 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012503 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012504 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012505 case X86ISD::SHUFPS: // Handle all target specific shuffles
12506 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012507 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012508 case X86ISD::PUNPCKHBW:
12509 case X86ISD::PUNPCKHWD:
12510 case X86ISD::PUNPCKHDQ:
12511 case X86ISD::PUNPCKHQDQ:
12512 case X86ISD::UNPCKHPS:
12513 case X86ISD::UNPCKHPD:
12514 case X86ISD::PUNPCKLBW:
12515 case X86ISD::PUNPCKLWD:
12516 case X86ISD::PUNPCKLDQ:
12517 case X86ISD::PUNPCKLQDQ:
12518 case X86ISD::UNPCKLPS:
12519 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012520 case X86ISD::VUNPCKLPS:
12521 case X86ISD::VUNPCKLPD:
12522 case X86ISD::VUNPCKLPSY:
12523 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012524 case X86ISD::MOVHLPS:
12525 case X86ISD::MOVLHPS:
12526 case X86ISD::PSHUFD:
12527 case X86ISD::PSHUFHW:
12528 case X86ISD::PSHUFLW:
12529 case X86ISD::MOVSS:
12530 case X86ISD::MOVSD:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +000012531 case X86ISD::VPERMIL:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012532 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012533 }
12534
Dan Gohman475871a2008-07-27 21:46:04 +000012535 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012536}
12537
Evan Chenge5b51ac2010-04-17 06:13:15 +000012538/// isTypeDesirableForOp - Return true if the target has native support for
12539/// the specified value type and it is 'desirable' to use the type for the
12540/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12541/// instruction encodings are longer and some i16 instructions are slow.
12542bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12543 if (!isTypeLegal(VT))
12544 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012545 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012546 return true;
12547
12548 switch (Opc) {
12549 default:
12550 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012551 case ISD::LOAD:
12552 case ISD::SIGN_EXTEND:
12553 case ISD::ZERO_EXTEND:
12554 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012555 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012556 case ISD::SRL:
12557 case ISD::SUB:
12558 case ISD::ADD:
12559 case ISD::MUL:
12560 case ISD::AND:
12561 case ISD::OR:
12562 case ISD::XOR:
12563 return false;
12564 }
12565}
12566
12567/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012568/// beneficial for dag combiner to promote the specified node. If true, it
12569/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012570bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012571 EVT VT = Op.getValueType();
12572 if (VT != MVT::i16)
12573 return false;
12574
Evan Cheng4c26e932010-04-19 19:29:22 +000012575 bool Promote = false;
12576 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012577 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012578 default: break;
12579 case ISD::LOAD: {
12580 LoadSDNode *LD = cast<LoadSDNode>(Op);
12581 // If the non-extending load has a single use and it's not live out, then it
12582 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012583 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12584 Op.hasOneUse()*/) {
12585 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12586 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12587 // The only case where we'd want to promote LOAD (rather then it being
12588 // promoted as an operand is when it's only use is liveout.
12589 if (UI->getOpcode() != ISD::CopyToReg)
12590 return false;
12591 }
12592 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012593 Promote = true;
12594 break;
12595 }
12596 case ISD::SIGN_EXTEND:
12597 case ISD::ZERO_EXTEND:
12598 case ISD::ANY_EXTEND:
12599 Promote = true;
12600 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012601 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012602 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012603 SDValue N0 = Op.getOperand(0);
12604 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012605 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012606 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012607 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012608 break;
12609 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012610 case ISD::ADD:
12611 case ISD::MUL:
12612 case ISD::AND:
12613 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012614 case ISD::XOR:
12615 Commute = true;
12616 // fallthrough
12617 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012618 SDValue N0 = Op.getOperand(0);
12619 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012620 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012621 return false;
12622 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012623 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012624 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012625 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012626 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012627 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012628 }
12629 }
12630
12631 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012632 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012633}
12634
Evan Cheng60c07e12006-07-05 22:17:51 +000012635//===----------------------------------------------------------------------===//
12636// X86 Inline Assembly Support
12637//===----------------------------------------------------------------------===//
12638
Chris Lattnerb8105652009-07-20 17:51:36 +000012639bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12640 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012641
12642 std::string AsmStr = IA->getAsmString();
12643
12644 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012645 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012646 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012647
12648 switch (AsmPieces.size()) {
12649 default: return false;
12650 case 1:
12651 AsmStr = AsmPieces[0];
12652 AsmPieces.clear();
12653 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12654
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012655 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012656 // we will turn this bswap into something that will be lowered to logical ops
12657 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12658 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012659 // bswap $0
12660 if (AsmPieces.size() == 2 &&
12661 (AsmPieces[0] == "bswap" ||
12662 AsmPieces[0] == "bswapq" ||
12663 AsmPieces[0] == "bswapl") &&
12664 (AsmPieces[1] == "$0" ||
12665 AsmPieces[1] == "${0:q}")) {
12666 // No need to check constraints, nothing other than the equivalent of
12667 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012668 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012669 if (!Ty || Ty->getBitWidth() % 16 != 0)
12670 return false;
12671 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012672 }
12673 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012674 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012675 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012676 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012677 AsmPieces[1] == "$$8," &&
12678 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012679 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12680 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012681 const std::string &ConstraintsStr = IA->getConstraintString();
12682 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012683 std::sort(AsmPieces.begin(), AsmPieces.end());
12684 if (AsmPieces.size() == 4 &&
12685 AsmPieces[0] == "~{cc}" &&
12686 AsmPieces[1] == "~{dirflag}" &&
12687 AsmPieces[2] == "~{flags}" &&
12688 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012689 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012690 if (!Ty || Ty->getBitWidth() % 16 != 0)
12691 return false;
12692 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012693 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012694 }
12695 break;
12696 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012697 if (CI->getType()->isIntegerTy(32) &&
12698 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12699 SmallVector<StringRef, 4> Words;
12700 SplitString(AsmPieces[0], Words, " \t,");
12701 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12702 Words[2] == "${0:w}") {
12703 Words.clear();
12704 SplitString(AsmPieces[1], Words, " \t,");
12705 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12706 Words[2] == "$0") {
12707 Words.clear();
12708 SplitString(AsmPieces[2], Words, " \t,");
12709 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12710 Words[2] == "${0:w}") {
12711 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012712 const std::string &ConstraintsStr = IA->getConstraintString();
12713 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012714 std::sort(AsmPieces.begin(), AsmPieces.end());
12715 if (AsmPieces.size() == 4 &&
12716 AsmPieces[0] == "~{cc}" &&
12717 AsmPieces[1] == "~{dirflag}" &&
12718 AsmPieces[2] == "~{flags}" &&
12719 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012720 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012721 if (!Ty || Ty->getBitWidth() % 16 != 0)
12722 return false;
12723 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012724 }
12725 }
12726 }
12727 }
12728 }
Evan Cheng55d42002011-01-08 01:24:27 +000012729
12730 if (CI->getType()->isIntegerTy(64)) {
12731 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12732 if (Constraints.size() >= 2 &&
12733 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12734 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12735 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12736 SmallVector<StringRef, 4> Words;
12737 SplitString(AsmPieces[0], Words, " \t");
12738 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012739 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012740 SplitString(AsmPieces[1], Words, " \t");
12741 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12742 Words.clear();
12743 SplitString(AsmPieces[2], Words, " \t,");
12744 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12745 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012746 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012747 if (!Ty || Ty->getBitWidth() % 16 != 0)
12748 return false;
12749 return IntrinsicLowering::LowerToByteSwap(CI);
12750 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012751 }
12752 }
12753 }
12754 }
12755 break;
12756 }
12757 return false;
12758}
12759
12760
12761
Chris Lattnerf4dff842006-07-11 02:54:03 +000012762/// getConstraintType - Given a constraint letter, return the type of
12763/// constraint it is for this target.
12764X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012765X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12766 if (Constraint.size() == 1) {
12767 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012768 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012769 case 'q':
12770 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012771 case 'f':
12772 case 't':
12773 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012774 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012775 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012776 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000012777 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000012778 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012779 case 'a':
12780 case 'b':
12781 case 'c':
12782 case 'd':
12783 case 'S':
12784 case 'D':
12785 case 'A':
12786 return C_Register;
12787 case 'I':
12788 case 'J':
12789 case 'K':
12790 case 'L':
12791 case 'M':
12792 case 'N':
12793 case 'G':
12794 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012795 case 'e':
12796 case 'Z':
12797 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012798 default:
12799 break;
12800 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012801 }
Chris Lattner4234f572007-03-25 02:14:49 +000012802 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012803}
12804
John Thompson44ab89e2010-10-29 17:29:13 +000012805/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012806/// This object must already have been set up with the operand type
12807/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012808TargetLowering::ConstraintWeight
12809 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012810 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012811 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012812 Value *CallOperandVal = info.CallOperandVal;
12813 // If we don't have a value, we can't do a match,
12814 // but allow it at the lowest weight.
12815 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012816 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012817 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012818 // Look at the constraint type.
12819 switch (*constraint) {
12820 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012821 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12822 case 'R':
12823 case 'q':
12824 case 'Q':
12825 case 'a':
12826 case 'b':
12827 case 'c':
12828 case 'd':
12829 case 'S':
12830 case 'D':
12831 case 'A':
12832 if (CallOperandVal->getType()->isIntegerTy())
12833 weight = CW_SpecificReg;
12834 break;
12835 case 'f':
12836 case 't':
12837 case 'u':
12838 if (type->isFloatingPointTy())
12839 weight = CW_SpecificReg;
12840 break;
12841 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012842 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012843 weight = CW_SpecificReg;
12844 break;
12845 case 'x':
12846 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012847 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012848 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012849 break;
12850 case 'I':
12851 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12852 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012853 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012854 }
12855 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012856 case 'J':
12857 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12858 if (C->getZExtValue() <= 63)
12859 weight = CW_Constant;
12860 }
12861 break;
12862 case 'K':
12863 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12864 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12865 weight = CW_Constant;
12866 }
12867 break;
12868 case 'L':
12869 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12870 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12871 weight = CW_Constant;
12872 }
12873 break;
12874 case 'M':
12875 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12876 if (C->getZExtValue() <= 3)
12877 weight = CW_Constant;
12878 }
12879 break;
12880 case 'N':
12881 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12882 if (C->getZExtValue() <= 0xff)
12883 weight = CW_Constant;
12884 }
12885 break;
12886 case 'G':
12887 case 'C':
12888 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12889 weight = CW_Constant;
12890 }
12891 break;
12892 case 'e':
12893 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12894 if ((C->getSExtValue() >= -0x80000000LL) &&
12895 (C->getSExtValue() <= 0x7fffffffLL))
12896 weight = CW_Constant;
12897 }
12898 break;
12899 case 'Z':
12900 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12901 if (C->getZExtValue() <= 0xffffffff)
12902 weight = CW_Constant;
12903 }
12904 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012905 }
12906 return weight;
12907}
12908
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012909/// LowerXConstraint - try to replace an X constraint, which matches anything,
12910/// with another that has more specific requirements based on the type of the
12911/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012912const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012913LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012914 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12915 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012916 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012917 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012918 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012919 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012920 return "x";
12921 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012922
Chris Lattner5e764232008-04-26 23:02:14 +000012923 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012924}
12925
Chris Lattner48884cd2007-08-25 00:47:38 +000012926/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12927/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012928void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012929 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012930 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012931 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012932 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012933
Eric Christopher100c8332011-06-02 23:16:42 +000012934 // Only support length 1 constraints for now.
12935 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012936
Eric Christopher100c8332011-06-02 23:16:42 +000012937 char ConstraintLetter = Constraint[0];
12938 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012939 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012940 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012941 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012942 if (C->getZExtValue() <= 31) {
12943 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012944 break;
12945 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012946 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012947 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012948 case 'J':
12949 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012950 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012951 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12952 break;
12953 }
12954 }
12955 return;
12956 case 'K':
12957 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012958 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012959 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12960 break;
12961 }
12962 }
12963 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012964 case 'N':
12965 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012966 if (C->getZExtValue() <= 255) {
12967 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012968 break;
12969 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012970 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012971 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012972 case 'e': {
12973 // 32-bit signed value
12974 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012975 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12976 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012977 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012978 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012979 break;
12980 }
12981 // FIXME gcc accepts some relocatable values here too, but only in certain
12982 // memory models; it's complicated.
12983 }
12984 return;
12985 }
12986 case 'Z': {
12987 // 32-bit unsigned value
12988 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012989 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12990 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012991 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12992 break;
12993 }
12994 }
12995 // FIXME gcc accepts some relocatable values here too, but only in certain
12996 // memory models; it's complicated.
12997 return;
12998 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012999 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013000 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000013001 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000013002 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000013003 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000013004 break;
13005 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013006
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000013007 // In any sort of PIC mode addresses need to be computed at runtime by
13008 // adding in a register or some sort of table lookup. These can't
13009 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000013010 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000013011 return;
13012
Chris Lattnerdc43a882007-05-03 16:52:29 +000013013 // If we are in non-pic codegen mode, we allow the address of a global (with
13014 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000013015 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000013016 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000013017
Chris Lattner49921962009-05-08 18:23:14 +000013018 // Match either (GA), (GA+C), (GA+C1+C2), etc.
13019 while (1) {
13020 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
13021 Offset += GA->getOffset();
13022 break;
13023 } else if (Op.getOpcode() == ISD::ADD) {
13024 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
13025 Offset += C->getZExtValue();
13026 Op = Op.getOperand(0);
13027 continue;
13028 }
13029 } else if (Op.getOpcode() == ISD::SUB) {
13030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
13031 Offset += -C->getZExtValue();
13032 Op = Op.getOperand(0);
13033 continue;
13034 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000013035 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000013036
Chris Lattner49921962009-05-08 18:23:14 +000013037 // Otherwise, this isn't something we can handle, reject it.
13038 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000013039 }
Eric Christopherfd179292009-08-27 18:07:15 +000013040
Dan Gohman46510a72010-04-15 01:51:59 +000013041 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000013042 // If we require an extra load to get this address, as in PIC mode, we
13043 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000013044 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
13045 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000013046 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000013047
Devang Patel0d881da2010-07-06 22:08:15 +000013048 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
13049 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000013050 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013051 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000013052 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013053
Gabor Greifba36cb52008-08-28 21:40:38 +000013054 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000013055 Ops.push_back(Result);
13056 return;
13057 }
Dale Johannesen1784d162010-06-25 21:55:36 +000013058 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000013059}
13060
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013061std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000013062X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000013063 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000013064 // First, see if this is a constraint that directly corresponds to an LLVM
13065 // register class.
13066 if (Constraint.size() == 1) {
13067 // GCC Constraint Letters
13068 switch (Constraint[0]) {
13069 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000013070 // TODO: Slight differences here in allocation order and leaving
13071 // RIP in the class. Do they matter any more here than they do
13072 // in the normal allocation?
13073 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
13074 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000013075 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000013076 return std::make_pair(0U, X86::GR32RegisterClass);
13077 else if (VT == MVT::i16)
13078 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000013079 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000013080 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000013081 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000013082 return std::make_pair(0U, X86::GR64RegisterClass);
13083 break;
13084 }
13085 // 32-bit fallthrough
13086 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000013087 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000013088 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
13089 else if (VT == MVT::i16)
13090 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000013091 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000013092 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
13093 else if (VT == MVT::i64)
13094 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
13095 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000013096 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000013097 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000013098 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000013099 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013100 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000013101 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000013102 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000013103 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000013104 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000013105 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000013106 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000013107 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
13108 if (VT == MVT::i16)
13109 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
13110 if (VT == MVT::i32 || !Subtarget->is64Bit())
13111 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
13112 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013113 case 'f': // FP Stack registers.
13114 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
13115 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000013116 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013117 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013118 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013119 return std::make_pair(0U, X86::RFP64RegisterClass);
13120 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000013121 case 'y': // MMX_REGS if MMX allowed.
13122 if (!Subtarget->hasMMX()) break;
13123 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013124 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013125 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000013126 // FALL THROUGH.
13127 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013128 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000013129
Owen Anderson825b72b2009-08-11 20:47:22 +000013130 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000013131 default: break;
13132 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013133 case MVT::f32:
13134 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000013135 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013136 case MVT::f64:
13137 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000013138 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013139 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013140 case MVT::v16i8:
13141 case MVT::v8i16:
13142 case MVT::v4i32:
13143 case MVT::v2i64:
13144 case MVT::v4f32:
13145 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000013146 return std::make_pair(0U, X86::VR128RegisterClass);
13147 }
Chris Lattnerad043e82007-04-09 05:11:28 +000013148 break;
13149 }
13150 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013151
Chris Lattnerf76d1802006-07-31 23:26:50 +000013152 // Use the default implementation in TargetLowering to convert the register
13153 // constraint into a member of a register class.
13154 std::pair<unsigned, const TargetRegisterClass*> Res;
13155 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000013156
13157 // Not found as a standard register?
13158 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013159 // Map st(0) -> st(7) -> ST0
13160 if (Constraint.size() == 7 && Constraint[0] == '{' &&
13161 tolower(Constraint[1]) == 's' &&
13162 tolower(Constraint[2]) == 't' &&
13163 Constraint[3] == '(' &&
13164 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
13165 Constraint[5] == ')' &&
13166 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000013167
Chris Lattner56d77c72009-09-13 22:41:48 +000013168 Res.first = X86::ST0+Constraint[4]-'0';
13169 Res.second = X86::RFP80RegisterClass;
13170 return Res;
13171 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013172
Chris Lattner56d77c72009-09-13 22:41:48 +000013173 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013174 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000013175 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000013176 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013177 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013178 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013179
13180 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013181 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013182 Res.first = X86::EFLAGS;
13183 Res.second = X86::CCRRegisterClass;
13184 return Res;
13185 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013186
Dale Johannesen330169f2008-11-13 21:52:36 +000013187 // 'A' means EAX + EDX.
13188 if (Constraint == "A") {
13189 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013190 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013191 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013192 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013193 return Res;
13194 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013195
Chris Lattnerf76d1802006-07-31 23:26:50 +000013196 // Otherwise, check to see if this is a register class of the wrong value
13197 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13198 // turn into {ax},{dx}.
13199 if (Res.second->hasType(VT))
13200 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013201
Chris Lattnerf76d1802006-07-31 23:26:50 +000013202 // All of the single-register GCC register classes map their values onto
13203 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13204 // really want an 8-bit or 32-bit register, map to the appropriate register
13205 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013206 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013207 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013208 unsigned DestReg = 0;
13209 switch (Res.first) {
13210 default: break;
13211 case X86::AX: DestReg = X86::AL; break;
13212 case X86::DX: DestReg = X86::DL; break;
13213 case X86::CX: DestReg = X86::CL; break;
13214 case X86::BX: DestReg = X86::BL; break;
13215 }
13216 if (DestReg) {
13217 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013218 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013219 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013220 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013221 unsigned DestReg = 0;
13222 switch (Res.first) {
13223 default: break;
13224 case X86::AX: DestReg = X86::EAX; break;
13225 case X86::DX: DestReg = X86::EDX; break;
13226 case X86::CX: DestReg = X86::ECX; break;
13227 case X86::BX: DestReg = X86::EBX; break;
13228 case X86::SI: DestReg = X86::ESI; break;
13229 case X86::DI: DestReg = X86::EDI; break;
13230 case X86::BP: DestReg = X86::EBP; break;
13231 case X86::SP: DestReg = X86::ESP; break;
13232 }
13233 if (DestReg) {
13234 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013235 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013236 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013237 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013238 unsigned DestReg = 0;
13239 switch (Res.first) {
13240 default: break;
13241 case X86::AX: DestReg = X86::RAX; break;
13242 case X86::DX: DestReg = X86::RDX; break;
13243 case X86::CX: DestReg = X86::RCX; break;
13244 case X86::BX: DestReg = X86::RBX; break;
13245 case X86::SI: DestReg = X86::RSI; break;
13246 case X86::DI: DestReg = X86::RDI; break;
13247 case X86::BP: DestReg = X86::RBP; break;
13248 case X86::SP: DestReg = X86::RSP; break;
13249 }
13250 if (DestReg) {
13251 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013252 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013253 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013254 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013255 } else if (Res.second == X86::FR32RegisterClass ||
13256 Res.second == X86::FR64RegisterClass ||
13257 Res.second == X86::VR128RegisterClass) {
13258 // Handle references to XMM physical registers that got mapped into the
13259 // wrong class. This can happen with constraints like {xmm0} where the
13260 // target independent register mapper will just pick the first match it can
13261 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013262 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013263 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013264 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013265 Res.second = X86::FR64RegisterClass;
13266 else if (X86::VR128RegisterClass->hasType(VT))
13267 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013268 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013269
Chris Lattnerf76d1802006-07-31 23:26:50 +000013270 return Res;
13271}