blob: 6a3a9cd80adb6f1f50bf64670c8403c3a7e1b4be [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!");
88
89 EVT ElVT = VT.getVectorElementType();
90
91 int Factor = VT.getSizeInBits() / 128;
92
93 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
94 ElVT,
95 VT.getVectorNumElements() / Factor);
96
97 // Extract from UNDEF is UNDEF.
98 if (Vec.getOpcode() == ISD::UNDEF)
99 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
100
101 if (isa<ConstantSDNode>(Idx)) {
102 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
103
104 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
105 // we can match to VEXTRACTF128.
106 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
107
108 // This is the index of the first element of the 128-bit chunk
109 // we want.
110 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
111 * ElemsPerChunk);
112
113 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
114
115 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
116 VecIdx);
117
118 return Result;
119 }
120
121 return SDValue();
122}
123
124/// Generate a DAG to put 128-bits into a vector > 128 bits. This
125/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000126/// simple superregister reference. Idx is an index in the 128 bits
127/// we want. It need not be aligned to a 128-bit bounday. That makes
128/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000129static SDValue Insert128BitVector(SDValue Result,
130 SDValue Vec,
131 SDValue Idx,
132 SelectionDAG &DAG,
133 DebugLoc dl) {
134 if (isa<ConstantSDNode>(Idx)) {
135 EVT VT = Vec.getValueType();
136 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
137
138 EVT ElVT = VT.getVectorElementType();
139
140 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
141
142 EVT ResultVT = Result.getValueType();
143
144 // Insert the relevant 128 bits.
145 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
146
147 // This is the index of the first element of the 128-bit chunk
148 // we want.
149 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
150 * ElemsPerChunk);
151
152 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
153
154 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
155 VecIdx);
156 return Result;
157 }
158
159 return SDValue();
160}
161
David Greenef125a292011-02-08 19:04:41 +0000162/// Given two vectors, concat them.
163static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
164 DebugLoc dl = Lower.getDebugLoc();
165
166 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
167
168 EVT VT = EVT::getVectorVT(*DAG.getContext(),
169 Lower.getValueType().getVectorElementType(),
170 Lower.getValueType().getVectorNumElements() * 2);
171
172 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
173 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
174
175 // Insert the upper subvector.
176 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
177 DAG.getConstant(
178 // This is half the length of the result
179 // vector. Start inserting the upper 128
180 // bits here.
181 Lower.getValueType().getVectorNumElements(),
182 MVT::i32),
183 DAG, dl);
184
185 // Insert the lower subvector.
186 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
187 return Vec;
188}
189
Chris Lattnerf0144122009-07-28 03:13:23 +0000190static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000191 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
192 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000193
Evan Cheng2bffee22011-02-01 01:14:13 +0000194 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000195 if (is64Bit)
196 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000197 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000198 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000199
Evan Cheng203576a2011-07-20 19:50:42 +0000200 if (Subtarget->isTargetELF())
201 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000202 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000203 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000204 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000205}
206
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000207X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000208 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000209 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000210 X86ScalarSSEf64 = Subtarget->hasXMMInt();
211 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000213
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000214 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000215 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000216
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000217 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000218 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219
220 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000221 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000222
Eric Christopherde5e1012011-03-11 01:05:58 +0000223 // For 64-bit since we have so many registers use the ILP scheduler, for
224 // 32-bit code use the register pressure specific scheduling.
225 if (Subtarget->is64Bit())
226 setSchedulingPreference(Sched::ILP);
227 else
228 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000230
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000231 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000232 // Setup Windows compiler runtime calls.
233 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000234 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000235 setLibcallName(RTLIB::SREM_I64, "_allrem");
236 setLibcallName(RTLIB::UREM_I64, "_aullrem");
237 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000238 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000239 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000240 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000241 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000242 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
243 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
244 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000245 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
246 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000247 }
248
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000249 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000250 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000251 setUseUnderscoreSetJmp(false);
252 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000253 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000254 // MS runtime is weird: it exports _setjmp, but longjmp!
255 setUseUnderscoreSetJmp(true);
256 setUseUnderscoreLongJmp(false);
257 } else {
258 setUseUnderscoreSetJmp(true);
259 setUseUnderscoreLongJmp(true);
260 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000261
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000262 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000264 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000270
Scott Michelfdc40a02009-02-17 22:15:04 +0000271 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000273 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000275 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
277 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000278
279 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
281 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
282 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
283 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
284 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
285 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000286
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
288 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
290 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
291 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000292
Evan Cheng25ab6902006-09-08 06:48:29 +0000293 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
295 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000296 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000297 // We have an algorithm for SSE2->double, and we turn this into a
298 // 64-bit FILD followed by conditional FADD for other targets.
299 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000300 // We have an algorithm for SSE2, and we turn this into a 64-bit
301 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000302 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000303 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000304
305 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
306 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
308 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000309
Devang Patel6a784892009-06-05 18:48:29 +0000310 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000311 // SSE has no i16 to fp conversion, only i32
312 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000314 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000316 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
318 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000319 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000320 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
322 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000323 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000324
Dale Johannesen73328d12007-09-19 23:55:34 +0000325 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
326 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
328 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000329
Evan Cheng02568ff2006-01-30 22:13:22 +0000330 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
331 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
333 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000334
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000335 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000337 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000339 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
341 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000342 }
343
344 // Handle FP_TO_UINT by promoting the destination to a larger signed
345 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
347 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
348 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000349
Evan Cheng25ab6902006-09-08 06:48:29 +0000350 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
352 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000353 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000354 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000355 // Expand FP_TO_UINT into a select.
356 // FIXME: We would like to use a Custom expander here eventually to do
357 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000359 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000360 // With SSE3 we can use fisttpll to convert to a signed i64; without
361 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000363 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000364
Chris Lattner399610a2006-12-05 18:22:22 +0000365 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000366 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000367 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
368 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000369 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000370 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000371 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000372 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000373 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000374 }
Chris Lattner21f66852005-12-23 05:15:23 +0000375
Dan Gohmanb00ee212008-02-18 19:34:53 +0000376 // Scalar integer divide and remainder are lowered to use operations that
377 // produce two results, to match the available instructions. This exposes
378 // the two-result form to trivial CSE, which is able to combine x/y and x%y
379 // into a single instruction.
380 //
381 // Scalar integer multiply-high is also lowered to use two-result
382 // operations, to match the available instructions. However, plain multiply
383 // (low) operations are left as Legal, as there are single-result
384 // instructions for this in x86. Using the two-result multiply instructions
385 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000386 for (unsigned i = 0, e = 4; i != e; ++i) {
387 MVT VT = IntVTs[i];
388 setOperationAction(ISD::MULHS, VT, Expand);
389 setOperationAction(ISD::MULHU, VT, Expand);
390 setOperationAction(ISD::SDIV, VT, Expand);
391 setOperationAction(ISD::UDIV, VT, Expand);
392 setOperationAction(ISD::SREM, VT, Expand);
393 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000394
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000395 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000396 setOperationAction(ISD::ADDC, VT, Custom);
397 setOperationAction(ISD::ADDE, VT, Custom);
398 setOperationAction(ISD::SUBC, VT, Custom);
399 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000400 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000401
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
403 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
404 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
405 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000406 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
408 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
409 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
410 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
411 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
412 setOperationAction(ISD::FREM , MVT::f32 , Expand);
413 setOperationAction(ISD::FREM , MVT::f64 , Expand);
414 setOperationAction(ISD::FREM , MVT::f80 , Expand);
415 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000416
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
418 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000419 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
420 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
422 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
425 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000426 }
427
Benjamin Kramer1292c222010-12-04 20:32:23 +0000428 if (Subtarget->hasPOPCNT()) {
429 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
430 } else {
431 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
432 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
433 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
434 if (Subtarget->is64Bit())
435 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
436 }
437
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
439 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000440
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000441 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000442 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000443 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000444 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000445 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
448 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
449 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
450 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000451 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
454 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
455 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000456 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000458 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000459 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000461
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000462 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
464 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
465 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
466 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000467 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
469 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000470 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000471 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
473 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
474 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
475 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000476 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000477 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000478 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
480 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
481 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000482 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
484 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
485 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000486 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000487
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000488 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000490
Eric Christopher9a9d2752010-07-22 02:48:34 +0000491 // We may not have a libcall for MEMBARRIER so we should lower this.
492 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000493
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000494 // On X86 and X86-64, atomic operations are lowered to locked instructions.
495 // Locked instructions, in turn, have implicit fence semantics (all memory
496 // operations are flushed before issuing the locked instruction, and they
497 // are not buffered), so we can fold away the common pattern of
498 // fence-atomic-fence.
499 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000500
Mon P Wang63307c32008-05-05 19:05:59 +0000501 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000502 for (unsigned i = 0, e = 4; i != e; ++i) {
503 MVT VT = IntVTs[i];
504 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
505 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
506 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000507
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000508 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000509 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
513 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
514 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
515 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000516 }
517
Evan Cheng3c992d22006-03-07 02:02:57 +0000518 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000519 if (!Subtarget->isTargetDarwin() &&
520 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000521 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000523 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000524
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
526 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
527 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
528 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000529 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000530 setExceptionPointerRegister(X86::RAX);
531 setExceptionSelectorRegister(X86::RDX);
532 } else {
533 setExceptionPointerRegister(X86::EAX);
534 setExceptionSelectorRegister(X86::EDX);
535 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
537 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000538
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000540
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000542
Nate Begemanacc398c2006-01-25 18:21:52 +0000543 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::VASTART , MVT::Other, Custom);
545 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000546 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::VAARG , MVT::Other, Custom);
548 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000549 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::VAARG , MVT::Other, Expand);
551 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000552 }
Evan Chengae642192007-03-02 23:16:35 +0000553
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
555 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
NAKAMURA Takumia2e07622011-03-24 07:07:00 +0000556 setOperationAction(ISD::DYNAMIC_STACKALLOC,
557 (Subtarget->is64Bit() ? MVT::i64 : MVT::i32),
558 (Subtarget->isTargetCOFF()
559 && !Subtarget->isTargetEnvMacho()
560 ? Custom : Expand));
Chris Lattnerb99329e2006-01-13 02:42:53 +0000561
Evan Chengc7ce29b2009-02-13 22:36:38 +0000562 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000563 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000564 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
566 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000567
Evan Cheng223547a2006-01-31 22:28:30 +0000568 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::FABS , MVT::f64, Custom);
570 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000571
572 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setOperationAction(ISD::FNEG , MVT::f64, Custom);
574 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000575
Evan Cheng68c47cb2007-01-05 07:55:56 +0000576 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
578 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000579
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000580 // Lower this to FGETSIGNx86 plus an AND.
581 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
582 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
583
Evan Chengd25e9e82006-02-02 00:28:23 +0000584 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::FSIN , MVT::f64, Expand);
586 setOperationAction(ISD::FCOS , MVT::f64, Expand);
587 setOperationAction(ISD::FSIN , MVT::f32, Expand);
588 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000589
Chris Lattnera54aa942006-01-29 06:26:08 +0000590 // Expand FP immediates into loads from the stack, except for the special
591 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592 addLegalFPImmediate(APFloat(+0.0)); // xorpd
593 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000594 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595 // Use SSE for f32, x87 for f64.
596 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
598 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599
600 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
603 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000605
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000607
608 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
610 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000611
612 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::FSIN , MVT::f32, Expand);
614 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615
Nate Begemane1795842008-02-14 08:57:00 +0000616 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000617 addLegalFPImmediate(APFloat(+0.0f)); // xorps
618 addLegalFPImmediate(APFloat(+0.0)); // FLD0
619 addLegalFPImmediate(APFloat(+1.0)); // FLD1
620 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
621 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
622
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
625 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000627 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000629 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
631 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000632
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
634 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
635 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
636 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000637
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000638 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
640 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000641 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000642 addLegalFPImmediate(APFloat(+0.0)); // FLD0
643 addLegalFPImmediate(APFloat(+1.0)); // FLD1
644 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
645 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000646 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
647 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
648 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
649 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000650 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000651
Cameron Zwarich33390842011-07-08 21:39:21 +0000652 // We don't support FMA.
653 setOperationAction(ISD::FMA, MVT::f64, Expand);
654 setOperationAction(ISD::FMA, MVT::f32, Expand);
655
Dale Johannesen59a58732007-08-05 18:49:15 +0000656 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000657 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
659 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
660 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000662 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000663 addLegalFPImmediate(TmpFlt); // FLD0
664 TmpFlt.changeSign();
665 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000666
667 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 APFloat TmpFlt2(+1.0);
669 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
670 &ignored);
671 addLegalFPImmediate(TmpFlt2); // FLD1
672 TmpFlt2.changeSign();
673 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
674 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000675
Evan Chengc7ce29b2009-02-13 22:36:38 +0000676 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
678 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000679 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000680
681 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000682 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000683
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000684 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
686 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
687 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000688
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 setOperationAction(ISD::FLOG, MVT::f80, Expand);
690 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
691 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
692 setOperationAction(ISD::FEXP, MVT::f80, Expand);
693 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000694
Mon P Wangf007a8b2008-11-06 05:31:54 +0000695 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000696 // (for widening) or expand (for scalarization). Then we will selectively
697 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
699 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
700 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000716 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
717 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000749 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000750 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
754 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
755 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
756 setTruncStoreAction((MVT::SimpleValueType)VT,
757 (MVT::SimpleValueType)InnerVT, Expand);
758 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
759 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
760 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000761 }
762
Evan Chengc7ce29b2009-02-13 22:36:38 +0000763 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
764 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000765 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000766 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000767 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000768 }
769
Dale Johannesen0488fb62010-09-30 23:57:10 +0000770 // MMX-sized vectors (other than x86mmx) are expected to be expanded
771 // into smaller operations.
772 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
773 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
774 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
775 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
776 setOperationAction(ISD::AND, MVT::v8i8, Expand);
777 setOperationAction(ISD::AND, MVT::v4i16, Expand);
778 setOperationAction(ISD::AND, MVT::v2i32, Expand);
779 setOperationAction(ISD::AND, MVT::v1i64, Expand);
780 setOperationAction(ISD::OR, MVT::v8i8, Expand);
781 setOperationAction(ISD::OR, MVT::v4i16, Expand);
782 setOperationAction(ISD::OR, MVT::v2i32, Expand);
783 setOperationAction(ISD::OR, MVT::v1i64, Expand);
784 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
785 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
786 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
787 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
788 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
789 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
790 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
791 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
792 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
793 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
794 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
795 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
796 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000797 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
798 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
799 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
800 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000801
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000802 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
806 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
807 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
808 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
809 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
810 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
811 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
812 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
813 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
814 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
815 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
816 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000817 }
818
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000819 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000821
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000822 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
823 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
825 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
826 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
827 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
830 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
831 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
832 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
833 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
834 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
835 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
836 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
837 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
838 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
839 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
840 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
841 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
842 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
843 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
844 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000845
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
847 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
848 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
849 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000850
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
852 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
853 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
854 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
855 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000856
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000857 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
858 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
859 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
860 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
861 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
862
Evan Cheng2c3ae372006-04-12 21:21:57 +0000863 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
865 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000866 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000867 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000868 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000869 // Do not attempt to custom lower non-128-bit vectors
870 if (!VT.is128BitVector())
871 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 setOperationAction(ISD::BUILD_VECTOR,
873 VT.getSimpleVT().SimpleTy, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE,
875 VT.getSimpleVT().SimpleTy, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
877 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000879
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
881 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
882 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
883 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
884 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
885 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000886
Nate Begemancdd1eec2008-02-12 22:51:28 +0000887 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
889 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000890 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000891
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000892 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
894 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000895 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000896
897 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000898 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000899 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000900
Owen Andersond6662ad2009-08-10 20:46:15 +0000901 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000903 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000905 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000907 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000909 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000911 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000914
Evan Cheng2c3ae372006-04-12 21:21:57 +0000915 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
917 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
918 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
919 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000920
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
922 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000923 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000924
Nate Begeman14d12ca2008-02-11 04:19:36 +0000925 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000926 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
927 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
928 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
929 setOperationAction(ISD::FRINT, MVT::f32, Legal);
930 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
931 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
932 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
933 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
934 setOperationAction(ISD::FRINT, MVT::f64, Legal);
935 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
936
Nate Begeman14d12ca2008-02-11 04:19:36 +0000937 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000939
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000940 // Can turn SHL into an integer multiply.
941 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000942 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000943
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944 // i8 and i16 vectors are custom , because the source register and source
945 // source memory operand types are not the same width. f32 vectors are
946 // custom since the immediate controlling the insert encodes additional
947 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
949 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
950 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
951 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000952
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
954 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
955 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
956 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000957
958 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
960 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000961 }
962 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000963
Nadav Rotem43012222011-05-11 08:12:09 +0000964 if (Subtarget->hasSSE2()) {
965 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
966 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
967 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
968
969 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
970 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
971 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
972
973 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
974 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
975 }
976
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000977 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000979
David Greene9b9838d2009-06-29 16:47:10 +0000980 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
982 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
983 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
984 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000985 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000986
Owen Anderson825b72b2009-08-11 20:47:22 +0000987 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000988 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
989 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000990
Owen Anderson825b72b2009-08-11 20:47:22 +0000991 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
992 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
993 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
994 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
995 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
996 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000997
Owen Anderson825b72b2009-08-11 20:47:22 +0000998 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
999 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1000 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1001 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1002 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1003 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001004
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001005 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001006 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001007 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1008 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1009 EVT VT = SVT;
1010
1011 // Extract subvector is special because the value type
1012 // (result) is 128-bit but the source is 256-bit wide.
1013 if (VT.is128BitVector())
1014 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1015
1016 // Do not attempt to custom lower other non-256-bit vectors
1017 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001018 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001019
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001020 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1021 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1022 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1023 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
1024 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
1025 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001026 }
1027
David Greene54d8eba2011-01-27 22:38:56 +00001028 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001029 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1030 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1031 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001032
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001033 // Do not attempt to promote non-256-bit vectors
1034 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001035 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001036
1037 setOperationAction(ISD::AND, SVT, Promote);
1038 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1039 setOperationAction(ISD::OR, SVT, Promote);
1040 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1041 setOperationAction(ISD::XOR, SVT, Promote);
1042 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1043 setOperationAction(ISD::LOAD, SVT, Promote);
1044 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1045 setOperationAction(ISD::SELECT, SVT, Promote);
1046 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001047 }
David Greene9b9838d2009-06-29 16:47:10 +00001048 }
1049
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001050 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1051 // of this type with custom code.
1052 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1053 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1054 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1055 }
1056
Evan Cheng6be2c582006-04-05 23:38:46 +00001057 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001058 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001059
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001060
Eli Friedman962f5492010-06-02 19:35:46 +00001061 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1062 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001063 //
Eli Friedman962f5492010-06-02 19:35:46 +00001064 // FIXME: We really should do custom legalization for addition and
1065 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1066 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001067 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1068 // Add/Sub/Mul with overflow operations are custom lowered.
1069 MVT VT = IntVTs[i];
1070 setOperationAction(ISD::SADDO, VT, Custom);
1071 setOperationAction(ISD::UADDO, VT, Custom);
1072 setOperationAction(ISD::SSUBO, VT, Custom);
1073 setOperationAction(ISD::USUBO, VT, Custom);
1074 setOperationAction(ISD::SMULO, VT, Custom);
1075 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001076 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001077
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001078 // There are no 8-bit 3-address imul/mul instructions
1079 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1080 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001081
Evan Chengd54f2d52009-03-31 19:38:51 +00001082 if (!Subtarget->is64Bit()) {
1083 // These libcalls are not available in 32-bit.
1084 setLibcallName(RTLIB::SHL_I128, 0);
1085 setLibcallName(RTLIB::SRL_I128, 0);
1086 setLibcallName(RTLIB::SRA_I128, 0);
1087 }
1088
Evan Cheng206ee9d2006-07-07 08:33:52 +00001089 // We have target-specific dag combine patterns for the following nodes:
1090 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001091 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001092 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001093 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001094 setTargetDAGCombine(ISD::SHL);
1095 setTargetDAGCombine(ISD::SRA);
1096 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001097 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001098 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001099 setTargetDAGCombine(ISD::ADD);
1100 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001101 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001102 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001103 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001104 if (Subtarget->is64Bit())
1105 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001106
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001107 computeRegisterProperties();
1108
Evan Cheng05219282011-01-06 06:52:41 +00001109 // On Darwin, -Os means optimize for size without hurting performance,
1110 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001111 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001112 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001113 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001114 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1115 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1116 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001117 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001118 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001119
1120 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001121}
1122
Scott Michel5b8f82e2008-03-10 15:42:14 +00001123
Owen Anderson825b72b2009-08-11 20:47:22 +00001124MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1125 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001126}
1127
1128
Evan Cheng29286502008-01-23 23:17:41 +00001129/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1130/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001131static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001132 if (MaxAlign == 16)
1133 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001134 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001135 if (VTy->getBitWidth() == 128)
1136 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001137 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001138 unsigned EltAlign = 0;
1139 getMaxByValAlign(ATy->getElementType(), EltAlign);
1140 if (EltAlign > MaxAlign)
1141 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001142 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001143 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1144 unsigned EltAlign = 0;
1145 getMaxByValAlign(STy->getElementType(i), EltAlign);
1146 if (EltAlign > MaxAlign)
1147 MaxAlign = EltAlign;
1148 if (MaxAlign == 16)
1149 break;
1150 }
1151 }
1152 return;
1153}
1154
1155/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1156/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001157/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1158/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001159unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001160 if (Subtarget->is64Bit()) {
1161 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001162 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001163 if (TyAlign > 8)
1164 return TyAlign;
1165 return 8;
1166 }
1167
Evan Cheng29286502008-01-23 23:17:41 +00001168 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001169 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001170 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001171 return Align;
1172}
Chris Lattner2b02a442007-02-25 08:29:00 +00001173
Evan Chengf0df0312008-05-15 08:39:06 +00001174/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001175/// and store operations as a result of memset, memcpy, and memmove
1176/// lowering. If DstAlign is zero that means it's safe to destination
1177/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1178/// means there isn't a need to check it against alignment requirement,
1179/// probably because the source does not need to be loaded. If
1180/// 'NonScalarIntSafe' is true, that means it's safe to return a
1181/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1182/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1183/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001184/// It returns EVT::Other if the type should be determined using generic
1185/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001186EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001187X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1188 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001189 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001190 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001191 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001192 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1193 // linux. This is because the stack realignment code can't handle certain
1194 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001195 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001196 if (NonScalarIntSafe &&
1197 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001198 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001199 (Subtarget->isUnalignedMemAccessFast() ||
1200 ((DstAlign == 0 || DstAlign >= 16) &&
1201 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001202 Subtarget->getStackAlignment() >= 16) {
1203 if (Subtarget->hasSSE2())
1204 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001205 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001206 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001207 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001208 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001209 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001210 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001211 // Do not use f64 to lower memcpy if source is string constant. It's
1212 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001213 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001214 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001215 }
Evan Chengf0df0312008-05-15 08:39:06 +00001216 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 return MVT::i64;
1218 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001219}
1220
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001221/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1222/// current function. The returned value is a member of the
1223/// MachineJumpTableInfo::JTEntryKind enum.
1224unsigned X86TargetLowering::getJumpTableEncoding() const {
1225 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1226 // symbol.
1227 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1228 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001229 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001230
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001231 // Otherwise, use the normal jump table encoding heuristics.
1232 return TargetLowering::getJumpTableEncoding();
1233}
1234
Chris Lattnerc64daab2010-01-26 05:02:42 +00001235const MCExpr *
1236X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1237 const MachineBasicBlock *MBB,
1238 unsigned uid,MCContext &Ctx) const{
1239 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1240 Subtarget->isPICStyleGOT());
1241 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1242 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001243 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1244 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001245}
1246
Evan Chengcc415862007-11-09 01:32:10 +00001247/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1248/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001249SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001250 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001251 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001252 // This doesn't have DebugLoc associated with it, but is not really the
1253 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001254 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001255 return Table;
1256}
1257
Chris Lattner589c6f62010-01-26 06:28:43 +00001258/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1259/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1260/// MCExpr.
1261const MCExpr *X86TargetLowering::
1262getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1263 MCContext &Ctx) const {
1264 // X86-64 uses RIP relative addressing based on the jump table label.
1265 if (Subtarget->isPICStyleRIPRel())
1266 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1267
1268 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001269 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001270}
1271
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001272// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001273std::pair<const TargetRegisterClass*, uint8_t>
1274X86TargetLowering::findRepresentativeClass(EVT VT) const{
1275 const TargetRegisterClass *RRC = 0;
1276 uint8_t Cost = 1;
1277 switch (VT.getSimpleVT().SimpleTy) {
1278 default:
1279 return TargetLowering::findRepresentativeClass(VT);
1280 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1281 RRC = (Subtarget->is64Bit()
1282 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1283 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001284 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001285 RRC = X86::VR64RegisterClass;
1286 break;
1287 case MVT::f32: case MVT::f64:
1288 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1289 case MVT::v4f32: case MVT::v2f64:
1290 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1291 case MVT::v4f64:
1292 RRC = X86::VR128RegisterClass;
1293 break;
1294 }
1295 return std::make_pair(RRC, Cost);
1296}
1297
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001298bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1299 unsigned &Offset) const {
1300 if (!Subtarget->isTargetLinux())
1301 return false;
1302
1303 if (Subtarget->is64Bit()) {
1304 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1305 Offset = 0x28;
1306 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1307 AddressSpace = 256;
1308 else
1309 AddressSpace = 257;
1310 } else {
1311 // %gs:0x14 on i386
1312 Offset = 0x14;
1313 AddressSpace = 256;
1314 }
1315 return true;
1316}
1317
1318
Chris Lattner2b02a442007-02-25 08:29:00 +00001319//===----------------------------------------------------------------------===//
1320// Return Value Calling Convention Implementation
1321//===----------------------------------------------------------------------===//
1322
Chris Lattner59ed56b2007-02-28 04:55:35 +00001323#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001324
Michael J. Spencerec38de22010-10-10 22:04:20 +00001325bool
Eric Christopher471e4222011-06-08 23:55:35 +00001326X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1327 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001328 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001329 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001330 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001331 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001332 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001333 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001334}
1335
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336SDValue
1337X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001338 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001340 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001341 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001342 MachineFunction &MF = DAG.getMachineFunction();
1343 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001344
Chris Lattner9774c912007-02-27 05:28:59 +00001345 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001346 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001347 RVLocs, *DAG.getContext());
1348 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001349
Evan Chengdcea1632010-02-04 02:40:39 +00001350 // Add the regs to the liveout set for the function.
1351 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1352 for (unsigned i = 0; i != RVLocs.size(); ++i)
1353 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1354 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001355
Dan Gohman475871a2008-07-27 21:46:04 +00001356 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001357
Dan Gohman475871a2008-07-27 21:46:04 +00001358 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001359 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1360 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001361 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1362 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001363
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001364 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001365 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1366 CCValAssign &VA = RVLocs[i];
1367 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001368 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001369 EVT ValVT = ValToCopy.getValueType();
1370
Dale Johannesenc4510512010-09-24 19:05:48 +00001371 // If this is x86-64, and we disabled SSE, we can't return FP values,
1372 // or SSE or MMX vectors.
1373 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1374 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001375 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001376 report_fatal_error("SSE register return with SSE disabled");
1377 }
1378 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1379 // llvm-gcc has never done it right and no one has noticed, so this
1380 // should be OK for now.
1381 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001382 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001383 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001384
Chris Lattner447ff682008-03-11 03:23:40 +00001385 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1386 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001387 if (VA.getLocReg() == X86::ST0 ||
1388 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001389 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1390 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001391 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001392 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001393 RetOps.push_back(ValToCopy);
1394 // Don't emit a copytoreg.
1395 continue;
1396 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001397
Evan Cheng242b38b2009-02-23 09:03:22 +00001398 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1399 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001400 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001401 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001402 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001403 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001404 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1405 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001406 // If we don't have SSE2 available, convert to v4f32 so the generated
1407 // register is legal.
1408 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001410 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001411 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001412 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001413
Dale Johannesendd64c412009-02-04 00:33:20 +00001414 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001415 Flag = Chain.getValue(1);
1416 }
Dan Gohman61a92132008-04-21 23:59:07 +00001417
1418 // The x86-64 ABI for returning structs by value requires that we copy
1419 // the sret argument into %rax for the return. We saved the argument into
1420 // a virtual register in the entry block, so now we copy the value out
1421 // and into %rax.
1422 if (Subtarget->is64Bit() &&
1423 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1424 MachineFunction &MF = DAG.getMachineFunction();
1425 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1426 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001427 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001428 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001429 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001430
Dale Johannesendd64c412009-02-04 00:33:20 +00001431 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001432 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001433
1434 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001435 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001437
Chris Lattner447ff682008-03-11 03:23:40 +00001438 RetOps[0] = Chain; // Update chain.
1439
1440 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001441 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001442 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001443
1444 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001445 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001446}
1447
Evan Cheng3d2125c2010-11-30 23:55:39 +00001448bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1449 if (N->getNumValues() != 1)
1450 return false;
1451 if (!N->hasNUsesOfValue(1, 0))
1452 return false;
1453
1454 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001455 if (Copy->getOpcode() != ISD::CopyToReg &&
1456 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001457 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001458
1459 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001460 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001461 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001462 if (UI->getOpcode() != X86ISD::RET_FLAG)
1463 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001464 HasRet = true;
1465 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001466
Evan Cheng1bf891a2010-12-01 22:59:46 +00001467 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001468}
1469
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001470EVT
1471X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001472 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001473 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001474 // TODO: Is this also valid on 32-bit?
1475 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001476 ReturnMVT = MVT::i8;
1477 else
1478 ReturnMVT = MVT::i32;
1479
1480 EVT MinVT = getRegisterType(Context, ReturnMVT);
1481 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001482}
1483
Dan Gohman98ca4f22009-08-05 01:29:28 +00001484/// LowerCallResult - Lower the result values of a call into the
1485/// appropriate copies out of appropriate physical registers.
1486///
1487SDValue
1488X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001489 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001490 const SmallVectorImpl<ISD::InputArg> &Ins,
1491 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001492 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001493
Chris Lattnere32bbf62007-02-28 07:09:55 +00001494 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001495 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001496 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001497 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1498 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001500
Chris Lattner3085e152007-02-25 08:59:22 +00001501 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001502 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001503 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001504 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001505
Torok Edwin3f142c32009-02-01 18:15:56 +00001506 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001507 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001508 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001509 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001510 }
1511
Evan Cheng79fb3b42009-02-20 20:43:02 +00001512 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001513
1514 // If this is a call to a function that returns an fp value on the floating
1515 // point stack, we must guarantee the the value is popped from the stack, so
1516 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001517 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001518 // instead.
1519 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1520 // If we prefer to use the value in xmm registers, copy it out as f80 and
1521 // use a truncate to move it from fp stack reg to xmm reg.
1522 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001523 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001524 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1525 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001526 Val = Chain.getValue(0);
1527
1528 // Round the f80 to the right size, which also moves it to the appropriate
1529 // xmm register.
1530 if (CopyVT != VA.getValVT())
1531 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1532 // This truncation won't change the value.
1533 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001534 } else {
1535 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1536 CopyVT, InFlag).getValue(1);
1537 Val = Chain.getValue(0);
1538 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001539 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001540 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001541 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001542
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001544}
1545
1546
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001547//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001548// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001549//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001550// StdCall calling convention seems to be standard for many Windows' API
1551// routines and around. It differs from C calling convention just a little:
1552// callee should clean up the stack, not caller. Symbols should be also
1553// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001554// For info on fast calling convention see Fast Calling Convention (tail call)
1555// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001556
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001558/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1560 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001561 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001562
Dan Gohman98ca4f22009-08-05 01:29:28 +00001563 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001564}
1565
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001566/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001567/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568static bool
1569ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1570 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001571 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001572
Dan Gohman98ca4f22009-08-05 01:29:28 +00001573 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001574}
1575
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001576/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1577/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001578/// the specific parameter attribute. The copy will be passed as a byval
1579/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001580static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001581CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001582 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1583 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001584 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001585
Dale Johannesendd64c412009-02-04 00:33:20 +00001586 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001587 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001588 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001589}
1590
Chris Lattner29689432010-03-11 00:22:57 +00001591/// IsTailCallConvention - Return true if the calling convention is one that
1592/// supports tail call optimization.
1593static bool IsTailCallConvention(CallingConv::ID CC) {
1594 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1595}
1596
Evan Cheng485fafc2011-03-21 01:19:09 +00001597bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1598 if (!CI->isTailCall())
1599 return false;
1600
1601 CallSite CS(CI);
1602 CallingConv::ID CalleeCC = CS.getCallingConv();
1603 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1604 return false;
1605
1606 return true;
1607}
1608
Evan Cheng0c439eb2010-01-27 00:07:07 +00001609/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1610/// a tailcall target by changing its ABI.
1611static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001612 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001613}
1614
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615SDValue
1616X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001617 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001618 const SmallVectorImpl<ISD::InputArg> &Ins,
1619 DebugLoc dl, SelectionDAG &DAG,
1620 const CCValAssign &VA,
1621 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001622 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001623 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001624 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001625 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001626 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001627 EVT ValVT;
1628
1629 // If value is passed by pointer we have address passed instead of the value
1630 // itself.
1631 if (VA.getLocInfo() == CCValAssign::Indirect)
1632 ValVT = VA.getLocVT();
1633 else
1634 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001635
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001636 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001637 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001638 // In case of tail call optimization mark all arguments mutable. Since they
1639 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001640 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001641 unsigned Bytes = Flags.getByValSize();
1642 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1643 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001644 return DAG.getFrameIndex(FI, getPointerTy());
1645 } else {
1646 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001647 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001648 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1649 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001650 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001651 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001652 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001653}
1654
Dan Gohman475871a2008-07-27 21:46:04 +00001655SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001656X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001657 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658 bool isVarArg,
1659 const SmallVectorImpl<ISD::InputArg> &Ins,
1660 DebugLoc dl,
1661 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001662 SmallVectorImpl<SDValue> &InVals)
1663 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001664 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001665 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001666
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 const Function* Fn = MF.getFunction();
1668 if (Fn->hasExternalLinkage() &&
1669 Subtarget->isTargetCygMing() &&
1670 Fn->getName() == "main")
1671 FuncInfo->setForceFramePointer(true);
1672
Evan Cheng1bc78042006-04-26 01:20:17 +00001673 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001674 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001675 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001676
Chris Lattner29689432010-03-11 00:22:57 +00001677 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1678 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001679
Chris Lattner638402b2007-02-28 07:00:42 +00001680 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001681 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001682 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001683 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001684
1685 // Allocate shadow area for Win64
1686 if (IsWin64) {
1687 CCInfo.AllocateStack(32, 8);
1688 }
1689
Duncan Sands45907662010-10-31 13:21:44 +00001690 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001691
Chris Lattnerf39f7712007-02-28 05:46:49 +00001692 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001693 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001694 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1695 CCValAssign &VA = ArgLocs[i];
1696 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1697 // places.
1698 assert(VA.getValNo() != LastVal &&
1699 "Don't support value assigned to multiple locs yet");
1700 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001701
Chris Lattnerf39f7712007-02-28 05:46:49 +00001702 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001703 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001704 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001706 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001709 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001713 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1714 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001715 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001716 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001717 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001718 RC = X86::VR64RegisterClass;
1719 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001720 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001721
Devang Patel68e6bee2011-02-21 23:21:26 +00001722 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001723 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001724
Chris Lattnerf39f7712007-02-28 05:46:49 +00001725 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1726 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1727 // right size.
1728 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001729 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001730 DAG.getValueType(VA.getValVT()));
1731 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001732 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001733 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001734 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001735 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001736
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001737 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001738 // Handle MMX values passed in XMM regs.
1739 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001740 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1741 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001742 } else
1743 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001744 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001745 } else {
1746 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001748 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001749
1750 // If value is passed via pointer - do a load.
1751 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001752 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1753 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001754
Dan Gohman98ca4f22009-08-05 01:29:28 +00001755 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001756 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001757
Dan Gohman61a92132008-04-21 23:59:07 +00001758 // The x86-64 ABI for returning structs by value requires that we copy
1759 // the sret argument into %rax for the return. Save the argument into
1760 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001761 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001762 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1763 unsigned Reg = FuncInfo->getSRetReturnReg();
1764 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001766 FuncInfo->setSRetReturnReg(Reg);
1767 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001768 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001770 }
1771
Chris Lattnerf39f7712007-02-28 05:46:49 +00001772 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001773 // Align stack specially for tail calls.
1774 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001775 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001776
Evan Cheng1bc78042006-04-26 01:20:17 +00001777 // If the function takes variable number of arguments, make a frame index for
1778 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001779 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001780 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1781 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001782 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 }
1784 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001785 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1786
1787 // FIXME: We should really autogenerate these arrays
1788 static const unsigned GPR64ArgRegsWin64[] = {
1789 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001790 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001791 static const unsigned GPR64ArgRegs64Bit[] = {
1792 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1793 };
1794 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001795 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1796 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1797 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001798 const unsigned *GPR64ArgRegs;
1799 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001800
1801 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001802 // The XMM registers which might contain var arg parameters are shadowed
1803 // in their paired GPR. So we only need to save the GPR to their home
1804 // slots.
1805 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001806 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001807 } else {
1808 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1809 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001810
1811 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001812 }
1813 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1814 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001815
Devang Patel578efa92009-06-05 21:57:13 +00001816 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001817 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001818 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001819 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001820 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001821 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001822 // Kernel mode asks for SSE to be disabled, so don't push them
1823 // on the stack.
1824 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001825
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001826 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001827 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001828 // Get to the caller-allocated home save location. Add 8 to account
1829 // for the return address.
1830 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001831 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001832 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001833 // Fixup to set vararg frame on shadow area (4 x i64).
1834 if (NumIntRegs < 4)
1835 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001836 } else {
1837 // For X86-64, if there are vararg parameters that are passed via
1838 // registers, then we must store them to their spots on the stack so they
1839 // may be loaded by deferencing the result of va_next.
1840 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1841 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1842 FuncInfo->setRegSaveFrameIndex(
1843 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001844 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001845 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001846
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001849 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1850 getPointerTy());
1851 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001852 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001853 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1854 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001855 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001856 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001858 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001859 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001860 MachinePointerInfo::getFixedStack(
1861 FuncInfo->getRegSaveFrameIndex(), Offset),
1862 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001864 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001865 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001866
Dan Gohmanface41a2009-08-16 21:24:25 +00001867 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1868 // Now store the XMM (fp + vector) parameter registers.
1869 SmallVector<SDValue, 11> SaveXMMOps;
1870 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001871
Devang Patel68e6bee2011-02-21 23:21:26 +00001872 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001873 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1874 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001875
Dan Gohman1e93df62010-04-17 14:41:14 +00001876 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1877 FuncInfo->getRegSaveFrameIndex()));
1878 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1879 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001880
Dan Gohmanface41a2009-08-16 21:24:25 +00001881 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001882 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001883 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001884 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1885 SaveXMMOps.push_back(Val);
1886 }
1887 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1888 MVT::Other,
1889 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001890 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001891
1892 if (!MemOps.empty())
1893 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1894 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001895 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001896 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001897
Gordon Henriksen86737662008-01-05 16:56:59 +00001898 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001899 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001900 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001901 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001902 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001903 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001904 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001905 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001906 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001907
Gordon Henriksen86737662008-01-05 16:56:59 +00001908 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001909 // RegSaveFrameIndex is X86-64 only.
1910 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001911 if (CallConv == CallingConv::X86_FastCall ||
1912 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001913 // fastcc functions can't have varargs.
1914 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001915 }
Evan Cheng25caf632006-05-23 21:06:34 +00001916
Dan Gohman98ca4f22009-08-05 01:29:28 +00001917 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001918}
1919
Dan Gohman475871a2008-07-27 21:46:04 +00001920SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001921X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1922 SDValue StackPtr, SDValue Arg,
1923 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001924 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001925 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001926 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001927 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001928 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001929 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001930 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001931
1932 return DAG.getStore(Chain, dl, Arg, PtrOff,
1933 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001934 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001935}
1936
Bill Wendling64e87322009-01-16 19:25:27 +00001937/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001938/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001939SDValue
1940X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001941 SDValue &OutRetAddr, SDValue Chain,
1942 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001943 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001944 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001945 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001946 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001947
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001948 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001949 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1950 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001951 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001952}
1953
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001954/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001955/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001956static SDValue
1957EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001958 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001959 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001960 // Store the return address to the appropriate stack slot.
1961 if (!FPDiff) return Chain;
1962 // Calculate the new stack slot for the return address.
1963 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001964 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001965 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001966 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001967 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001968 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001969 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001970 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001971 return Chain;
1972}
1973
Dan Gohman98ca4f22009-08-05 01:29:28 +00001974SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001975X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001976 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001977 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001979 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 const SmallVectorImpl<ISD::InputArg> &Ins,
1981 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001982 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001983 MachineFunction &MF = DAG.getMachineFunction();
1984 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001985 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001986 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001987 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001988
Evan Cheng5f941932010-02-05 02:21:12 +00001989 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001990 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001991 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1992 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001993 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001994
1995 // Sibcalls are automatically detected tailcalls which do not require
1996 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001997 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001998 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001999
2000 if (isTailCall)
2001 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002002 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002003
Chris Lattner29689432010-03-11 00:22:57 +00002004 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2005 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002006
Chris Lattner638402b2007-02-28 07:00:42 +00002007 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002008 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002009 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002010 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002011
2012 // Allocate shadow area for Win64
2013 if (IsWin64) {
2014 CCInfo.AllocateStack(32, 8);
2015 }
2016
Duncan Sands45907662010-10-31 13:21:44 +00002017 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002018
Chris Lattner423c5f42007-02-28 05:31:48 +00002019 // Get a count of how many bytes are to be pushed on the stack.
2020 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002021 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002022 // This is a sibcall. The memory operands are available in caller's
2023 // own caller's stack.
2024 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002025 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002026 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002027
Gordon Henriksen86737662008-01-05 16:56:59 +00002028 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002029 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002030 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002031 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002032 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2033 FPDiff = NumBytesCallerPushed - NumBytes;
2034
2035 // Set the delta of movement of the returnaddr stackslot.
2036 // But only set if delta is greater than previous delta.
2037 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2038 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2039 }
2040
Evan Chengf22f9b32010-02-06 03:28:46 +00002041 if (!IsSibcall)
2042 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002043
Dan Gohman475871a2008-07-27 21:46:04 +00002044 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002045 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002046 if (isTailCall && FPDiff)
2047 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2048 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002049
Dan Gohman475871a2008-07-27 21:46:04 +00002050 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2051 SmallVector<SDValue, 8> MemOpChains;
2052 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002053
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002054 // Walk the register/memloc assignments, inserting copies/loads. In the case
2055 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002056 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2057 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002058 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002059 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002060 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002061 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002062
Chris Lattner423c5f42007-02-28 05:31:48 +00002063 // Promote the value if needed.
2064 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002065 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002066 case CCValAssign::Full: break;
2067 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002068 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002069 break;
2070 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002071 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002072 break;
2073 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002074 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2075 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002076 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002077 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2078 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002079 } else
2080 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2081 break;
2082 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002083 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002084 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002085 case CCValAssign::Indirect: {
2086 // Store the argument.
2087 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002088 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002089 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002090 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002091 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002092 Arg = SpillSlot;
2093 break;
2094 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002096
Chris Lattner423c5f42007-02-28 05:31:48 +00002097 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002098 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2099 if (isVarArg && IsWin64) {
2100 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2101 // shadow reg if callee is a varargs function.
2102 unsigned ShadowReg = 0;
2103 switch (VA.getLocReg()) {
2104 case X86::XMM0: ShadowReg = X86::RCX; break;
2105 case X86::XMM1: ShadowReg = X86::RDX; break;
2106 case X86::XMM2: ShadowReg = X86::R8; break;
2107 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002108 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002109 if (ShadowReg)
2110 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002111 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002112 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002113 assert(VA.isMemLoc());
2114 if (StackPtr.getNode() == 0)
2115 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2116 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2117 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002118 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002119 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002120
Evan Cheng32fe1032006-05-25 00:59:30 +00002121 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002122 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002123 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002124
Evan Cheng347d5f72006-04-28 21:29:37 +00002125 // Build a sequence of copy-to-reg nodes chained together with token chain
2126 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002127 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002128 // Tail call byval lowering might overwrite argument registers so in case of
2129 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002130 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002131 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002132 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002133 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002134 InFlag = Chain.getValue(1);
2135 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002136
Chris Lattner88e1fd52009-07-09 04:24:46 +00002137 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002138 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2139 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002140 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002141 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2142 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002143 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002144 InFlag);
2145 InFlag = Chain.getValue(1);
2146 } else {
2147 // If we are tail calling and generating PIC/GOT style code load the
2148 // address of the callee into ECX. The value in ecx is used as target of
2149 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2150 // for tail calls on PIC/GOT architectures. Normally we would just put the
2151 // address of GOT into ebx and then call target@PLT. But for tail calls
2152 // ebx would be restored (since ebx is callee saved) before jumping to the
2153 // target@PLT.
2154
2155 // Note: The actual moving to ECX is done further down.
2156 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2157 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2158 !G->getGlobal()->hasProtectedVisibility())
2159 Callee = LowerGlobalAddress(Callee, DAG);
2160 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002161 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002162 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002163 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002164
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002165 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002166 // From AMD64 ABI document:
2167 // For calls that may call functions that use varargs or stdargs
2168 // (prototype-less calls or calls to functions containing ellipsis (...) in
2169 // the declaration) %al is used as hidden argument to specify the number
2170 // of SSE registers used. The contents of %al do not need to match exactly
2171 // the number of registers, but must be an ubound on the number of SSE
2172 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002173
Gordon Henriksen86737662008-01-05 16:56:59 +00002174 // Count the number of XMM registers allocated.
2175 static const unsigned XMMArgRegs[] = {
2176 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2177 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2178 };
2179 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002180 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002181 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002182
Dale Johannesendd64c412009-02-04 00:33:20 +00002183 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002184 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002185 InFlag = Chain.getValue(1);
2186 }
2187
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002188
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002189 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002190 if (isTailCall) {
2191 // Force all the incoming stack arguments to be loaded from the stack
2192 // before any new outgoing arguments are stored to the stack, because the
2193 // outgoing stack slots may alias the incoming argument stack slots, and
2194 // the alias isn't otherwise explicit. This is slightly more conservative
2195 // than necessary, because it means that each store effectively depends
2196 // on every argument instead of just those arguments it would clobber.
2197 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2198
Dan Gohman475871a2008-07-27 21:46:04 +00002199 SmallVector<SDValue, 8> MemOpChains2;
2200 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002201 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002202 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002203 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002204 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002205 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2206 CCValAssign &VA = ArgLocs[i];
2207 if (VA.isRegLoc())
2208 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002209 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002210 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002211 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002212 // Create frame index.
2213 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002214 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002215 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002216 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002217
Duncan Sands276dcbd2008-03-21 09:14:45 +00002218 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002219 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002220 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002221 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002222 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002223 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002224 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002225
Dan Gohman98ca4f22009-08-05 01:29:28 +00002226 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2227 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002228 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002229 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002230 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002231 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002232 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002233 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002234 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002235 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002236 }
2237 }
2238
2239 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002240 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002241 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002242
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002243 // Copy arguments to their registers.
2244 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002245 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002246 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002247 InFlag = Chain.getValue(1);
2248 }
Dan Gohman475871a2008-07-27 21:46:04 +00002249 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002250
Gordon Henriksen86737662008-01-05 16:56:59 +00002251 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002252 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002253 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002254 }
2255
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002256 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2257 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2258 // In the 64-bit large code model, we have to make all calls
2259 // through a register, since the call instruction's 32-bit
2260 // pc-relative offset may not be large enough to hold the whole
2261 // address.
2262 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002263 // If the callee is a GlobalAddress node (quite common, every direct call
2264 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2265 // it.
2266
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002267 // We should use extra load for direct calls to dllimported functions in
2268 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002269 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002270 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002271 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002272 bool ExtraLoad = false;
2273 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002274
Chris Lattner48a7d022009-07-09 05:02:21 +00002275 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2276 // external symbols most go through the PLT in PIC mode. If the symbol
2277 // has hidden or protected visibility, or if it is static or local, then
2278 // we don't need to use the PLT - we can directly call it.
2279 if (Subtarget->isTargetELF() &&
2280 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002281 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002282 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002283 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002284 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002285 (!Subtarget->getTargetTriple().isMacOSX() ||
2286 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002287 // PC-relative references to external symbols should go through $stub,
2288 // unless we're building with the leopard linker or later, which
2289 // automatically synthesizes these stubs.
2290 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002291 } else if (Subtarget->isPICStyleRIPRel() &&
2292 isa<Function>(GV) &&
2293 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2294 // If the function is marked as non-lazy, generate an indirect call
2295 // which loads from the GOT directly. This avoids runtime overhead
2296 // at the cost of eager binding (and one extra byte of encoding).
2297 OpFlags = X86II::MO_GOTPCREL;
2298 WrapperKind = X86ISD::WrapperRIP;
2299 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002300 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002301
Devang Patel0d881da2010-07-06 22:08:15 +00002302 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002303 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002304
2305 // Add a wrapper if needed.
2306 if (WrapperKind != ISD::DELETED_NODE)
2307 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2308 // Add extra indirection if needed.
2309 if (ExtraLoad)
2310 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2311 MachinePointerInfo::getGOT(),
2312 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002313 }
Bill Wendling056292f2008-09-16 21:48:12 +00002314 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002315 unsigned char OpFlags = 0;
2316
Evan Cheng1bf891a2010-12-01 22:59:46 +00002317 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2318 // external symbols should go through the PLT.
2319 if (Subtarget->isTargetELF() &&
2320 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2321 OpFlags = X86II::MO_PLT;
2322 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002323 (!Subtarget->getTargetTriple().isMacOSX() ||
2324 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002325 // PC-relative references to external symbols should go through $stub,
2326 // unless we're building with the leopard linker or later, which
2327 // automatically synthesizes these stubs.
2328 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002329 }
Eric Christopherfd179292009-08-27 18:07:15 +00002330
Chris Lattner48a7d022009-07-09 05:02:21 +00002331 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2332 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002333 }
2334
Chris Lattnerd96d0722007-02-25 06:40:16 +00002335 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002336 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002337 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002338
Evan Chengf22f9b32010-02-06 03:28:46 +00002339 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002340 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2341 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002342 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002343 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002344
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002345 Ops.push_back(Chain);
2346 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002347
Dan Gohman98ca4f22009-08-05 01:29:28 +00002348 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002349 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002350
Gordon Henriksen86737662008-01-05 16:56:59 +00002351 // Add argument registers to the end of the list so that they are known live
2352 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002353 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2354 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2355 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002356
Evan Cheng586ccac2008-03-18 23:36:35 +00002357 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002358 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002359 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2360
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002361 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002362 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002363 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002364
Gabor Greifba36cb52008-08-28 21:40:38 +00002365 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002366 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002367
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002369 // We used to do:
2370 //// If this is the first return lowered for this function, add the regs
2371 //// to the liveout set for the function.
2372 // This isn't right, although it's probably harmless on x86; liveouts
2373 // should be computed from returns not tail calls. Consider a void
2374 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002375 return DAG.getNode(X86ISD::TC_RETURN, dl,
2376 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002377 }
2378
Dale Johannesenace16102009-02-03 19:33:06 +00002379 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002380 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002381
Chris Lattner2d297092006-05-23 18:50:38 +00002382 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002383 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002384 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002385 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002386 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002387 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002388 // pops the hidden struct pointer, so we have to push it back.
2389 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002390 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002391 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002392 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002393
Gordon Henriksenae636f82008-01-03 16:47:34 +00002394 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002395 if (!IsSibcall) {
2396 Chain = DAG.getCALLSEQ_END(Chain,
2397 DAG.getIntPtrConstant(NumBytes, true),
2398 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2399 true),
2400 InFlag);
2401 InFlag = Chain.getValue(1);
2402 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002403
Chris Lattner3085e152007-02-25 08:59:22 +00002404 // Handle result values, copying them out of physregs into vregs that we
2405 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002406 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2407 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002408}
2409
Evan Cheng25ab6902006-09-08 06:48:29 +00002410
2411//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002412// Fast Calling Convention (tail call) implementation
2413//===----------------------------------------------------------------------===//
2414
2415// Like std call, callee cleans arguments, convention except that ECX is
2416// reserved for storing the tail called function address. Only 2 registers are
2417// free for argument passing (inreg). Tail call optimization is performed
2418// provided:
2419// * tailcallopt is enabled
2420// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002421// On X86_64 architecture with GOT-style position independent code only local
2422// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002423// To keep the stack aligned according to platform abi the function
2424// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2425// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002426// If a tail called function callee has more arguments than the caller the
2427// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002428// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002429// original REtADDR, but before the saved framepointer or the spilled registers
2430// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2431// stack layout:
2432// arg1
2433// arg2
2434// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002435// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002436// move area ]
2437// (possible EBP)
2438// ESI
2439// EDI
2440// local1 ..
2441
2442/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2443/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002444unsigned
2445X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2446 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002447 MachineFunction &MF = DAG.getMachineFunction();
2448 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002449 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002450 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002451 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002452 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002453 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002454 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2455 // Number smaller than 12 so just add the difference.
2456 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2457 } else {
2458 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002459 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002460 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002461 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002462 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002463}
2464
Evan Cheng5f941932010-02-05 02:21:12 +00002465/// MatchingStackOffset - Return true if the given stack call argument is
2466/// already available in the same position (relatively) of the caller's
2467/// incoming argument stack.
2468static
2469bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2470 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2471 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002472 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2473 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002474 if (Arg.getOpcode() == ISD::CopyFromReg) {
2475 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002476 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002477 return false;
2478 MachineInstr *Def = MRI->getVRegDef(VR);
2479 if (!Def)
2480 return false;
2481 if (!Flags.isByVal()) {
2482 if (!TII->isLoadFromStackSlot(Def, FI))
2483 return false;
2484 } else {
2485 unsigned Opcode = Def->getOpcode();
2486 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2487 Def->getOperand(1).isFI()) {
2488 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002489 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002490 } else
2491 return false;
2492 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002493 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2494 if (Flags.isByVal())
2495 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002496 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002497 // define @foo(%struct.X* %A) {
2498 // tail call @bar(%struct.X* byval %A)
2499 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002500 return false;
2501 SDValue Ptr = Ld->getBasePtr();
2502 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2503 if (!FINode)
2504 return false;
2505 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002506 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002507 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002508 FI = FINode->getIndex();
2509 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002510 } else
2511 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002512
Evan Cheng4cae1332010-03-05 08:38:04 +00002513 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002514 if (!MFI->isFixedObjectIndex(FI))
2515 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002516 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002517}
2518
Dan Gohman98ca4f22009-08-05 01:29:28 +00002519/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2520/// for tail call optimization. Targets which want to do tail call
2521/// optimization should implement this function.
2522bool
2523X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002524 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002525 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002526 bool isCalleeStructRet,
2527 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002528 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002529 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002530 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002531 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002532 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002533 CalleeCC != CallingConv::C)
2534 return false;
2535
Evan Cheng7096ae42010-01-29 06:45:59 +00002536 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002537 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002538 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002539 CallingConv::ID CallerCC = CallerF->getCallingConv();
2540 bool CCMatch = CallerCC == CalleeCC;
2541
Dan Gohman1797ed52010-02-08 20:27:50 +00002542 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002543 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002544 return true;
2545 return false;
2546 }
2547
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002548 // Look for obvious safe cases to perform tail call optimization that do not
2549 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002550
Evan Cheng2c12cb42010-03-26 16:26:03 +00002551 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2552 // emit a special epilogue.
2553 if (RegInfo->needsStackRealignment(MF))
2554 return false;
2555
Evan Chenga375d472010-03-15 18:54:48 +00002556 // Also avoid sibcall optimization if either caller or callee uses struct
2557 // return semantics.
2558 if (isCalleeStructRet || isCallerStructRet)
2559 return false;
2560
Chad Rosier2416da32011-06-24 21:15:36 +00002561 // An stdcall caller is expected to clean up its arguments; the callee
2562 // isn't going to do that.
2563 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2564 return false;
2565
Chad Rosier871f6642011-05-18 19:59:50 +00002566 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002567 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002568 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002569
2570 // Optimizing for varargs on Win64 is unlikely to be safe without
2571 // additional testing.
2572 if (Subtarget->isTargetWin64())
2573 return false;
2574
Chad Rosier871f6642011-05-18 19:59:50 +00002575 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002576 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2577 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002578
Chad Rosier871f6642011-05-18 19:59:50 +00002579 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2580 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2581 if (!ArgLocs[i].isRegLoc())
2582 return false;
2583 }
2584
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002585 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2586 // Therefore if it's not used by the call it is not safe to optimize this into
2587 // a sibcall.
2588 bool Unused = false;
2589 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2590 if (!Ins[i].Used) {
2591 Unused = true;
2592 break;
2593 }
2594 }
2595 if (Unused) {
2596 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002597 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2598 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002599 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002600 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002601 CCValAssign &VA = RVLocs[i];
2602 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2603 return false;
2604 }
2605 }
2606
Evan Cheng13617962010-04-30 01:12:32 +00002607 // If the calling conventions do not match, then we'd better make sure the
2608 // results are returned in the same way as what the caller expects.
2609 if (!CCMatch) {
2610 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002611 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2612 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002613 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2614
2615 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002616 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2617 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002618 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2619
2620 if (RVLocs1.size() != RVLocs2.size())
2621 return false;
2622 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2623 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2624 return false;
2625 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2626 return false;
2627 if (RVLocs1[i].isRegLoc()) {
2628 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2629 return false;
2630 } else {
2631 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2632 return false;
2633 }
2634 }
2635 }
2636
Evan Chenga6bff982010-01-30 01:22:00 +00002637 // If the callee takes no arguments then go on to check the results of the
2638 // call.
2639 if (!Outs.empty()) {
2640 // Check if stack adjustment is needed. For now, do not do this if any
2641 // argument is passed on the stack.
2642 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002643 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2644 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002645
2646 // Allocate shadow area for Win64
2647 if (Subtarget->isTargetWin64()) {
2648 CCInfo.AllocateStack(32, 8);
2649 }
2650
Duncan Sands45907662010-10-31 13:21:44 +00002651 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002652 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002653 MachineFunction &MF = DAG.getMachineFunction();
2654 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2655 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002656
2657 // Check if the arguments are already laid out in the right way as
2658 // the caller's fixed stack objects.
2659 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002660 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2661 const X86InstrInfo *TII =
2662 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002663 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2664 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002665 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002666 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002667 if (VA.getLocInfo() == CCValAssign::Indirect)
2668 return false;
2669 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002670 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2671 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002672 return false;
2673 }
2674 }
2675 }
Evan Cheng9c044672010-05-29 01:35:22 +00002676
2677 // If the tailcall address may be in a register, then make sure it's
2678 // possible to register allocate for it. In 32-bit, the call address can
2679 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002680 // callee-saved registers are restored. These happen to be the same
2681 // registers used to pass 'inreg' arguments so watch out for those.
2682 if (!Subtarget->is64Bit() &&
2683 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002684 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002685 unsigned NumInRegs = 0;
2686 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2687 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002688 if (!VA.isRegLoc())
2689 continue;
2690 unsigned Reg = VA.getLocReg();
2691 switch (Reg) {
2692 default: break;
2693 case X86::EAX: case X86::EDX: case X86::ECX:
2694 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002695 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002696 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002697 }
2698 }
2699 }
Evan Chenga6bff982010-01-30 01:22:00 +00002700 }
Evan Chengb1712452010-01-27 06:25:16 +00002701
Evan Cheng86809cc2010-02-03 03:28:02 +00002702 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002703}
2704
Dan Gohman3df24e62008-09-03 23:12:08 +00002705FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002706X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2707 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002708}
2709
2710
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002711//===----------------------------------------------------------------------===//
2712// Other Lowering Hooks
2713//===----------------------------------------------------------------------===//
2714
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002715static bool MayFoldLoad(SDValue Op) {
2716 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2717}
2718
2719static bool MayFoldIntoStore(SDValue Op) {
2720 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2721}
2722
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002723static bool isTargetShuffle(unsigned Opcode) {
2724 switch(Opcode) {
2725 default: return false;
2726 case X86ISD::PSHUFD:
2727 case X86ISD::PSHUFHW:
2728 case X86ISD::PSHUFLW:
2729 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002730 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002731 case X86ISD::SHUFPS:
2732 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002733 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002734 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002735 case X86ISD::MOVLPS:
2736 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002737 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002738 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002739 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002740 case X86ISD::MOVSS:
2741 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002742 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002743 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002744 case X86ISD::VUNPCKLPS:
2745 case X86ISD::VUNPCKLPD:
2746 case X86ISD::VUNPCKLPSY:
2747 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002748 case X86ISD::PUNPCKLWD:
2749 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002750 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002751 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002752 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002753 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002754 case X86ISD::PUNPCKHWD:
2755 case X86ISD::PUNPCKHBW:
2756 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002757 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002758 return true;
2759 }
2760 return false;
2761}
2762
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002763static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002764 SDValue V1, SelectionDAG &DAG) {
2765 switch(Opc) {
2766 default: llvm_unreachable("Unknown x86 shuffle node");
2767 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002768 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002769 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002770 return DAG.getNode(Opc, dl, VT, V1);
2771 }
2772
2773 return SDValue();
2774}
2775
2776static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002777 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002778 switch(Opc) {
2779 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002780 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002781 case X86ISD::PSHUFHW:
2782 case X86ISD::PSHUFLW:
2783 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2784 }
2785
2786 return SDValue();
2787}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002788
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002789static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2790 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2791 switch(Opc) {
2792 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002793 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002794 case X86ISD::SHUFPD:
2795 case X86ISD::SHUFPS:
2796 return DAG.getNode(Opc, dl, VT, V1, V2,
2797 DAG.getConstant(TargetMask, MVT::i8));
2798 }
2799 return SDValue();
2800}
2801
2802static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2803 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2804 switch(Opc) {
2805 default: llvm_unreachable("Unknown x86 shuffle node");
2806 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002807 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002808 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002809 case X86ISD::MOVLPS:
2810 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002811 case X86ISD::MOVSS:
2812 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002813 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002814 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002815 case X86ISD::VUNPCKLPS:
2816 case X86ISD::VUNPCKLPD:
2817 case X86ISD::VUNPCKLPSY:
2818 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002819 case X86ISD::PUNPCKLWD:
2820 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002821 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002822 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002823 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002824 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002825 case X86ISD::PUNPCKHWD:
2826 case X86ISD::PUNPCKHBW:
2827 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002828 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002829 return DAG.getNode(Opc, dl, VT, V1, V2);
2830 }
2831 return SDValue();
2832}
2833
Dan Gohmand858e902010-04-17 15:26:15 +00002834SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002835 MachineFunction &MF = DAG.getMachineFunction();
2836 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2837 int ReturnAddrIndex = FuncInfo->getRAIndex();
2838
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002839 if (ReturnAddrIndex == 0) {
2840 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002841 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002842 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002843 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002844 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002845 }
2846
Evan Cheng25ab6902006-09-08 06:48:29 +00002847 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002848}
2849
2850
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002851bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2852 bool hasSymbolicDisplacement) {
2853 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002854 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002855 return false;
2856
2857 // If we don't have a symbolic displacement - we don't have any extra
2858 // restrictions.
2859 if (!hasSymbolicDisplacement)
2860 return true;
2861
2862 // FIXME: Some tweaks might be needed for medium code model.
2863 if (M != CodeModel::Small && M != CodeModel::Kernel)
2864 return false;
2865
2866 // For small code model we assume that latest object is 16MB before end of 31
2867 // bits boundary. We may also accept pretty large negative constants knowing
2868 // that all objects are in the positive half of address space.
2869 if (M == CodeModel::Small && Offset < 16*1024*1024)
2870 return true;
2871
2872 // For kernel code model we know that all object resist in the negative half
2873 // of 32bits address space. We may not accept negative offsets, since they may
2874 // be just off and we may accept pretty large positive ones.
2875 if (M == CodeModel::Kernel && Offset > 0)
2876 return true;
2877
2878 return false;
2879}
2880
Evan Chengef41ff62011-06-23 17:54:54 +00002881/// isCalleePop - Determines whether the callee is required to pop its
2882/// own arguments. Callee pop is necessary to support tail calls.
2883bool X86::isCalleePop(CallingConv::ID CallingConv,
2884 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2885 if (IsVarArg)
2886 return false;
2887
2888 switch (CallingConv) {
2889 default:
2890 return false;
2891 case CallingConv::X86_StdCall:
2892 return !is64Bit;
2893 case CallingConv::X86_FastCall:
2894 return !is64Bit;
2895 case CallingConv::X86_ThisCall:
2896 return !is64Bit;
2897 case CallingConv::Fast:
2898 return TailCallOpt;
2899 case CallingConv::GHC:
2900 return TailCallOpt;
2901 }
2902}
2903
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002904/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2905/// specific condition code, returning the condition code and the LHS/RHS of the
2906/// comparison to make.
2907static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2908 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002909 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002910 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2911 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2912 // X > -1 -> X == 0, jump !sign.
2913 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002914 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002915 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2916 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002917 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002918 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002919 // X < 1 -> X <= 0
2920 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002921 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002922 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002923 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002924
Evan Chengd9558e02006-01-06 00:43:03 +00002925 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002926 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002927 case ISD::SETEQ: return X86::COND_E;
2928 case ISD::SETGT: return X86::COND_G;
2929 case ISD::SETGE: return X86::COND_GE;
2930 case ISD::SETLT: return X86::COND_L;
2931 case ISD::SETLE: return X86::COND_LE;
2932 case ISD::SETNE: return X86::COND_NE;
2933 case ISD::SETULT: return X86::COND_B;
2934 case ISD::SETUGT: return X86::COND_A;
2935 case ISD::SETULE: return X86::COND_BE;
2936 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002937 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002939
Chris Lattner4c78e022008-12-23 23:42:27 +00002940 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002941
Chris Lattner4c78e022008-12-23 23:42:27 +00002942 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002943 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2944 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002945 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2946 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002947 }
2948
Chris Lattner4c78e022008-12-23 23:42:27 +00002949 switch (SetCCOpcode) {
2950 default: break;
2951 case ISD::SETOLT:
2952 case ISD::SETOLE:
2953 case ISD::SETUGT:
2954 case ISD::SETUGE:
2955 std::swap(LHS, RHS);
2956 break;
2957 }
2958
2959 // On a floating point condition, the flags are set as follows:
2960 // ZF PF CF op
2961 // 0 | 0 | 0 | X > Y
2962 // 0 | 0 | 1 | X < Y
2963 // 1 | 0 | 0 | X == Y
2964 // 1 | 1 | 1 | unordered
2965 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002966 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002967 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002968 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002969 case ISD::SETOLT: // flipped
2970 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002971 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002972 case ISD::SETOLE: // flipped
2973 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002974 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002975 case ISD::SETUGT: // flipped
2976 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002977 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002978 case ISD::SETUGE: // flipped
2979 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002980 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002981 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002982 case ISD::SETNE: return X86::COND_NE;
2983 case ISD::SETUO: return X86::COND_P;
2984 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002985 case ISD::SETOEQ:
2986 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002987 }
Evan Chengd9558e02006-01-06 00:43:03 +00002988}
2989
Evan Cheng4a460802006-01-11 00:33:36 +00002990/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2991/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002992/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002993static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002994 switch (X86CC) {
2995 default:
2996 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002997 case X86::COND_B:
2998 case X86::COND_BE:
2999 case X86::COND_E:
3000 case X86::COND_P:
3001 case X86::COND_A:
3002 case X86::COND_AE:
3003 case X86::COND_NE:
3004 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003005 return true;
3006 }
3007}
3008
Evan Chengeb2f9692009-10-27 19:56:55 +00003009/// isFPImmLegal - Returns true if the target can instruction select the
3010/// specified FP immediate natively. If false, the legalizer will
3011/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003012bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003013 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3014 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3015 return true;
3016 }
3017 return false;
3018}
3019
Nate Begeman9008ca62009-04-27 18:41:29 +00003020/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3021/// the specified range (L, H].
3022static bool isUndefOrInRange(int Val, int Low, int Hi) {
3023 return (Val < 0) || (Val >= Low && Val < Hi);
3024}
3025
3026/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3027/// specified value.
3028static bool isUndefOrEqual(int Val, int CmpVal) {
3029 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003030 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003031 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003032}
3033
Nate Begeman9008ca62009-04-27 18:41:29 +00003034/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3035/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3036/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003037static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003038 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003041 return (Mask[0] < 2 && Mask[1] < 2);
3042 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003043}
3044
Nate Begeman9008ca62009-04-27 18:41:29 +00003045bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003046 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 N->getMask(M);
3048 return ::isPSHUFDMask(M, N->getValueType(0));
3049}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003050
Nate Begeman9008ca62009-04-27 18:41:29 +00003051/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3052/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003053static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003054 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003055 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003056
Nate Begeman9008ca62009-04-27 18:41:29 +00003057 // Lower quadword copied in order or undef.
3058 for (int i = 0; i != 4; ++i)
3059 if (Mask[i] >= 0 && Mask[i] != i)
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 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003063 for (int i = 4; i != 8; ++i)
3064 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003065 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003066
Evan Cheng506d3df2006-03-29 23:07:14 +00003067 return true;
3068}
3069
Nate Begeman9008ca62009-04-27 18:41:29 +00003070bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003071 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003072 N->getMask(M);
3073 return ::isPSHUFHWMask(M, N->getValueType(0));
3074}
Evan Cheng506d3df2006-03-29 23:07:14 +00003075
Nate Begeman9008ca62009-04-27 18:41:29 +00003076/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3077/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003078static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003079 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003080 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003081
Rafael Espindola15684b22009-04-24 12:40:33 +00003082 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003083 for (int i = 4; i != 8; ++i)
3084 if (Mask[i] >= 0 && Mask[i] != i)
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 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003088 for (int i = 0; i != 4; ++i)
3089 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003090 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003091
Rafael Espindola15684b22009-04-24 12:40:33 +00003092 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003093}
3094
Nate Begeman9008ca62009-04-27 18:41:29 +00003095bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003096 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003097 N->getMask(M);
3098 return ::isPSHUFLWMask(M, N->getValueType(0));
3099}
3100
Nate Begemana09008b2009-10-19 02:17:23 +00003101/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3102/// is suitable for input to PALIGNR.
3103static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3104 bool hasSSSE3) {
3105 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003106
Nate Begemana09008b2009-10-19 02:17:23 +00003107 // Do not handle v2i64 / v2f64 shuffles with palignr.
3108 if (e < 4 || !hasSSSE3)
3109 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003110
Nate Begemana09008b2009-10-19 02:17:23 +00003111 for (i = 0; i != e; ++i)
3112 if (Mask[i] >= 0)
3113 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003114
Nate Begemana09008b2009-10-19 02:17:23 +00003115 // All undef, not a palignr.
3116 if (i == e)
3117 return false;
3118
3119 // Determine if it's ok to perform a palignr with only the LHS, since we
3120 // don't have access to the actual shuffle elements to see if RHS is undef.
3121 bool Unary = Mask[i] < (int)e;
3122 bool NeedsUnary = false;
3123
3124 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003125
Nate Begemana09008b2009-10-19 02:17:23 +00003126 // Check the rest of the elements to see if they are consecutive.
3127 for (++i; i != e; ++i) {
3128 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003129 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003130 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003131
Nate Begemana09008b2009-10-19 02:17:23 +00003132 Unary = Unary && (m < (int)e);
3133 NeedsUnary = NeedsUnary || (m < s);
3134
3135 if (NeedsUnary && !Unary)
3136 return false;
3137 if (Unary && m != ((s+i) & (e-1)))
3138 return false;
3139 if (!Unary && m != (s+i))
3140 return false;
3141 }
3142 return true;
3143}
3144
3145bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3146 SmallVector<int, 8> M;
3147 N->getMask(M);
3148 return ::isPALIGNRMask(M, N->getValueType(0), true);
3149}
3150
Evan Cheng14aed5e2006-03-24 01:18:28 +00003151/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3152/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003153static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 int NumElems = VT.getVectorNumElements();
3155 if (NumElems != 2 && NumElems != 4)
3156 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003157
Nate Begeman9008ca62009-04-27 18:41:29 +00003158 int Half = NumElems / 2;
3159 for (int i = 0; i < Half; ++i)
3160 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003161 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 for (int i = Half; i < NumElems; ++i)
3163 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003164 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003165
Evan Cheng14aed5e2006-03-24 01:18:28 +00003166 return true;
3167}
3168
Nate Begeman9008ca62009-04-27 18:41:29 +00003169bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3170 SmallVector<int, 8> M;
3171 N->getMask(M);
3172 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003173}
3174
Evan Cheng213d2cf2007-05-17 18:45:50 +00003175/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003176/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3177/// half elements to come from vector 1 (which would equal the dest.) and
3178/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003179static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003181
3182 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003184
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 int Half = NumElems / 2;
3186 for (int i = 0; i < Half; ++i)
3187 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003188 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003189 for (int i = Half; i < NumElems; ++i)
3190 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003191 return false;
3192 return true;
3193}
3194
Nate Begeman9008ca62009-04-27 18:41:29 +00003195static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3196 SmallVector<int, 8> M;
3197 N->getMask(M);
3198 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003199}
3200
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003201/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3202/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003203bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3204 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003205 return false;
3206
Evan Cheng2064a2b2006-03-28 06:50:32 +00003207 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003208 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3209 isUndefOrEqual(N->getMaskElt(1), 7) &&
3210 isUndefOrEqual(N->getMaskElt(2), 2) &&
3211 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003212}
3213
Nate Begeman0b10b912009-11-07 23:17:15 +00003214/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3215/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3216/// <2, 3, 2, 3>
3217bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3218 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003219
Nate Begeman0b10b912009-11-07 23:17:15 +00003220 if (NumElems != 4)
3221 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003222
Nate Begeman0b10b912009-11-07 23:17:15 +00003223 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3224 isUndefOrEqual(N->getMaskElt(1), 3) &&
3225 isUndefOrEqual(N->getMaskElt(2), 2) &&
3226 isUndefOrEqual(N->getMaskElt(3), 3);
3227}
3228
Evan Cheng5ced1d82006-04-06 23:23:56 +00003229/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3230/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003231bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3232 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003233
Evan Cheng5ced1d82006-04-06 23:23:56 +00003234 if (NumElems != 2 && NumElems != 4)
3235 return false;
3236
Evan Chengc5cdff22006-04-07 21:53:05 +00003237 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003238 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003239 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003240
Evan Chengc5cdff22006-04-07 21:53:05 +00003241 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003243 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003244
3245 return true;
3246}
3247
Nate Begeman0b10b912009-11-07 23:17:15 +00003248/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3249/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3250bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003251 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003252
David Greenea20244d2011-03-02 17:23:43 +00003253 if ((NumElems != 2 && NumElems != 4)
3254 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003255 return false;
3256
Evan Chengc5cdff22006-04-07 21:53:05 +00003257 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003259 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003260
Nate Begeman9008ca62009-04-27 18:41:29 +00003261 for (unsigned i = 0; i < NumElems/2; ++i)
3262 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003263 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003264
3265 return true;
3266}
3267
Evan Cheng0038e592006-03-28 00:39:58 +00003268/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3269/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003270static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003271 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003272 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003273 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003274 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003275
David Greenea20244d2011-03-02 17:23:43 +00003276 // Handle vector lengths > 128 bits. Define a "section" as a set of
3277 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3278 // sections.
3279 unsigned NumSections = VT.getSizeInBits() / 128;
3280 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3281 unsigned NumSectionElts = NumElts / NumSections;
3282
3283 unsigned Start = 0;
3284 unsigned End = NumSectionElts;
3285 for (unsigned s = 0; s < NumSections; ++s) {
3286 for (unsigned i = Start, j = s * NumSectionElts;
3287 i != End;
3288 i += 2, ++j) {
3289 int BitI = Mask[i];
3290 int BitI1 = Mask[i+1];
3291 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003292 return false;
David Greenea20244d2011-03-02 17:23:43 +00003293 if (V2IsSplat) {
3294 if (!isUndefOrEqual(BitI1, NumElts))
3295 return false;
3296 } else {
3297 if (!isUndefOrEqual(BitI1, j + NumElts))
3298 return false;
3299 }
Evan Cheng39623da2006-04-20 08:58:49 +00003300 }
David Greenea20244d2011-03-02 17:23:43 +00003301 // Process the next 128 bits.
3302 Start += NumSectionElts;
3303 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003304 }
David Greenea20244d2011-03-02 17:23:43 +00003305
Evan Cheng0038e592006-03-28 00:39:58 +00003306 return true;
3307}
3308
Nate Begeman9008ca62009-04-27 18:41:29 +00003309bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3310 SmallVector<int, 8> M;
3311 N->getMask(M);
3312 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003313}
3314
Evan Cheng4fcb9222006-03-28 02:43:26 +00003315/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3316/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003317static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003318 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003319 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003320 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003321 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003322
Nate Begeman9008ca62009-04-27 18:41:29 +00003323 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3324 int BitI = Mask[i];
3325 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003326 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003327 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003328 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003329 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003330 return false;
3331 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003332 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003333 return false;
3334 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003335 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003336 return true;
3337}
3338
Nate Begeman9008ca62009-04-27 18:41:29 +00003339bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3340 SmallVector<int, 8> M;
3341 N->getMask(M);
3342 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003343}
3344
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003345/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3346/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3347/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003348static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003350 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003351 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003352
David Greenea20244d2011-03-02 17:23:43 +00003353 // Handle vector lengths > 128 bits. Define a "section" as a set of
3354 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3355 // sections.
3356 unsigned NumSections = VT.getSizeInBits() / 128;
3357 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3358 unsigned NumSectionElts = NumElems / NumSections;
3359
3360 for (unsigned s = 0; s < NumSections; ++s) {
3361 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3362 i != NumSectionElts * (s + 1);
3363 i += 2, ++j) {
3364 int BitI = Mask[i];
3365 int BitI1 = Mask[i+1];
3366
3367 if (!isUndefOrEqual(BitI, j))
3368 return false;
3369 if (!isUndefOrEqual(BitI1, j))
3370 return false;
3371 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003372 }
David Greenea20244d2011-03-02 17:23:43 +00003373
Rafael Espindola15684b22009-04-24 12:40:33 +00003374 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003375}
3376
Nate Begeman9008ca62009-04-27 18:41:29 +00003377bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3378 SmallVector<int, 8> M;
3379 N->getMask(M);
3380 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3381}
3382
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003383/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3384/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3385/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003386static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003388 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3389 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003390
Nate Begeman9008ca62009-04-27 18:41:29 +00003391 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3392 int BitI = Mask[i];
3393 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003394 if (!isUndefOrEqual(BitI, j))
3395 return false;
3396 if (!isUndefOrEqual(BitI1, j))
3397 return false;
3398 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003399 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003400}
3401
Nate Begeman9008ca62009-04-27 18:41:29 +00003402bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3403 SmallVector<int, 8> M;
3404 N->getMask(M);
3405 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3406}
3407
Evan Cheng017dcc62006-04-21 01:05:10 +00003408/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3409/// specifies a shuffle of elements that is suitable for input to MOVSS,
3410/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003411static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003412 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003413 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003414
3415 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003416
Nate Begeman9008ca62009-04-27 18:41:29 +00003417 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003418 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003419
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 for (int i = 1; i < NumElts; ++i)
3421 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003422 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003423
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003424 return true;
3425}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003426
Nate Begeman9008ca62009-04-27 18:41:29 +00003427bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3428 SmallVector<int, 8> M;
3429 N->getMask(M);
3430 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003431}
3432
Evan Cheng017dcc62006-04-21 01:05:10 +00003433/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3434/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003435/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003436static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 bool V2IsSplat = false, bool V2IsUndef = false) {
3438 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003439 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003440 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003441
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003443 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003444
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 for (int i = 1; i < NumOps; ++i)
3446 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3447 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3448 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003449 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003450
Evan Cheng39623da2006-04-20 08:58:49 +00003451 return true;
3452}
3453
Nate Begeman9008ca62009-04-27 18:41:29 +00003454static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003455 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 SmallVector<int, 8> M;
3457 N->getMask(M);
3458 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003459}
3460
Evan Chengd9539472006-04-14 21:59:03 +00003461/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3462/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003463bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3464 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003465 return false;
3466
3467 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003468 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 int Elt = N->getMaskElt(i);
3470 if (Elt >= 0 && Elt != 1)
3471 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003472 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003473
3474 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003475 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 int Elt = N->getMaskElt(i);
3477 if (Elt >= 0 && Elt != 3)
3478 return false;
3479 if (Elt == 3)
3480 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003481 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003482 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003483 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003484 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003485}
3486
3487/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3488/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003489bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3490 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003491 return false;
3492
3493 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003494 for (unsigned i = 0; i < 2; ++i)
3495 if (N->getMaskElt(i) > 0)
3496 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003497
3498 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003499 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003500 int Elt = N->getMaskElt(i);
3501 if (Elt >= 0 && Elt != 2)
3502 return false;
3503 if (Elt == 2)
3504 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003505 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003506 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003507 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003508}
3509
Evan Cheng0b457f02008-09-25 20:50:48 +00003510/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3511/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003512bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3513 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003514
Nate Begeman9008ca62009-04-27 18:41:29 +00003515 for (int i = 0; i < e; ++i)
3516 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003517 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003518 for (int i = 0; i < e; ++i)
3519 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003520 return false;
3521 return true;
3522}
3523
David Greenec38a03e2011-02-03 15:50:00 +00003524/// isVEXTRACTF128Index - Return true if the specified
3525/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3526/// suitable for input to VEXTRACTF128.
3527bool X86::isVEXTRACTF128Index(SDNode *N) {
3528 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3529 return false;
3530
3531 // The index should be aligned on a 128-bit boundary.
3532 uint64_t Index =
3533 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3534
3535 unsigned VL = N->getValueType(0).getVectorNumElements();
3536 unsigned VBits = N->getValueType(0).getSizeInBits();
3537 unsigned ElSize = VBits / VL;
3538 bool Result = (Index * ElSize) % 128 == 0;
3539
3540 return Result;
3541}
3542
David Greeneccacdc12011-02-04 16:08:29 +00003543/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3544/// operand specifies a subvector insert that is suitable for input to
3545/// VINSERTF128.
3546bool X86::isVINSERTF128Index(SDNode *N) {
3547 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3548 return false;
3549
3550 // The index should be aligned on a 128-bit boundary.
3551 uint64_t Index =
3552 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3553
3554 unsigned VL = N->getValueType(0).getVectorNumElements();
3555 unsigned VBits = N->getValueType(0).getSizeInBits();
3556 unsigned ElSize = VBits / VL;
3557 bool Result = (Index * ElSize) % 128 == 0;
3558
3559 return Result;
3560}
3561
Evan Cheng63d33002006-03-22 08:01:21 +00003562/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003563/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003564unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003565 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3566 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3567
Evan Chengb9df0ca2006-03-22 02:53:00 +00003568 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3569 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003570 for (int i = 0; i < NumOperands; ++i) {
3571 int Val = SVOp->getMaskElt(NumOperands-i-1);
3572 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003573 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003574 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003575 if (i != NumOperands - 1)
3576 Mask <<= Shift;
3577 }
Evan Cheng63d33002006-03-22 08:01:21 +00003578 return Mask;
3579}
3580
Evan Cheng506d3df2006-03-29 23:07:14 +00003581/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003582/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003583unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003584 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003585 unsigned Mask = 0;
3586 // 8 nodes, but we only care about the last 4.
3587 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003588 int Val = SVOp->getMaskElt(i);
3589 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003590 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003591 if (i != 4)
3592 Mask <<= 2;
3593 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003594 return Mask;
3595}
3596
3597/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003598/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003599unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003601 unsigned Mask = 0;
3602 // 8 nodes, but we only care about the first 4.
3603 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003604 int Val = SVOp->getMaskElt(i);
3605 if (Val >= 0)
3606 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003607 if (i != 0)
3608 Mask <<= 2;
3609 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003610 return Mask;
3611}
3612
Nate Begemana09008b2009-10-19 02:17:23 +00003613/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3614/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3615unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3616 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3617 EVT VVT = N->getValueType(0);
3618 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3619 int Val = 0;
3620
3621 unsigned i, e;
3622 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3623 Val = SVOp->getMaskElt(i);
3624 if (Val >= 0)
3625 break;
3626 }
3627 return (Val - i) * EltSize;
3628}
3629
David Greenec38a03e2011-02-03 15:50:00 +00003630/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3631/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3632/// instructions.
3633unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3634 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3635 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3636
3637 uint64_t Index =
3638 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3639
3640 EVT VecVT = N->getOperand(0).getValueType();
3641 EVT ElVT = VecVT.getVectorElementType();
3642
3643 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3644
3645 return Index / NumElemsPerChunk;
3646}
3647
David Greeneccacdc12011-02-04 16:08:29 +00003648/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3649/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3650/// instructions.
3651unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3652 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3653 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3654
3655 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003656 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003657
3658 EVT VecVT = N->getValueType(0);
3659 EVT ElVT = VecVT.getVectorElementType();
3660
3661 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3662
3663 return Index / NumElemsPerChunk;
3664}
3665
Evan Cheng37b73872009-07-30 08:33:02 +00003666/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3667/// constant +0.0.
3668bool X86::isZeroNode(SDValue Elt) {
3669 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003670 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003671 (isa<ConstantFPSDNode>(Elt) &&
3672 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3673}
3674
Nate Begeman9008ca62009-04-27 18:41:29 +00003675/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3676/// their permute mask.
3677static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3678 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003679 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003680 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003681 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003682
Nate Begeman5a5ca152009-04-29 05:20:52 +00003683 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003684 int idx = SVOp->getMaskElt(i);
3685 if (idx < 0)
3686 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003687 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003688 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003689 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003690 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003691 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003692 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3693 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003694}
3695
Evan Cheng779ccea2007-12-07 21:30:01 +00003696/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3697/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003698static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003699 unsigned NumElems = VT.getVectorNumElements();
3700 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 int idx = Mask[i];
3702 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003703 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003704 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003705 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003706 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003707 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003708 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003709}
3710
Evan Cheng533a0aa2006-04-19 20:35:22 +00003711/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3712/// match movhlps. The lower half elements should come from upper half of
3713/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003714/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003715static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3716 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003717 return false;
3718 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003719 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003720 return false;
3721 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003722 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003723 return false;
3724 return true;
3725}
3726
Evan Cheng5ced1d82006-04-06 23:23:56 +00003727/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003728/// is promoted to a vector. It also returns the LoadSDNode by reference if
3729/// required.
3730static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003731 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3732 return false;
3733 N = N->getOperand(0).getNode();
3734 if (!ISD::isNON_EXTLoad(N))
3735 return false;
3736 if (LD)
3737 *LD = cast<LoadSDNode>(N);
3738 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003739}
3740
Evan Cheng533a0aa2006-04-19 20:35:22 +00003741/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3742/// match movlp{s|d}. The lower half elements should come from lower half of
3743/// V1 (and in order), and the upper half elements should come from the upper
3744/// half of V2 (and in order). And since V1 will become the source of the
3745/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003746static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3747 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003748 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003749 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003750 // Is V2 is a vector load, don't do this transformation. We will try to use
3751 // load folding shufps op.
3752 if (ISD::isNON_EXTLoad(V2))
3753 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003754
Nate Begeman5a5ca152009-04-29 05:20:52 +00003755 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003756
Evan Cheng533a0aa2006-04-19 20:35:22 +00003757 if (NumElems != 2 && NumElems != 4)
3758 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003759 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003760 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003761 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003762 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003763 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003764 return false;
3765 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003766}
3767
Evan Cheng39623da2006-04-20 08:58:49 +00003768/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3769/// all the same.
3770static bool isSplatVector(SDNode *N) {
3771 if (N->getOpcode() != ISD::BUILD_VECTOR)
3772 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003773
Dan Gohman475871a2008-07-27 21:46:04 +00003774 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003775 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3776 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003777 return false;
3778 return true;
3779}
3780
Evan Cheng213d2cf2007-05-17 18:45:50 +00003781/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003782/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003783/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003784static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003785 SDValue V1 = N->getOperand(0);
3786 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003787 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3788 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003789 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003790 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003791 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003792 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3793 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003794 if (Opc != ISD::BUILD_VECTOR ||
3795 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 return false;
3797 } else if (Idx >= 0) {
3798 unsigned Opc = V1.getOpcode();
3799 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3800 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003801 if (Opc != ISD::BUILD_VECTOR ||
3802 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003803 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003804 }
3805 }
3806 return true;
3807}
3808
3809/// getZeroVector - Returns a vector of specified type with all zero elements.
3810///
Owen Andersone50ed302009-08-10 22:56:29 +00003811static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003812 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003813 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003814
Dale Johannesen0488fb62010-09-30 23:57:10 +00003815 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003816 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003817 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003818 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003819 if (HasSSE2) { // SSE2
3820 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3821 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3822 } else { // SSE1
3823 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3824 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3825 }
3826 } else if (VT.getSizeInBits() == 256) { // AVX
3827 // 256-bit logic and arithmetic instructions in AVX are
3828 // all floating-point, no support for integer ops. Default
3829 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003830 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003831 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3832 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003833 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003834 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003835}
3836
Chris Lattner8a594482007-11-25 00:24:49 +00003837/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003838/// Always build ones vectors as <4 x i32> or <8 x i32> bitcasted to
3839/// their original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00003840static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003841 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003842 assert((VT.is128BitVector() || VT.is256BitVector())
3843 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003844
Owen Anderson825b72b2009-08-11 20:47:22 +00003845 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003846
Dan Gohman475871a2008-07-27 21:46:04 +00003847 SDValue Vec;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003848 if (VT.is256BitVector()) {
3849 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3850 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
3851 } else
3852 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003853 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003854}
3855
Evan Cheng39623da2006-04-20 08:58:49 +00003856/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3857/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003858static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003859 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003860 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003861
Evan Cheng39623da2006-04-20 08:58:49 +00003862 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003863 SmallVector<int, 8> MaskVec;
3864 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003865
Nate Begeman5a5ca152009-04-29 05:20:52 +00003866 for (unsigned i = 0; i != NumElems; ++i) {
3867 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003868 MaskVec[i] = NumElems;
3869 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003870 }
Evan Cheng39623da2006-04-20 08:58:49 +00003871 }
Evan Cheng39623da2006-04-20 08:58:49 +00003872 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003873 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3874 SVOp->getOperand(1), &MaskVec[0]);
3875 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003876}
3877
Evan Cheng017dcc62006-04-21 01:05:10 +00003878/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3879/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003880static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003881 SDValue V2) {
3882 unsigned NumElems = VT.getVectorNumElements();
3883 SmallVector<int, 8> Mask;
3884 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003885 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003886 Mask.push_back(i);
3887 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003888}
3889
Nate Begeman9008ca62009-04-27 18:41:29 +00003890/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003891static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 SDValue V2) {
3893 unsigned NumElems = VT.getVectorNumElements();
3894 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003895 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003896 Mask.push_back(i);
3897 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003898 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003899 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003900}
3901
Nate Begeman9008ca62009-04-27 18:41:29 +00003902/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003903static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 SDValue V2) {
3905 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003906 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003907 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003908 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 Mask.push_back(i + Half);
3910 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003911 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003912 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003913}
3914
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003915/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3916static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003917 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003918 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003919 DebugLoc dl = SV->getDebugLoc();
3920 SDValue V1 = SV->getOperand(0);
3921 int NumElems = VT.getVectorNumElements();
3922 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003923
Nate Begeman9008ca62009-04-27 18:41:29 +00003924 // unpack elements to the correct location
3925 while (NumElems > 4) {
3926 if (EltNo < NumElems/2) {
3927 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3928 } else {
3929 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3930 EltNo -= NumElems/2;
3931 }
3932 NumElems >>= 1;
3933 }
Eric Christopherfd179292009-08-27 18:07:15 +00003934
Nate Begeman9008ca62009-04-27 18:41:29 +00003935 // Perform the splat.
3936 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003937 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003939 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003940}
3941
Evan Chengba05f722006-04-21 23:03:30 +00003942/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003943/// vector of zero or undef vector. This produces a shuffle where the low
3944/// element of V2 is swizzled into the zero/undef vector, landing at element
3945/// 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 +00003946static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003947 bool isZero, bool HasSSE2,
3948 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003949 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003950 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003951 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3952 unsigned NumElems = VT.getVectorNumElements();
3953 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003954 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003955 // If this is the insertion idx, put the low elt of V2 here.
3956 MaskVec.push_back(i == Idx ? NumElems : i);
3957 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003958}
3959
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003960/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3961/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003962static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3963 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003964 if (Depth == 6)
3965 return SDValue(); // Limit search depth.
3966
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003967 SDValue V = SDValue(N, 0);
3968 EVT VT = V.getValueType();
3969 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003970
3971 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3972 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3973 Index = SV->getMaskElt(Index);
3974
3975 if (Index < 0)
3976 return DAG.getUNDEF(VT.getVectorElementType());
3977
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003978 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003979 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003980 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003981 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003982
3983 // Recurse into target specific vector shuffles to find scalars.
3984 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003985 int NumElems = VT.getVectorNumElements();
3986 SmallVector<unsigned, 16> ShuffleMask;
3987 SDValue ImmN;
3988
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003989 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003990 case X86ISD::SHUFPS:
3991 case X86ISD::SHUFPD:
3992 ImmN = N->getOperand(N->getNumOperands()-1);
3993 DecodeSHUFPSMask(NumElems,
3994 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3995 ShuffleMask);
3996 break;
3997 case X86ISD::PUNPCKHBW:
3998 case X86ISD::PUNPCKHWD:
3999 case X86ISD::PUNPCKHDQ:
4000 case X86ISD::PUNPCKHQDQ:
4001 DecodePUNPCKHMask(NumElems, ShuffleMask);
4002 break;
4003 case X86ISD::UNPCKHPS:
4004 case X86ISD::UNPCKHPD:
4005 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4006 break;
4007 case X86ISD::PUNPCKLBW:
4008 case X86ISD::PUNPCKLWD:
4009 case X86ISD::PUNPCKLDQ:
4010 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004011 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004012 break;
4013 case X86ISD::UNPCKLPS:
4014 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004015 case X86ISD::VUNPCKLPS:
4016 case X86ISD::VUNPCKLPD:
4017 case X86ISD::VUNPCKLPSY:
4018 case X86ISD::VUNPCKLPDY:
4019 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004020 break;
4021 case X86ISD::MOVHLPS:
4022 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4023 break;
4024 case X86ISD::MOVLHPS:
4025 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4026 break;
4027 case X86ISD::PSHUFD:
4028 ImmN = N->getOperand(N->getNumOperands()-1);
4029 DecodePSHUFMask(NumElems,
4030 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4031 ShuffleMask);
4032 break;
4033 case X86ISD::PSHUFHW:
4034 ImmN = N->getOperand(N->getNumOperands()-1);
4035 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4036 ShuffleMask);
4037 break;
4038 case X86ISD::PSHUFLW:
4039 ImmN = N->getOperand(N->getNumOperands()-1);
4040 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4041 ShuffleMask);
4042 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004043 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004044 case X86ISD::MOVSD: {
4045 // The index 0 always comes from the first element of the second source,
4046 // this is why MOVSS and MOVSD are used in the first place. The other
4047 // elements come from the other positions of the first source vector.
4048 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004049 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4050 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004051 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004052 default:
4053 assert("not implemented for target shuffle node");
4054 return SDValue();
4055 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004056
4057 Index = ShuffleMask[Index];
4058 if (Index < 0)
4059 return DAG.getUNDEF(VT.getVectorElementType());
4060
4061 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4062 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4063 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004064 }
4065
4066 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004067 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004068 V = V.getOperand(0);
4069 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004070 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004071
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004072 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004073 return SDValue();
4074 }
4075
4076 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4077 return (Index == 0) ? V.getOperand(0)
4078 : DAG.getUNDEF(VT.getVectorElementType());
4079
4080 if (V.getOpcode() == ISD::BUILD_VECTOR)
4081 return V.getOperand(Index);
4082
4083 return SDValue();
4084}
4085
4086/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4087/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004088/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004089static
4090unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4091 bool ZerosFromLeft, SelectionDAG &DAG) {
4092 int i = 0;
4093
4094 while (i < NumElems) {
4095 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004096 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004097 if (!(Elt.getNode() &&
4098 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4099 break;
4100 ++i;
4101 }
4102
4103 return i;
4104}
4105
4106/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4107/// MaskE correspond consecutively to elements from one of the vector operands,
4108/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4109static
4110bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4111 int OpIdx, int NumElems, unsigned &OpNum) {
4112 bool SeenV1 = false;
4113 bool SeenV2 = false;
4114
4115 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4116 int Idx = SVOp->getMaskElt(i);
4117 // Ignore undef indicies
4118 if (Idx < 0)
4119 continue;
4120
4121 if (Idx < NumElems)
4122 SeenV1 = true;
4123 else
4124 SeenV2 = true;
4125
4126 // Only accept consecutive elements from the same vector
4127 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4128 return false;
4129 }
4130
4131 OpNum = SeenV1 ? 0 : 1;
4132 return true;
4133}
4134
4135/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4136/// logical left shift of a vector.
4137static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4138 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4139 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4140 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4141 false /* check zeros from right */, DAG);
4142 unsigned OpSrc;
4143
4144 if (!NumZeros)
4145 return false;
4146
4147 // Considering the elements in the mask that are not consecutive zeros,
4148 // check if they consecutively come from only one of the source vectors.
4149 //
4150 // V1 = {X, A, B, C} 0
4151 // \ \ \ /
4152 // vector_shuffle V1, V2 <1, 2, 3, X>
4153 //
4154 if (!isShuffleMaskConsecutive(SVOp,
4155 0, // Mask Start Index
4156 NumElems-NumZeros-1, // Mask End Index
4157 NumZeros, // Where to start looking in the src vector
4158 NumElems, // Number of elements in vector
4159 OpSrc)) // Which source operand ?
4160 return false;
4161
4162 isLeft = false;
4163 ShAmt = NumZeros;
4164 ShVal = SVOp->getOperand(OpSrc);
4165 return true;
4166}
4167
4168/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4169/// logical left shift of a vector.
4170static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4171 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4172 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4173 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4174 true /* check zeros from left */, DAG);
4175 unsigned OpSrc;
4176
4177 if (!NumZeros)
4178 return false;
4179
4180 // Considering the elements in the mask that are not consecutive zeros,
4181 // check if they consecutively come from only one of the source vectors.
4182 //
4183 // 0 { A, B, X, X } = V2
4184 // / \ / /
4185 // vector_shuffle V1, V2 <X, X, 4, 5>
4186 //
4187 if (!isShuffleMaskConsecutive(SVOp,
4188 NumZeros, // Mask Start Index
4189 NumElems-1, // Mask End Index
4190 0, // Where to start looking in the src vector
4191 NumElems, // Number of elements in vector
4192 OpSrc)) // Which source operand ?
4193 return false;
4194
4195 isLeft = true;
4196 ShAmt = NumZeros;
4197 ShVal = SVOp->getOperand(OpSrc);
4198 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004199}
4200
4201/// isVectorShift - Returns true if the shuffle can be implemented as a
4202/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004203static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004204 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004205 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4206 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4207 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004208
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004209 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004210}
4211
Evan Chengc78d3b42006-04-24 18:01:45 +00004212/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4213///
Dan Gohman475871a2008-07-27 21:46:04 +00004214static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004215 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004216 SelectionDAG &DAG,
4217 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004218 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004219 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004220
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004221 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004222 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004223 bool First = true;
4224 for (unsigned i = 0; i < 16; ++i) {
4225 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4226 if (ThisIsNonZero && First) {
4227 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004229 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004231 First = false;
4232 }
4233
4234 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004235 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004236 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4237 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004238 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004240 }
4241 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4243 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4244 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004245 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004246 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004247 } else
4248 ThisElt = LastElt;
4249
Gabor Greifba36cb52008-08-28 21:40:38 +00004250 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004251 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004252 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004253 }
4254 }
4255
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004256 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004257}
4258
Bill Wendlinga348c562007-03-22 18:42:45 +00004259/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004260///
Dan Gohman475871a2008-07-27 21:46:04 +00004261static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004262 unsigned NumNonZero, unsigned NumZero,
4263 SelectionDAG &DAG,
4264 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004265 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004266 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004267
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004268 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004269 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004270 bool First = true;
4271 for (unsigned i = 0; i < 8; ++i) {
4272 bool isNonZero = (NonZeros & (1 << i)) != 0;
4273 if (isNonZero) {
4274 if (First) {
4275 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004276 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004277 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004278 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004279 First = false;
4280 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004281 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004282 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004283 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004284 }
4285 }
4286
4287 return V;
4288}
4289
Evan Chengf26ffe92008-05-29 08:22:04 +00004290/// getVShift - Return a vector logical shift node.
4291///
Owen Andersone50ed302009-08-10 22:56:29 +00004292static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004293 unsigned NumBits, SelectionDAG &DAG,
4294 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004295 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004296 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004297 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4298 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004299 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004300 DAG.getConstant(NumBits,
4301 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004302}
4303
Dan Gohman475871a2008-07-27 21:46:04 +00004304SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004305X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004306 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004307
Evan Chengc3630942009-12-09 21:00:30 +00004308 // Check if the scalar load can be widened into a vector load. And if
4309 // the address is "base + cst" see if the cst can be "absorbed" into
4310 // the shuffle mask.
4311 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4312 SDValue Ptr = LD->getBasePtr();
4313 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4314 return SDValue();
4315 EVT PVT = LD->getValueType(0);
4316 if (PVT != MVT::i32 && PVT != MVT::f32)
4317 return SDValue();
4318
4319 int FI = -1;
4320 int64_t Offset = 0;
4321 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4322 FI = FINode->getIndex();
4323 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004324 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004325 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4326 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4327 Offset = Ptr.getConstantOperandVal(1);
4328 Ptr = Ptr.getOperand(0);
4329 } else {
4330 return SDValue();
4331 }
4332
4333 SDValue Chain = LD->getChain();
4334 // Make sure the stack object alignment is at least 16.
4335 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4336 if (DAG.InferPtrAlignment(Ptr) < 16) {
4337 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004338 // Can't change the alignment. FIXME: It's possible to compute
4339 // the exact stack offset and reference FI + adjust offset instead.
4340 // If someone *really* cares about this. That's the way to implement it.
4341 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004342 } else {
4343 MFI->setObjectAlignment(FI, 16);
4344 }
4345 }
4346
4347 // (Offset % 16) must be multiple of 4. Then address is then
4348 // Ptr + (Offset & ~15).
4349 if (Offset < 0)
4350 return SDValue();
4351 if ((Offset % 16) & 3)
4352 return SDValue();
4353 int64_t StartOffset = Offset & ~15;
4354 if (StartOffset)
4355 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4356 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4357
4358 int EltNo = (Offset - StartOffset) >> 2;
4359 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4360 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004361 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4362 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004363 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004364 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004365 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4366 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004367 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004368 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004369 }
4370
4371 return SDValue();
4372}
4373
Michael J. Spencerec38de22010-10-10 22:04:20 +00004374/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4375/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004376/// load which has the same value as a build_vector whose operands are 'elts'.
4377///
4378/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004379///
Nate Begeman1449f292010-03-24 22:19:06 +00004380/// FIXME: we'd also like to handle the case where the last elements are zero
4381/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4382/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004383static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004384 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004385 EVT EltVT = VT.getVectorElementType();
4386 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004387
Nate Begemanfdea31a2010-03-24 20:49:50 +00004388 LoadSDNode *LDBase = NULL;
4389 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004390
Nate Begeman1449f292010-03-24 22:19:06 +00004391 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004392 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004393 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004394 for (unsigned i = 0; i < NumElems; ++i) {
4395 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004396
Nate Begemanfdea31a2010-03-24 20:49:50 +00004397 if (!Elt.getNode() ||
4398 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4399 return SDValue();
4400 if (!LDBase) {
4401 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4402 return SDValue();
4403 LDBase = cast<LoadSDNode>(Elt.getNode());
4404 LastLoadedElt = i;
4405 continue;
4406 }
4407 if (Elt.getOpcode() == ISD::UNDEF)
4408 continue;
4409
4410 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4411 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4412 return SDValue();
4413 LastLoadedElt = i;
4414 }
Nate Begeman1449f292010-03-24 22:19:06 +00004415
4416 // If we have found an entire vector of loads and undefs, then return a large
4417 // load of the entire vector width starting at the base pointer. If we found
4418 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004419 if (LastLoadedElt == NumElems - 1) {
4420 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004421 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004422 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004423 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004424 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004425 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004426 LDBase->isVolatile(), LDBase->isNonTemporal(),
4427 LDBase->getAlignment());
4428 } else if (NumElems == 4 && LastLoadedElt == 1) {
4429 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4430 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004431 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4432 Ops, 2, MVT::i32,
4433 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004434 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004435 }
4436 return SDValue();
4437}
4438
Evan Chengc3630942009-12-09 21:00:30 +00004439SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004440X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004441 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004442
David Greenef125a292011-02-08 19:04:41 +00004443 EVT VT = Op.getValueType();
4444 EVT ExtVT = VT.getVectorElementType();
4445
4446 unsigned NumElems = Op.getNumOperands();
4447
4448 // For AVX-length vectors, build the individual 128-bit pieces and
4449 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004450 if (VT.getSizeInBits() > 256 &&
4451 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004452 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4453 SmallVector<SDValue, 8> V;
4454 V.resize(NumElems);
4455 for (unsigned i = 0; i < NumElems; ++i) {
4456 V[i] = Op.getOperand(i);
4457 }
Owen Anderson95771af2011-02-25 21:41:48 +00004458
David Greenef125a292011-02-08 19:04:41 +00004459 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4460
4461 // Build the lower subvector.
4462 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4463 // Build the upper subvector.
4464 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4465 NumElems/2);
4466
4467 return ConcatVectors(Lower, Upper, DAG);
4468 }
4469
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004470 // All zero's:
4471 // - pxor (SSE2), xorps (SSE1), vpxor (128 AVX), xorp[s|d] (256 AVX)
4472 // All one's:
4473 // - pcmpeqd (SSE2 and 128 AVX), fallback to constant pools (256 AVX)
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004474 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004475 ISD::isBuildVectorAllOnes(Op.getNode())) {
4476 // Canonicalize this to <4 x i32> or <8 x 32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004477 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4478 // eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004479 if (Op.getValueType() == MVT::v4i32 ||
4480 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004481 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004482
Gabor Greifba36cb52008-08-28 21:40:38 +00004483 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004484 return getOnesVector(Op.getValueType(), DAG, dl);
4485 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004486 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004487
Owen Andersone50ed302009-08-10 22:56:29 +00004488 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004489
Evan Cheng0db9fe62006-04-25 20:13:52 +00004490 unsigned NumZero = 0;
4491 unsigned NumNonZero = 0;
4492 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004493 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004494 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004495 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004496 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004497 if (Elt.getOpcode() == ISD::UNDEF)
4498 continue;
4499 Values.insert(Elt);
4500 if (Elt.getOpcode() != ISD::Constant &&
4501 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004502 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004503 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004504 NumZero++;
4505 else {
4506 NonZeros |= (1 << i);
4507 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004508 }
4509 }
4510
Chris Lattner97a2a562010-08-26 05:24:29 +00004511 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4512 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004513 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004514
Chris Lattner67f453a2008-03-09 05:42:06 +00004515 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004516 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004517 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004518 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004519
Chris Lattner62098042008-03-09 01:05:04 +00004520 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4521 // the value are obviously zero, truncate the value to i32 and do the
4522 // insertion that way. Only do this if the value is non-constant or if the
4523 // value is a constant being inserted into element 0. It is cheaper to do
4524 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004525 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004526 (!IsAllConstants || Idx == 0)) {
4527 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004528 // Handle SSE only.
4529 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4530 EVT VecVT = MVT::v4i32;
4531 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004532
Chris Lattner62098042008-03-09 01:05:04 +00004533 // Truncate the value (which may itself be a constant) to i32, and
4534 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004535 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004536 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004537 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4538 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004539
Chris Lattner62098042008-03-09 01:05:04 +00004540 // Now we have our 32-bit value zero extended in the low element of
4541 // a vector. If Idx != 0, swizzle it into place.
4542 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004543 SmallVector<int, 4> Mask;
4544 Mask.push_back(Idx);
4545 for (unsigned i = 1; i != VecElts; ++i)
4546 Mask.push_back(i);
4547 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004548 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004549 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004550 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004551 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004552 }
4553 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004554
Chris Lattner19f79692008-03-08 22:59:52 +00004555 // If we have a constant or non-constant insertion into the low element of
4556 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4557 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004558 // depending on what the source datatype is.
4559 if (Idx == 0) {
4560 if (NumZero == 0) {
4561 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004562 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4563 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004564 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4565 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4566 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4567 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004568 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4569 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004570 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4571 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004572 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4573 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4574 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004575 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004576 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004577 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004578
4579 // Is it a vector logical left shift?
4580 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004581 X86::isZeroNode(Op.getOperand(0)) &&
4582 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004583 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004584 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004585 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004586 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004587 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004588 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004589
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004590 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004591 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592
Chris Lattner19f79692008-03-08 22:59:52 +00004593 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4594 // is a non-constant being inserted into an element other than the low one,
4595 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4596 // movd/movss) to move this into the low element, then shuffle it into
4597 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004599 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004600
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004602 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4603 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004604 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004606 MaskVec.push_back(i == Idx ? 0 : 1);
4607 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004608 }
4609 }
4610
Chris Lattner67f453a2008-03-09 05:42:06 +00004611 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004612 if (Values.size() == 1) {
4613 if (EVTBits == 32) {
4614 // Instead of a shuffle like this:
4615 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4616 // Check if it's possible to issue this instead.
4617 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4618 unsigned Idx = CountTrailingZeros_32(NonZeros);
4619 SDValue Item = Op.getOperand(Idx);
4620 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4621 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4622 }
Dan Gohman475871a2008-07-27 21:46:04 +00004623 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004624 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004625
Dan Gohmana3941172007-07-24 22:55:08 +00004626 // A vector full of immediates; various special cases are already
4627 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004628 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004629 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004630
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004631 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004632 if (EVTBits == 64) {
4633 if (NumNonZero == 1) {
4634 // One half is zero or undef.
4635 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004636 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004637 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004638 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4639 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004640 }
Dan Gohman475871a2008-07-27 21:46:04 +00004641 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004642 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004643
4644 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004645 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004646 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004647 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004648 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004649 }
4650
Bill Wendling826f36f2007-03-28 00:57:11 +00004651 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004652 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004653 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004654 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004655 }
4656
4657 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004658 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004659 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004660 if (NumElems == 4 && NumZero > 0) {
4661 for (unsigned i = 0; i < 4; ++i) {
4662 bool isZero = !(NonZeros & (1 << i));
4663 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004664 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004665 else
Dale Johannesenace16102009-02-03 19:33:06 +00004666 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004667 }
4668
4669 for (unsigned i = 0; i < 2; ++i) {
4670 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4671 default: break;
4672 case 0:
4673 V[i] = V[i*2]; // Must be a zero vector.
4674 break;
4675 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004676 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004677 break;
4678 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004680 break;
4681 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004682 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004683 break;
4684 }
4685 }
4686
Nate Begeman9008ca62009-04-27 18:41:29 +00004687 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004688 bool Reverse = (NonZeros & 0x3) == 2;
4689 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004690 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004691 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4692 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004693 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4694 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004695 }
4696
Nate Begemanfdea31a2010-03-24 20:49:50 +00004697 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4698 // Check for a build vector of consecutive loads.
4699 for (unsigned i = 0; i < NumElems; ++i)
4700 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004701
Nate Begemanfdea31a2010-03-24 20:49:50 +00004702 // Check for elements which are consecutive loads.
4703 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4704 if (LD.getNode())
4705 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004706
4707 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004708 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004709 SDValue Result;
4710 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4711 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4712 else
4713 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004714
Chris Lattner24faf612010-08-28 17:59:08 +00004715 for (unsigned i = 1; i < NumElems; ++i) {
4716 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4717 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004718 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004719 }
4720 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004721 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004722
Chris Lattner6e80e442010-08-28 17:15:43 +00004723 // Otherwise, expand into a number of unpckl*, start by extending each of
4724 // our (non-undef) elements to the full vector width with the element in the
4725 // bottom slot of the vector (which generates no code for SSE).
4726 for (unsigned i = 0; i < NumElems; ++i) {
4727 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4728 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4729 else
4730 V[i] = DAG.getUNDEF(VT);
4731 }
4732
4733 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004734 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4735 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4736 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004737 unsigned EltStride = NumElems >> 1;
4738 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004739 for (unsigned i = 0; i < EltStride; ++i) {
4740 // If V[i+EltStride] is undef and this is the first round of mixing,
4741 // then it is safe to just drop this shuffle: V[i] is already in the
4742 // right place, the one element (since it's the first round) being
4743 // inserted as undef can be dropped. This isn't safe for successive
4744 // rounds because they will permute elements within both vectors.
4745 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4746 EltStride == NumElems/2)
4747 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004748
Chris Lattner6e80e442010-08-28 17:15:43 +00004749 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004750 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004751 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004752 }
4753 return V[0];
4754 }
Dan Gohman475871a2008-07-27 21:46:04 +00004755 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004756}
4757
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004758SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004759X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004760 // We support concatenate two MMX registers and place them in a MMX
4761 // register. This is better than doing a stack convert.
4762 DebugLoc dl = Op.getDebugLoc();
4763 EVT ResVT = Op.getValueType();
4764 assert(Op.getNumOperands() == 2);
4765 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4766 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4767 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004768 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004769 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4770 InVec = Op.getOperand(1);
4771 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4772 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004773 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004774 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4775 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4776 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004777 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004778 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4779 Mask[0] = 0; Mask[1] = 2;
4780 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4781 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004782 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004783}
4784
Nate Begemanb9a47b82009-02-23 08:49:38 +00004785// v8i16 shuffles - Prefer shuffles in the following order:
4786// 1. [all] pshuflw, pshufhw, optional move
4787// 2. [ssse3] 1 x pshufb
4788// 3. [ssse3] 2 x pshufb + 1 x por
4789// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004790SDValue
4791X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4792 SelectionDAG &DAG) const {
4793 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004794 SDValue V1 = SVOp->getOperand(0);
4795 SDValue V2 = SVOp->getOperand(1);
4796 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004797 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004798
Nate Begemanb9a47b82009-02-23 08:49:38 +00004799 // Determine if more than 1 of the words in each of the low and high quadwords
4800 // of the result come from the same quadword of one of the two inputs. Undef
4801 // mask values count as coming from any quadword, for better codegen.
4802 SmallVector<unsigned, 4> LoQuad(4);
4803 SmallVector<unsigned, 4> HiQuad(4);
4804 BitVector InputQuads(4);
4805 for (unsigned i = 0; i < 8; ++i) {
4806 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004807 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 MaskVals.push_back(EltIdx);
4809 if (EltIdx < 0) {
4810 ++Quad[0];
4811 ++Quad[1];
4812 ++Quad[2];
4813 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004814 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004815 }
4816 ++Quad[EltIdx / 4];
4817 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004818 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004819
Nate Begemanb9a47b82009-02-23 08:49:38 +00004820 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004821 unsigned MaxQuad = 1;
4822 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004823 if (LoQuad[i] > MaxQuad) {
4824 BestLoQuad = i;
4825 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004826 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004827 }
4828
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004830 MaxQuad = 1;
4831 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 if (HiQuad[i] > MaxQuad) {
4833 BestHiQuad = i;
4834 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004835 }
4836 }
4837
Nate Begemanb9a47b82009-02-23 08:49:38 +00004838 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004839 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004840 // single pshufb instruction is necessary. If There are more than 2 input
4841 // quads, disable the next transformation since it does not help SSSE3.
4842 bool V1Used = InputQuads[0] || InputQuads[1];
4843 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004844 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004845 if (InputQuads.count() == 2 && V1Used && V2Used) {
4846 BestLoQuad = InputQuads.find_first();
4847 BestHiQuad = InputQuads.find_next(BestLoQuad);
4848 }
4849 if (InputQuads.count() > 2) {
4850 BestLoQuad = -1;
4851 BestHiQuad = -1;
4852 }
4853 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004854
Nate Begemanb9a47b82009-02-23 08:49:38 +00004855 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4856 // the shuffle mask. If a quad is scored as -1, that means that it contains
4857 // words from all 4 input quadwords.
4858 SDValue NewV;
4859 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004860 SmallVector<int, 8> MaskV;
4861 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4862 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004863 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004864 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4865 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4866 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004867
Nate Begemanb9a47b82009-02-23 08:49:38 +00004868 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4869 // source words for the shuffle, to aid later transformations.
4870 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004871 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004872 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004873 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004874 if (idx != (int)i)
4875 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004876 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004877 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004878 AllWordsInNewV = false;
4879 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004880 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004881
Nate Begemanb9a47b82009-02-23 08:49:38 +00004882 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4883 if (AllWordsInNewV) {
4884 for (int i = 0; i != 8; ++i) {
4885 int idx = MaskVals[i];
4886 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004887 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004888 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004889 if ((idx != i) && idx < 4)
4890 pshufhw = false;
4891 if ((idx != i) && idx > 3)
4892 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004893 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004894 V1 = NewV;
4895 V2Used = false;
4896 BestLoQuad = 0;
4897 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004898 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004899
Nate Begemanb9a47b82009-02-23 08:49:38 +00004900 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4901 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004902 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004903 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4904 unsigned TargetMask = 0;
4905 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004906 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004907 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4908 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4909 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004910 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004911 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004912 }
Eric Christopherfd179292009-08-27 18:07:15 +00004913
Nate Begemanb9a47b82009-02-23 08:49:38 +00004914 // If we have SSSE3, and all words of the result are from 1 input vector,
4915 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4916 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004917 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004918 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004919
Nate Begemanb9a47b82009-02-23 08:49:38 +00004920 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004921 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004922 // mask, and elements that come from V1 in the V2 mask, so that the two
4923 // results can be OR'd together.
4924 bool TwoInputs = V1Used && V2Used;
4925 for (unsigned i = 0; i != 8; ++i) {
4926 int EltIdx = MaskVals[i] * 2;
4927 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004928 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4929 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004930 continue;
4931 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004932 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4933 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004934 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004935 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004936 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004937 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004938 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004939 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004940 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004941
Nate Begemanb9a47b82009-02-23 08:49:38 +00004942 // Calculate the shuffle mask for the second input, shuffle it, and
4943 // OR it with the first shuffled input.
4944 pshufbMask.clear();
4945 for (unsigned i = 0; i != 8; ++i) {
4946 int EltIdx = MaskVals[i] * 2;
4947 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004948 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4949 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004950 continue;
4951 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004952 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4953 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004954 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004955 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004956 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004957 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004958 MVT::v16i8, &pshufbMask[0], 16));
4959 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004960 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004961 }
4962
4963 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4964 // and update MaskVals with new element order.
4965 BitVector InOrder(8);
4966 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004967 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004968 for (int i = 0; i != 4; ++i) {
4969 int idx = MaskVals[i];
4970 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004971 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004972 InOrder.set(i);
4973 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004974 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004975 InOrder.set(i);
4976 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004977 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004978 }
4979 }
4980 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004981 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004982 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004983 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004984
4985 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4986 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4987 NewV.getOperand(0),
4988 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4989 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004990 }
Eric Christopherfd179292009-08-27 18:07:15 +00004991
Nate Begemanb9a47b82009-02-23 08:49:38 +00004992 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4993 // and update MaskVals with the new element order.
4994 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004995 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004996 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004997 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004998 for (unsigned i = 4; i != 8; ++i) {
4999 int idx = MaskVals[i];
5000 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005001 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005002 InOrder.set(i);
5003 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005004 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005005 InOrder.set(i);
5006 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005007 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005008 }
5009 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005010 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005011 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005012
5013 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5014 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5015 NewV.getOperand(0),
5016 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5017 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005018 }
Eric Christopherfd179292009-08-27 18:07:15 +00005019
Nate Begemanb9a47b82009-02-23 08:49:38 +00005020 // In case BestHi & BestLo were both -1, which means each quadword has a word
5021 // from each of the four input quadwords, calculate the InOrder bitvector now
5022 // before falling through to the insert/extract cleanup.
5023 if (BestLoQuad == -1 && BestHiQuad == -1) {
5024 NewV = V1;
5025 for (int i = 0; i != 8; ++i)
5026 if (MaskVals[i] < 0 || MaskVals[i] == i)
5027 InOrder.set(i);
5028 }
Eric Christopherfd179292009-08-27 18:07:15 +00005029
Nate Begemanb9a47b82009-02-23 08:49:38 +00005030 // The other elements are put in the right place using pextrw and pinsrw.
5031 for (unsigned i = 0; i != 8; ++i) {
5032 if (InOrder[i])
5033 continue;
5034 int EltIdx = MaskVals[i];
5035 if (EltIdx < 0)
5036 continue;
5037 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005038 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005039 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005040 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005041 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005042 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005043 DAG.getIntPtrConstant(i));
5044 }
5045 return NewV;
5046}
5047
5048// v16i8 shuffles - Prefer shuffles in the following order:
5049// 1. [ssse3] 1 x pshufb
5050// 2. [ssse3] 2 x pshufb + 1 x por
5051// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5052static
Nate Begeman9008ca62009-04-27 18:41:29 +00005053SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005054 SelectionDAG &DAG,
5055 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005056 SDValue V1 = SVOp->getOperand(0);
5057 SDValue V2 = SVOp->getOperand(1);
5058 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005059 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005060 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005061
Nate Begemanb9a47b82009-02-23 08:49:38 +00005062 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005063 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005064 // present, fall back to case 3.
5065 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5066 bool V1Only = true;
5067 bool V2Only = true;
5068 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005069 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005070 if (EltIdx < 0)
5071 continue;
5072 if (EltIdx < 16)
5073 V2Only = false;
5074 else
5075 V1Only = false;
5076 }
Eric Christopherfd179292009-08-27 18:07:15 +00005077
Nate Begemanb9a47b82009-02-23 08:49:38 +00005078 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5079 if (TLI.getSubtarget()->hasSSSE3()) {
5080 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005081
Nate Begemanb9a47b82009-02-23 08:49:38 +00005082 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005083 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005084 //
5085 // Otherwise, we have elements from both input vectors, and must zero out
5086 // elements that come from V2 in the first mask, and V1 in the second mask
5087 // so that we can OR them together.
5088 bool TwoInputs = !(V1Only || V2Only);
5089 for (unsigned i = 0; i != 16; ++i) {
5090 int EltIdx = MaskVals[i];
5091 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005092 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005093 continue;
5094 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005095 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005096 }
5097 // If all the elements are from V2, assign it to V1 and return after
5098 // building the first pshufb.
5099 if (V2Only)
5100 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005101 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005102 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005103 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005104 if (!TwoInputs)
5105 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005106
Nate Begemanb9a47b82009-02-23 08:49:38 +00005107 // Calculate the shuffle mask for the second input, shuffle it, and
5108 // OR it with the first shuffled input.
5109 pshufbMask.clear();
5110 for (unsigned i = 0; i != 16; ++i) {
5111 int EltIdx = MaskVals[i];
5112 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005113 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005114 continue;
5115 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005116 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005117 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005118 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005119 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005120 MVT::v16i8, &pshufbMask[0], 16));
5121 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005122 }
Eric Christopherfd179292009-08-27 18:07:15 +00005123
Nate Begemanb9a47b82009-02-23 08:49:38 +00005124 // No SSSE3 - Calculate in place words and then fix all out of place words
5125 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5126 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005127 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5128 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005129 SDValue NewV = V2Only ? V2 : V1;
5130 for (int i = 0; i != 8; ++i) {
5131 int Elt0 = MaskVals[i*2];
5132 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005133
Nate Begemanb9a47b82009-02-23 08:49:38 +00005134 // This word of the result is all undef, skip it.
5135 if (Elt0 < 0 && Elt1 < 0)
5136 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005137
Nate Begemanb9a47b82009-02-23 08:49:38 +00005138 // This word of the result is already in the correct place, skip it.
5139 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5140 continue;
5141 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5142 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005143
Nate Begemanb9a47b82009-02-23 08:49:38 +00005144 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5145 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5146 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005147
5148 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5149 // using a single extract together, load it and store it.
5150 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005151 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005152 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005153 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005154 DAG.getIntPtrConstant(i));
5155 continue;
5156 }
5157
Nate Begemanb9a47b82009-02-23 08:49:38 +00005158 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005159 // source byte is not also odd, shift the extracted word left 8 bits
5160 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005161 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005162 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005163 DAG.getIntPtrConstant(Elt1 / 2));
5164 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005165 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005166 DAG.getConstant(8,
5167 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005168 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005169 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5170 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005171 }
5172 // If Elt0 is defined, extract it from the appropriate source. If the
5173 // source byte is not also even, shift the extracted word right 8 bits. If
5174 // Elt1 was also defined, OR the extracted values together before
5175 // inserting them in the result.
5176 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005177 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005178 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5179 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005180 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005181 DAG.getConstant(8,
5182 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005183 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005184 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5185 DAG.getConstant(0x00FF, MVT::i16));
5186 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005187 : InsElt0;
5188 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005189 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005190 DAG.getIntPtrConstant(i));
5191 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005192 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005193}
5194
Evan Cheng7a831ce2007-12-15 03:00:47 +00005195/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005196/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005197/// done when every pair / quad of shuffle mask elements point to elements in
5198/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005199/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005200static
Nate Begeman9008ca62009-04-27 18:41:29 +00005201SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005202 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005203 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005204 SDValue V1 = SVOp->getOperand(0);
5205 SDValue V2 = SVOp->getOperand(1);
5206 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005207 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005208 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005209 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005210 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005211 case MVT::v4f32: NewVT = MVT::v2f64; break;
5212 case MVT::v4i32: NewVT = MVT::v2i64; break;
5213 case MVT::v8i16: NewVT = MVT::v4i32; break;
5214 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005215 }
5216
Nate Begeman9008ca62009-04-27 18:41:29 +00005217 int Scale = NumElems / NewWidth;
5218 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005219 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005220 int StartIdx = -1;
5221 for (int j = 0; j < Scale; ++j) {
5222 int EltIdx = SVOp->getMaskElt(i+j);
5223 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005224 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005225 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005226 StartIdx = EltIdx - (EltIdx % Scale);
5227 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005228 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005229 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005230 if (StartIdx == -1)
5231 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005232 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005233 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005234 }
5235
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005236 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5237 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005238 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005239}
5240
Evan Chengd880b972008-05-09 21:53:03 +00005241/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005242///
Owen Andersone50ed302009-08-10 22:56:29 +00005243static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005244 SDValue SrcOp, SelectionDAG &DAG,
5245 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005246 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005247 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005248 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005249 LD = dyn_cast<LoadSDNode>(SrcOp);
5250 if (!LD) {
5251 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5252 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005253 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005254 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005255 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005256 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005257 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005258 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005259 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005260 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005261 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5262 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5263 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005264 SrcOp.getOperand(0)
5265 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005266 }
5267 }
5268 }
5269
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005270 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005271 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005272 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005273 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005274}
5275
Evan Chengace3c172008-07-22 21:13:36 +00005276/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5277/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005278static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005279LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5280 SDValue V1 = SVOp->getOperand(0);
5281 SDValue V2 = SVOp->getOperand(1);
5282 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005283 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005284
Evan Chengace3c172008-07-22 21:13:36 +00005285 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005286 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005287 SmallVector<int, 8> Mask1(4U, -1);
5288 SmallVector<int, 8> PermMask;
5289 SVOp->getMask(PermMask);
5290
Evan Chengace3c172008-07-22 21:13:36 +00005291 unsigned NumHi = 0;
5292 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005293 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005294 int Idx = PermMask[i];
5295 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005296 Locs[i] = std::make_pair(-1, -1);
5297 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5299 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005300 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005301 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005302 NumLo++;
5303 } else {
5304 Locs[i] = std::make_pair(1, NumHi);
5305 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005306 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005307 NumHi++;
5308 }
5309 }
5310 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005311
Evan Chengace3c172008-07-22 21:13:36 +00005312 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005313 // If no more than two elements come from either vector. This can be
5314 // implemented with two shuffles. First shuffle gather the elements.
5315 // The second shuffle, which takes the first shuffle as both of its
5316 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005317 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005318
Nate Begeman9008ca62009-04-27 18:41:29 +00005319 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005320
Evan Chengace3c172008-07-22 21:13:36 +00005321 for (unsigned i = 0; i != 4; ++i) {
5322 if (Locs[i].first == -1)
5323 continue;
5324 else {
5325 unsigned Idx = (i < 2) ? 0 : 4;
5326 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005327 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005328 }
5329 }
5330
Nate Begeman9008ca62009-04-27 18:41:29 +00005331 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005332 } else if (NumLo == 3 || NumHi == 3) {
5333 // Otherwise, we must have three elements from one vector, call it X, and
5334 // one element from the other, call it Y. First, use a shufps to build an
5335 // intermediate vector with the one element from Y and the element from X
5336 // that will be in the same half in the final destination (the indexes don't
5337 // matter). Then, use a shufps to build the final vector, taking the half
5338 // containing the element from Y from the intermediate, and the other half
5339 // from X.
5340 if (NumHi == 3) {
5341 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005342 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005343 std::swap(V1, V2);
5344 }
5345
5346 // Find the element from V2.
5347 unsigned HiIndex;
5348 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005349 int Val = PermMask[HiIndex];
5350 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005351 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005352 if (Val >= 4)
5353 break;
5354 }
5355
Nate Begeman9008ca62009-04-27 18:41:29 +00005356 Mask1[0] = PermMask[HiIndex];
5357 Mask1[1] = -1;
5358 Mask1[2] = PermMask[HiIndex^1];
5359 Mask1[3] = -1;
5360 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005361
5362 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005363 Mask1[0] = PermMask[0];
5364 Mask1[1] = PermMask[1];
5365 Mask1[2] = HiIndex & 1 ? 6 : 4;
5366 Mask1[3] = HiIndex & 1 ? 4 : 6;
5367 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005368 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005369 Mask1[0] = HiIndex & 1 ? 2 : 0;
5370 Mask1[1] = HiIndex & 1 ? 0 : 2;
5371 Mask1[2] = PermMask[2];
5372 Mask1[3] = PermMask[3];
5373 if (Mask1[2] >= 0)
5374 Mask1[2] += 4;
5375 if (Mask1[3] >= 0)
5376 Mask1[3] += 4;
5377 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005378 }
Evan Chengace3c172008-07-22 21:13:36 +00005379 }
5380
5381 // Break it into (shuffle shuffle_hi, shuffle_lo).
5382 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005383 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005384 SmallVector<int,8> LoMask(4U, -1);
5385 SmallVector<int,8> HiMask(4U, -1);
5386
5387 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005388 unsigned MaskIdx = 0;
5389 unsigned LoIdx = 0;
5390 unsigned HiIdx = 2;
5391 for (unsigned i = 0; i != 4; ++i) {
5392 if (i == 2) {
5393 MaskPtr = &HiMask;
5394 MaskIdx = 1;
5395 LoIdx = 0;
5396 HiIdx = 2;
5397 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005398 int Idx = PermMask[i];
5399 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005400 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005401 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005402 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005403 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005404 LoIdx++;
5405 } else {
5406 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005407 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005408 HiIdx++;
5409 }
5410 }
5411
Nate Begeman9008ca62009-04-27 18:41:29 +00005412 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5413 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5414 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005415 for (unsigned i = 0; i != 4; ++i) {
5416 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005417 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005418 } else {
5419 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005420 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005421 }
5422 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005423 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005424}
5425
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005426static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005427 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005428 V = V.getOperand(0);
5429 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5430 V = V.getOperand(0);
5431 if (MayFoldLoad(V))
5432 return true;
5433 return false;
5434}
5435
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005436// FIXME: the version above should always be used. Since there's
5437// a bug where several vector shuffles can't be folded because the
5438// DAG is not updated during lowering and a node claims to have two
5439// uses while it only has one, use this version, and let isel match
5440// another instruction if the load really happens to have more than
5441// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005442// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005443static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005444 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005445 V = V.getOperand(0);
5446 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5447 V = V.getOperand(0);
5448 if (ISD::isNormalLoad(V.getNode()))
5449 return true;
5450 return false;
5451}
5452
5453/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5454/// a vector extract, and if both can be later optimized into a single load.
5455/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5456/// here because otherwise a target specific shuffle node is going to be
5457/// emitted for this shuffle, and the optimization not done.
5458/// FIXME: This is probably not the best approach, but fix the problem
5459/// until the right path is decided.
5460static
5461bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5462 const TargetLowering &TLI) {
5463 EVT VT = V.getValueType();
5464 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5465
5466 // Be sure that the vector shuffle is present in a pattern like this:
5467 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5468 if (!V.hasOneUse())
5469 return false;
5470
5471 SDNode *N = *V.getNode()->use_begin();
5472 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5473 return false;
5474
5475 SDValue EltNo = N->getOperand(1);
5476 if (!isa<ConstantSDNode>(EltNo))
5477 return false;
5478
5479 // If the bit convert changed the number of elements, it is unsafe
5480 // to examine the mask.
5481 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005482 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005483 EVT SrcVT = V.getOperand(0).getValueType();
5484 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5485 return false;
5486 V = V.getOperand(0);
5487 HasShuffleIntoBitcast = true;
5488 }
5489
5490 // Select the input vector, guarding against out of range extract vector.
5491 unsigned NumElems = VT.getVectorNumElements();
5492 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5493 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5494 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5495
5496 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005497 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005498 V = V.getOperand(0);
5499
5500 if (ISD::isNormalLoad(V.getNode())) {
5501 // Is the original load suitable?
5502 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5503
5504 // FIXME: avoid the multi-use bug that is preventing lots of
5505 // of foldings to be detected, this is still wrong of course, but
5506 // give the temporary desired behavior, and if it happens that
5507 // the load has real more uses, during isel it will not fold, and
5508 // will generate poor code.
5509 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5510 return false;
5511
5512 if (!HasShuffleIntoBitcast)
5513 return true;
5514
5515 // If there's a bitcast before the shuffle, check if the load type and
5516 // alignment is valid.
5517 unsigned Align = LN0->getAlignment();
5518 unsigned NewAlign =
5519 TLI.getTargetData()->getABITypeAlignment(
5520 VT.getTypeForEVT(*DAG.getContext()));
5521
5522 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5523 return false;
5524 }
5525
5526 return true;
5527}
5528
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005529static
Evan Cheng835580f2010-10-07 20:50:20 +00005530SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5531 EVT VT = Op.getValueType();
5532
5533 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005534 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5535 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005536 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5537 V1, DAG));
5538}
5539
5540static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005541SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5542 bool HasSSE2) {
5543 SDValue V1 = Op.getOperand(0);
5544 SDValue V2 = Op.getOperand(1);
5545 EVT VT = Op.getValueType();
5546
5547 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5548
5549 if (HasSSE2 && VT == MVT::v2f64)
5550 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5551
5552 // v4f32 or v4i32
5553 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5554}
5555
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005556static
5557SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5558 SDValue V1 = Op.getOperand(0);
5559 SDValue V2 = Op.getOperand(1);
5560 EVT VT = Op.getValueType();
5561
5562 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5563 "unsupported shuffle type");
5564
5565 if (V2.getOpcode() == ISD::UNDEF)
5566 V2 = V1;
5567
5568 // v4i32 or v4f32
5569 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5570}
5571
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005572static
5573SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5574 SDValue V1 = Op.getOperand(0);
5575 SDValue V2 = Op.getOperand(1);
5576 EVT VT = Op.getValueType();
5577 unsigned NumElems = VT.getVectorNumElements();
5578
5579 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5580 // operand of these instructions is only memory, so check if there's a
5581 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5582 // same masks.
5583 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005584
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005585 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005586 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005587 CanFoldLoad = true;
5588
5589 // When V1 is a load, it can be folded later into a store in isel, example:
5590 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5591 // turns into:
5592 // (MOVLPSmr addr:$src1, VR128:$src2)
5593 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005594 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005595 CanFoldLoad = true;
5596
Eric Christopher893a8822011-02-20 05:04:42 +00005597 // Both of them can't be memory operations though.
5598 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5599 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005600
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005601 if (CanFoldLoad) {
5602 if (HasSSE2 && NumElems == 2)
5603 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5604
5605 if (NumElems == 4)
5606 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5607 }
5608
5609 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5610 // movl and movlp will both match v2i64, but v2i64 is never matched by
5611 // movl earlier because we make it strict to avoid messing with the movlp load
5612 // folding logic (see the code above getMOVLP call). Match it here then,
5613 // this is horrible, but will stay like this until we move all shuffle
5614 // matching to x86 specific nodes. Note that for the 1st condition all
5615 // types are matched with movsd.
5616 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5617 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5618 else if (HasSSE2)
5619 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5620
5621
5622 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5623
5624 // Invert the operand order and use SHUFPS to match it.
5625 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5626 X86::getShuffleSHUFImmediate(SVOp), DAG);
5627}
5628
David Greenec4db4e52011-02-28 19:06:56 +00005629static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005630 switch(VT.getSimpleVT().SimpleTy) {
5631 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5632 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005633 case MVT::v4f32:
5634 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5635 case MVT::v2f64:
5636 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5637 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5638 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005639 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5640 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5641 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005642 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005643 }
5644 return 0;
5645}
5646
5647static inline unsigned getUNPCKHOpcode(EVT VT) {
5648 switch(VT.getSimpleVT().SimpleTy) {
5649 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5650 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5651 case MVT::v4f32: return X86ISD::UNPCKHPS;
5652 case MVT::v2f64: return X86ISD::UNPCKHPD;
5653 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5654 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5655 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005656 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005657 }
5658 return 0;
5659}
5660
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005661static
5662SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005663 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005664 const X86Subtarget *Subtarget) {
5665 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5666 EVT VT = Op.getValueType();
5667 DebugLoc dl = Op.getDebugLoc();
5668 SDValue V1 = Op.getOperand(0);
5669 SDValue V2 = Op.getOperand(1);
5670
5671 if (isZeroShuffle(SVOp))
5672 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5673
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005674 // Handle splat operations
5675 if (SVOp->isSplat()) {
5676 // Special case, this is the only place now where it's
5677 // allowed to return a vector_shuffle operation without
5678 // using a target specific node, because *hopefully* it
5679 // will be optimized away by the dag combiner.
5680 if (VT.getVectorNumElements() <= 4 &&
5681 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5682 return Op;
5683
5684 // Handle splats by matching through known masks
5685 if (VT.getVectorNumElements() <= 4)
5686 return SDValue();
5687
Evan Cheng835580f2010-10-07 20:50:20 +00005688 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005689 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005690 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005691
5692 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5693 // do it!
5694 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5695 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5696 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005697 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005698 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5699 // FIXME: Figure out a cleaner way to do this.
5700 // Try to make use of movq to zero out the top part.
5701 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5702 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5703 if (NewOp.getNode()) {
5704 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5705 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5706 DAG, Subtarget, dl);
5707 }
5708 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5709 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5710 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5711 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5712 DAG, Subtarget, dl);
5713 }
5714 }
5715 return SDValue();
5716}
5717
Dan Gohman475871a2008-07-27 21:46:04 +00005718SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005719X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005720 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005721 SDValue V1 = Op.getOperand(0);
5722 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005723 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005724 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005725 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005726 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005727 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5728 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005729 bool V1IsSplat = false;
5730 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005731 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005732 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005733 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005734 MachineFunction &MF = DAG.getMachineFunction();
5735 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005736
Dale Johannesen0488fb62010-09-30 23:57:10 +00005737 // Shuffle operations on MMX not supported.
5738 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005739 return Op;
5740
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005741 // Vector shuffle lowering takes 3 steps:
5742 //
5743 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5744 // narrowing and commutation of operands should be handled.
5745 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5746 // shuffle nodes.
5747 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5748 // so the shuffle can be broken into other shuffles and the legalizer can
5749 // try the lowering again.
5750 //
5751 // The general ideia is that no vector_shuffle operation should be left to
5752 // be matched during isel, all of them must be converted to a target specific
5753 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005754
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005755 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5756 // narrowing and commutation of operands should be handled. The actual code
5757 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005758 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005759 if (NewOp.getNode())
5760 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005761
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005762 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5763 // unpckh_undef). Only use pshufd if speed is more important than size.
5764 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5765 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005766 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005767 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5768 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5769 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005770
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005771 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005772 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005773 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005774
Dale Johannesen0488fb62010-09-30 23:57:10 +00005775 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005776 return getMOVHighToLow(Op, dl, DAG);
5777
5778 // Use to match splats
5779 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5780 (VT == MVT::v2f64 || VT == MVT::v2i64))
5781 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5782
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005783 if (X86::isPSHUFDMask(SVOp)) {
5784 // The actual implementation will match the mask in the if above and then
5785 // during isel it can match several different instructions, not only pshufd
5786 // as its name says, sad but true, emulate the behavior for now...
5787 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5788 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5789
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005790 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5791
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005792 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005793 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5794
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005795 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005796 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5797 TargetMask, DAG);
5798
5799 if (VT == MVT::v4f32)
5800 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5801 TargetMask, DAG);
5802 }
Eric Christopherfd179292009-08-27 18:07:15 +00005803
Evan Chengf26ffe92008-05-29 08:22:04 +00005804 // Check if this can be converted into a logical shift.
5805 bool isLeft = false;
5806 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005807 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005808 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005809 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005810 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005811 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005812 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005813 EVT EltVT = VT.getVectorElementType();
5814 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005815 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005816 }
Eric Christopherfd179292009-08-27 18:07:15 +00005817
Nate Begeman9008ca62009-04-27 18:41:29 +00005818 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005819 if (V1IsUndef)
5820 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005821 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005822 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005823 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005824 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005825 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5826
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005827 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005828 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5829 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005830 }
Eric Christopherfd179292009-08-27 18:07:15 +00005831
Nate Begeman9008ca62009-04-27 18:41:29 +00005832 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005833 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5834 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005835
Dale Johannesen0488fb62010-09-30 23:57:10 +00005836 if (X86::isMOVHLPSMask(SVOp))
5837 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005838
Dale Johannesen0488fb62010-09-30 23:57:10 +00005839 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5840 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005841
Dale Johannesen0488fb62010-09-30 23:57:10 +00005842 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5843 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005844
Dale Johannesen0488fb62010-09-30 23:57:10 +00005845 if (X86::isMOVLPMask(SVOp))
5846 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005847
Nate Begeman9008ca62009-04-27 18:41:29 +00005848 if (ShouldXformToMOVHLPS(SVOp) ||
5849 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5850 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005851
Evan Chengf26ffe92008-05-29 08:22:04 +00005852 if (isShift) {
5853 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005854 EVT EltVT = VT.getVectorElementType();
5855 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005856 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005857 }
Eric Christopherfd179292009-08-27 18:07:15 +00005858
Evan Cheng9eca5e82006-10-25 21:49:50 +00005859 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005860 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5861 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005862 V1IsSplat = isSplatVector(V1.getNode());
5863 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005864
Chris Lattner8a594482007-11-25 00:24:49 +00005865 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005866 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005867 Op = CommuteVectorShuffle(SVOp, DAG);
5868 SVOp = cast<ShuffleVectorSDNode>(Op);
5869 V1 = SVOp->getOperand(0);
5870 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005871 std::swap(V1IsSplat, V2IsSplat);
5872 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005873 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005874 }
5875
Nate Begeman9008ca62009-04-27 18:41:29 +00005876 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5877 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005878 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005879 return V1;
5880 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5881 // the instruction selector will not match, so get a canonical MOVL with
5882 // swapped operands to undo the commute.
5883 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005884 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005885
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005886 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005887 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5888 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005889
5890 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005891 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005892
Evan Cheng9bbbb982006-10-25 20:48:19 +00005893 if (V2IsSplat) {
5894 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005895 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005896 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005897 SDValue NewMask = NormalizeMask(SVOp, DAG);
5898 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5899 if (NSVOp != SVOp) {
5900 if (X86::isUNPCKLMask(NSVOp, true)) {
5901 return NewMask;
5902 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5903 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005904 }
5905 }
5906 }
5907
Evan Cheng9eca5e82006-10-25 21:49:50 +00005908 if (Commuted) {
5909 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005910 // FIXME: this seems wrong.
5911 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5912 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005913
5914 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005915 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5916 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005917
5918 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005919 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005920 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005921
Nate Begeman9008ca62009-04-27 18:41:29 +00005922 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005923 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005924 return CommuteVectorShuffle(SVOp, DAG);
5925
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005926 // The checks below are all present in isShuffleMaskLegal, but they are
5927 // inlined here right now to enable us to directly emit target specific
5928 // nodes, and remove one by one until they don't return Op anymore.
5929 SmallVector<int, 16> M;
5930 SVOp->getMask(M);
5931
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005932 if (isPALIGNRMask(M, VT, HasSSSE3))
5933 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5934 X86::getShufflePALIGNRImmediate(SVOp),
5935 DAG);
5936
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005937 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5938 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005939 if (VT == MVT::v2f64) {
5940 X86ISD::NodeType Opcode =
5941 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5942 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5943 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005944 if (VT == MVT::v2i64)
5945 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5946 }
5947
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005948 if (isPSHUFHWMask(M, VT))
5949 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5950 X86::getShufflePSHUFHWImmediate(SVOp),
5951 DAG);
5952
5953 if (isPSHUFLWMask(M, VT))
5954 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5955 X86::getShufflePSHUFLWImmediate(SVOp),
5956 DAG);
5957
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005958 if (isSHUFPMask(M, VT)) {
5959 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5960 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5961 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5962 TargetMask, DAG);
5963 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5964 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5965 TargetMask, DAG);
5966 }
5967
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005968 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5969 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005970 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5971 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005972 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5973 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5974 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5975
Evan Cheng14b32e12007-12-11 01:46:18 +00005976 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005977 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005978 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005979 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005980 return NewOp;
5981 }
5982
Owen Anderson825b72b2009-08-11 20:47:22 +00005983 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005984 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005985 if (NewOp.getNode())
5986 return NewOp;
5987 }
Eric Christopherfd179292009-08-27 18:07:15 +00005988
Dale Johannesen0488fb62010-09-30 23:57:10 +00005989 // Handle all 4 wide cases with a number of shuffles.
5990 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005991 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005992
Dan Gohman475871a2008-07-27 21:46:04 +00005993 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005994}
5995
Dan Gohman475871a2008-07-27 21:46:04 +00005996SDValue
5997X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005998 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005999 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006000 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006001 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006002 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006003 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006005 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006006 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006007 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006008 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6009 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6010 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006011 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6012 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006013 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006014 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006015 Op.getOperand(0)),
6016 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006017 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006018 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006019 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006020 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006021 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006022 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006023 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6024 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006025 // result has a single use which is a store or a bitcast to i32. And in
6026 // the case of a store, it's not worth it if the index is a constant 0,
6027 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006028 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006029 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006030 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006031 if ((User->getOpcode() != ISD::STORE ||
6032 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6033 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006034 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006035 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006036 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006037 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006038 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006039 Op.getOperand(0)),
6040 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006041 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006042 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006043 // ExtractPS works with constant index.
6044 if (isa<ConstantSDNode>(Op.getOperand(1)))
6045 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006046 }
Dan Gohman475871a2008-07-27 21:46:04 +00006047 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006048}
6049
6050
Dan Gohman475871a2008-07-27 21:46:04 +00006051SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006052X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6053 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006054 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006055 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006056
David Greene74a579d2011-02-10 16:57:36 +00006057 SDValue Vec = Op.getOperand(0);
6058 EVT VecVT = Vec.getValueType();
6059
6060 // If this is a 256-bit vector result, first extract the 128-bit
6061 // vector and then extract from the 128-bit vector.
6062 if (VecVT.getSizeInBits() > 128) {
6063 DebugLoc dl = Op.getNode()->getDebugLoc();
6064 unsigned NumElems = VecVT.getVectorNumElements();
6065 SDValue Idx = Op.getOperand(1);
6066
6067 if (!isa<ConstantSDNode>(Idx))
6068 return SDValue();
6069
6070 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6071 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6072
6073 // Get the 128-bit vector.
6074 bool Upper = IdxVal >= ExtractNumElems;
6075 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6076
6077 // Extract from it.
6078 SDValue ScaledIdx = Idx;
6079 if (Upper)
6080 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6081 DAG.getConstant(ExtractNumElems,
6082 Idx.getValueType()));
6083 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6084 ScaledIdx);
6085 }
6086
6087 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6088
Evan Cheng62a3f152008-03-24 21:52:23 +00006089 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006090 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006091 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006092 return Res;
6093 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006094
Owen Andersone50ed302009-08-10 22:56:29 +00006095 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006096 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006097 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006098 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006099 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006100 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006101 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006102 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6103 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006104 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006105 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006106 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006107 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006108 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006109 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006110 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006111 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006112 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006113 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006114 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006115 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006116 if (Idx == 0)
6117 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006118
Evan Cheng0db9fe62006-04-25 20:13:52 +00006119 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006120 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006121 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006122 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006123 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006124 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006125 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006126 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006127 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6128 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6129 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006130 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006131 if (Idx == 0)
6132 return Op;
6133
6134 // UNPCKHPD the element to the lowest double word, then movsd.
6135 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6136 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006137 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006138 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006139 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006140 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006141 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006142 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006143 }
6144
Dan Gohman475871a2008-07-27 21:46:04 +00006145 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006146}
6147
Dan Gohman475871a2008-07-27 21:46:04 +00006148SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006149X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6150 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006151 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006152 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006153 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006154
Dan Gohman475871a2008-07-27 21:46:04 +00006155 SDValue N0 = Op.getOperand(0);
6156 SDValue N1 = Op.getOperand(1);
6157 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006158
Dan Gohman8a55ce42009-09-23 21:02:20 +00006159 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006160 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006161 unsigned Opc;
6162 if (VT == MVT::v8i16)
6163 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006164 else if (VT == MVT::v16i8)
6165 Opc = X86ISD::PINSRB;
6166 else
6167 Opc = X86ISD::PINSRB;
6168
Nate Begeman14d12ca2008-02-11 04:19:36 +00006169 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6170 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006171 if (N1.getValueType() != MVT::i32)
6172 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6173 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006174 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006175 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006176 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006177 // Bits [7:6] of the constant are the source select. This will always be
6178 // zero here. The DAG Combiner may combine an extract_elt index into these
6179 // bits. For example (insert (extract, 3), 2) could be matched by putting
6180 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006181 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006182 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006183 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006184 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006185 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006186 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006187 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006188 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006189 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006190 // PINSR* works with constant index.
6191 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006192 }
Dan Gohman475871a2008-07-27 21:46:04 +00006193 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006194}
6195
Dan Gohman475871a2008-07-27 21:46:04 +00006196SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006197X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006198 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006199 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006200
David Greene6b381262011-02-09 15:32:06 +00006201 DebugLoc dl = Op.getDebugLoc();
6202 SDValue N0 = Op.getOperand(0);
6203 SDValue N1 = Op.getOperand(1);
6204 SDValue N2 = Op.getOperand(2);
6205
6206 // If this is a 256-bit vector result, first insert into a 128-bit
6207 // vector and then insert into the 256-bit vector.
6208 if (VT.getSizeInBits() > 128) {
6209 if (!isa<ConstantSDNode>(N2))
6210 return SDValue();
6211
6212 // Get the 128-bit vector.
6213 unsigned NumElems = VT.getVectorNumElements();
6214 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6215 bool Upper = IdxVal >= NumElems / 2;
6216
6217 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6218
6219 // Insert into it.
6220 SDValue ScaledN2 = N2;
6221 if (Upper)
6222 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006223 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006224 (VT.getSizeInBits() / 128),
6225 N2.getValueType()));
6226 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6227 N1, ScaledN2);
6228
6229 // Insert the 128-bit vector
6230 // FIXME: Why UNDEF?
6231 return Insert128BitVector(N0, Op, N2, DAG, dl);
6232 }
6233
Nate Begeman14d12ca2008-02-11 04:19:36 +00006234 if (Subtarget->hasSSE41())
6235 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6236
Dan Gohman8a55ce42009-09-23 21:02:20 +00006237 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006238 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006239
Dan Gohman8a55ce42009-09-23 21:02:20 +00006240 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006241 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6242 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006243 if (N1.getValueType() != MVT::i32)
6244 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6245 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006246 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006247 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006248 }
Dan Gohman475871a2008-07-27 21:46:04 +00006249 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006250}
6251
Dan Gohman475871a2008-07-27 21:46:04 +00006252SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006253X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006254 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006255 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006256 EVT OpVT = Op.getValueType();
6257
6258 // If this is a 256-bit vector result, first insert into a 128-bit
6259 // vector and then insert into the 256-bit vector.
6260 if (OpVT.getSizeInBits() > 128) {
6261 // Insert into a 128-bit vector.
6262 EVT VT128 = EVT::getVectorVT(*Context,
6263 OpVT.getVectorElementType(),
6264 OpVT.getVectorNumElements() / 2);
6265
6266 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6267
6268 // Insert the 128-bit vector.
6269 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6270 DAG.getConstant(0, MVT::i32),
6271 DAG, dl);
6272 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006273
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006274 if (Op.getValueType() == MVT::v1i64 &&
6275 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006276 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006277
Owen Anderson825b72b2009-08-11 20:47:22 +00006278 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006279 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6280 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006281 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006282 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006283}
6284
David Greene91585092011-01-26 15:38:49 +00006285// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6286// a simple subregister reference or explicit instructions to grab
6287// upper bits of a vector.
6288SDValue
6289X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6290 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006291 DebugLoc dl = Op.getNode()->getDebugLoc();
6292 SDValue Vec = Op.getNode()->getOperand(0);
6293 SDValue Idx = Op.getNode()->getOperand(1);
6294
6295 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6296 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6297 return Extract128BitVector(Vec, Idx, DAG, dl);
6298 }
David Greene91585092011-01-26 15:38:49 +00006299 }
6300 return SDValue();
6301}
6302
David Greenecfe33c42011-01-26 19:13:22 +00006303// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6304// simple superregister reference or explicit instructions to insert
6305// the upper bits of a vector.
6306SDValue
6307X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6308 if (Subtarget->hasAVX()) {
6309 DebugLoc dl = Op.getNode()->getDebugLoc();
6310 SDValue Vec = Op.getNode()->getOperand(0);
6311 SDValue SubVec = Op.getNode()->getOperand(1);
6312 SDValue Idx = Op.getNode()->getOperand(2);
6313
6314 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6315 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006316 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006317 }
6318 }
6319 return SDValue();
6320}
6321
Bill Wendling056292f2008-09-16 21:48:12 +00006322// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6323// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6324// one of the above mentioned nodes. It has to be wrapped because otherwise
6325// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6326// be used to form addressing mode. These wrapped nodes will be selected
6327// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006328SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006329X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006330 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006331
Chris Lattner41621a22009-06-26 19:22:52 +00006332 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6333 // global base reg.
6334 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006335 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006336 CodeModel::Model M = getTargetMachine().getCodeModel();
6337
Chris Lattner4f066492009-07-11 20:29:19 +00006338 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006339 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006340 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006341 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006342 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006343 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006344 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006345
Evan Cheng1606e8e2009-03-13 07:51:59 +00006346 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006347 CP->getAlignment(),
6348 CP->getOffset(), OpFlag);
6349 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006350 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006351 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006352 if (OpFlag) {
6353 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006354 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006355 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006356 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006357 }
6358
6359 return Result;
6360}
6361
Dan Gohmand858e902010-04-17 15:26:15 +00006362SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006363 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006364
Chris Lattner18c59872009-06-27 04:16:01 +00006365 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6366 // global base reg.
6367 unsigned char OpFlag = 0;
6368 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006369 CodeModel::Model M = getTargetMachine().getCodeModel();
6370
Chris Lattner4f066492009-07-11 20:29:19 +00006371 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006372 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006373 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006374 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006375 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006376 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006377 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006378
Chris Lattner18c59872009-06-27 04:16:01 +00006379 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6380 OpFlag);
6381 DebugLoc DL = JT->getDebugLoc();
6382 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006383
Chris Lattner18c59872009-06-27 04:16:01 +00006384 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006385 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006386 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6387 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006388 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006389 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006390
Chris Lattner18c59872009-06-27 04:16:01 +00006391 return Result;
6392}
6393
6394SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006395X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006396 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006397
Chris Lattner18c59872009-06-27 04:16:01 +00006398 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6399 // global base reg.
6400 unsigned char OpFlag = 0;
6401 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006402 CodeModel::Model M = getTargetMachine().getCodeModel();
6403
Chris Lattner4f066492009-07-11 20:29:19 +00006404 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006405 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006406 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006407 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006408 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006409 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006410 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006411
Chris Lattner18c59872009-06-27 04:16:01 +00006412 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006413
Chris Lattner18c59872009-06-27 04:16:01 +00006414 DebugLoc DL = Op.getDebugLoc();
6415 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006416
6417
Chris Lattner18c59872009-06-27 04:16:01 +00006418 // With PIC, the address is actually $g + Offset.
6419 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006420 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006421 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6422 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006423 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006424 Result);
6425 }
Eric Christopherfd179292009-08-27 18:07:15 +00006426
Chris Lattner18c59872009-06-27 04:16:01 +00006427 return Result;
6428}
6429
Dan Gohman475871a2008-07-27 21:46:04 +00006430SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006431X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006432 // Create the TargetBlockAddressAddress node.
6433 unsigned char OpFlags =
6434 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006435 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006436 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006437 DebugLoc dl = Op.getDebugLoc();
6438 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6439 /*isTarget=*/true, OpFlags);
6440
Dan Gohmanf705adb2009-10-30 01:28:02 +00006441 if (Subtarget->isPICStyleRIPRel() &&
6442 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006443 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6444 else
6445 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006446
Dan Gohman29cbade2009-11-20 23:18:13 +00006447 // With PIC, the address is actually $g + Offset.
6448 if (isGlobalRelativeToPICBase(OpFlags)) {
6449 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6450 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6451 Result);
6452 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006453
6454 return Result;
6455}
6456
6457SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006458X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006459 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006460 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006461 // Create the TargetGlobalAddress node, folding in the constant
6462 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006463 unsigned char OpFlags =
6464 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006465 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006466 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006467 if (OpFlags == X86II::MO_NO_FLAG &&
6468 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006469 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006470 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006471 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006472 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006473 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006474 }
Eric Christopherfd179292009-08-27 18:07:15 +00006475
Chris Lattner4f066492009-07-11 20:29:19 +00006476 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006477 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006478 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6479 else
6480 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006481
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006482 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006483 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006484 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6485 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006486 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006487 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006488
Chris Lattner36c25012009-07-10 07:34:39 +00006489 // For globals that require a load from a stub to get the address, emit the
6490 // load.
6491 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006492 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006493 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006494
Dan Gohman6520e202008-10-18 02:06:02 +00006495 // If there was a non-zero offset that we didn't fold, create an explicit
6496 // addition for it.
6497 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006498 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006499 DAG.getConstant(Offset, getPointerTy()));
6500
Evan Cheng0db9fe62006-04-25 20:13:52 +00006501 return Result;
6502}
6503
Evan Chengda43bcf2008-09-24 00:05:32 +00006504SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006505X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006506 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006507 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006508 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006509}
6510
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006511static SDValue
6512GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006513 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006514 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006515 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006516 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006517 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006518 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006519 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006520 GA->getOffset(),
6521 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006522 if (InFlag) {
6523 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006524 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006525 } else {
6526 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006527 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006528 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006529
6530 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006531 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006532
Rafael Espindola15f1b662009-04-24 12:59:40 +00006533 SDValue Flag = Chain.getValue(1);
6534 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006535}
6536
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006537// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006538static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006539LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006540 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006541 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006542 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6543 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006544 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006545 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006546 InFlag = Chain.getValue(1);
6547
Chris Lattnerb903bed2009-06-26 21:20:29 +00006548 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006549}
6550
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006551// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006552static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006553LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006554 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006555 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6556 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006557}
6558
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006559// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6560// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006561static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006562 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006563 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006564 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006565
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006566 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6567 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6568 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006569
Michael J. Spencerec38de22010-10-10 22:04:20 +00006570 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006571 DAG.getIntPtrConstant(0),
6572 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006573
Chris Lattnerb903bed2009-06-26 21:20:29 +00006574 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006575 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6576 // initialexec.
6577 unsigned WrapperKind = X86ISD::Wrapper;
6578 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006579 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006580 } else if (is64Bit) {
6581 assert(model == TLSModel::InitialExec);
6582 OperandFlags = X86II::MO_GOTTPOFF;
6583 WrapperKind = X86ISD::WrapperRIP;
6584 } else {
6585 assert(model == TLSModel::InitialExec);
6586 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006587 }
Eric Christopherfd179292009-08-27 18:07:15 +00006588
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006589 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6590 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006591 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006592 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006593 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006594 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006595
Rafael Espindola9a580232009-02-27 13:37:18 +00006596 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006597 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006598 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006599
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006600 // The address of the thread local variable is the add of the thread
6601 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006602 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006603}
6604
Dan Gohman475871a2008-07-27 21:46:04 +00006605SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006606X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006607
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006608 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006609 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006610
Eric Christopher30ef0e52010-06-03 04:07:48 +00006611 if (Subtarget->isTargetELF()) {
6612 // TODO: implement the "local dynamic" model
6613 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006614
Eric Christopher30ef0e52010-06-03 04:07:48 +00006615 // If GV is an alias then use the aliasee for determining
6616 // thread-localness.
6617 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6618 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006619
6620 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006621 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006622
Eric Christopher30ef0e52010-06-03 04:07:48 +00006623 switch (model) {
6624 case TLSModel::GeneralDynamic:
6625 case TLSModel::LocalDynamic: // not implemented
6626 if (Subtarget->is64Bit())
6627 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6628 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006629
Eric Christopher30ef0e52010-06-03 04:07:48 +00006630 case TLSModel::InitialExec:
6631 case TLSModel::LocalExec:
6632 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6633 Subtarget->is64Bit());
6634 }
6635 } else if (Subtarget->isTargetDarwin()) {
6636 // Darwin only has one model of TLS. Lower to that.
6637 unsigned char OpFlag = 0;
6638 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6639 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006640
Eric Christopher30ef0e52010-06-03 04:07:48 +00006641 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6642 // global base reg.
6643 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6644 !Subtarget->is64Bit();
6645 if (PIC32)
6646 OpFlag = X86II::MO_TLVP_PIC_BASE;
6647 else
6648 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006649 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006650 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006651 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006652 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006653 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006654
Eric Christopher30ef0e52010-06-03 04:07:48 +00006655 // With PIC32, the address is actually $g + Offset.
6656 if (PIC32)
6657 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6658 DAG.getNode(X86ISD::GlobalBaseReg,
6659 DebugLoc(), getPointerTy()),
6660 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006661
Eric Christopher30ef0e52010-06-03 04:07:48 +00006662 // Lowering the machine isd will make sure everything is in the right
6663 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006664 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006665 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006666 SDValue Args[] = { Chain, Offset };
6667 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006668
Eric Christopher30ef0e52010-06-03 04:07:48 +00006669 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6670 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6671 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006672
Eric Christopher30ef0e52010-06-03 04:07:48 +00006673 // And our return value (tls address) is in the standard call return value
6674 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006675 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6676 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006677 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006678
Eric Christopher30ef0e52010-06-03 04:07:48 +00006679 assert(false &&
6680 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006681
Torok Edwinc23197a2009-07-14 16:55:14 +00006682 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006683 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006684}
6685
Evan Cheng0db9fe62006-04-25 20:13:52 +00006686
Nadav Rotem43012222011-05-11 08:12:09 +00006687/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006688/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006689SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006690 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006691 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006692 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006693 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006694 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006695 SDValue ShOpLo = Op.getOperand(0);
6696 SDValue ShOpHi = Op.getOperand(1);
6697 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006698 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006699 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006700 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006701
Dan Gohman475871a2008-07-27 21:46:04 +00006702 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006703 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006704 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6705 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006706 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006707 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6708 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006709 }
Evan Chenge3413162006-01-09 18:33:28 +00006710
Owen Anderson825b72b2009-08-11 20:47:22 +00006711 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6712 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006713 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006714 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006715
Dan Gohman475871a2008-07-27 21:46:04 +00006716 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006717 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006718 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6719 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006720
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006721 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006722 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6723 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006724 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006725 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6726 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006727 }
6728
Dan Gohman475871a2008-07-27 21:46:04 +00006729 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006730 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006731}
Evan Chenga3195e82006-01-12 22:54:21 +00006732
Dan Gohmand858e902010-04-17 15:26:15 +00006733SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6734 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006735 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006736
Dale Johannesen0488fb62010-09-30 23:57:10 +00006737 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006738 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006739
Owen Anderson825b72b2009-08-11 20:47:22 +00006740 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006741 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006742
Eli Friedman36df4992009-05-27 00:47:34 +00006743 // These are really Legal; return the operand so the caller accepts it as
6744 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006745 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006746 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006747 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006748 Subtarget->is64Bit()) {
6749 return Op;
6750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006751
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006752 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006753 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006754 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006755 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006756 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006757 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006758 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006759 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006760 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006761 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6762}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006763
Owen Andersone50ed302009-08-10 22:56:29 +00006764SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006765 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006766 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006767 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006768 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006769 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006770 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006771 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006772 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006773 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006774 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006775
Chris Lattner492a43e2010-09-22 01:28:21 +00006776 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006777
Stuart Hastings84be9582011-06-02 15:57:11 +00006778 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6779 MachineMemOperand *MMO;
6780 if (FI) {
6781 int SSFI = FI->getIndex();
6782 MMO =
6783 DAG.getMachineFunction()
6784 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6785 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6786 } else {
6787 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6788 StackSlot = StackSlot.getOperand(1);
6789 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006790 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006791 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6792 X86ISD::FILD, DL,
6793 Tys, Ops, array_lengthof(Ops),
6794 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006795
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006796 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006797 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006798 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006799
6800 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6801 // shouldn't be necessary except that RFP cannot be live across
6802 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006803 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006804 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6805 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006806 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006807 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006808 SDValue Ops[] = {
6809 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6810 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006811 MachineMemOperand *MMO =
6812 DAG.getMachineFunction()
6813 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006814 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006815
Chris Lattner492a43e2010-09-22 01:28:21 +00006816 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6817 Ops, array_lengthof(Ops),
6818 Op.getValueType(), MMO);
6819 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006820 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006821 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006822 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006823
Evan Cheng0db9fe62006-04-25 20:13:52 +00006824 return Result;
6825}
6826
Bill Wendling8b8a6362009-01-17 03:56:04 +00006827// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006828SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6829 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006830 // This algorithm is not obvious. Here it is in C code, more or less:
6831 /*
6832 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6833 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6834 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006835
Bill Wendling8b8a6362009-01-17 03:56:04 +00006836 // Copy ints to xmm registers.
6837 __m128i xh = _mm_cvtsi32_si128( hi );
6838 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006839
Bill Wendling8b8a6362009-01-17 03:56:04 +00006840 // Combine into low half of a single xmm register.
6841 __m128i x = _mm_unpacklo_epi32( xh, xl );
6842 __m128d d;
6843 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006844
Bill Wendling8b8a6362009-01-17 03:56:04 +00006845 // Merge in appropriate exponents to give the integer bits the right
6846 // magnitude.
6847 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006848
Bill Wendling8b8a6362009-01-17 03:56:04 +00006849 // Subtract away the biases to deal with the IEEE-754 double precision
6850 // implicit 1.
6851 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006852
Bill Wendling8b8a6362009-01-17 03:56:04 +00006853 // All conversions up to here are exact. The correctly rounded result is
6854 // calculated using the current rounding mode using the following
6855 // horizontal add.
6856 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6857 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6858 // store doesn't really need to be here (except
6859 // maybe to zero the other double)
6860 return sd;
6861 }
6862 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006863
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006864 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006865 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006866
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006867 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006868 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006869 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6870 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6871 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6872 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006873 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006874 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006875
Bill Wendling8b8a6362009-01-17 03:56:04 +00006876 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006877 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006878 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006879 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006880 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006881 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006882 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006883
Owen Anderson825b72b2009-08-11 20:47:22 +00006884 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6885 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006886 Op.getOperand(0),
6887 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006888 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6889 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006890 Op.getOperand(0),
6891 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006892 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6893 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006894 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006895 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006896 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006897 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006898 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006899 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006900 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006902
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006903 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006904 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006905 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6906 DAG.getUNDEF(MVT::v2f64), ShufMask);
6907 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6908 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006909 DAG.getIntPtrConstant(0));
6910}
6911
Bill Wendling8b8a6362009-01-17 03:56:04 +00006912// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006913SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6914 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006915 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006916 // FP constant to bias correct the final result.
6917 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006918 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006919
6920 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006921 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6922 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006923 Op.getOperand(0),
6924 DAG.getIntPtrConstant(0)));
6925
Owen Anderson825b72b2009-08-11 20:47:22 +00006926 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006927 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006928 DAG.getIntPtrConstant(0));
6929
6930 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006931 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006932 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006933 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006934 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006935 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006936 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006937 MVT::v2f64, Bias)));
6938 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006939 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006940 DAG.getIntPtrConstant(0));
6941
6942 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006943 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006944
6945 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006946 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006947
Owen Anderson825b72b2009-08-11 20:47:22 +00006948 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006949 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006950 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006951 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006952 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006953 }
6954
6955 // Handle final rounding.
6956 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006957}
6958
Dan Gohmand858e902010-04-17 15:26:15 +00006959SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6960 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006961 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006962 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006963
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006964 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006965 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6966 // the optimization here.
6967 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006968 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006969
Owen Andersone50ed302009-08-10 22:56:29 +00006970 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006971 EVT DstVT = Op.getValueType();
6972 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006973 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006974 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006975 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006976
6977 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006978 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006979 if (SrcVT == MVT::i32) {
6980 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6981 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6982 getPointerTy(), StackSlot, WordOff);
6983 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006984 StackSlot, MachinePointerInfo(),
6985 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006986 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006987 OffsetSlot, MachinePointerInfo(),
6988 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006989 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6990 return Fild;
6991 }
6992
6993 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6994 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006995 StackSlot, MachinePointerInfo(),
6996 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006997 // For i64 source, we need to add the appropriate power of 2 if the input
6998 // was negative. This is the same as the optimization in
6999 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7000 // we must be careful to do the computation in x87 extended precision, not
7001 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007002 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7003 MachineMemOperand *MMO =
7004 DAG.getMachineFunction()
7005 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7006 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007007
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007008 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7009 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007010 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7011 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007012
7013 APInt FF(32, 0x5F800000ULL);
7014
7015 // Check whether the sign bit is set.
7016 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7017 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7018 ISD::SETLT);
7019
7020 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7021 SDValue FudgePtr = DAG.getConstantPool(
7022 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7023 getPointerTy());
7024
7025 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7026 SDValue Zero = DAG.getIntPtrConstant(0);
7027 SDValue Four = DAG.getIntPtrConstant(4);
7028 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7029 Zero, Four);
7030 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7031
7032 // Load the value out, extending it from f32 to f80.
7033 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007034 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007035 FudgePtr, MachinePointerInfo::getConstantPool(),
7036 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007037 // Extend everything to 80 bits to force it to be done on x87.
7038 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7039 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007040}
7041
Dan Gohman475871a2008-07-27 21:46:04 +00007042std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007043FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007044 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007045
Owen Andersone50ed302009-08-10 22:56:29 +00007046 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007047
7048 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7050 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007051 }
7052
Owen Anderson825b72b2009-08-11 20:47:22 +00007053 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7054 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007055 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007056
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007057 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007058 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007059 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007060 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007061 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007063 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007064 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007065
Evan Cheng87c89352007-10-15 20:11:21 +00007066 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7067 // stack slot.
7068 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007069 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007070 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007071 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007072
Michael J. Spencerec38de22010-10-10 22:04:20 +00007073
7074
Evan Cheng0db9fe62006-04-25 20:13:52 +00007075 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007077 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007078 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7079 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7080 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007081 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007082
Dan Gohman475871a2008-07-27 21:46:04 +00007083 SDValue Chain = DAG.getEntryNode();
7084 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007085 EVT TheVT = Op.getOperand(0).getValueType();
7086 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007087 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007088 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007089 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007090 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007091 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007092 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007093 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007094 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007095
Chris Lattner492a43e2010-09-22 01:28:21 +00007096 MachineMemOperand *MMO =
7097 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7098 MachineMemOperand::MOLoad, MemSize, MemSize);
7099 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7100 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007101 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007102 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007103 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7104 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007105
Chris Lattner07290932010-09-22 01:05:16 +00007106 MachineMemOperand *MMO =
7107 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7108 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007109
Evan Cheng0db9fe62006-04-25 20:13:52 +00007110 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007111 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007112 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7113 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007114
Chris Lattner27a6c732007-11-24 07:07:01 +00007115 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007116}
7117
Dan Gohmand858e902010-04-17 15:26:15 +00007118SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7119 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007120 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007121 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007122
Eli Friedman948e95a2009-05-23 09:59:16 +00007123 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007124 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007125 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7126 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007127
Chris Lattner27a6c732007-11-24 07:07:01 +00007128 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007129 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007130 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007131}
7132
Dan Gohmand858e902010-04-17 15:26:15 +00007133SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7134 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007135 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7136 SDValue FIST = Vals.first, StackSlot = Vals.second;
7137 assert(FIST.getNode() && "Unexpected failure");
7138
7139 // Load the result.
7140 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007141 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007142}
7143
Dan Gohmand858e902010-04-17 15:26:15 +00007144SDValue X86TargetLowering::LowerFABS(SDValue Op,
7145 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007146 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007147 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007148 EVT VT = Op.getValueType();
7149 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007150 if (VT.isVector())
7151 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007152 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007153 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007154 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007155 CV.push_back(C);
7156 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007157 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007158 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007159 CV.push_back(C);
7160 CV.push_back(C);
7161 CV.push_back(C);
7162 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007163 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007164 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007165 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007166 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007167 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007168 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007169 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007170}
7171
Dan Gohmand858e902010-04-17 15:26:15 +00007172SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007173 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007174 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007175 EVT VT = Op.getValueType();
7176 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007177 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007178 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007179 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007180 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007181 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007182 CV.push_back(C);
7183 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007184 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007185 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007186 CV.push_back(C);
7187 CV.push_back(C);
7188 CV.push_back(C);
7189 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007190 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007191 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007192 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007193 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007194 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007195 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007196 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007197 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007198 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007199 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007200 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007201 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007202 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007203 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007204 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007205}
7206
Dan Gohmand858e902010-04-17 15:26:15 +00007207SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007208 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007209 SDValue Op0 = Op.getOperand(0);
7210 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007211 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007212 EVT VT = Op.getValueType();
7213 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007214
7215 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007216 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007217 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007218 SrcVT = VT;
7219 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007220 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007221 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007222 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007223 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007224 }
7225
7226 // At this point the operands and the result should have the same
7227 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007228
Evan Cheng68c47cb2007-01-05 07:55:56 +00007229 // First get the sign bit of second operand.
7230 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007231 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007232 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7233 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007234 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007235 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7236 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7237 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7238 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007239 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007240 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007241 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007242 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007243 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007244 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007245 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007246
7247 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007248 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007249 // Op0 is MVT::f32, Op1 is MVT::f64.
7250 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7251 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7252 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007253 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007254 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007255 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007256 }
7257
Evan Cheng73d6cf12007-01-05 21:37:56 +00007258 // Clear first operand sign bit.
7259 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007260 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007261 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7262 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007263 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007264 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7265 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7266 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7267 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007268 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007269 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007270 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007271 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007272 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007273 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007274 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007275
7276 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007277 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007278}
7279
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007280SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7281 SDValue N0 = Op.getOperand(0);
7282 DebugLoc dl = Op.getDebugLoc();
7283 EVT VT = Op.getValueType();
7284
7285 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7286 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7287 DAG.getConstant(1, VT));
7288 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7289}
7290
Dan Gohman076aee32009-03-04 19:44:21 +00007291/// Emit nodes that will be selected as "test Op0,Op0", or something
7292/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007293SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007294 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007295 DebugLoc dl = Op.getDebugLoc();
7296
Dan Gohman31125812009-03-07 01:58:32 +00007297 // CF and OF aren't always set the way we want. Determine which
7298 // of these we need.
7299 bool NeedCF = false;
7300 bool NeedOF = false;
7301 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007302 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007303 case X86::COND_A: case X86::COND_AE:
7304 case X86::COND_B: case X86::COND_BE:
7305 NeedCF = true;
7306 break;
7307 case X86::COND_G: case X86::COND_GE:
7308 case X86::COND_L: case X86::COND_LE:
7309 case X86::COND_O: case X86::COND_NO:
7310 NeedOF = true;
7311 break;
Dan Gohman31125812009-03-07 01:58:32 +00007312 }
7313
Dan Gohman076aee32009-03-04 19:44:21 +00007314 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007315 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7316 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007317 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7318 // Emit a CMP with 0, which is the TEST pattern.
7319 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7320 DAG.getConstant(0, Op.getValueType()));
7321
7322 unsigned Opcode = 0;
7323 unsigned NumOperands = 0;
7324 switch (Op.getNode()->getOpcode()) {
7325 case ISD::ADD:
7326 // Due to an isel shortcoming, be conservative if this add is likely to be
7327 // selected as part of a load-modify-store instruction. When the root node
7328 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7329 // uses of other nodes in the match, such as the ADD in this case. This
7330 // leads to the ADD being left around and reselected, with the result being
7331 // two adds in the output. Alas, even if none our users are stores, that
7332 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7333 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7334 // climbing the DAG back to the root, and it doesn't seem to be worth the
7335 // effort.
7336 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007337 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007338 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7339 goto default_case;
7340
7341 if (ConstantSDNode *C =
7342 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7343 // An add of one will be selected as an INC.
7344 if (C->getAPIntValue() == 1) {
7345 Opcode = X86ISD::INC;
7346 NumOperands = 1;
7347 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007348 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007349
7350 // An add of negative one (subtract of one) will be selected as a DEC.
7351 if (C->getAPIntValue().isAllOnesValue()) {
7352 Opcode = X86ISD::DEC;
7353 NumOperands = 1;
7354 break;
7355 }
Dan Gohman076aee32009-03-04 19:44:21 +00007356 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007357
7358 // Otherwise use a regular EFLAGS-setting add.
7359 Opcode = X86ISD::ADD;
7360 NumOperands = 2;
7361 break;
7362 case ISD::AND: {
7363 // If the primary and result isn't used, don't bother using X86ISD::AND,
7364 // because a TEST instruction will be better.
7365 bool NonFlagUse = false;
7366 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7367 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7368 SDNode *User = *UI;
7369 unsigned UOpNo = UI.getOperandNo();
7370 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7371 // Look pass truncate.
7372 UOpNo = User->use_begin().getOperandNo();
7373 User = *User->use_begin();
7374 }
7375
7376 if (User->getOpcode() != ISD::BRCOND &&
7377 User->getOpcode() != ISD::SETCC &&
7378 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7379 NonFlagUse = true;
7380 break;
7381 }
Dan Gohman076aee32009-03-04 19:44:21 +00007382 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007383
7384 if (!NonFlagUse)
7385 break;
7386 }
7387 // FALL THROUGH
7388 case ISD::SUB:
7389 case ISD::OR:
7390 case ISD::XOR:
7391 // Due to the ISEL shortcoming noted above, be conservative if this op is
7392 // likely to be selected as part of a load-modify-store instruction.
7393 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7394 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7395 if (UI->getOpcode() == ISD::STORE)
7396 goto default_case;
7397
7398 // Otherwise use a regular EFLAGS-setting instruction.
7399 switch (Op.getNode()->getOpcode()) {
7400 default: llvm_unreachable("unexpected operator!");
7401 case ISD::SUB: Opcode = X86ISD::SUB; break;
7402 case ISD::OR: Opcode = X86ISD::OR; break;
7403 case ISD::XOR: Opcode = X86ISD::XOR; break;
7404 case ISD::AND: Opcode = X86ISD::AND; break;
7405 }
7406
7407 NumOperands = 2;
7408 break;
7409 case X86ISD::ADD:
7410 case X86ISD::SUB:
7411 case X86ISD::INC:
7412 case X86ISD::DEC:
7413 case X86ISD::OR:
7414 case X86ISD::XOR:
7415 case X86ISD::AND:
7416 return SDValue(Op.getNode(), 1);
7417 default:
7418 default_case:
7419 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007420 }
7421
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007422 if (Opcode == 0)
7423 // Emit a CMP with 0, which is the TEST pattern.
7424 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7425 DAG.getConstant(0, Op.getValueType()));
7426
7427 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7428 SmallVector<SDValue, 4> Ops;
7429 for (unsigned i = 0; i != NumOperands; ++i)
7430 Ops.push_back(Op.getOperand(i));
7431
7432 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7433 DAG.ReplaceAllUsesWith(Op, New);
7434 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007435}
7436
7437/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7438/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007439SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007440 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007441 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7442 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007443 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007444
7445 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007446 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007447}
7448
Evan Chengd40d03e2010-01-06 19:38:29 +00007449/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7450/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007451SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7452 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007453 SDValue Op0 = And.getOperand(0);
7454 SDValue Op1 = And.getOperand(1);
7455 if (Op0.getOpcode() == ISD::TRUNCATE)
7456 Op0 = Op0.getOperand(0);
7457 if (Op1.getOpcode() == ISD::TRUNCATE)
7458 Op1 = Op1.getOperand(0);
7459
Evan Chengd40d03e2010-01-06 19:38:29 +00007460 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007461 if (Op1.getOpcode() == ISD::SHL)
7462 std::swap(Op0, Op1);
7463 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007464 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7465 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007466 // If we looked past a truncate, check that it's only truncating away
7467 // known zeros.
7468 unsigned BitWidth = Op0.getValueSizeInBits();
7469 unsigned AndBitWidth = And.getValueSizeInBits();
7470 if (BitWidth > AndBitWidth) {
7471 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7472 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7473 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7474 return SDValue();
7475 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007476 LHS = Op1;
7477 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007478 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007479 } else if (Op1.getOpcode() == ISD::Constant) {
7480 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7481 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007482 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7483 LHS = AndLHS.getOperand(0);
7484 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007485 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007486 }
Evan Cheng0488db92007-09-25 01:57:46 +00007487
Evan Chengd40d03e2010-01-06 19:38:29 +00007488 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007489 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007490 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007491 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007492 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007493 // Also promote i16 to i32 for performance / code size reason.
7494 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007495 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007496 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007497
Evan Chengd40d03e2010-01-06 19:38:29 +00007498 // If the operand types disagree, extend the shift amount to match. Since
7499 // BT ignores high bits (like shifts) we can use anyextend.
7500 if (LHS.getValueType() != RHS.getValueType())
7501 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007502
Evan Chengd40d03e2010-01-06 19:38:29 +00007503 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7504 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7505 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7506 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007507 }
7508
Evan Cheng54de3ea2010-01-05 06:52:31 +00007509 return SDValue();
7510}
7511
Dan Gohmand858e902010-04-17 15:26:15 +00007512SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007513 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7514 SDValue Op0 = Op.getOperand(0);
7515 SDValue Op1 = Op.getOperand(1);
7516 DebugLoc dl = Op.getDebugLoc();
7517 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7518
7519 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007520 // Lower (X & (1 << N)) == 0 to BT(X, N).
7521 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7522 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007523 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007524 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007525 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007526 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7527 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7528 if (NewSetCC.getNode())
7529 return NewSetCC;
7530 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007531
Chris Lattner481eebc2010-12-19 21:23:48 +00007532 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7533 // these.
7534 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007535 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007536 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7537 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007538
Chris Lattner481eebc2010-12-19 21:23:48 +00007539 // If the input is a setcc, then reuse the input setcc or use a new one with
7540 // the inverted condition.
7541 if (Op0.getOpcode() == X86ISD::SETCC) {
7542 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7543 bool Invert = (CC == ISD::SETNE) ^
7544 cast<ConstantSDNode>(Op1)->isNullValue();
7545 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007546
Evan Cheng2c755ba2010-02-27 07:36:59 +00007547 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007548 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7549 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7550 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007551 }
7552
Evan Chenge5b51ac2010-04-17 06:13:15 +00007553 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007554 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007555 if (X86CC == X86::COND_INVALID)
7556 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007557
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007558 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007559 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007560 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007561}
7562
Dan Gohmand858e902010-04-17 15:26:15 +00007563SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007564 SDValue Cond;
7565 SDValue Op0 = Op.getOperand(0);
7566 SDValue Op1 = Op.getOperand(1);
7567 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007568 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007569 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7570 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007571 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007572
7573 if (isFP) {
7574 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007575 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007576 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7577 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007578 bool Swap = false;
7579
7580 switch (SetCCOpcode) {
7581 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007582 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007583 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007584 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007585 case ISD::SETGT: Swap = true; // Fallthrough
7586 case ISD::SETLT:
7587 case ISD::SETOLT: SSECC = 1; break;
7588 case ISD::SETOGE:
7589 case ISD::SETGE: Swap = true; // Fallthrough
7590 case ISD::SETLE:
7591 case ISD::SETOLE: SSECC = 2; break;
7592 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007593 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007594 case ISD::SETNE: SSECC = 4; break;
7595 case ISD::SETULE: Swap = true;
7596 case ISD::SETUGE: SSECC = 5; break;
7597 case ISD::SETULT: Swap = true;
7598 case ISD::SETUGT: SSECC = 6; break;
7599 case ISD::SETO: SSECC = 7; break;
7600 }
7601 if (Swap)
7602 std::swap(Op0, Op1);
7603
Nate Begemanfb8ead02008-07-25 19:05:58 +00007604 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007605 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007606 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007607 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007608 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7609 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007610 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007611 }
7612 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007613 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007614 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7615 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007616 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007617 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007618 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007619 }
7620 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007621 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007622 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007623
Nate Begeman30a0de92008-07-17 16:51:19 +00007624 // We are handling one of the integer comparisons here. Since SSE only has
7625 // GT and EQ comparisons for integer, swapping operands and multiple
7626 // operations may be required for some comparisons.
7627 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7628 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007629
Owen Anderson825b72b2009-08-11 20:47:22 +00007630 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007631 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007632 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007633 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007634 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7635 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007636 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007637
Nate Begeman30a0de92008-07-17 16:51:19 +00007638 switch (SetCCOpcode) {
7639 default: break;
7640 case ISD::SETNE: Invert = true;
7641 case ISD::SETEQ: Opc = EQOpc; break;
7642 case ISD::SETLT: Swap = true;
7643 case ISD::SETGT: Opc = GTOpc; break;
7644 case ISD::SETGE: Swap = true;
7645 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7646 case ISD::SETULT: Swap = true;
7647 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7648 case ISD::SETUGE: Swap = true;
7649 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7650 }
7651 if (Swap)
7652 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007653
Nate Begeman30a0de92008-07-17 16:51:19 +00007654 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7655 // bits of the inputs before performing those operations.
7656 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007657 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007658 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7659 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007660 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007661 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7662 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007663 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7664 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007665 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007666
Dale Johannesenace16102009-02-03 19:33:06 +00007667 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007668
7669 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007670 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007671 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007672
Nate Begeman30a0de92008-07-17 16:51:19 +00007673 return Result;
7674}
Evan Cheng0488db92007-09-25 01:57:46 +00007675
Evan Cheng370e5342008-12-03 08:38:43 +00007676// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007677static bool isX86LogicalCmp(SDValue Op) {
7678 unsigned Opc = Op.getNode()->getOpcode();
7679 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7680 return true;
7681 if (Op.getResNo() == 1 &&
7682 (Opc == X86ISD::ADD ||
7683 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007684 Opc == X86ISD::ADC ||
7685 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007686 Opc == X86ISD::SMUL ||
7687 Opc == X86ISD::UMUL ||
7688 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007689 Opc == X86ISD::DEC ||
7690 Opc == X86ISD::OR ||
7691 Opc == X86ISD::XOR ||
7692 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007693 return true;
7694
Chris Lattner9637d5b2010-12-05 07:49:54 +00007695 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7696 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007697
Dan Gohman076aee32009-03-04 19:44:21 +00007698 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007699}
7700
Chris Lattnera2b56002010-12-05 01:23:24 +00007701static bool isZero(SDValue V) {
7702 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7703 return C && C->isNullValue();
7704}
7705
Chris Lattner96908b12010-12-05 02:00:51 +00007706static bool isAllOnes(SDValue V) {
7707 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7708 return C && C->isAllOnesValue();
7709}
7710
Dan Gohmand858e902010-04-17 15:26:15 +00007711SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007712 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007713 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007714 SDValue Op1 = Op.getOperand(1);
7715 SDValue Op2 = Op.getOperand(2);
7716 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007717 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007718
Dan Gohman1a492952009-10-20 16:22:37 +00007719 if (Cond.getOpcode() == ISD::SETCC) {
7720 SDValue NewCond = LowerSETCC(Cond, DAG);
7721 if (NewCond.getNode())
7722 Cond = NewCond;
7723 }
Evan Cheng734503b2006-09-11 02:19:56 +00007724
Chris Lattnera2b56002010-12-05 01:23:24 +00007725 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007726 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007727 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007728 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007729 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007730 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7731 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007732 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007733
Chris Lattnera2b56002010-12-05 01:23:24 +00007734 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007735
7736 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007737 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7738 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007739
7740 SDValue CmpOp0 = Cmp.getOperand(0);
7741 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7742 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007743
Chris Lattner96908b12010-12-05 02:00:51 +00007744 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007745 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7746 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007747
Chris Lattner96908b12010-12-05 02:00:51 +00007748 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7749 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007750
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007751 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007752 if (N2C == 0 || !N2C->isNullValue())
7753 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7754 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007755 }
7756 }
7757
Chris Lattnera2b56002010-12-05 01:23:24 +00007758 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007759 if (Cond.getOpcode() == ISD::AND &&
7760 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7761 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007762 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007763 Cond = Cond.getOperand(0);
7764 }
7765
Evan Cheng3f41d662007-10-08 22:16:29 +00007766 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7767 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007768 if (Cond.getOpcode() == X86ISD::SETCC ||
7769 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007770 CC = Cond.getOperand(0);
7771
Dan Gohman475871a2008-07-27 21:46:04 +00007772 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007773 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007774 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007775
Evan Cheng3f41d662007-10-08 22:16:29 +00007776 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007777 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007778 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007779 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007780
Chris Lattnerd1980a52009-03-12 06:52:53 +00007781 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7782 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007783 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007784 addTest = false;
7785 }
7786 }
7787
7788 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007789 // Look pass the truncate.
7790 if (Cond.getOpcode() == ISD::TRUNCATE)
7791 Cond = Cond.getOperand(0);
7792
7793 // We know the result of AND is compared against zero. Try to match
7794 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007795 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007796 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007797 if (NewSetCC.getNode()) {
7798 CC = NewSetCC.getOperand(0);
7799 Cond = NewSetCC.getOperand(1);
7800 addTest = false;
7801 }
7802 }
7803 }
7804
7805 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007806 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007807 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007808 }
7809
Benjamin Kramere915ff32010-12-22 23:09:28 +00007810 // a < b ? -1 : 0 -> RES = ~setcc_carry
7811 // a < b ? 0 : -1 -> RES = setcc_carry
7812 // a >= b ? -1 : 0 -> RES = setcc_carry
7813 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7814 if (Cond.getOpcode() == X86ISD::CMP) {
7815 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7816
7817 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7818 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7819 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7820 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7821 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7822 return DAG.getNOT(DL, Res, Res.getValueType());
7823 return Res;
7824 }
7825 }
7826
Evan Cheng0488db92007-09-25 01:57:46 +00007827 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7828 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007829 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007830 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007831 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007832}
7833
Evan Cheng370e5342008-12-03 08:38:43 +00007834// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7835// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7836// from the AND / OR.
7837static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7838 Opc = Op.getOpcode();
7839 if (Opc != ISD::OR && Opc != ISD::AND)
7840 return false;
7841 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7842 Op.getOperand(0).hasOneUse() &&
7843 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7844 Op.getOperand(1).hasOneUse());
7845}
7846
Evan Cheng961d6d42009-02-02 08:19:07 +00007847// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7848// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007849static bool isXor1OfSetCC(SDValue Op) {
7850 if (Op.getOpcode() != ISD::XOR)
7851 return false;
7852 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7853 if (N1C && N1C->getAPIntValue() == 1) {
7854 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7855 Op.getOperand(0).hasOneUse();
7856 }
7857 return false;
7858}
7859
Dan Gohmand858e902010-04-17 15:26:15 +00007860SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007861 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007862 SDValue Chain = Op.getOperand(0);
7863 SDValue Cond = Op.getOperand(1);
7864 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007865 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007866 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007867
Dan Gohman1a492952009-10-20 16:22:37 +00007868 if (Cond.getOpcode() == ISD::SETCC) {
7869 SDValue NewCond = LowerSETCC(Cond, DAG);
7870 if (NewCond.getNode())
7871 Cond = NewCond;
7872 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007873#if 0
7874 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007875 else if (Cond.getOpcode() == X86ISD::ADD ||
7876 Cond.getOpcode() == X86ISD::SUB ||
7877 Cond.getOpcode() == X86ISD::SMUL ||
7878 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007879 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007880#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007881
Evan Chengad9c0a32009-12-15 00:53:42 +00007882 // Look pass (and (setcc_carry (cmp ...)), 1).
7883 if (Cond.getOpcode() == ISD::AND &&
7884 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7885 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007886 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007887 Cond = Cond.getOperand(0);
7888 }
7889
Evan Cheng3f41d662007-10-08 22:16:29 +00007890 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7891 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007892 if (Cond.getOpcode() == X86ISD::SETCC ||
7893 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007894 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007895
Dan Gohman475871a2008-07-27 21:46:04 +00007896 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007897 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007898 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007899 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007900 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007901 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007902 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007903 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007904 default: break;
7905 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007906 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007907 // These can only come from an arithmetic instruction with overflow,
7908 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007909 Cond = Cond.getNode()->getOperand(1);
7910 addTest = false;
7911 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007912 }
Evan Cheng0488db92007-09-25 01:57:46 +00007913 }
Evan Cheng370e5342008-12-03 08:38:43 +00007914 } else {
7915 unsigned CondOpc;
7916 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7917 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007918 if (CondOpc == ISD::OR) {
7919 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7920 // two branches instead of an explicit OR instruction with a
7921 // separate test.
7922 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007923 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007924 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007925 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007926 Chain, Dest, CC, Cmp);
7927 CC = Cond.getOperand(1).getOperand(0);
7928 Cond = Cmp;
7929 addTest = false;
7930 }
7931 } else { // ISD::AND
7932 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7933 // two branches instead of an explicit AND instruction with a
7934 // separate test. However, we only do this if this block doesn't
7935 // have a fall-through edge, because this requires an explicit
7936 // jmp when the condition is false.
7937 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007938 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007939 Op.getNode()->hasOneUse()) {
7940 X86::CondCode CCode =
7941 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7942 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007943 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007944 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007945 // Look for an unconditional branch following this conditional branch.
7946 // We need this because we need to reverse the successors in order
7947 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007948 if (User->getOpcode() == ISD::BR) {
7949 SDValue FalseBB = User->getOperand(1);
7950 SDNode *NewBR =
7951 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007952 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007953 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007954 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007955
Dale Johannesene4d209d2009-02-03 20:21:25 +00007956 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007957 Chain, Dest, CC, Cmp);
7958 X86::CondCode CCode =
7959 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7960 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007961 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007962 Cond = Cmp;
7963 addTest = false;
7964 }
7965 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007966 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007967 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7968 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7969 // It should be transformed during dag combiner except when the condition
7970 // is set by a arithmetics with overflow node.
7971 X86::CondCode CCode =
7972 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7973 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007974 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007975 Cond = Cond.getOperand(0).getOperand(1);
7976 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007977 }
Evan Cheng0488db92007-09-25 01:57:46 +00007978 }
7979
7980 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007981 // Look pass the truncate.
7982 if (Cond.getOpcode() == ISD::TRUNCATE)
7983 Cond = Cond.getOperand(0);
7984
7985 // We know the result of AND is compared against zero. Try to match
7986 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007987 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007988 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7989 if (NewSetCC.getNode()) {
7990 CC = NewSetCC.getOperand(0);
7991 Cond = NewSetCC.getOperand(1);
7992 addTest = false;
7993 }
7994 }
7995 }
7996
7997 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007998 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007999 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008000 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008001 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008002 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008003}
8004
Anton Korobeynikove060b532007-04-17 19:34:00 +00008005
8006// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8007// Calls to _alloca is needed to probe the stack when allocating more than 4k
8008// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8009// that the guard pages used by the OS virtual memory manager are allocated in
8010// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008011SDValue
8012X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008013 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008014 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008015 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008016 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008017 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008018
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008019 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008020 SDValue Chain = Op.getOperand(0);
8021 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008022 // FIXME: Ensure alignment here
8023
Dan Gohman475871a2008-07-27 21:46:04 +00008024 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008025
Owen Anderson825b72b2009-08-11 20:47:22 +00008026 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008027 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008028
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008029 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008030 Flag = Chain.getValue(1);
8031
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008032 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008033
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008034 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008035 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008036
Dale Johannesendd64c412009-02-04 00:33:20 +00008037 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008038
Dan Gohman475871a2008-07-27 21:46:04 +00008039 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008040 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008041}
8042
Dan Gohmand858e902010-04-17 15:26:15 +00008043SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008044 MachineFunction &MF = DAG.getMachineFunction();
8045 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8046
Dan Gohman69de1932008-02-06 22:27:42 +00008047 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008048 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008049
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008050 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008051 // vastart just stores the address of the VarArgsFrameIndex slot into the
8052 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008053 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8054 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008055 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8056 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008057 }
8058
8059 // __va_list_tag:
8060 // gp_offset (0 - 6 * 8)
8061 // fp_offset (48 - 48 + 8 * 16)
8062 // overflow_arg_area (point to parameters coming in memory).
8063 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008064 SmallVector<SDValue, 8> MemOps;
8065 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008066 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008067 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008068 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8069 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008070 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008071 MemOps.push_back(Store);
8072
8073 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008074 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008075 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008076 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008077 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8078 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008079 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008080 MemOps.push_back(Store);
8081
8082 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008083 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008084 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008085 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8086 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008087 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8088 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008089 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008090 MemOps.push_back(Store);
8091
8092 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008093 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008094 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008095 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8096 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008097 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8098 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008099 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008100 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008101 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008102}
8103
Dan Gohmand858e902010-04-17 15:26:15 +00008104SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008105 assert(Subtarget->is64Bit() &&
8106 "LowerVAARG only handles 64-bit va_arg!");
8107 assert((Subtarget->isTargetLinux() ||
8108 Subtarget->isTargetDarwin()) &&
8109 "Unhandled target in LowerVAARG");
8110 assert(Op.getNode()->getNumOperands() == 4);
8111 SDValue Chain = Op.getOperand(0);
8112 SDValue SrcPtr = Op.getOperand(1);
8113 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8114 unsigned Align = Op.getConstantOperandVal(3);
8115 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008116
Dan Gohman320afb82010-10-12 18:00:49 +00008117 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008118 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00008119 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8120 uint8_t ArgMode;
8121
8122 // Decide which area this value should be read from.
8123 // TODO: Implement the AMD64 ABI in its entirety. This simple
8124 // selection mechanism works only for the basic types.
8125 if (ArgVT == MVT::f80) {
8126 llvm_unreachable("va_arg for f80 not yet implemented");
8127 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8128 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8129 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8130 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8131 } else {
8132 llvm_unreachable("Unhandled argument type in LowerVAARG");
8133 }
8134
8135 if (ArgMode == 2) {
8136 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008137 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008138 !(DAG.getMachineFunction()
8139 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008140 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008141 }
8142
8143 // Insert VAARG_64 node into the DAG
8144 // VAARG_64 returns two values: Variable Argument Address, Chain
8145 SmallVector<SDValue, 11> InstOps;
8146 InstOps.push_back(Chain);
8147 InstOps.push_back(SrcPtr);
8148 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8149 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8150 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8151 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8152 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8153 VTs, &InstOps[0], InstOps.size(),
8154 MVT::i64,
8155 MachinePointerInfo(SV),
8156 /*Align=*/0,
8157 /*Volatile=*/false,
8158 /*ReadMem=*/true,
8159 /*WriteMem=*/true);
8160 Chain = VAARG.getValue(1);
8161
8162 // Load the next argument and return it
8163 return DAG.getLoad(ArgVT, dl,
8164 Chain,
8165 VAARG,
8166 MachinePointerInfo(),
8167 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008168}
8169
Dan Gohmand858e902010-04-17 15:26:15 +00008170SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008171 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008172 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008173 SDValue Chain = Op.getOperand(0);
8174 SDValue DstPtr = Op.getOperand(1);
8175 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008176 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8177 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008178 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008179
Chris Lattnere72f2022010-09-21 05:40:29 +00008180 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008181 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008182 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008183 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008184}
8185
Dan Gohman475871a2008-07-27 21:46:04 +00008186SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008187X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008188 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008189 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008190 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008191 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008192 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008193 case Intrinsic::x86_sse_comieq_ss:
8194 case Intrinsic::x86_sse_comilt_ss:
8195 case Intrinsic::x86_sse_comile_ss:
8196 case Intrinsic::x86_sse_comigt_ss:
8197 case Intrinsic::x86_sse_comige_ss:
8198 case Intrinsic::x86_sse_comineq_ss:
8199 case Intrinsic::x86_sse_ucomieq_ss:
8200 case Intrinsic::x86_sse_ucomilt_ss:
8201 case Intrinsic::x86_sse_ucomile_ss:
8202 case Intrinsic::x86_sse_ucomigt_ss:
8203 case Intrinsic::x86_sse_ucomige_ss:
8204 case Intrinsic::x86_sse_ucomineq_ss:
8205 case Intrinsic::x86_sse2_comieq_sd:
8206 case Intrinsic::x86_sse2_comilt_sd:
8207 case Intrinsic::x86_sse2_comile_sd:
8208 case Intrinsic::x86_sse2_comigt_sd:
8209 case Intrinsic::x86_sse2_comige_sd:
8210 case Intrinsic::x86_sse2_comineq_sd:
8211 case Intrinsic::x86_sse2_ucomieq_sd:
8212 case Intrinsic::x86_sse2_ucomilt_sd:
8213 case Intrinsic::x86_sse2_ucomile_sd:
8214 case Intrinsic::x86_sse2_ucomigt_sd:
8215 case Intrinsic::x86_sse2_ucomige_sd:
8216 case Intrinsic::x86_sse2_ucomineq_sd: {
8217 unsigned Opc = 0;
8218 ISD::CondCode CC = ISD::SETCC_INVALID;
8219 switch (IntNo) {
8220 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008221 case Intrinsic::x86_sse_comieq_ss:
8222 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008223 Opc = X86ISD::COMI;
8224 CC = ISD::SETEQ;
8225 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008226 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008227 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008228 Opc = X86ISD::COMI;
8229 CC = ISD::SETLT;
8230 break;
8231 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008232 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008233 Opc = X86ISD::COMI;
8234 CC = ISD::SETLE;
8235 break;
8236 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008237 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008238 Opc = X86ISD::COMI;
8239 CC = ISD::SETGT;
8240 break;
8241 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008242 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008243 Opc = X86ISD::COMI;
8244 CC = ISD::SETGE;
8245 break;
8246 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008247 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008248 Opc = X86ISD::COMI;
8249 CC = ISD::SETNE;
8250 break;
8251 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008252 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008253 Opc = X86ISD::UCOMI;
8254 CC = ISD::SETEQ;
8255 break;
8256 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008257 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008258 Opc = X86ISD::UCOMI;
8259 CC = ISD::SETLT;
8260 break;
8261 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008262 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008263 Opc = X86ISD::UCOMI;
8264 CC = ISD::SETLE;
8265 break;
8266 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008267 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008268 Opc = X86ISD::UCOMI;
8269 CC = ISD::SETGT;
8270 break;
8271 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008272 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008273 Opc = X86ISD::UCOMI;
8274 CC = ISD::SETGE;
8275 break;
8276 case Intrinsic::x86_sse_ucomineq_ss:
8277 case Intrinsic::x86_sse2_ucomineq_sd:
8278 Opc = X86ISD::UCOMI;
8279 CC = ISD::SETNE;
8280 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008281 }
Evan Cheng734503b2006-09-11 02:19:56 +00008282
Dan Gohman475871a2008-07-27 21:46:04 +00008283 SDValue LHS = Op.getOperand(1);
8284 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008285 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008286 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008287 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8288 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8289 DAG.getConstant(X86CC, MVT::i8), Cond);
8290 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008291 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008292 // ptest and testp intrinsics. The intrinsic these come from are designed to
8293 // return an integer value, not just an instruction so lower it to the ptest
8294 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008295 case Intrinsic::x86_sse41_ptestz:
8296 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008297 case Intrinsic::x86_sse41_ptestnzc:
8298 case Intrinsic::x86_avx_ptestz_256:
8299 case Intrinsic::x86_avx_ptestc_256:
8300 case Intrinsic::x86_avx_ptestnzc_256:
8301 case Intrinsic::x86_avx_vtestz_ps:
8302 case Intrinsic::x86_avx_vtestc_ps:
8303 case Intrinsic::x86_avx_vtestnzc_ps:
8304 case Intrinsic::x86_avx_vtestz_pd:
8305 case Intrinsic::x86_avx_vtestc_pd:
8306 case Intrinsic::x86_avx_vtestnzc_pd:
8307 case Intrinsic::x86_avx_vtestz_ps_256:
8308 case Intrinsic::x86_avx_vtestc_ps_256:
8309 case Intrinsic::x86_avx_vtestnzc_ps_256:
8310 case Intrinsic::x86_avx_vtestz_pd_256:
8311 case Intrinsic::x86_avx_vtestc_pd_256:
8312 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8313 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008314 unsigned X86CC = 0;
8315 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008316 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008317 case Intrinsic::x86_avx_vtestz_ps:
8318 case Intrinsic::x86_avx_vtestz_pd:
8319 case Intrinsic::x86_avx_vtestz_ps_256:
8320 case Intrinsic::x86_avx_vtestz_pd_256:
8321 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008322 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008323 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008324 // ZF = 1
8325 X86CC = X86::COND_E;
8326 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008327 case Intrinsic::x86_avx_vtestc_ps:
8328 case Intrinsic::x86_avx_vtestc_pd:
8329 case Intrinsic::x86_avx_vtestc_ps_256:
8330 case Intrinsic::x86_avx_vtestc_pd_256:
8331 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008332 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008333 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008334 // CF = 1
8335 X86CC = X86::COND_B;
8336 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008337 case Intrinsic::x86_avx_vtestnzc_ps:
8338 case Intrinsic::x86_avx_vtestnzc_pd:
8339 case Intrinsic::x86_avx_vtestnzc_ps_256:
8340 case Intrinsic::x86_avx_vtestnzc_pd_256:
8341 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008342 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008343 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008344 // ZF and CF = 0
8345 X86CC = X86::COND_A;
8346 break;
8347 }
Eric Christopherfd179292009-08-27 18:07:15 +00008348
Eric Christopher71c67532009-07-29 00:28:05 +00008349 SDValue LHS = Op.getOperand(1);
8350 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008351 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8352 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008353 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8354 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8355 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008356 }
Evan Cheng5759f972008-05-04 09:15:50 +00008357
8358 // Fix vector shift instructions where the last operand is a non-immediate
8359 // i32 value.
8360 case Intrinsic::x86_sse2_pslli_w:
8361 case Intrinsic::x86_sse2_pslli_d:
8362 case Intrinsic::x86_sse2_pslli_q:
8363 case Intrinsic::x86_sse2_psrli_w:
8364 case Intrinsic::x86_sse2_psrli_d:
8365 case Intrinsic::x86_sse2_psrli_q:
8366 case Intrinsic::x86_sse2_psrai_w:
8367 case Intrinsic::x86_sse2_psrai_d:
8368 case Intrinsic::x86_mmx_pslli_w:
8369 case Intrinsic::x86_mmx_pslli_d:
8370 case Intrinsic::x86_mmx_pslli_q:
8371 case Intrinsic::x86_mmx_psrli_w:
8372 case Intrinsic::x86_mmx_psrli_d:
8373 case Intrinsic::x86_mmx_psrli_q:
8374 case Intrinsic::x86_mmx_psrai_w:
8375 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008376 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008377 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008378 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008379
8380 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008381 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008382 switch (IntNo) {
8383 case Intrinsic::x86_sse2_pslli_w:
8384 NewIntNo = Intrinsic::x86_sse2_psll_w;
8385 break;
8386 case Intrinsic::x86_sse2_pslli_d:
8387 NewIntNo = Intrinsic::x86_sse2_psll_d;
8388 break;
8389 case Intrinsic::x86_sse2_pslli_q:
8390 NewIntNo = Intrinsic::x86_sse2_psll_q;
8391 break;
8392 case Intrinsic::x86_sse2_psrli_w:
8393 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8394 break;
8395 case Intrinsic::x86_sse2_psrli_d:
8396 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8397 break;
8398 case Intrinsic::x86_sse2_psrli_q:
8399 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8400 break;
8401 case Intrinsic::x86_sse2_psrai_w:
8402 NewIntNo = Intrinsic::x86_sse2_psra_w;
8403 break;
8404 case Intrinsic::x86_sse2_psrai_d:
8405 NewIntNo = Intrinsic::x86_sse2_psra_d;
8406 break;
8407 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008408 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008409 switch (IntNo) {
8410 case Intrinsic::x86_mmx_pslli_w:
8411 NewIntNo = Intrinsic::x86_mmx_psll_w;
8412 break;
8413 case Intrinsic::x86_mmx_pslli_d:
8414 NewIntNo = Intrinsic::x86_mmx_psll_d;
8415 break;
8416 case Intrinsic::x86_mmx_pslli_q:
8417 NewIntNo = Intrinsic::x86_mmx_psll_q;
8418 break;
8419 case Intrinsic::x86_mmx_psrli_w:
8420 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8421 break;
8422 case Intrinsic::x86_mmx_psrli_d:
8423 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8424 break;
8425 case Intrinsic::x86_mmx_psrli_q:
8426 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8427 break;
8428 case Intrinsic::x86_mmx_psrai_w:
8429 NewIntNo = Intrinsic::x86_mmx_psra_w;
8430 break;
8431 case Intrinsic::x86_mmx_psrai_d:
8432 NewIntNo = Intrinsic::x86_mmx_psra_d;
8433 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008434 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008435 }
8436 break;
8437 }
8438 }
Mon P Wangefa42202009-09-03 19:56:25 +00008439
8440 // The vector shift intrinsics with scalars uses 32b shift amounts but
8441 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8442 // to be zero.
8443 SDValue ShOps[4];
8444 ShOps[0] = ShAmt;
8445 ShOps[1] = DAG.getConstant(0, MVT::i32);
8446 if (ShAmtVT == MVT::v4i32) {
8447 ShOps[2] = DAG.getUNDEF(MVT::i32);
8448 ShOps[3] = DAG.getUNDEF(MVT::i32);
8449 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8450 } else {
8451 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008452// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008453 }
8454
Owen Andersone50ed302009-08-10 22:56:29 +00008455 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008456 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008457 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008458 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008459 Op.getOperand(1), ShAmt);
8460 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008461 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008462}
Evan Cheng72261582005-12-20 06:22:03 +00008463
Dan Gohmand858e902010-04-17 15:26:15 +00008464SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8465 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008466 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8467 MFI->setReturnAddressIsTaken(true);
8468
Bill Wendling64e87322009-01-16 19:25:27 +00008469 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008470 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008471
8472 if (Depth > 0) {
8473 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8474 SDValue Offset =
8475 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008476 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008477 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008478 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008479 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008480 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008481 }
8482
8483 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008484 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008485 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008486 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008487}
8488
Dan Gohmand858e902010-04-17 15:26:15 +00008489SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008490 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8491 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008492
Owen Andersone50ed302009-08-10 22:56:29 +00008493 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008494 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008495 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8496 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008497 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008498 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008499 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8500 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008501 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008502 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008503}
8504
Dan Gohman475871a2008-07-27 21:46:04 +00008505SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008506 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008507 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008508}
8509
Dan Gohmand858e902010-04-17 15:26:15 +00008510SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008511 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008512 SDValue Chain = Op.getOperand(0);
8513 SDValue Offset = Op.getOperand(1);
8514 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008515 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008516
Dan Gohmand8816272010-08-11 18:14:00 +00008517 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8518 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8519 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008520 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008521
Dan Gohmand8816272010-08-11 18:14:00 +00008522 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8523 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008524 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008525 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8526 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008527 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008528 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008529
Dale Johannesene4d209d2009-02-03 20:21:25 +00008530 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008531 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008532 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008533}
8534
Dan Gohman475871a2008-07-27 21:46:04 +00008535SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008536 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008537 SDValue Root = Op.getOperand(0);
8538 SDValue Trmp = Op.getOperand(1); // trampoline
8539 SDValue FPtr = Op.getOperand(2); // nested function
8540 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008541 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008542
Dan Gohman69de1932008-02-06 22:27:42 +00008543 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008544
8545 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008546 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008547
8548 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008549 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8550 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008551
Evan Cheng0e6a0522011-07-18 20:57:22 +00008552 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
8553 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008554
8555 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8556
8557 // Load the pointer to the nested function into R11.
8558 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008559 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008560 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008561 Addr, MachinePointerInfo(TrmpAddr),
8562 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008563
Owen Anderson825b72b2009-08-11 20:47:22 +00008564 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8565 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008566 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8567 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008568 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008569
8570 // Load the 'nest' parameter value into R10.
8571 // R10 is specified in X86CallingConv.td
8572 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008573 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8574 DAG.getConstant(10, MVT::i64));
8575 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008576 Addr, MachinePointerInfo(TrmpAddr, 10),
8577 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008578
Owen Anderson825b72b2009-08-11 20:47:22 +00008579 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8580 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008581 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8582 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008583 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008584
8585 // Jump to the nested function.
8586 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008587 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8588 DAG.getConstant(20, MVT::i64));
8589 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008590 Addr, MachinePointerInfo(TrmpAddr, 20),
8591 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008592
8593 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008594 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8595 DAG.getConstant(22, MVT::i64));
8596 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008597 MachinePointerInfo(TrmpAddr, 22),
8598 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008599
Dan Gohman475871a2008-07-27 21:46:04 +00008600 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008601 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008602 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008603 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008604 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008605 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008606 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008607 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008608
8609 switch (CC) {
8610 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008611 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008612 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008613 case CallingConv::X86_StdCall: {
8614 // Pass 'nest' parameter in ECX.
8615 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008616 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008617
8618 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008619 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008620 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008621
Chris Lattner58d74912008-03-12 17:45:29 +00008622 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008623 unsigned InRegCount = 0;
8624 unsigned Idx = 1;
8625
8626 for (FunctionType::param_iterator I = FTy->param_begin(),
8627 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008628 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008629 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008630 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008631
8632 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008633 report_fatal_error("Nest register in use - reduce number of inreg"
8634 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008635 }
8636 }
8637 break;
8638 }
8639 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008640 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008641 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008642 // Pass 'nest' parameter in EAX.
8643 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008644 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008645 break;
8646 }
8647
Dan Gohman475871a2008-07-27 21:46:04 +00008648 SDValue OutChains[4];
8649 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008650
Owen Anderson825b72b2009-08-11 20:47:22 +00008651 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8652 DAG.getConstant(10, MVT::i32));
8653 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008654
Chris Lattnera62fe662010-02-05 19:20:30 +00008655 // This is storing the opcode for MOV32ri.
8656 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00008657 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008658 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008659 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008660 Trmp, MachinePointerInfo(TrmpAddr),
8661 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008662
Owen Anderson825b72b2009-08-11 20:47:22 +00008663 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8664 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008665 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8666 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008667 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008668
Chris Lattnera62fe662010-02-05 19:20:30 +00008669 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008670 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8671 DAG.getConstant(5, MVT::i32));
8672 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008673 MachinePointerInfo(TrmpAddr, 5),
8674 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008675
Owen Anderson825b72b2009-08-11 20:47:22 +00008676 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8677 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008678 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8679 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008680 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008681
Dan Gohman475871a2008-07-27 21:46:04 +00008682 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008683 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008684 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008685 }
8686}
8687
Dan Gohmand858e902010-04-17 15:26:15 +00008688SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8689 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008690 /*
8691 The rounding mode is in bits 11:10 of FPSR, and has the following
8692 settings:
8693 00 Round to nearest
8694 01 Round to -inf
8695 10 Round to +inf
8696 11 Round to 0
8697
8698 FLT_ROUNDS, on the other hand, expects the following:
8699 -1 Undefined
8700 0 Round to 0
8701 1 Round to nearest
8702 2 Round to +inf
8703 3 Round to -inf
8704
8705 To perform the conversion, we do:
8706 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8707 */
8708
8709 MachineFunction &MF = DAG.getMachineFunction();
8710 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008711 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008712 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008713 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008714 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008715
8716 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008717 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008718 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008719
Michael J. Spencerec38de22010-10-10 22:04:20 +00008720
Chris Lattner2156b792010-09-22 01:11:26 +00008721 MachineMemOperand *MMO =
8722 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8723 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008724
Chris Lattner2156b792010-09-22 01:11:26 +00008725 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8726 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8727 DAG.getVTList(MVT::Other),
8728 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008729
8730 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008731 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008732 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008733
8734 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008735 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008736 DAG.getNode(ISD::SRL, DL, MVT::i16,
8737 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008738 CWD, DAG.getConstant(0x800, MVT::i16)),
8739 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008740 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008741 DAG.getNode(ISD::SRL, DL, MVT::i16,
8742 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008743 CWD, DAG.getConstant(0x400, MVT::i16)),
8744 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008745
Dan Gohman475871a2008-07-27 21:46:04 +00008746 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008747 DAG.getNode(ISD::AND, DL, MVT::i16,
8748 DAG.getNode(ISD::ADD, DL, MVT::i16,
8749 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008750 DAG.getConstant(1, MVT::i16)),
8751 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008752
8753
Duncan Sands83ec4b62008-06-06 12:08:01 +00008754 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008755 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008756}
8757
Dan Gohmand858e902010-04-17 15:26:15 +00008758SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008759 EVT VT = Op.getValueType();
8760 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008761 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008762 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008763
8764 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008765 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008766 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008767 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008768 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008769 }
Evan Cheng18efe262007-12-14 02:13:44 +00008770
Evan Cheng152804e2007-12-14 08:30:15 +00008771 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008772 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008773 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008774
8775 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008776 SDValue Ops[] = {
8777 Op,
8778 DAG.getConstant(NumBits+NumBits-1, OpVT),
8779 DAG.getConstant(X86::COND_E, MVT::i8),
8780 Op.getValue(1)
8781 };
8782 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008783
8784 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008785 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008786
Owen Anderson825b72b2009-08-11 20:47:22 +00008787 if (VT == MVT::i8)
8788 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008789 return Op;
8790}
8791
Dan Gohmand858e902010-04-17 15:26:15 +00008792SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008793 EVT VT = Op.getValueType();
8794 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008795 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008796 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008797
8798 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008799 if (VT == MVT::i8) {
8800 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008801 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008802 }
Evan Cheng152804e2007-12-14 08:30:15 +00008803
8804 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008805 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008806 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008807
8808 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008809 SDValue Ops[] = {
8810 Op,
8811 DAG.getConstant(NumBits, OpVT),
8812 DAG.getConstant(X86::COND_E, MVT::i8),
8813 Op.getValue(1)
8814 };
8815 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008816
Owen Anderson825b72b2009-08-11 20:47:22 +00008817 if (VT == MVT::i8)
8818 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008819 return Op;
8820}
8821
Dan Gohmand858e902010-04-17 15:26:15 +00008822SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008823 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008824 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008825 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008826
Mon P Wangaf9b9522008-12-18 21:42:19 +00008827 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8828 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8829 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8830 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8831 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8832 //
8833 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8834 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8835 // return AloBlo + AloBhi + AhiBlo;
8836
8837 SDValue A = Op.getOperand(0);
8838 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008839
Dale Johannesene4d209d2009-02-03 20:21:25 +00008840 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008841 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8842 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008843 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008844 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8845 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008846 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008847 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008848 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008849 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008850 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008851 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008852 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008853 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008854 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008855 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008856 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8857 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008858 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008859 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8860 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008861 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8862 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008863 return Res;
8864}
8865
Nadav Rotem43012222011-05-11 08:12:09 +00008866SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8867
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008868 EVT VT = Op.getValueType();
8869 DebugLoc dl = Op.getDebugLoc();
8870 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008871 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008872
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008873 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008874
Nadav Rotem43012222011-05-11 08:12:09 +00008875 // Must have SSE2.
8876 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008877
Nadav Rotem43012222011-05-11 08:12:09 +00008878 // Optimize shl/srl/sra with constant shift amount.
8879 if (isSplatVector(Amt.getNode())) {
8880 SDValue SclrAmt = Amt->getOperand(0);
8881 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8882 uint64_t ShiftAmt = C->getZExtValue();
8883
8884 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8885 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8886 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8887 R, DAG.getConstant(ShiftAmt, MVT::i32));
8888
8889 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8890 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8891 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8892 R, DAG.getConstant(ShiftAmt, MVT::i32));
8893
8894 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8895 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8896 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8897 R, DAG.getConstant(ShiftAmt, MVT::i32));
8898
8899 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8900 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8901 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8902 R, DAG.getConstant(ShiftAmt, MVT::i32));
8903
8904 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8905 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8906 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8907 R, DAG.getConstant(ShiftAmt, MVT::i32));
8908
8909 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8910 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8911 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8912 R, DAG.getConstant(ShiftAmt, MVT::i32));
8913
8914 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8915 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8916 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8917 R, DAG.getConstant(ShiftAmt, MVT::i32));
8918
8919 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8920 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8921 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8922 R, DAG.getConstant(ShiftAmt, MVT::i32));
8923 }
8924 }
8925
8926 // Lower SHL with variable shift amount.
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00008927 // Cannot lower SHL without SSE2 or later.
8928 if (!Subtarget->hasSSE2()) return SDValue();
Nadav Rotem43012222011-05-11 08:12:09 +00008929
8930 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008931 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8932 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8933 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8934
8935 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008936
Nate Begeman51409212010-07-28 00:21:48 +00008937 std::vector<Constant*> CV(4, CI);
8938 Constant *C = ConstantVector::get(CV);
8939 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8940 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008941 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008942 false, false, 16);
8943
8944 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008945 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008946 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8947 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8948 }
Nadav Rotem43012222011-05-11 08:12:09 +00008949 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008950 // a = a << 5;
8951 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8952 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8953 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8954
8955 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8956 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8957
8958 std::vector<Constant*> CVM1(16, CM1);
8959 std::vector<Constant*> CVM2(16, CM2);
8960 Constant *C = ConstantVector::get(CVM1);
8961 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8962 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008963 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008964 false, false, 16);
8965
8966 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8967 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8968 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8969 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8970 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008971 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008972 // a += a
8973 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008974
Nate Begeman51409212010-07-28 00:21:48 +00008975 C = ConstantVector::get(CVM2);
8976 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8977 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008978 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008979 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008980
Nate Begeman51409212010-07-28 00:21:48 +00008981 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8982 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8983 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8984 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8985 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008986 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008987 // a += a
8988 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008989
Nate Begeman51409212010-07-28 00:21:48 +00008990 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008991 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008992 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8993 return R;
8994 }
8995 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008996}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008997
Dan Gohmand858e902010-04-17 15:26:15 +00008998SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008999 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9000 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009001 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9002 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009003 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009004 SDValue LHS = N->getOperand(0);
9005 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009006 unsigned BaseOp = 0;
9007 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009008 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009009 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009010 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009011 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009012 // A subtract of one will be selected as a INC. Note that INC doesn't
9013 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009014 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9015 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009016 BaseOp = X86ISD::INC;
9017 Cond = X86::COND_O;
9018 break;
9019 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009020 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009021 Cond = X86::COND_O;
9022 break;
9023 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009024 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009025 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009026 break;
9027 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009028 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9029 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9031 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009032 BaseOp = X86ISD::DEC;
9033 Cond = X86::COND_O;
9034 break;
9035 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009036 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009037 Cond = X86::COND_O;
9038 break;
9039 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009040 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009041 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009042 break;
9043 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009044 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009045 Cond = X86::COND_O;
9046 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009047 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9048 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9049 MVT::i32);
9050 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009051
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009052 SDValue SetCC =
9053 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9054 DAG.getConstant(X86::COND_O, MVT::i32),
9055 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009056
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009057 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9058 return Sum;
9059 }
Bill Wendling74c37652008-12-09 22:08:41 +00009060 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009061
Bill Wendling61edeb52008-12-02 01:06:39 +00009062 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009063 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009064 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009065
Bill Wendling61edeb52008-12-02 01:06:39 +00009066 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009067 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9068 DAG.getConstant(Cond, MVT::i32),
9069 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009070
Bill Wendling61edeb52008-12-02 01:06:39 +00009071 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9072 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009073}
9074
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009075SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
9076 DebugLoc dl = Op.getDebugLoc();
9077 SDNode* Node = Op.getNode();
9078 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
9079 EVT VT = Node->getValueType(0);
9080
9081 if (Subtarget->hasSSE2() && VT.isVector()) {
9082 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
9083 ExtraVT.getScalarType().getSizeInBits();
9084 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
9085
9086 unsigned SHLIntrinsicsID = 0;
9087 unsigned SRAIntrinsicsID = 0;
9088 switch (VT.getSimpleVT().SimpleTy) {
9089 default:
9090 return SDValue();
9091 case MVT::v2i64: {
9092 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
9093 SRAIntrinsicsID = 0;
9094 break;
9095 }
9096 case MVT::v4i32: {
9097 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
9098 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
9099 break;
9100 }
9101 case MVT::v8i16: {
9102 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
9103 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
9104 break;
9105 }
9106 }
9107
9108 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9109 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
9110 Node->getOperand(0), ShAmt);
9111
9112 // In case of 1 bit sext, no need to shr
9113 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
9114
9115 if (SRAIntrinsicsID) {
9116 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9117 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
9118 Tmp1, ShAmt);
9119 }
9120 return Tmp1;
9121 }
9122
9123 return SDValue();
9124}
9125
9126
Eric Christopher9a9d2752010-07-22 02:48:34 +00009127SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9128 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009129
Eric Christopher77ed1352011-07-08 00:04:56 +00009130 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
9131 // There isn't any reason to disable it if the target processor supports it.
9132 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009133 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +00009134 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009135 SDValue Ops[] = {
9136 DAG.getRegister(X86::ESP, MVT::i32), // Base
9137 DAG.getTargetConstant(1, MVT::i8), // Scale
9138 DAG.getRegister(0, MVT::i32), // Index
9139 DAG.getTargetConstant(0, MVT::i32), // Disp
9140 DAG.getRegister(0, MVT::i32), // Segment.
9141 Zero,
9142 Chain
9143 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009144 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009145 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9146 array_lengthof(Ops));
9147 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009148 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009149
Eric Christopher9a9d2752010-07-22 02:48:34 +00009150 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009151 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009152 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009153
Chris Lattner132929a2010-08-14 17:26:09 +00009154 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9155 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9156 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9157 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009158
Chris Lattner132929a2010-08-14 17:26:09 +00009159 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9160 if (!Op1 && !Op2 && !Op3 && Op4)
9161 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009162
Chris Lattner132929a2010-08-14 17:26:09 +00009163 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9164 if (Op1 && !Op2 && !Op3 && !Op4)
9165 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009166
9167 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009168 // (MFENCE)>;
9169 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009170}
9171
Dan Gohmand858e902010-04-17 15:26:15 +00009172SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009173 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009174 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009175 unsigned Reg = 0;
9176 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009177 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009178 default:
9179 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009180 case MVT::i8: Reg = X86::AL; size = 1; break;
9181 case MVT::i16: Reg = X86::AX; size = 2; break;
9182 case MVT::i32: Reg = X86::EAX; size = 4; break;
9183 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009184 assert(Subtarget->is64Bit() && "Node not type legal!");
9185 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009186 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009187 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009188 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009189 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009190 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009191 Op.getOperand(1),
9192 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009193 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009194 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009195 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009196 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9197 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9198 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009199 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009200 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009201 return cpOut;
9202}
9203
Duncan Sands1607f052008-12-01 11:39:25 +00009204SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009205 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009206 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009207 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009208 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009209 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009210 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009211 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9212 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009213 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009214 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9215 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009216 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009217 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009218 rdx.getValue(1)
9219 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009220 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009221}
9222
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009223SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009224 SelectionDAG &DAG) const {
9225 EVT SrcVT = Op.getOperand(0).getValueType();
9226 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009227 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9228 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009229 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009230 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009231 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009232 // i64 <=> MMX conversions are Legal.
9233 if (SrcVT==MVT::i64 && DstVT.isVector())
9234 return Op;
9235 if (DstVT==MVT::i64 && SrcVT.isVector())
9236 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009237 // MMX <=> MMX conversions are Legal.
9238 if (SrcVT.isVector() && DstVT.isVector())
9239 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009240 // All other conversions need to be expanded.
9241 return SDValue();
9242}
Chris Lattner5b856542010-12-20 00:59:46 +00009243
Dan Gohmand858e902010-04-17 15:26:15 +00009244SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009245 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009246 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009247 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009248 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009249 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009250 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009251 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009252 Node->getOperand(0),
9253 Node->getOperand(1), negOp,
9254 cast<AtomicSDNode>(Node)->getSrcValue(),
9255 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009256}
9257
Chris Lattner5b856542010-12-20 00:59:46 +00009258static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9259 EVT VT = Op.getNode()->getValueType(0);
9260
9261 // Let legalize expand this if it isn't a legal type yet.
9262 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9263 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009264
Chris Lattner5b856542010-12-20 00:59:46 +00009265 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009266
Chris Lattner5b856542010-12-20 00:59:46 +00009267 unsigned Opc;
9268 bool ExtraOp = false;
9269 switch (Op.getOpcode()) {
9270 default: assert(0 && "Invalid code");
9271 case ISD::ADDC: Opc = X86ISD::ADD; break;
9272 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9273 case ISD::SUBC: Opc = X86ISD::SUB; break;
9274 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9275 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009276
Chris Lattner5b856542010-12-20 00:59:46 +00009277 if (!ExtraOp)
9278 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9279 Op.getOperand(1));
9280 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9281 Op.getOperand(1), Op.getOperand(2));
9282}
9283
Evan Cheng0db9fe62006-04-25 20:13:52 +00009284/// LowerOperation - Provide custom lowering hooks for some operations.
9285///
Dan Gohmand858e902010-04-17 15:26:15 +00009286SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009287 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009288 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009289 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +00009290 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009291 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9292 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009293 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009294 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009295 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9296 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9297 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009298 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009299 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009300 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9301 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9302 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009303 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009304 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009305 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009306 case ISD::SHL_PARTS:
9307 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009308 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009309 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009310 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009311 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009312 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009313 case ISD::FABS: return LowerFABS(Op, DAG);
9314 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009315 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009316 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009317 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009318 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009319 case ISD::SELECT: return LowerSELECT(Op, DAG);
9320 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009321 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009322 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009323 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009324 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009325 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009326 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9327 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009328 case ISD::FRAME_TO_ARGS_OFFSET:
9329 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009330 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009331 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009332 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009333 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009334 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9335 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009336 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009337 case ISD::SRA:
9338 case ISD::SRL:
9339 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009340 case ISD::SADDO:
9341 case ISD::UADDO:
9342 case ISD::SSUBO:
9343 case ISD::USUBO:
9344 case ISD::SMULO:
9345 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009346 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009347 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009348 case ISD::ADDC:
9349 case ISD::ADDE:
9350 case ISD::SUBC:
9351 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009352 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009353}
9354
Duncan Sands1607f052008-12-01 11:39:25 +00009355void X86TargetLowering::
9356ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009357 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009358 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009359 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009360 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009361
9362 SDValue Chain = Node->getOperand(0);
9363 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009364 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009365 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009366 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009367 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009368 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009369 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009370 SDValue Result =
9371 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9372 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009373 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009374 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009375 Results.push_back(Result.getValue(2));
9376}
9377
Duncan Sands126d9072008-07-04 11:47:58 +00009378/// ReplaceNodeResults - Replace a node with an illegal result type
9379/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009380void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9381 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009382 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009383 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009384 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009385 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009386 assert(false && "Do not know how to custom type legalize this operation!");
9387 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009388 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +00009389 case ISD::ADDC:
9390 case ISD::ADDE:
9391 case ISD::SUBC:
9392 case ISD::SUBE:
9393 // We don't want to expand or promote these.
9394 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009395 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009396 std::pair<SDValue,SDValue> Vals =
9397 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009398 SDValue FIST = Vals.first, StackSlot = Vals.second;
9399 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009400 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009401 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009402 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9403 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009404 }
9405 return;
9406 }
9407 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009408 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009409 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009410 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009411 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009412 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009413 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009414 eax.getValue(2));
9415 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9416 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009417 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009418 Results.push_back(edx.getValue(1));
9419 return;
9420 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009421 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009422 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009423 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009424 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009425 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9426 DAG.getConstant(0, MVT::i32));
9427 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9428 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009429 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9430 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009431 cpInL.getValue(1));
9432 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009433 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9434 DAG.getConstant(0, MVT::i32));
9435 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9436 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009437 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009438 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009439 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009440 swapInL.getValue(1));
9441 SDValue Ops[] = { swapInH.getValue(0),
9442 N->getOperand(1),
9443 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009444 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009445 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9446 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9447 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009448 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009449 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009450 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009451 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009452 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009453 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009454 Results.push_back(cpOutH.getValue(1));
9455 return;
9456 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009457 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009458 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9459 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009460 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009461 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9462 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009463 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009464 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9465 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009466 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009467 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9468 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009469 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009470 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9471 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009472 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009473 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9474 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009475 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009476 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9477 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009478 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009479}
9480
Evan Cheng72261582005-12-20 06:22:03 +00009481const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9482 switch (Opcode) {
9483 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009484 case X86ISD::BSF: return "X86ISD::BSF";
9485 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009486 case X86ISD::SHLD: return "X86ISD::SHLD";
9487 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009488 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009489 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009490 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009491 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009492 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009493 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009494 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9495 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9496 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009497 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009498 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009499 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009500 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009501 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009502 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009503 case X86ISD::COMI: return "X86ISD::COMI";
9504 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009505 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009506 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009507 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9508 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009509 case X86ISD::CMOV: return "X86ISD::CMOV";
9510 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009511 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009512 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9513 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009514 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009515 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009516 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009517 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009518 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009519 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9520 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009521 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009522 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +00009523 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +00009524 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9525 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9526 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009527 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009528 case X86ISD::FMAX: return "X86ISD::FMAX";
9529 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009530 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9531 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009532 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009533 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009534 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009535 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009536 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009537 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9538 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009539 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9540 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9541 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9542 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9543 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9544 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009545 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9546 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009547 case X86ISD::VSHL: return "X86ISD::VSHL";
9548 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009549 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9550 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9551 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9552 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9553 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9554 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9555 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9556 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9557 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9558 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009559 case X86ISD::ADD: return "X86ISD::ADD";
9560 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009561 case X86ISD::ADC: return "X86ISD::ADC";
9562 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009563 case X86ISD::SMUL: return "X86ISD::SMUL";
9564 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009565 case X86ISD::INC: return "X86ISD::INC";
9566 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009567 case X86ISD::OR: return "X86ISD::OR";
9568 case X86ISD::XOR: return "X86ISD::XOR";
9569 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009570 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009571 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009572 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009573 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9574 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9575 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9576 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9577 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9578 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9579 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9580 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9581 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009582 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009583 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009584 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009585 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9586 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009587 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9588 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9589 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9590 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9591 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9592 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9593 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9594 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9595 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009596 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9597 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9598 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9599 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009600 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9601 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9602 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9603 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9604 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9605 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9606 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9607 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9608 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9609 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009610 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009611 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009612 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009613 }
9614}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009615
Chris Lattnerc9addb72007-03-30 23:15:24 +00009616// isLegalAddressingMode - Return true if the addressing mode represented
9617// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009618bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009619 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00009620 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009621 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009622 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009623
Chris Lattnerc9addb72007-03-30 23:15:24 +00009624 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009625 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009626 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009627
Chris Lattnerc9addb72007-03-30 23:15:24 +00009628 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009629 unsigned GVFlags =
9630 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009631
Chris Lattnerdfed4132009-07-10 07:38:24 +00009632 // If a reference to this global requires an extra load, we can't fold it.
9633 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009634 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009635
Chris Lattnerdfed4132009-07-10 07:38:24 +00009636 // If BaseGV requires a register for the PIC base, we cannot also have a
9637 // BaseReg specified.
9638 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009639 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009640
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009641 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009642 if ((M != CodeModel::Small || R != Reloc::Static) &&
9643 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009644 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009645 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009646
Chris Lattnerc9addb72007-03-30 23:15:24 +00009647 switch (AM.Scale) {
9648 case 0:
9649 case 1:
9650 case 2:
9651 case 4:
9652 case 8:
9653 // These scales always work.
9654 break;
9655 case 3:
9656 case 5:
9657 case 9:
9658 // These scales are formed with basereg+scalereg. Only accept if there is
9659 // no basereg yet.
9660 if (AM.HasBaseReg)
9661 return false;
9662 break;
9663 default: // Other stuff never works.
9664 return false;
9665 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009666
Chris Lattnerc9addb72007-03-30 23:15:24 +00009667 return true;
9668}
9669
9670
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009671bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009672 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009673 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009674 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9675 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009676 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009677 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009678 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009679}
9680
Owen Andersone50ed302009-08-10 22:56:29 +00009681bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009682 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009683 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009684 unsigned NumBits1 = VT1.getSizeInBits();
9685 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009686 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009687 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009688 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009689}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009690
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009691bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009692 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009693 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009694}
9695
Owen Andersone50ed302009-08-10 22:56:29 +00009696bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009697 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009698 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009699}
9700
Owen Andersone50ed302009-08-10 22:56:29 +00009701bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009702 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009703 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009704}
9705
Evan Cheng60c07e12006-07-05 22:17:51 +00009706/// isShuffleMaskLegal - Targets can use this to indicate that they only
9707/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9708/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9709/// are assumed to be legal.
9710bool
Eric Christopherfd179292009-08-27 18:07:15 +00009711X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009712 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009713 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009714 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009715 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009716
Nate Begemana09008b2009-10-19 02:17:23 +00009717 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009718 return (VT.getVectorNumElements() == 2 ||
9719 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9720 isMOVLMask(M, VT) ||
9721 isSHUFPMask(M, VT) ||
9722 isPSHUFDMask(M, VT) ||
9723 isPSHUFHWMask(M, VT) ||
9724 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009725 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009726 isUNPCKLMask(M, VT) ||
9727 isUNPCKHMask(M, VT) ||
9728 isUNPCKL_v_undef_Mask(M, VT) ||
9729 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009730}
9731
Dan Gohman7d8143f2008-04-09 20:09:42 +00009732bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009733X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009734 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009735 unsigned NumElts = VT.getVectorNumElements();
9736 // FIXME: This collection of masks seems suspect.
9737 if (NumElts == 2)
9738 return true;
9739 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9740 return (isMOVLMask(Mask, VT) ||
9741 isCommutedMOVLMask(Mask, VT, true) ||
9742 isSHUFPMask(Mask, VT) ||
9743 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009744 }
9745 return false;
9746}
9747
9748//===----------------------------------------------------------------------===//
9749// X86 Scheduler Hooks
9750//===----------------------------------------------------------------------===//
9751
Mon P Wang63307c32008-05-05 19:05:59 +00009752// private utility function
9753MachineBasicBlock *
9754X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9755 MachineBasicBlock *MBB,
9756 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009757 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009758 unsigned LoadOpc,
9759 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009760 unsigned notOpc,
9761 unsigned EAXreg,
9762 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009763 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009764 // For the atomic bitwise operator, we generate
9765 // thisMBB:
9766 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009767 // ld t1 = [bitinstr.addr]
9768 // op t2 = t1, [bitinstr.val]
9769 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009770 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9771 // bz newMBB
9772 // fallthrough -->nextMBB
9773 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9774 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009775 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009776 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009777
Mon P Wang63307c32008-05-05 19:05:59 +00009778 /// First build the CFG
9779 MachineFunction *F = MBB->getParent();
9780 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009781 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9782 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9783 F->insert(MBBIter, newMBB);
9784 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009785
Dan Gohman14152b42010-07-06 20:24:04 +00009786 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9787 nextMBB->splice(nextMBB->begin(), thisMBB,
9788 llvm::next(MachineBasicBlock::iterator(bInstr)),
9789 thisMBB->end());
9790 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009791
Mon P Wang63307c32008-05-05 19:05:59 +00009792 // Update thisMBB to fall through to newMBB
9793 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009794
Mon P Wang63307c32008-05-05 19:05:59 +00009795 // newMBB jumps to itself and fall through to nextMBB
9796 newMBB->addSuccessor(nextMBB);
9797 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009798
Mon P Wang63307c32008-05-05 19:05:59 +00009799 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009800 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009801 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009802 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009803 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009804 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009805 int numArgs = bInstr->getNumOperands() - 1;
9806 for (int i=0; i < numArgs; ++i)
9807 argOpers[i] = &bInstr->getOperand(i+1);
9808
9809 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009810 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009811 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009812
Dale Johannesen140be2d2008-08-19 18:47:28 +00009813 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009814 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009815 for (int i=0; i <= lastAddrIndx; ++i)
9816 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009817
Dale Johannesen140be2d2008-08-19 18:47:28 +00009818 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009819 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009820 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009821 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009822 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009823 tt = t1;
9824
Dale Johannesen140be2d2008-08-19 18:47:28 +00009825 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009826 assert((argOpers[valArgIndx]->isReg() ||
9827 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009828 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009829 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009830 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009831 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009832 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009833 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009834 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009835
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009836 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009837 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009838
Dale Johannesene4d209d2009-02-03 20:21:25 +00009839 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009840 for (int i=0; i <= lastAddrIndx; ++i)
9841 (*MIB).addOperand(*argOpers[i]);
9842 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009843 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009844 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9845 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009846
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009847 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009848 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009849
Mon P Wang63307c32008-05-05 19:05:59 +00009850 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009851 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009852
Dan Gohman14152b42010-07-06 20:24:04 +00009853 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009854 return nextMBB;
9855}
9856
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009857// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009858MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009859X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9860 MachineBasicBlock *MBB,
9861 unsigned regOpcL,
9862 unsigned regOpcH,
9863 unsigned immOpcL,
9864 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009865 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009866 // For the atomic bitwise operator, we generate
9867 // thisMBB (instructions are in pairs, except cmpxchg8b)
9868 // ld t1,t2 = [bitinstr.addr]
9869 // newMBB:
9870 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9871 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009872 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009873 // mov ECX, EBX <- t5, t6
9874 // mov EAX, EDX <- t1, t2
9875 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9876 // mov t3, t4 <- EAX, EDX
9877 // bz newMBB
9878 // result in out1, out2
9879 // fallthrough -->nextMBB
9880
9881 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9882 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009883 const unsigned NotOpc = X86::NOT32r;
9884 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9885 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9886 MachineFunction::iterator MBBIter = MBB;
9887 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009888
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009889 /// First build the CFG
9890 MachineFunction *F = MBB->getParent();
9891 MachineBasicBlock *thisMBB = MBB;
9892 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9893 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9894 F->insert(MBBIter, newMBB);
9895 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009896
Dan Gohman14152b42010-07-06 20:24:04 +00009897 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9898 nextMBB->splice(nextMBB->begin(), thisMBB,
9899 llvm::next(MachineBasicBlock::iterator(bInstr)),
9900 thisMBB->end());
9901 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009902
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009903 // Update thisMBB to fall through to newMBB
9904 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009905
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009906 // newMBB jumps to itself and fall through to nextMBB
9907 newMBB->addSuccessor(nextMBB);
9908 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009909
Dale Johannesene4d209d2009-02-03 20:21:25 +00009910 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009911 // Insert instructions into newMBB based on incoming instruction
9912 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009913 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009914 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009915 MachineOperand& dest1Oper = bInstr->getOperand(0);
9916 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009917 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9918 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009919 argOpers[i] = &bInstr->getOperand(i+2);
9920
Dan Gohman71ea4e52010-05-14 21:01:44 +00009921 // We use some of the operands multiple times, so conservatively just
9922 // clear any kill flags that might be present.
9923 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9924 argOpers[i]->setIsKill(false);
9925 }
9926
Evan Chengad5b52f2010-01-08 19:14:57 +00009927 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009928 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009929
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009930 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009931 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009932 for (int i=0; i <= lastAddrIndx; ++i)
9933 (*MIB).addOperand(*argOpers[i]);
9934 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009935 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009936 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009937 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009938 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009939 MachineOperand newOp3 = *(argOpers[3]);
9940 if (newOp3.isImm())
9941 newOp3.setImm(newOp3.getImm()+4);
9942 else
9943 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009944 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009945 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009946
9947 // t3/4 are defined later, at the bottom of the loop
9948 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9949 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009950 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009951 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009952 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009953 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9954
Evan Cheng306b4ca2010-01-08 23:41:50 +00009955 // The subsequent operations should be using the destination registers of
9956 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009957 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009958 t1 = F->getRegInfo().createVirtualRegister(RC);
9959 t2 = F->getRegInfo().createVirtualRegister(RC);
9960 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9961 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009962 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009963 t1 = dest1Oper.getReg();
9964 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009965 }
9966
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009967 int valArgIndx = lastAddrIndx + 1;
9968 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009969 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009970 "invalid operand");
9971 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9972 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009973 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009974 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009975 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009976 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009977 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009978 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009979 (*MIB).addOperand(*argOpers[valArgIndx]);
9980 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009981 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009982 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009983 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009984 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009985 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009986 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009987 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009988 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009989 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009990 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009991
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009992 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009993 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009994 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009995 MIB.addReg(t2);
9996
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009997 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009998 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009999 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010000 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000010001
Dale Johannesene4d209d2009-02-03 20:21:25 +000010002 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010003 for (int i=0; i <= lastAddrIndx; ++i)
10004 (*MIB).addOperand(*argOpers[i]);
10005
10006 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010007 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10008 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010009
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010010 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010011 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010012 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010013 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010014
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010015 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010016 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010017
Dan Gohman14152b42010-07-06 20:24:04 +000010018 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010019 return nextMBB;
10020}
10021
10022// private utility function
10023MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000010024X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
10025 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010026 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010027 // For the atomic min/max operator, we generate
10028 // thisMBB:
10029 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010030 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000010031 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000010032 // cmp t1, t2
10033 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000010034 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010035 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10036 // bz newMBB
10037 // fallthrough -->nextMBB
10038 //
10039 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10040 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010041 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010042 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010043
Mon P Wang63307c32008-05-05 19:05:59 +000010044 /// First build the CFG
10045 MachineFunction *F = MBB->getParent();
10046 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010047 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10048 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10049 F->insert(MBBIter, newMBB);
10050 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010051
Dan Gohman14152b42010-07-06 20:24:04 +000010052 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10053 nextMBB->splice(nextMBB->begin(), thisMBB,
10054 llvm::next(MachineBasicBlock::iterator(mInstr)),
10055 thisMBB->end());
10056 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010057
Mon P Wang63307c32008-05-05 19:05:59 +000010058 // Update thisMBB to fall through to newMBB
10059 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010060
Mon P Wang63307c32008-05-05 19:05:59 +000010061 // newMBB jumps to newMBB and fall through to nextMBB
10062 newMBB->addSuccessor(nextMBB);
10063 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010064
Dale Johannesene4d209d2009-02-03 20:21:25 +000010065 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010066 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010067 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010068 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010069 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010070 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010071 int numArgs = mInstr->getNumOperands() - 1;
10072 for (int i=0; i < numArgs; ++i)
10073 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010074
Mon P Wang63307c32008-05-05 19:05:59 +000010075 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010076 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010077 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010078
Mon P Wangab3e7472008-05-05 22:56:23 +000010079 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010080 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010081 for (int i=0; i <= lastAddrIndx; ++i)
10082 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010083
Mon P Wang63307c32008-05-05 19:05:59 +000010084 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010085 assert((argOpers[valArgIndx]->isReg() ||
10086 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010087 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010088
10089 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010090 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010091 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010092 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010093 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010094 (*MIB).addOperand(*argOpers[valArgIndx]);
10095
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010096 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010097 MIB.addReg(t1);
10098
Dale Johannesene4d209d2009-02-03 20:21:25 +000010099 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010100 MIB.addReg(t1);
10101 MIB.addReg(t2);
10102
10103 // Generate movc
10104 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010105 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010106 MIB.addReg(t2);
10107 MIB.addReg(t1);
10108
10109 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010110 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010111 for (int i=0; i <= lastAddrIndx; ++i)
10112 (*MIB).addOperand(*argOpers[i]);
10113 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010114 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010115 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10116 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010117
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010118 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010119 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010120
Mon P Wang63307c32008-05-05 19:05:59 +000010121 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010122 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010123
Dan Gohman14152b42010-07-06 20:24:04 +000010124 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010125 return nextMBB;
10126}
10127
Eric Christopherf83a5de2009-08-27 18:08:16 +000010128// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010129// or XMM0_V32I8 in AVX all of this code can be replaced with that
10130// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010131MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010132X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010133 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010134 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10135 "Target must have SSE4.2 or AVX features enabled");
10136
Eric Christopherb120ab42009-08-18 22:50:32 +000010137 DebugLoc dl = MI->getDebugLoc();
10138 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010139 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010140 if (!Subtarget->hasAVX()) {
10141 if (memArg)
10142 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10143 else
10144 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10145 } else {
10146 if (memArg)
10147 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10148 else
10149 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10150 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010151
Eric Christopher41c902f2010-11-30 08:20:21 +000010152 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010153 for (unsigned i = 0; i < numArgs; ++i) {
10154 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010155 if (!(Op.isReg() && Op.isImplicit()))
10156 MIB.addOperand(Op);
10157 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010158 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010159 .addReg(X86::XMM0);
10160
Dan Gohman14152b42010-07-06 20:24:04 +000010161 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010162 return BB;
10163}
10164
10165MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010166X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010167 DebugLoc dl = MI->getDebugLoc();
10168 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010169
Eric Christopher228232b2010-11-30 07:20:12 +000010170 // Address into RAX/EAX, other two args into ECX, EDX.
10171 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10172 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10173 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10174 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010175 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010176
Eric Christopher228232b2010-11-30 07:20:12 +000010177 unsigned ValOps = X86::AddrNumOperands;
10178 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10179 .addReg(MI->getOperand(ValOps).getReg());
10180 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10181 .addReg(MI->getOperand(ValOps+1).getReg());
10182
10183 // The instruction doesn't actually take any operands though.
10184 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010185
Eric Christopher228232b2010-11-30 07:20:12 +000010186 MI->eraseFromParent(); // The pseudo is gone now.
10187 return BB;
10188}
10189
10190MachineBasicBlock *
10191X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010192 DebugLoc dl = MI->getDebugLoc();
10193 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010194
Eric Christopher228232b2010-11-30 07:20:12 +000010195 // First arg in ECX, the second in EAX.
10196 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10197 .addReg(MI->getOperand(0).getReg());
10198 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10199 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010200
Eric Christopher228232b2010-11-30 07:20:12 +000010201 // The instruction doesn't actually take any operands though.
10202 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010203
Eric Christopher228232b2010-11-30 07:20:12 +000010204 MI->eraseFromParent(); // The pseudo is gone now.
10205 return BB;
10206}
10207
10208MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010209X86TargetLowering::EmitVAARG64WithCustomInserter(
10210 MachineInstr *MI,
10211 MachineBasicBlock *MBB) const {
10212 // Emit va_arg instruction on X86-64.
10213
10214 // Operands to this pseudo-instruction:
10215 // 0 ) Output : destination address (reg)
10216 // 1-5) Input : va_list address (addr, i64mem)
10217 // 6 ) ArgSize : Size (in bytes) of vararg type
10218 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10219 // 8 ) Align : Alignment of type
10220 // 9 ) EFLAGS (implicit-def)
10221
10222 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10223 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10224
10225 unsigned DestReg = MI->getOperand(0).getReg();
10226 MachineOperand &Base = MI->getOperand(1);
10227 MachineOperand &Scale = MI->getOperand(2);
10228 MachineOperand &Index = MI->getOperand(3);
10229 MachineOperand &Disp = MI->getOperand(4);
10230 MachineOperand &Segment = MI->getOperand(5);
10231 unsigned ArgSize = MI->getOperand(6).getImm();
10232 unsigned ArgMode = MI->getOperand(7).getImm();
10233 unsigned Align = MI->getOperand(8).getImm();
10234
10235 // Memory Reference
10236 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10237 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10238 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10239
10240 // Machine Information
10241 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10242 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10243 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10244 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10245 DebugLoc DL = MI->getDebugLoc();
10246
10247 // struct va_list {
10248 // i32 gp_offset
10249 // i32 fp_offset
10250 // i64 overflow_area (address)
10251 // i64 reg_save_area (address)
10252 // }
10253 // sizeof(va_list) = 24
10254 // alignment(va_list) = 8
10255
10256 unsigned TotalNumIntRegs = 6;
10257 unsigned TotalNumXMMRegs = 8;
10258 bool UseGPOffset = (ArgMode == 1);
10259 bool UseFPOffset = (ArgMode == 2);
10260 unsigned MaxOffset = TotalNumIntRegs * 8 +
10261 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10262
10263 /* Align ArgSize to a multiple of 8 */
10264 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10265 bool NeedsAlign = (Align > 8);
10266
10267 MachineBasicBlock *thisMBB = MBB;
10268 MachineBasicBlock *overflowMBB;
10269 MachineBasicBlock *offsetMBB;
10270 MachineBasicBlock *endMBB;
10271
10272 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10273 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10274 unsigned OffsetReg = 0;
10275
10276 if (!UseGPOffset && !UseFPOffset) {
10277 // If we only pull from the overflow region, we don't create a branch.
10278 // We don't need to alter control flow.
10279 OffsetDestReg = 0; // unused
10280 OverflowDestReg = DestReg;
10281
10282 offsetMBB = NULL;
10283 overflowMBB = thisMBB;
10284 endMBB = thisMBB;
10285 } else {
10286 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10287 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10288 // If not, pull from overflow_area. (branch to overflowMBB)
10289 //
10290 // thisMBB
10291 // | .
10292 // | .
10293 // offsetMBB overflowMBB
10294 // | .
10295 // | .
10296 // endMBB
10297
10298 // Registers for the PHI in endMBB
10299 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10300 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10301
10302 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10303 MachineFunction *MF = MBB->getParent();
10304 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10305 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10306 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10307
10308 MachineFunction::iterator MBBIter = MBB;
10309 ++MBBIter;
10310
10311 // Insert the new basic blocks
10312 MF->insert(MBBIter, offsetMBB);
10313 MF->insert(MBBIter, overflowMBB);
10314 MF->insert(MBBIter, endMBB);
10315
10316 // Transfer the remainder of MBB and its successor edges to endMBB.
10317 endMBB->splice(endMBB->begin(), thisMBB,
10318 llvm::next(MachineBasicBlock::iterator(MI)),
10319 thisMBB->end());
10320 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10321
10322 // Make offsetMBB and overflowMBB successors of thisMBB
10323 thisMBB->addSuccessor(offsetMBB);
10324 thisMBB->addSuccessor(overflowMBB);
10325
10326 // endMBB is a successor of both offsetMBB and overflowMBB
10327 offsetMBB->addSuccessor(endMBB);
10328 overflowMBB->addSuccessor(endMBB);
10329
10330 // Load the offset value into a register
10331 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10332 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10333 .addOperand(Base)
10334 .addOperand(Scale)
10335 .addOperand(Index)
10336 .addDisp(Disp, UseFPOffset ? 4 : 0)
10337 .addOperand(Segment)
10338 .setMemRefs(MMOBegin, MMOEnd);
10339
10340 // Check if there is enough room left to pull this argument.
10341 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10342 .addReg(OffsetReg)
10343 .addImm(MaxOffset + 8 - ArgSizeA8);
10344
10345 // Branch to "overflowMBB" if offset >= max
10346 // Fall through to "offsetMBB" otherwise
10347 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10348 .addMBB(overflowMBB);
10349 }
10350
10351 // In offsetMBB, emit code to use the reg_save_area.
10352 if (offsetMBB) {
10353 assert(OffsetReg != 0);
10354
10355 // Read the reg_save_area address.
10356 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10357 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10358 .addOperand(Base)
10359 .addOperand(Scale)
10360 .addOperand(Index)
10361 .addDisp(Disp, 16)
10362 .addOperand(Segment)
10363 .setMemRefs(MMOBegin, MMOEnd);
10364
10365 // Zero-extend the offset
10366 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10367 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10368 .addImm(0)
10369 .addReg(OffsetReg)
10370 .addImm(X86::sub_32bit);
10371
10372 // Add the offset to the reg_save_area to get the final address.
10373 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10374 .addReg(OffsetReg64)
10375 .addReg(RegSaveReg);
10376
10377 // Compute the offset for the next argument
10378 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10379 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10380 .addReg(OffsetReg)
10381 .addImm(UseFPOffset ? 16 : 8);
10382
10383 // Store it back into the va_list.
10384 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10385 .addOperand(Base)
10386 .addOperand(Scale)
10387 .addOperand(Index)
10388 .addDisp(Disp, UseFPOffset ? 4 : 0)
10389 .addOperand(Segment)
10390 .addReg(NextOffsetReg)
10391 .setMemRefs(MMOBegin, MMOEnd);
10392
10393 // Jump to endMBB
10394 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10395 .addMBB(endMBB);
10396 }
10397
10398 //
10399 // Emit code to use overflow area
10400 //
10401
10402 // Load the overflow_area address into a register.
10403 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10404 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10405 .addOperand(Base)
10406 .addOperand(Scale)
10407 .addOperand(Index)
10408 .addDisp(Disp, 8)
10409 .addOperand(Segment)
10410 .setMemRefs(MMOBegin, MMOEnd);
10411
10412 // If we need to align it, do so. Otherwise, just copy the address
10413 // to OverflowDestReg.
10414 if (NeedsAlign) {
10415 // Align the overflow address
10416 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10417 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10418
10419 // aligned_addr = (addr + (align-1)) & ~(align-1)
10420 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10421 .addReg(OverflowAddrReg)
10422 .addImm(Align-1);
10423
10424 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10425 .addReg(TmpReg)
10426 .addImm(~(uint64_t)(Align-1));
10427 } else {
10428 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10429 .addReg(OverflowAddrReg);
10430 }
10431
10432 // Compute the next overflow address after this argument.
10433 // (the overflow address should be kept 8-byte aligned)
10434 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10435 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10436 .addReg(OverflowDestReg)
10437 .addImm(ArgSizeA8);
10438
10439 // Store the new overflow address.
10440 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10441 .addOperand(Base)
10442 .addOperand(Scale)
10443 .addOperand(Index)
10444 .addDisp(Disp, 8)
10445 .addOperand(Segment)
10446 .addReg(NextAddrReg)
10447 .setMemRefs(MMOBegin, MMOEnd);
10448
10449 // If we branched, emit the PHI to the front of endMBB.
10450 if (offsetMBB) {
10451 BuildMI(*endMBB, endMBB->begin(), DL,
10452 TII->get(X86::PHI), DestReg)
10453 .addReg(OffsetDestReg).addMBB(offsetMBB)
10454 .addReg(OverflowDestReg).addMBB(overflowMBB);
10455 }
10456
10457 // Erase the pseudo instruction
10458 MI->eraseFromParent();
10459
10460 return endMBB;
10461}
10462
10463MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010464X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10465 MachineInstr *MI,
10466 MachineBasicBlock *MBB) const {
10467 // Emit code to save XMM registers to the stack. The ABI says that the
10468 // number of registers to save is given in %al, so it's theoretically
10469 // possible to do an indirect jump trick to avoid saving all of them,
10470 // however this code takes a simpler approach and just executes all
10471 // of the stores if %al is non-zero. It's less code, and it's probably
10472 // easier on the hardware branch predictor, and stores aren't all that
10473 // expensive anyway.
10474
10475 // Create the new basic blocks. One block contains all the XMM stores,
10476 // and one block is the final destination regardless of whether any
10477 // stores were performed.
10478 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10479 MachineFunction *F = MBB->getParent();
10480 MachineFunction::iterator MBBIter = MBB;
10481 ++MBBIter;
10482 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10483 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10484 F->insert(MBBIter, XMMSaveMBB);
10485 F->insert(MBBIter, EndMBB);
10486
Dan Gohman14152b42010-07-06 20:24:04 +000010487 // Transfer the remainder of MBB and its successor edges to EndMBB.
10488 EndMBB->splice(EndMBB->begin(), MBB,
10489 llvm::next(MachineBasicBlock::iterator(MI)),
10490 MBB->end());
10491 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10492
Dan Gohmand6708ea2009-08-15 01:38:56 +000010493 // The original block will now fall through to the XMM save block.
10494 MBB->addSuccessor(XMMSaveMBB);
10495 // The XMMSaveMBB will fall through to the end block.
10496 XMMSaveMBB->addSuccessor(EndMBB);
10497
10498 // Now add the instructions.
10499 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10500 DebugLoc DL = MI->getDebugLoc();
10501
10502 unsigned CountReg = MI->getOperand(0).getReg();
10503 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10504 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10505
10506 if (!Subtarget->isTargetWin64()) {
10507 // If %al is 0, branch around the XMM save block.
10508 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010509 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010510 MBB->addSuccessor(EndMBB);
10511 }
10512
10513 // In the XMM save block, save all the XMM argument registers.
10514 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10515 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010516 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010517 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010518 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010519 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010520 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010521 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10522 .addFrameIndex(RegSaveFrameIndex)
10523 .addImm(/*Scale=*/1)
10524 .addReg(/*IndexReg=*/0)
10525 .addImm(/*Disp=*/Offset)
10526 .addReg(/*Segment=*/0)
10527 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010528 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010529 }
10530
Dan Gohman14152b42010-07-06 20:24:04 +000010531 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010532
10533 return EndMBB;
10534}
Mon P Wang63307c32008-05-05 19:05:59 +000010535
Evan Cheng60c07e12006-07-05 22:17:51 +000010536MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010537X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010538 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010539 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10540 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010541
Chris Lattner52600972009-09-02 05:57:00 +000010542 // To "insert" a SELECT_CC instruction, we actually have to insert the
10543 // diamond control-flow pattern. The incoming instruction knows the
10544 // destination vreg to set, the condition code register to branch on, the
10545 // true/false values to select between, and a branch opcode to use.
10546 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10547 MachineFunction::iterator It = BB;
10548 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010549
Chris Lattner52600972009-09-02 05:57:00 +000010550 // thisMBB:
10551 // ...
10552 // TrueVal = ...
10553 // cmpTY ccX, r1, r2
10554 // bCC copy1MBB
10555 // fallthrough --> copy0MBB
10556 MachineBasicBlock *thisMBB = BB;
10557 MachineFunction *F = BB->getParent();
10558 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10559 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010560 F->insert(It, copy0MBB);
10561 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010562
Bill Wendling730c07e2010-06-25 20:48:10 +000010563 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10564 // live into the sink and copy blocks.
10565 const MachineFunction *MF = BB->getParent();
10566 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10567 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010568
Dan Gohman14152b42010-07-06 20:24:04 +000010569 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10570 const MachineOperand &MO = MI->getOperand(I);
10571 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010572 unsigned Reg = MO.getReg();
10573 if (Reg != X86::EFLAGS) continue;
10574 copy0MBB->addLiveIn(Reg);
10575 sinkMBB->addLiveIn(Reg);
10576 }
10577
Dan Gohman14152b42010-07-06 20:24:04 +000010578 // Transfer the remainder of BB and its successor edges to sinkMBB.
10579 sinkMBB->splice(sinkMBB->begin(), BB,
10580 llvm::next(MachineBasicBlock::iterator(MI)),
10581 BB->end());
10582 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10583
10584 // Add the true and fallthrough blocks as its successors.
10585 BB->addSuccessor(copy0MBB);
10586 BB->addSuccessor(sinkMBB);
10587
10588 // Create the conditional branch instruction.
10589 unsigned Opc =
10590 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10591 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10592
Chris Lattner52600972009-09-02 05:57:00 +000010593 // copy0MBB:
10594 // %FalseValue = ...
10595 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010596 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010597
Chris Lattner52600972009-09-02 05:57:00 +000010598 // sinkMBB:
10599 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10600 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010601 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10602 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010603 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10604 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10605
Dan Gohman14152b42010-07-06 20:24:04 +000010606 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010607 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010608}
10609
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010610MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010611X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010612 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010613 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10614 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010615
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010616 assert(!Subtarget->isTargetEnvMacho());
10617
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010618 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10619 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010620
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010621 if (Subtarget->isTargetWin64()) {
10622 if (Subtarget->isTargetCygMing()) {
10623 // ___chkstk(Mingw64):
10624 // Clobbers R10, R11, RAX and EFLAGS.
10625 // Updates RSP.
10626 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10627 .addExternalSymbol("___chkstk")
10628 .addReg(X86::RAX, RegState::Implicit)
10629 .addReg(X86::RSP, RegState::Implicit)
10630 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10631 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10632 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10633 } else {
10634 // __chkstk(MSVCRT): does not update stack pointer.
10635 // Clobbers R10, R11 and EFLAGS.
10636 // FIXME: RAX(allocated size) might be reused and not killed.
10637 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10638 .addExternalSymbol("__chkstk")
10639 .addReg(X86::RAX, RegState::Implicit)
10640 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10641 // RAX has the offset to subtracted from RSP.
10642 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10643 .addReg(X86::RSP)
10644 .addReg(X86::RAX);
10645 }
10646 } else {
10647 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010648 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10649
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010650 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10651 .addExternalSymbol(StackProbeSymbol)
10652 .addReg(X86::EAX, RegState::Implicit)
10653 .addReg(X86::ESP, RegState::Implicit)
10654 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10655 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10656 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10657 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010658
Dan Gohman14152b42010-07-06 20:24:04 +000010659 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010660 return BB;
10661}
Chris Lattner52600972009-09-02 05:57:00 +000010662
10663MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010664X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10665 MachineBasicBlock *BB) const {
10666 // This is pretty easy. We're taking the value that we received from
10667 // our load from the relocation, sticking it in either RDI (x86-64)
10668 // or EAX and doing an indirect call. The return value will then
10669 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010670 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010671 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010672 DebugLoc DL = MI->getDebugLoc();
10673 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010674
10675 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010676 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010677
Eric Christopher30ef0e52010-06-03 04:07:48 +000010678 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010679 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10680 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010681 .addReg(X86::RIP)
10682 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010683 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010684 MI->getOperand(3).getTargetFlags())
10685 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010686 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010687 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010688 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010689 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10690 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010691 .addReg(0)
10692 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010693 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010694 MI->getOperand(3).getTargetFlags())
10695 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010696 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010697 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010698 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010699 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10700 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010701 .addReg(TII->getGlobalBaseReg(F))
10702 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010703 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010704 MI->getOperand(3).getTargetFlags())
10705 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010706 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010707 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010708 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010709
Dan Gohman14152b42010-07-06 20:24:04 +000010710 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010711 return BB;
10712}
10713
10714MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010715X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010716 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010717 switch (MI->getOpcode()) {
10718 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010719 case X86::TAILJMPd64:
10720 case X86::TAILJMPr64:
10721 case X86::TAILJMPm64:
10722 assert(!"TAILJMP64 would not be touched here.");
10723 case X86::TCRETURNdi64:
10724 case X86::TCRETURNri64:
10725 case X86::TCRETURNmi64:
10726 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10727 // On AMD64, additional defs should be added before register allocation.
10728 if (!Subtarget->isTargetWin64()) {
10729 MI->addRegisterDefined(X86::RSI);
10730 MI->addRegisterDefined(X86::RDI);
10731 MI->addRegisterDefined(X86::XMM6);
10732 MI->addRegisterDefined(X86::XMM7);
10733 MI->addRegisterDefined(X86::XMM8);
10734 MI->addRegisterDefined(X86::XMM9);
10735 MI->addRegisterDefined(X86::XMM10);
10736 MI->addRegisterDefined(X86::XMM11);
10737 MI->addRegisterDefined(X86::XMM12);
10738 MI->addRegisterDefined(X86::XMM13);
10739 MI->addRegisterDefined(X86::XMM14);
10740 MI->addRegisterDefined(X86::XMM15);
10741 }
10742 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010743 case X86::WIN_ALLOCA:
10744 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010745 case X86::TLSCall_32:
10746 case X86::TLSCall_64:
10747 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010748 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010749 case X86::CMOV_FR32:
10750 case X86::CMOV_FR64:
10751 case X86::CMOV_V4F32:
10752 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010753 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010754 case X86::CMOV_GR16:
10755 case X86::CMOV_GR32:
10756 case X86::CMOV_RFP32:
10757 case X86::CMOV_RFP64:
10758 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010759 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010760
Dale Johannesen849f2142007-07-03 00:53:03 +000010761 case X86::FP32_TO_INT16_IN_MEM:
10762 case X86::FP32_TO_INT32_IN_MEM:
10763 case X86::FP32_TO_INT64_IN_MEM:
10764 case X86::FP64_TO_INT16_IN_MEM:
10765 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010766 case X86::FP64_TO_INT64_IN_MEM:
10767 case X86::FP80_TO_INT16_IN_MEM:
10768 case X86::FP80_TO_INT32_IN_MEM:
10769 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010770 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10771 DebugLoc DL = MI->getDebugLoc();
10772
Evan Cheng60c07e12006-07-05 22:17:51 +000010773 // Change the floating point control register to use "round towards zero"
10774 // mode when truncating to an integer value.
10775 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010776 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010777 addFrameReference(BuildMI(*BB, MI, DL,
10778 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010779
10780 // Load the old value of the high byte of the control word...
10781 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010782 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010783 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010784 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010785
10786 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010787 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010788 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010789
10790 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010791 addFrameReference(BuildMI(*BB, MI, DL,
10792 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010793
10794 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010795 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010796 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010797
10798 // Get the X86 opcode to use.
10799 unsigned Opc;
10800 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010801 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010802 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10803 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10804 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10805 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10806 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10807 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010808 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10809 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10810 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010811 }
10812
10813 X86AddressMode AM;
10814 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010815 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010816 AM.BaseType = X86AddressMode::RegBase;
10817 AM.Base.Reg = Op.getReg();
10818 } else {
10819 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010820 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010821 }
10822 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010823 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010824 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010825 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010826 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010827 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010828 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010829 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010830 AM.GV = Op.getGlobal();
10831 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010832 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010833 }
Dan Gohman14152b42010-07-06 20:24:04 +000010834 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010835 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010836
10837 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010838 addFrameReference(BuildMI(*BB, MI, DL,
10839 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010840
Dan Gohman14152b42010-07-06 20:24:04 +000010841 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010842 return BB;
10843 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010844 // String/text processing lowering.
10845 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010846 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010847 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10848 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010849 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010850 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10851 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010852 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010853 return EmitPCMP(MI, BB, 5, false /* in mem */);
10854 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010855 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010856 return EmitPCMP(MI, BB, 5, true /* in mem */);
10857
Eric Christopher228232b2010-11-30 07:20:12 +000010858 // Thread synchronization.
10859 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010860 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010861 case X86::MWAIT:
10862 return EmitMwait(MI, BB);
10863
Eric Christopherb120ab42009-08-18 22:50:32 +000010864 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010865 case X86::ATOMAND32:
10866 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010867 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010868 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010869 X86::NOT32r, X86::EAX,
10870 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010871 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010872 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10873 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010874 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010875 X86::NOT32r, X86::EAX,
10876 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010877 case X86::ATOMXOR32:
10878 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010879 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010880 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010881 X86::NOT32r, X86::EAX,
10882 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010883 case X86::ATOMNAND32:
10884 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010885 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010886 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010887 X86::NOT32r, X86::EAX,
10888 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010889 case X86::ATOMMIN32:
10890 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10891 case X86::ATOMMAX32:
10892 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10893 case X86::ATOMUMIN32:
10894 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10895 case X86::ATOMUMAX32:
10896 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010897
10898 case X86::ATOMAND16:
10899 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10900 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010901 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010902 X86::NOT16r, X86::AX,
10903 X86::GR16RegisterClass);
10904 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010905 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010906 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010907 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010908 X86::NOT16r, X86::AX,
10909 X86::GR16RegisterClass);
10910 case X86::ATOMXOR16:
10911 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10912 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010913 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010914 X86::NOT16r, X86::AX,
10915 X86::GR16RegisterClass);
10916 case X86::ATOMNAND16:
10917 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10918 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010919 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010920 X86::NOT16r, X86::AX,
10921 X86::GR16RegisterClass, true);
10922 case X86::ATOMMIN16:
10923 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10924 case X86::ATOMMAX16:
10925 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10926 case X86::ATOMUMIN16:
10927 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10928 case X86::ATOMUMAX16:
10929 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10930
10931 case X86::ATOMAND8:
10932 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10933 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010934 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010935 X86::NOT8r, X86::AL,
10936 X86::GR8RegisterClass);
10937 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010938 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010939 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010940 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010941 X86::NOT8r, X86::AL,
10942 X86::GR8RegisterClass);
10943 case X86::ATOMXOR8:
10944 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10945 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010946 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010947 X86::NOT8r, X86::AL,
10948 X86::GR8RegisterClass);
10949 case X86::ATOMNAND8:
10950 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10951 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010952 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010953 X86::NOT8r, X86::AL,
10954 X86::GR8RegisterClass, true);
10955 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010956 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010957 case X86::ATOMAND64:
10958 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010959 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010960 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010961 X86::NOT64r, X86::RAX,
10962 X86::GR64RegisterClass);
10963 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010964 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10965 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010966 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010967 X86::NOT64r, X86::RAX,
10968 X86::GR64RegisterClass);
10969 case X86::ATOMXOR64:
10970 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010971 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010972 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010973 X86::NOT64r, X86::RAX,
10974 X86::GR64RegisterClass);
10975 case X86::ATOMNAND64:
10976 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10977 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010978 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010979 X86::NOT64r, X86::RAX,
10980 X86::GR64RegisterClass, true);
10981 case X86::ATOMMIN64:
10982 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10983 case X86::ATOMMAX64:
10984 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10985 case X86::ATOMUMIN64:
10986 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10987 case X86::ATOMUMAX64:
10988 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010989
10990 // This group does 64-bit operations on a 32-bit host.
10991 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010992 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010993 X86::AND32rr, X86::AND32rr,
10994 X86::AND32ri, X86::AND32ri,
10995 false);
10996 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010997 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010998 X86::OR32rr, X86::OR32rr,
10999 X86::OR32ri, X86::OR32ri,
11000 false);
11001 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011002 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011003 X86::XOR32rr, X86::XOR32rr,
11004 X86::XOR32ri, X86::XOR32ri,
11005 false);
11006 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011007 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011008 X86::AND32rr, X86::AND32rr,
11009 X86::AND32ri, X86::AND32ri,
11010 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011011 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011012 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011013 X86::ADD32rr, X86::ADC32rr,
11014 X86::ADD32ri, X86::ADC32ri,
11015 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011016 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011017 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011018 X86::SUB32rr, X86::SBB32rr,
11019 X86::SUB32ri, X86::SBB32ri,
11020 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000011021 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011022 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000011023 X86::MOV32rr, X86::MOV32rr,
11024 X86::MOV32ri, X86::MOV32ri,
11025 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011026 case X86::VASTART_SAVE_XMM_REGS:
11027 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000011028
11029 case X86::VAARG_64:
11030 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011031 }
11032}
11033
11034//===----------------------------------------------------------------------===//
11035// X86 Optimization Hooks
11036//===----------------------------------------------------------------------===//
11037
Dan Gohman475871a2008-07-27 21:46:04 +000011038void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000011039 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011040 APInt &KnownZero,
11041 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000011042 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000011043 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011044 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000011045 assert((Opc >= ISD::BUILTIN_OP_END ||
11046 Opc == ISD::INTRINSIC_WO_CHAIN ||
11047 Opc == ISD::INTRINSIC_W_CHAIN ||
11048 Opc == ISD::INTRINSIC_VOID) &&
11049 "Should use MaskedValueIsZero if you don't know whether Op"
11050 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011051
Dan Gohmanf4f92f52008-02-13 23:07:24 +000011052 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011053 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000011054 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011055 case X86ISD::ADD:
11056 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000011057 case X86ISD::ADC:
11058 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011059 case X86ISD::SMUL:
11060 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011061 case X86ISD::INC:
11062 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011063 case X86ISD::OR:
11064 case X86ISD::XOR:
11065 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011066 // These nodes' second result is a boolean.
11067 if (Op.getResNo() == 0)
11068 break;
11069 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011070 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011071 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11072 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011073 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011074 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011075}
Chris Lattner259e97c2006-01-31 19:43:35 +000011076
Owen Andersonbc146b02010-09-21 20:42:50 +000011077unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11078 unsigned Depth) const {
11079 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11080 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11081 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011082
Owen Andersonbc146b02010-09-21 20:42:50 +000011083 // Fallback case.
11084 return 1;
11085}
11086
Evan Cheng206ee9d2006-07-07 08:33:52 +000011087/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011088/// node is a GlobalAddress + offset.
11089bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011090 const GlobalValue* &GA,
11091 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011092 if (N->getOpcode() == X86ISD::Wrapper) {
11093 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011094 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011095 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011096 return true;
11097 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011098 }
Evan Chengad4196b2008-05-12 19:56:52 +000011099 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011100}
11101
Evan Cheng206ee9d2006-07-07 08:33:52 +000011102/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11103/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11104/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011105/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011106static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011107 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011108 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011109 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011110
Eli Friedman7a5e5552009-06-07 06:52:44 +000011111 if (VT.getSizeInBits() != 128)
11112 return SDValue();
11113
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011114 // Don't create instructions with illegal types after legalize types has run.
11115 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11116 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11117 return SDValue();
11118
Nate Begemanfdea31a2010-03-24 20:49:50 +000011119 SmallVector<SDValue, 16> Elts;
11120 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011121 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011122
Nate Begemanfdea31a2010-03-24 20:49:50 +000011123 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011124}
Evan Chengd880b972008-05-09 21:53:03 +000011125
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011126/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11127/// generation and convert it from being a bunch of shuffles and extracts
11128/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011129static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11130 const TargetLowering &TLI) {
11131 SDValue InputVector = N->getOperand(0);
11132
11133 // Only operate on vectors of 4 elements, where the alternative shuffling
11134 // gets to be more expensive.
11135 if (InputVector.getValueType() != MVT::v4i32)
11136 return SDValue();
11137
11138 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11139 // single use which is a sign-extend or zero-extend, and all elements are
11140 // used.
11141 SmallVector<SDNode *, 4> Uses;
11142 unsigned ExtractedElements = 0;
11143 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11144 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11145 if (UI.getUse().getResNo() != InputVector.getResNo())
11146 return SDValue();
11147
11148 SDNode *Extract = *UI;
11149 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11150 return SDValue();
11151
11152 if (Extract->getValueType(0) != MVT::i32)
11153 return SDValue();
11154 if (!Extract->hasOneUse())
11155 return SDValue();
11156 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11157 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11158 return SDValue();
11159 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11160 return SDValue();
11161
11162 // Record which element was extracted.
11163 ExtractedElements |=
11164 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11165
11166 Uses.push_back(Extract);
11167 }
11168
11169 // If not all the elements were used, this may not be worthwhile.
11170 if (ExtractedElements != 15)
11171 return SDValue();
11172
11173 // Ok, we've now decided to do the transformation.
11174 DebugLoc dl = InputVector.getDebugLoc();
11175
11176 // Store the value to a temporary stack slot.
11177 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011178 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11179 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011180
11181 // Replace each use (extract) with a load of the appropriate element.
11182 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11183 UE = Uses.end(); UI != UE; ++UI) {
11184 SDNode *Extract = *UI;
11185
Nadav Rotem86694292011-05-17 08:31:57 +000011186 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011187 SDValue Idx = Extract->getOperand(1);
11188 unsigned EltSize =
11189 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11190 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11191 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11192
Nadav Rotem86694292011-05-17 08:31:57 +000011193 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011194 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011195
11196 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011197 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011198 ScalarAddr, MachinePointerInfo(),
11199 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011200
11201 // Replace the exact with the load.
11202 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11203 }
11204
11205 // The replacement was made in place; don't return anything.
11206 return SDValue();
11207}
11208
Chris Lattner83e6c992006-10-04 06:57:07 +000011209/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011210static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011211 const X86Subtarget *Subtarget) {
11212 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011213 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011214 // Get the LHS/RHS of the select.
11215 SDValue LHS = N->getOperand(1);
11216 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011217
Dan Gohman670e5392009-09-21 18:03:22 +000011218 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011219 // instructions match the semantics of the common C idiom x<y?x:y but not
11220 // x<=y?x:y, because of how they handle negative zero (which can be
11221 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011222 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011223 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011224 Cond.getOpcode() == ISD::SETCC) {
11225 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011226
Chris Lattner47b4ce82009-03-11 05:48:52 +000011227 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011228 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011229 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11230 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011231 switch (CC) {
11232 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011233 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011234 // Converting this to a min would handle NaNs incorrectly, and swapping
11235 // the operands would cause it to handle comparisons between positive
11236 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011237 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011238 if (!UnsafeFPMath &&
11239 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11240 break;
11241 std::swap(LHS, RHS);
11242 }
Dan Gohman670e5392009-09-21 18:03:22 +000011243 Opcode = X86ISD::FMIN;
11244 break;
11245 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011246 // Converting this to a min would handle comparisons between positive
11247 // and negative zero incorrectly.
11248 if (!UnsafeFPMath &&
11249 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11250 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011251 Opcode = X86ISD::FMIN;
11252 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011253 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011254 // Converting this to a min would handle both negative zeros and NaNs
11255 // incorrectly, but we can swap the operands to fix both.
11256 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011257 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011258 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011259 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011260 Opcode = X86ISD::FMIN;
11261 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011262
Dan Gohman670e5392009-09-21 18:03:22 +000011263 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011264 // Converting this to a max would handle comparisons between positive
11265 // and negative zero incorrectly.
11266 if (!UnsafeFPMath &&
11267 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11268 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011269 Opcode = X86ISD::FMAX;
11270 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011271 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011272 // Converting this to a max would handle NaNs incorrectly, and swapping
11273 // the operands would cause it to handle comparisons between positive
11274 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011275 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011276 if (!UnsafeFPMath &&
11277 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11278 break;
11279 std::swap(LHS, RHS);
11280 }
Dan Gohman670e5392009-09-21 18:03:22 +000011281 Opcode = X86ISD::FMAX;
11282 break;
11283 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011284 // Converting this to a max would handle both negative zeros and NaNs
11285 // incorrectly, but we can swap the operands to fix both.
11286 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011287 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011288 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011289 case ISD::SETGE:
11290 Opcode = X86ISD::FMAX;
11291 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011292 }
Dan Gohman670e5392009-09-21 18:03:22 +000011293 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011294 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11295 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011296 switch (CC) {
11297 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011298 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011299 // Converting this to a min would handle comparisons between positive
11300 // and negative zero incorrectly, and swapping the operands would
11301 // cause it to handle NaNs incorrectly.
11302 if (!UnsafeFPMath &&
11303 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011304 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011305 break;
11306 std::swap(LHS, RHS);
11307 }
Dan Gohman670e5392009-09-21 18:03:22 +000011308 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011309 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011310 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011311 // Converting this to a min would handle NaNs incorrectly.
11312 if (!UnsafeFPMath &&
11313 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11314 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011315 Opcode = X86ISD::FMIN;
11316 break;
11317 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011318 // Converting this to a min would handle both negative zeros and NaNs
11319 // incorrectly, but we can swap the operands to fix both.
11320 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011321 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011322 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011323 case ISD::SETGE:
11324 Opcode = X86ISD::FMIN;
11325 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011326
Dan Gohman670e5392009-09-21 18:03:22 +000011327 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011328 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011329 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011330 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011331 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011332 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011333 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011334 // Converting this to a max would handle comparisons between positive
11335 // and negative zero incorrectly, and swapping the operands would
11336 // cause it to handle NaNs incorrectly.
11337 if (!UnsafeFPMath &&
11338 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011339 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011340 break;
11341 std::swap(LHS, RHS);
11342 }
Dan Gohman670e5392009-09-21 18:03:22 +000011343 Opcode = X86ISD::FMAX;
11344 break;
11345 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011346 // Converting this to a max would handle both negative zeros and NaNs
11347 // incorrectly, but we can swap the operands to fix both.
11348 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011349 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011350 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011351 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011352 Opcode = X86ISD::FMAX;
11353 break;
11354 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011355 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011356
Chris Lattner47b4ce82009-03-11 05:48:52 +000011357 if (Opcode)
11358 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011359 }
Eric Christopherfd179292009-08-27 18:07:15 +000011360
Chris Lattnerd1980a52009-03-12 06:52:53 +000011361 // If this is a select between two integer constants, try to do some
11362 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011363 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11364 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011365 // Don't do this for crazy integer types.
11366 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11367 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011368 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011369 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011370
Chris Lattnercee56e72009-03-13 05:53:31 +000011371 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011372 // Efficiently invertible.
11373 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11374 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11375 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11376 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011377 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011378 }
Eric Christopherfd179292009-08-27 18:07:15 +000011379
Chris Lattnerd1980a52009-03-12 06:52:53 +000011380 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011381 if (FalseC->getAPIntValue() == 0 &&
11382 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011383 if (NeedsCondInvert) // Invert the condition if needed.
11384 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11385 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011386
Chris Lattnerd1980a52009-03-12 06:52:53 +000011387 // Zero extend the condition if needed.
11388 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011389
Chris Lattnercee56e72009-03-13 05:53:31 +000011390 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011391 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011392 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011393 }
Eric Christopherfd179292009-08-27 18:07:15 +000011394
Chris Lattner97a29a52009-03-13 05:22:11 +000011395 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011396 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011397 if (NeedsCondInvert) // Invert the condition if needed.
11398 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11399 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011400
Chris Lattner97a29a52009-03-13 05:22:11 +000011401 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011402 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11403 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011404 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011405 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011406 }
Eric Christopherfd179292009-08-27 18:07:15 +000011407
Chris Lattnercee56e72009-03-13 05:53:31 +000011408 // Optimize cases that will turn into an LEA instruction. This requires
11409 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011410 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011411 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011412 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011413
Chris Lattnercee56e72009-03-13 05:53:31 +000011414 bool isFastMultiplier = false;
11415 if (Diff < 10) {
11416 switch ((unsigned char)Diff) {
11417 default: break;
11418 case 1: // result = add base, cond
11419 case 2: // result = lea base( , cond*2)
11420 case 3: // result = lea base(cond, cond*2)
11421 case 4: // result = lea base( , cond*4)
11422 case 5: // result = lea base(cond, cond*4)
11423 case 8: // result = lea base( , cond*8)
11424 case 9: // result = lea base(cond, cond*8)
11425 isFastMultiplier = true;
11426 break;
11427 }
11428 }
Eric Christopherfd179292009-08-27 18:07:15 +000011429
Chris Lattnercee56e72009-03-13 05:53:31 +000011430 if (isFastMultiplier) {
11431 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11432 if (NeedsCondInvert) // Invert the condition if needed.
11433 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11434 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011435
Chris Lattnercee56e72009-03-13 05:53:31 +000011436 // Zero extend the condition if needed.
11437 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11438 Cond);
11439 // Scale the condition by the difference.
11440 if (Diff != 1)
11441 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11442 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011443
Chris Lattnercee56e72009-03-13 05:53:31 +000011444 // Add the base if non-zero.
11445 if (FalseC->getAPIntValue() != 0)
11446 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11447 SDValue(FalseC, 0));
11448 return Cond;
11449 }
Eric Christopherfd179292009-08-27 18:07:15 +000011450 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011451 }
11452 }
Eric Christopherfd179292009-08-27 18:07:15 +000011453
Dan Gohman475871a2008-07-27 21:46:04 +000011454 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011455}
11456
Chris Lattnerd1980a52009-03-12 06:52:53 +000011457/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11458static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11459 TargetLowering::DAGCombinerInfo &DCI) {
11460 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011461
Chris Lattnerd1980a52009-03-12 06:52:53 +000011462 // If the flag operand isn't dead, don't touch this CMOV.
11463 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11464 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011465
Evan Chengb5a55d92011-05-24 01:48:22 +000011466 SDValue FalseOp = N->getOperand(0);
11467 SDValue TrueOp = N->getOperand(1);
11468 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11469 SDValue Cond = N->getOperand(3);
11470 if (CC == X86::COND_E || CC == X86::COND_NE) {
11471 switch (Cond.getOpcode()) {
11472 default: break;
11473 case X86ISD::BSR:
11474 case X86ISD::BSF:
11475 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11476 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11477 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11478 }
11479 }
11480
Chris Lattnerd1980a52009-03-12 06:52:53 +000011481 // If this is a select between two integer constants, try to do some
11482 // optimizations. Note that the operands are ordered the opposite of SELECT
11483 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011484 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11485 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011486 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11487 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011488 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11489 CC = X86::GetOppositeBranchCondition(CC);
11490 std::swap(TrueC, FalseC);
11491 }
Eric Christopherfd179292009-08-27 18:07:15 +000011492
Chris Lattnerd1980a52009-03-12 06:52:53 +000011493 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011494 // This is efficient for any integer data type (including i8/i16) and
11495 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011496 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011497 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11498 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011499
Chris Lattnerd1980a52009-03-12 06:52:53 +000011500 // Zero extend the condition if needed.
11501 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011502
Chris Lattnerd1980a52009-03-12 06:52:53 +000011503 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11504 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011505 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011506 if (N->getNumValues() == 2) // Dead flag value?
11507 return DCI.CombineTo(N, Cond, SDValue());
11508 return Cond;
11509 }
Eric Christopherfd179292009-08-27 18:07:15 +000011510
Chris Lattnercee56e72009-03-13 05:53:31 +000011511 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11512 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011513 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011514 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11515 DAG.getConstant(CC, MVT::i8), Cond);
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 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11521 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011522
Chris Lattner97a29a52009-03-13 05:22:11 +000011523 if (N->getNumValues() == 2) // Dead flag value?
11524 return DCI.CombineTo(N, Cond, SDValue());
11525 return Cond;
11526 }
Eric Christopherfd179292009-08-27 18:07:15 +000011527
Chris Lattnercee56e72009-03-13 05:53:31 +000011528 // Optimize cases that will turn into an LEA instruction. This requires
11529 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011530 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011531 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011532 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011533
Chris Lattnercee56e72009-03-13 05:53:31 +000011534 bool isFastMultiplier = false;
11535 if (Diff < 10) {
11536 switch ((unsigned char)Diff) {
11537 default: break;
11538 case 1: // result = add base, cond
11539 case 2: // result = lea base( , cond*2)
11540 case 3: // result = lea base(cond, cond*2)
11541 case 4: // result = lea base( , cond*4)
11542 case 5: // result = lea base(cond, cond*4)
11543 case 8: // result = lea base( , cond*8)
11544 case 9: // result = lea base(cond, cond*8)
11545 isFastMultiplier = true;
11546 break;
11547 }
11548 }
Eric Christopherfd179292009-08-27 18:07:15 +000011549
Chris Lattnercee56e72009-03-13 05:53:31 +000011550 if (isFastMultiplier) {
11551 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011552 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11553 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011554 // Zero extend the condition if needed.
11555 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11556 Cond);
11557 // Scale the condition by the difference.
11558 if (Diff != 1)
11559 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11560 DAG.getConstant(Diff, Cond.getValueType()));
11561
11562 // Add the base if non-zero.
11563 if (FalseC->getAPIntValue() != 0)
11564 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11565 SDValue(FalseC, 0));
11566 if (N->getNumValues() == 2) // Dead flag value?
11567 return DCI.CombineTo(N, Cond, SDValue());
11568 return Cond;
11569 }
Eric Christopherfd179292009-08-27 18:07:15 +000011570 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011571 }
11572 }
11573 return SDValue();
11574}
11575
11576
Evan Cheng0b0cd912009-03-28 05:57:29 +000011577/// PerformMulCombine - Optimize a single multiply with constant into two
11578/// in order to implement it with two cheaper instructions, e.g.
11579/// LEA + SHL, LEA + LEA.
11580static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11581 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011582 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11583 return SDValue();
11584
Owen Andersone50ed302009-08-10 22:56:29 +000011585 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011586 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011587 return SDValue();
11588
11589 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11590 if (!C)
11591 return SDValue();
11592 uint64_t MulAmt = C->getZExtValue();
11593 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11594 return SDValue();
11595
11596 uint64_t MulAmt1 = 0;
11597 uint64_t MulAmt2 = 0;
11598 if ((MulAmt % 9) == 0) {
11599 MulAmt1 = 9;
11600 MulAmt2 = MulAmt / 9;
11601 } else if ((MulAmt % 5) == 0) {
11602 MulAmt1 = 5;
11603 MulAmt2 = MulAmt / 5;
11604 } else if ((MulAmt % 3) == 0) {
11605 MulAmt1 = 3;
11606 MulAmt2 = MulAmt / 3;
11607 }
11608 if (MulAmt2 &&
11609 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11610 DebugLoc DL = N->getDebugLoc();
11611
11612 if (isPowerOf2_64(MulAmt2) &&
11613 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11614 // If second multiplifer is pow2, issue it first. We want the multiply by
11615 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11616 // is an add.
11617 std::swap(MulAmt1, MulAmt2);
11618
11619 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011620 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011621 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011622 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011623 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011624 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011625 DAG.getConstant(MulAmt1, VT));
11626
Eric Christopherfd179292009-08-27 18:07:15 +000011627 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011628 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011629 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011630 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011631 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011632 DAG.getConstant(MulAmt2, VT));
11633
11634 // Do not add new nodes to DAG combiner worklist.
11635 DCI.CombineTo(N, NewMul, false);
11636 }
11637 return SDValue();
11638}
11639
Evan Chengad9c0a32009-12-15 00:53:42 +000011640static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11641 SDValue N0 = N->getOperand(0);
11642 SDValue N1 = N->getOperand(1);
11643 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11644 EVT VT = N0.getValueType();
11645
11646 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11647 // since the result of setcc_c is all zero's or all ones.
11648 if (N1C && N0.getOpcode() == ISD::AND &&
11649 N0.getOperand(1).getOpcode() == ISD::Constant) {
11650 SDValue N00 = N0.getOperand(0);
11651 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11652 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11653 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11654 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11655 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11656 APInt ShAmt = N1C->getAPIntValue();
11657 Mask = Mask.shl(ShAmt);
11658 if (Mask != 0)
11659 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11660 N00, DAG.getConstant(Mask, VT));
11661 }
11662 }
11663
11664 return SDValue();
11665}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011666
Nate Begeman740ab032009-01-26 00:52:55 +000011667/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11668/// when possible.
11669static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11670 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011671 EVT VT = N->getValueType(0);
11672 if (!VT.isVector() && VT.isInteger() &&
11673 N->getOpcode() == ISD::SHL)
11674 return PerformSHLCombine(N, DAG);
11675
Nate Begeman740ab032009-01-26 00:52:55 +000011676 // On X86 with SSE2 support, we can transform this to a vector shift if
11677 // all elements are shifted by the same amount. We can't do this in legalize
11678 // because the a constant vector is typically transformed to a constant pool
11679 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011680 if (!Subtarget->hasSSE2())
11681 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011682
Owen Anderson825b72b2009-08-11 20:47:22 +000011683 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011684 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011685
Mon P Wang3becd092009-01-28 08:12:05 +000011686 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011687 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011688 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011689 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011690 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11691 unsigned NumElts = VT.getVectorNumElements();
11692 unsigned i = 0;
11693 for (; i != NumElts; ++i) {
11694 SDValue Arg = ShAmtOp.getOperand(i);
11695 if (Arg.getOpcode() == ISD::UNDEF) continue;
11696 BaseShAmt = Arg;
11697 break;
11698 }
11699 for (; i != NumElts; ++i) {
11700 SDValue Arg = ShAmtOp.getOperand(i);
11701 if (Arg.getOpcode() == ISD::UNDEF) continue;
11702 if (Arg != BaseShAmt) {
11703 return SDValue();
11704 }
11705 }
11706 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011707 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011708 SDValue InVec = ShAmtOp.getOperand(0);
11709 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11710 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11711 unsigned i = 0;
11712 for (; i != NumElts; ++i) {
11713 SDValue Arg = InVec.getOperand(i);
11714 if (Arg.getOpcode() == ISD::UNDEF) continue;
11715 BaseShAmt = Arg;
11716 break;
11717 }
11718 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11719 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011720 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011721 if (C->getZExtValue() == SplatIdx)
11722 BaseShAmt = InVec.getOperand(1);
11723 }
11724 }
11725 if (BaseShAmt.getNode() == 0)
11726 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11727 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011728 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011729 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011730
Mon P Wangefa42202009-09-03 19:56:25 +000011731 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011732 if (EltVT.bitsGT(MVT::i32))
11733 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11734 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011735 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011736
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011737 // The shift amount is identical so we can do a vector shift.
11738 SDValue ValOp = N->getOperand(0);
11739 switch (N->getOpcode()) {
11740 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011741 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011742 break;
11743 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011744 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011745 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011746 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011747 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011748 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011749 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011750 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011751 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011752 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011753 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011754 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011755 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011756 break;
11757 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011758 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011759 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011760 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011761 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011762 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011763 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011764 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011765 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011766 break;
11767 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011768 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011769 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011770 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011771 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011772 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011773 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011774 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011775 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011776 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011777 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011778 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011779 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011780 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011781 }
11782 return SDValue();
11783}
11784
Nate Begemanb65c1752010-12-17 22:55:37 +000011785
Stuart Hastings865f0932011-06-03 23:53:54 +000011786// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11787// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11788// and friends. Likewise for OR -> CMPNEQSS.
11789static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11790 TargetLowering::DAGCombinerInfo &DCI,
11791 const X86Subtarget *Subtarget) {
11792 unsigned opcode;
11793
11794 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11795 // we're requiring SSE2 for both.
11796 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11797 SDValue N0 = N->getOperand(0);
11798 SDValue N1 = N->getOperand(1);
11799 SDValue CMP0 = N0->getOperand(1);
11800 SDValue CMP1 = N1->getOperand(1);
11801 DebugLoc DL = N->getDebugLoc();
11802
11803 // The SETCCs should both refer to the same CMP.
11804 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11805 return SDValue();
11806
11807 SDValue CMP00 = CMP0->getOperand(0);
11808 SDValue CMP01 = CMP0->getOperand(1);
11809 EVT VT = CMP00.getValueType();
11810
11811 if (VT == MVT::f32 || VT == MVT::f64) {
11812 bool ExpectingFlags = false;
11813 // Check for any users that want flags:
11814 for (SDNode::use_iterator UI = N->use_begin(),
11815 UE = N->use_end();
11816 !ExpectingFlags && UI != UE; ++UI)
11817 switch (UI->getOpcode()) {
11818 default:
11819 case ISD::BR_CC:
11820 case ISD::BRCOND:
11821 case ISD::SELECT:
11822 ExpectingFlags = true;
11823 break;
11824 case ISD::CopyToReg:
11825 case ISD::SIGN_EXTEND:
11826 case ISD::ZERO_EXTEND:
11827 case ISD::ANY_EXTEND:
11828 break;
11829 }
11830
11831 if (!ExpectingFlags) {
11832 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11833 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11834
11835 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11836 X86::CondCode tmp = cc0;
11837 cc0 = cc1;
11838 cc1 = tmp;
11839 }
11840
11841 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11842 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11843 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11844 X86ISD::NodeType NTOperator = is64BitFP ?
11845 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11846 // FIXME: need symbolic constants for these magic numbers.
11847 // See X86ATTInstPrinter.cpp:printSSECC().
11848 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11849 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11850 DAG.getConstant(x86cc, MVT::i8));
11851 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11852 OnesOrZeroesF);
11853 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11854 DAG.getConstant(1, MVT::i32));
11855 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11856 return OneBitOfTruth;
11857 }
11858 }
11859 }
11860 }
11861 return SDValue();
11862}
11863
Nate Begemanb65c1752010-12-17 22:55:37 +000011864static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11865 TargetLowering::DAGCombinerInfo &DCI,
11866 const X86Subtarget *Subtarget) {
11867 if (DCI.isBeforeLegalizeOps())
11868 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011869
Stuart Hastings865f0932011-06-03 23:53:54 +000011870 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11871 if (R.getNode())
11872 return R;
11873
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011874 // Want to form ANDNP nodes:
11875 // 1) In the hopes of then easily combining them with OR and AND nodes
11876 // to form PBLEND/PSIGN.
11877 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000011878 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011879 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000011880 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011881
Nate Begemanb65c1752010-12-17 22:55:37 +000011882 SDValue N0 = N->getOperand(0);
11883 SDValue N1 = N->getOperand(1);
11884 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011885
Nate Begemanb65c1752010-12-17 22:55:37 +000011886 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011887 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011888 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011889 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000011890
11891 // Check RHS for vnot
11892 if (N1.getOpcode() == ISD::XOR &&
11893 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011894 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011895
Nate Begemanb65c1752010-12-17 22:55:37 +000011896 return SDValue();
11897}
11898
Evan Cheng760d1942010-01-04 21:22:48 +000011899static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011900 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011901 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011902 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011903 return SDValue();
11904
Stuart Hastings865f0932011-06-03 23:53:54 +000011905 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11906 if (R.getNode())
11907 return R;
11908
Evan Cheng760d1942010-01-04 21:22:48 +000011909 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011910 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011911 return SDValue();
11912
Evan Cheng760d1942010-01-04 21:22:48 +000011913 SDValue N0 = N->getOperand(0);
11914 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011915
Nate Begemanb65c1752010-12-17 22:55:37 +000011916 // look for psign/blend
11917 if (Subtarget->hasSSSE3()) {
11918 if (VT == MVT::v2i64) {
11919 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011920 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000011921 std::swap(N0, N1);
11922 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011923 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000011924 SDValue Mask = N1.getOperand(0);
11925 SDValue X = N1.getOperand(1);
11926 SDValue Y;
11927 if (N0.getOperand(0) == Mask)
11928 Y = N0.getOperand(1);
11929 if (N0.getOperand(1) == Mask)
11930 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011931
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011932 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000011933 if (!Y.getNode())
11934 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011935
Nate Begemanb65c1752010-12-17 22:55:37 +000011936 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11937 if (Mask.getOpcode() != ISD::BITCAST ||
11938 X.getOpcode() != ISD::BITCAST ||
11939 Y.getOpcode() != ISD::BITCAST)
11940 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011941
Nate Begemanb65c1752010-12-17 22:55:37 +000011942 // Look through mask bitcast.
11943 Mask = Mask.getOperand(0);
11944 EVT MaskVT = Mask.getValueType();
11945
11946 // Validate that the Mask operand is a vector sra node. The sra node
11947 // will be an intrinsic.
11948 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11949 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011950
Nate Begemanb65c1752010-12-17 22:55:37 +000011951 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11952 // there is no psrai.b
11953 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11954 case Intrinsic::x86_sse2_psrai_w:
11955 case Intrinsic::x86_sse2_psrai_d:
11956 break;
11957 default: return SDValue();
11958 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011959
Nate Begemanb65c1752010-12-17 22:55:37 +000011960 // Check that the SRA is all signbits.
11961 SDValue SraC = Mask.getOperand(2);
11962 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11963 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11964 if ((SraAmt + 1) != EltBits)
11965 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011966
Nate Begemanb65c1752010-12-17 22:55:37 +000011967 DebugLoc DL = N->getDebugLoc();
11968
11969 // Now we know we at least have a plendvb with the mask val. See if
11970 // we can form a psignb/w/d.
11971 // psign = x.type == y.type == mask.type && y = sub(0, x);
11972 X = X.getOperand(0);
11973 Y = Y.getOperand(0);
11974 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11975 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11976 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11977 unsigned Opc = 0;
11978 switch (EltBits) {
11979 case 8: Opc = X86ISD::PSIGNB; break;
11980 case 16: Opc = X86ISD::PSIGNW; break;
11981 case 32: Opc = X86ISD::PSIGND; break;
11982 default: break;
11983 }
11984 if (Opc) {
11985 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11986 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11987 }
11988 }
11989 // PBLENDVB only available on SSE 4.1
11990 if (!Subtarget->hasSSE41())
11991 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011992
Nate Begemanb65c1752010-12-17 22:55:37 +000011993 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11994 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11995 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011996 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011997 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11998 }
11999 }
12000 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012001
Nate Begemanb65c1752010-12-17 22:55:37 +000012002 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000012003 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
12004 std::swap(N0, N1);
12005 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
12006 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000012007 if (!N0.hasOneUse() || !N1.hasOneUse())
12008 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000012009
12010 SDValue ShAmt0 = N0.getOperand(1);
12011 if (ShAmt0.getValueType() != MVT::i8)
12012 return SDValue();
12013 SDValue ShAmt1 = N1.getOperand(1);
12014 if (ShAmt1.getValueType() != MVT::i8)
12015 return SDValue();
12016 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
12017 ShAmt0 = ShAmt0.getOperand(0);
12018 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
12019 ShAmt1 = ShAmt1.getOperand(0);
12020
12021 DebugLoc DL = N->getDebugLoc();
12022 unsigned Opc = X86ISD::SHLD;
12023 SDValue Op0 = N0.getOperand(0);
12024 SDValue Op1 = N1.getOperand(0);
12025 if (ShAmt0.getOpcode() == ISD::SUB) {
12026 Opc = X86ISD::SHRD;
12027 std::swap(Op0, Op1);
12028 std::swap(ShAmt0, ShAmt1);
12029 }
12030
Evan Cheng8b1190a2010-04-28 01:18:01 +000012031 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000012032 if (ShAmt1.getOpcode() == ISD::SUB) {
12033 SDValue Sum = ShAmt1.getOperand(0);
12034 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000012035 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
12036 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
12037 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
12038 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000012039 return DAG.getNode(Opc, DL, VT,
12040 Op0, Op1,
12041 DAG.getNode(ISD::TRUNCATE, DL,
12042 MVT::i8, ShAmt0));
12043 }
12044 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
12045 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
12046 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000012047 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000012048 return DAG.getNode(Opc, DL, VT,
12049 N0.getOperand(0), N1.getOperand(0),
12050 DAG.getNode(ISD::TRUNCATE, DL,
12051 MVT::i8, ShAmt0));
12052 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012053
Evan Cheng760d1942010-01-04 21:22:48 +000012054 return SDValue();
12055}
12056
Chris Lattner149a4e52008-02-22 02:09:43 +000012057/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012058static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000012059 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000012060 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
12061 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000012062 // A preferable solution to the general problem is to figure out the right
12063 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000012064
12065 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012066 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012067 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012068 if (VT.getSizeInBits() != 64)
12069 return SDValue();
12070
Devang Patel578efa92009-06-05 21:57:13 +000012071 const Function *F = DAG.getMachineFunction().getFunction();
12072 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012073 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012074 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012075 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012076 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012077 isa<LoadSDNode>(St->getValue()) &&
12078 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12079 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012080 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012081 LoadSDNode *Ld = 0;
12082 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012083 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012084 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012085 // Must be a store of a load. We currently handle two cases: the load
12086 // is a direct child, and it's under an intervening TokenFactor. It is
12087 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012088 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012089 Ld = cast<LoadSDNode>(St->getChain());
12090 else if (St->getValue().hasOneUse() &&
12091 ChainVal->getOpcode() == ISD::TokenFactor) {
12092 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012093 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012094 TokenFactorIndex = i;
12095 Ld = cast<LoadSDNode>(St->getValue());
12096 } else
12097 Ops.push_back(ChainVal->getOperand(i));
12098 }
12099 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012100
Evan Cheng536e6672009-03-12 05:59:15 +000012101 if (!Ld || !ISD::isNormalLoad(Ld))
12102 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012103
Evan Cheng536e6672009-03-12 05:59:15 +000012104 // If this is not the MMX case, i.e. we are just turning i64 load/store
12105 // into f64 load/store, avoid the transformation if there are multiple
12106 // uses of the loaded value.
12107 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12108 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012109
Evan Cheng536e6672009-03-12 05:59:15 +000012110 DebugLoc LdDL = Ld->getDebugLoc();
12111 DebugLoc StDL = N->getDebugLoc();
12112 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12113 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12114 // pair instead.
12115 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012116 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012117 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12118 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012119 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012120 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012121 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012122 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012123 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012124 Ops.size());
12125 }
Evan Cheng536e6672009-03-12 05:59:15 +000012126 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012127 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012128 St->isVolatile(), St->isNonTemporal(),
12129 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012130 }
Evan Cheng536e6672009-03-12 05:59:15 +000012131
12132 // Otherwise, lower to two pairs of 32-bit loads / stores.
12133 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012134 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12135 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012136
Owen Anderson825b72b2009-08-11 20:47:22 +000012137 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012138 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012139 Ld->isVolatile(), Ld->isNonTemporal(),
12140 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012141 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012142 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012143 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012144 MinAlign(Ld->getAlignment(), 4));
12145
12146 SDValue NewChain = LoLd.getValue(1);
12147 if (TokenFactorIndex != -1) {
12148 Ops.push_back(LoLd);
12149 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012150 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012151 Ops.size());
12152 }
12153
12154 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012155 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12156 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012157
12158 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012159 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012160 St->isVolatile(), St->isNonTemporal(),
12161 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012162 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012163 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012164 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012165 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012166 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012167 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012168 }
Dan Gohman475871a2008-07-27 21:46:04 +000012169 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012170}
12171
Chris Lattner6cf73262008-01-25 06:14:17 +000012172/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12173/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012174static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012175 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12176 // F[X]OR(0.0, x) -> x
12177 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012178 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12179 if (C->getValueAPF().isPosZero())
12180 return N->getOperand(1);
12181 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12182 if (C->getValueAPF().isPosZero())
12183 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012184 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012185}
12186
12187/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012188static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012189 // FAND(0.0, x) -> 0.0
12190 // FAND(x, 0.0) -> 0.0
12191 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12192 if (C->getValueAPF().isPosZero())
12193 return N->getOperand(0);
12194 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12195 if (C->getValueAPF().isPosZero())
12196 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012197 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012198}
12199
Dan Gohmane5af2d32009-01-29 01:59:02 +000012200static SDValue PerformBTCombine(SDNode *N,
12201 SelectionDAG &DAG,
12202 TargetLowering::DAGCombinerInfo &DCI) {
12203 // BT ignores high bits in the bit index operand.
12204 SDValue Op1 = N->getOperand(1);
12205 if (Op1.hasOneUse()) {
12206 unsigned BitWidth = Op1.getValueSizeInBits();
12207 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12208 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012209 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12210 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012211 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012212 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12213 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12214 DCI.CommitTargetLoweringOpt(TLO);
12215 }
12216 return SDValue();
12217}
Chris Lattner83e6c992006-10-04 06:57:07 +000012218
Eli Friedman7a5e5552009-06-07 06:52:44 +000012219static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12220 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012221 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012222 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012223 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012224 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012225 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012226 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012227 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012228 }
12229 return SDValue();
12230}
12231
Evan Cheng2e489c42009-12-16 00:53:11 +000012232static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12233 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12234 // (and (i32 x86isd::setcc_carry), 1)
12235 // This eliminates the zext. This transformation is necessary because
12236 // ISD::SETCC is always legalized to i8.
12237 DebugLoc dl = N->getDebugLoc();
12238 SDValue N0 = N->getOperand(0);
12239 EVT VT = N->getValueType(0);
12240 if (N0.getOpcode() == ISD::AND &&
12241 N0.hasOneUse() &&
12242 N0.getOperand(0).hasOneUse()) {
12243 SDValue N00 = N0.getOperand(0);
12244 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12245 return SDValue();
12246 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12247 if (!C || C->getZExtValue() != 1)
12248 return SDValue();
12249 return DAG.getNode(ISD::AND, dl, VT,
12250 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12251 N00.getOperand(0), N00.getOperand(1)),
12252 DAG.getConstant(1, VT));
12253 }
12254
12255 return SDValue();
12256}
12257
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012258// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12259static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12260 unsigned X86CC = N->getConstantOperandVal(0);
12261 SDValue EFLAG = N->getOperand(1);
12262 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012263
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012264 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12265 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12266 // cases.
12267 if (X86CC == X86::COND_B)
12268 return DAG.getNode(ISD::AND, DL, MVT::i8,
12269 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12270 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12271 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012272
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012273 return SDValue();
12274}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012275
Benjamin Kramer1396c402011-06-18 11:09:41 +000012276static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12277 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012278 SDValue Op0 = N->getOperand(0);
12279 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12280 // a 32-bit target where SSE doesn't support i64->FP operations.
12281 if (Op0.getOpcode() == ISD::LOAD) {
12282 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12283 EVT VT = Ld->getValueType(0);
12284 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12285 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12286 !XTLI->getSubtarget()->is64Bit() &&
12287 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012288 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12289 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012290 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12291 return FILDChain;
12292 }
12293 }
12294 return SDValue();
12295}
12296
Chris Lattner23a01992010-12-20 01:37:09 +000012297// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12298static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12299 X86TargetLowering::DAGCombinerInfo &DCI) {
12300 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12301 // the result is either zero or one (depending on the input carry bit).
12302 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12303 if (X86::isZeroNode(N->getOperand(0)) &&
12304 X86::isZeroNode(N->getOperand(1)) &&
12305 // We don't have a good way to replace an EFLAGS use, so only do this when
12306 // dead right now.
12307 SDValue(N, 1).use_empty()) {
12308 DebugLoc DL = N->getDebugLoc();
12309 EVT VT = N->getValueType(0);
12310 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12311 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12312 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12313 DAG.getConstant(X86::COND_B,MVT::i8),
12314 N->getOperand(2)),
12315 DAG.getConstant(1, VT));
12316 return DCI.CombineTo(N, Res1, CarryOut);
12317 }
12318
12319 return SDValue();
12320}
12321
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012322// fold (add Y, (sete X, 0)) -> adc 0, Y
12323// (add Y, (setne X, 0)) -> sbb -1, Y
12324// (sub (sete X, 0), Y) -> sbb 0, Y
12325// (sub (setne X, 0), Y) -> adc -1, Y
12326static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12327 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012328
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012329 // Look through ZExts.
12330 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12331 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12332 return SDValue();
12333
12334 SDValue SetCC = Ext.getOperand(0);
12335 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12336 return SDValue();
12337
12338 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12339 if (CC != X86::COND_E && CC != X86::COND_NE)
12340 return SDValue();
12341
12342 SDValue Cmp = SetCC.getOperand(1);
12343 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012344 !X86::isZeroNode(Cmp.getOperand(1)) ||
12345 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012346 return SDValue();
12347
12348 SDValue CmpOp0 = Cmp.getOperand(0);
12349 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12350 DAG.getConstant(1, CmpOp0.getValueType()));
12351
12352 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12353 if (CC == X86::COND_NE)
12354 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12355 DL, OtherVal.getValueType(), OtherVal,
12356 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12357 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12358 DL, OtherVal.getValueType(), OtherVal,
12359 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12360}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012361
Dan Gohman475871a2008-07-27 21:46:04 +000012362SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012363 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012364 SelectionDAG &DAG = DCI.DAG;
12365 switch (N->getOpcode()) {
12366 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012367 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012368 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012369 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012370 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012371 case ISD::ADD:
12372 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012373 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012374 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012375 case ISD::SHL:
12376 case ISD::SRA:
12377 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012378 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012379 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012380 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012381 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012382 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012383 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12384 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012385 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012386 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012387 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012388 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012389 case X86ISD::SHUFPS: // Handle all target specific shuffles
12390 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012391 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012392 case X86ISD::PUNPCKHBW:
12393 case X86ISD::PUNPCKHWD:
12394 case X86ISD::PUNPCKHDQ:
12395 case X86ISD::PUNPCKHQDQ:
12396 case X86ISD::UNPCKHPS:
12397 case X86ISD::UNPCKHPD:
12398 case X86ISD::PUNPCKLBW:
12399 case X86ISD::PUNPCKLWD:
12400 case X86ISD::PUNPCKLDQ:
12401 case X86ISD::PUNPCKLQDQ:
12402 case X86ISD::UNPCKLPS:
12403 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012404 case X86ISD::VUNPCKLPS:
12405 case X86ISD::VUNPCKLPD:
12406 case X86ISD::VUNPCKLPSY:
12407 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012408 case X86ISD::MOVHLPS:
12409 case X86ISD::MOVLHPS:
12410 case X86ISD::PSHUFD:
12411 case X86ISD::PSHUFHW:
12412 case X86ISD::PSHUFLW:
12413 case X86ISD::MOVSS:
12414 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012415 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012416 }
12417
Dan Gohman475871a2008-07-27 21:46:04 +000012418 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012419}
12420
Evan Chenge5b51ac2010-04-17 06:13:15 +000012421/// isTypeDesirableForOp - Return true if the target has native support for
12422/// the specified value type and it is 'desirable' to use the type for the
12423/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12424/// instruction encodings are longer and some i16 instructions are slow.
12425bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12426 if (!isTypeLegal(VT))
12427 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012428 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012429 return true;
12430
12431 switch (Opc) {
12432 default:
12433 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012434 case ISD::LOAD:
12435 case ISD::SIGN_EXTEND:
12436 case ISD::ZERO_EXTEND:
12437 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012438 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012439 case ISD::SRL:
12440 case ISD::SUB:
12441 case ISD::ADD:
12442 case ISD::MUL:
12443 case ISD::AND:
12444 case ISD::OR:
12445 case ISD::XOR:
12446 return false;
12447 }
12448}
12449
12450/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012451/// beneficial for dag combiner to promote the specified node. If true, it
12452/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012453bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012454 EVT VT = Op.getValueType();
12455 if (VT != MVT::i16)
12456 return false;
12457
Evan Cheng4c26e932010-04-19 19:29:22 +000012458 bool Promote = false;
12459 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012460 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012461 default: break;
12462 case ISD::LOAD: {
12463 LoadSDNode *LD = cast<LoadSDNode>(Op);
12464 // If the non-extending load has a single use and it's not live out, then it
12465 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012466 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12467 Op.hasOneUse()*/) {
12468 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12469 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12470 // The only case where we'd want to promote LOAD (rather then it being
12471 // promoted as an operand is when it's only use is liveout.
12472 if (UI->getOpcode() != ISD::CopyToReg)
12473 return false;
12474 }
12475 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012476 Promote = true;
12477 break;
12478 }
12479 case ISD::SIGN_EXTEND:
12480 case ISD::ZERO_EXTEND:
12481 case ISD::ANY_EXTEND:
12482 Promote = true;
12483 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012484 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012485 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012486 SDValue N0 = Op.getOperand(0);
12487 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012488 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012489 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012490 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012491 break;
12492 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012493 case ISD::ADD:
12494 case ISD::MUL:
12495 case ISD::AND:
12496 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012497 case ISD::XOR:
12498 Commute = true;
12499 // fallthrough
12500 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012501 SDValue N0 = Op.getOperand(0);
12502 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012503 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012504 return false;
12505 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012506 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012507 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012508 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012509 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012510 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012511 }
12512 }
12513
12514 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012515 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012516}
12517
Evan Cheng60c07e12006-07-05 22:17:51 +000012518//===----------------------------------------------------------------------===//
12519// X86 Inline Assembly Support
12520//===----------------------------------------------------------------------===//
12521
Chris Lattnerb8105652009-07-20 17:51:36 +000012522bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12523 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012524
12525 std::string AsmStr = IA->getAsmString();
12526
12527 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012528 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012529 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012530
12531 switch (AsmPieces.size()) {
12532 default: return false;
12533 case 1:
12534 AsmStr = AsmPieces[0];
12535 AsmPieces.clear();
12536 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12537
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012538 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012539 // we will turn this bswap into something that will be lowered to logical ops
12540 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12541 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012542 // bswap $0
12543 if (AsmPieces.size() == 2 &&
12544 (AsmPieces[0] == "bswap" ||
12545 AsmPieces[0] == "bswapq" ||
12546 AsmPieces[0] == "bswapl") &&
12547 (AsmPieces[1] == "$0" ||
12548 AsmPieces[1] == "${0:q}")) {
12549 // No need to check constraints, nothing other than the equivalent of
12550 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012551 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012552 if (!Ty || Ty->getBitWidth() % 16 != 0)
12553 return false;
12554 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012555 }
12556 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012557 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012558 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012559 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012560 AsmPieces[1] == "$$8," &&
12561 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012562 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12563 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012564 const std::string &ConstraintsStr = IA->getConstraintString();
12565 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012566 std::sort(AsmPieces.begin(), AsmPieces.end());
12567 if (AsmPieces.size() == 4 &&
12568 AsmPieces[0] == "~{cc}" &&
12569 AsmPieces[1] == "~{dirflag}" &&
12570 AsmPieces[2] == "~{flags}" &&
12571 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012572 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012573 if (!Ty || Ty->getBitWidth() % 16 != 0)
12574 return false;
12575 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012576 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012577 }
12578 break;
12579 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012580 if (CI->getType()->isIntegerTy(32) &&
12581 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12582 SmallVector<StringRef, 4> Words;
12583 SplitString(AsmPieces[0], Words, " \t,");
12584 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12585 Words[2] == "${0:w}") {
12586 Words.clear();
12587 SplitString(AsmPieces[1], Words, " \t,");
12588 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12589 Words[2] == "$0") {
12590 Words.clear();
12591 SplitString(AsmPieces[2], Words, " \t,");
12592 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12593 Words[2] == "${0:w}") {
12594 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012595 const std::string &ConstraintsStr = IA->getConstraintString();
12596 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012597 std::sort(AsmPieces.begin(), AsmPieces.end());
12598 if (AsmPieces.size() == 4 &&
12599 AsmPieces[0] == "~{cc}" &&
12600 AsmPieces[1] == "~{dirflag}" &&
12601 AsmPieces[2] == "~{flags}" &&
12602 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012603 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012604 if (!Ty || Ty->getBitWidth() % 16 != 0)
12605 return false;
12606 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012607 }
12608 }
12609 }
12610 }
12611 }
Evan Cheng55d42002011-01-08 01:24:27 +000012612
12613 if (CI->getType()->isIntegerTy(64)) {
12614 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12615 if (Constraints.size() >= 2 &&
12616 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12617 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12618 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12619 SmallVector<StringRef, 4> Words;
12620 SplitString(AsmPieces[0], Words, " \t");
12621 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012622 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012623 SplitString(AsmPieces[1], Words, " \t");
12624 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12625 Words.clear();
12626 SplitString(AsmPieces[2], Words, " \t,");
12627 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12628 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012629 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000012630 if (!Ty || Ty->getBitWidth() % 16 != 0)
12631 return false;
12632 return IntrinsicLowering::LowerToByteSwap(CI);
12633 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012634 }
12635 }
12636 }
12637 }
12638 break;
12639 }
12640 return false;
12641}
12642
12643
12644
Chris Lattnerf4dff842006-07-11 02:54:03 +000012645/// getConstraintType - Given a constraint letter, return the type of
12646/// constraint it is for this target.
12647X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012648X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12649 if (Constraint.size() == 1) {
12650 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012651 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012652 case 'q':
12653 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012654 case 'f':
12655 case 't':
12656 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012657 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012658 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012659 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000012660 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000012661 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012662 case 'a':
12663 case 'b':
12664 case 'c':
12665 case 'd':
12666 case 'S':
12667 case 'D':
12668 case 'A':
12669 return C_Register;
12670 case 'I':
12671 case 'J':
12672 case 'K':
12673 case 'L':
12674 case 'M':
12675 case 'N':
12676 case 'G':
12677 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012678 case 'e':
12679 case 'Z':
12680 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012681 default:
12682 break;
12683 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012684 }
Chris Lattner4234f572007-03-25 02:14:49 +000012685 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012686}
12687
John Thompson44ab89e2010-10-29 17:29:13 +000012688/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012689/// This object must already have been set up with the operand type
12690/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012691TargetLowering::ConstraintWeight
12692 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012693 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012694 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012695 Value *CallOperandVal = info.CallOperandVal;
12696 // If we don't have a value, we can't do a match,
12697 // but allow it at the lowest weight.
12698 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012699 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012700 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012701 // Look at the constraint type.
12702 switch (*constraint) {
12703 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012704 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12705 case 'R':
12706 case 'q':
12707 case 'Q':
12708 case 'a':
12709 case 'b':
12710 case 'c':
12711 case 'd':
12712 case 'S':
12713 case 'D':
12714 case 'A':
12715 if (CallOperandVal->getType()->isIntegerTy())
12716 weight = CW_SpecificReg;
12717 break;
12718 case 'f':
12719 case 't':
12720 case 'u':
12721 if (type->isFloatingPointTy())
12722 weight = CW_SpecificReg;
12723 break;
12724 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012725 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012726 weight = CW_SpecificReg;
12727 break;
12728 case 'x':
12729 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012730 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012731 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012732 break;
12733 case 'I':
12734 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12735 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012736 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012737 }
12738 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012739 case 'J':
12740 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12741 if (C->getZExtValue() <= 63)
12742 weight = CW_Constant;
12743 }
12744 break;
12745 case 'K':
12746 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12747 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12748 weight = CW_Constant;
12749 }
12750 break;
12751 case 'L':
12752 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12753 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12754 weight = CW_Constant;
12755 }
12756 break;
12757 case 'M':
12758 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12759 if (C->getZExtValue() <= 3)
12760 weight = CW_Constant;
12761 }
12762 break;
12763 case 'N':
12764 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12765 if (C->getZExtValue() <= 0xff)
12766 weight = CW_Constant;
12767 }
12768 break;
12769 case 'G':
12770 case 'C':
12771 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12772 weight = CW_Constant;
12773 }
12774 break;
12775 case 'e':
12776 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12777 if ((C->getSExtValue() >= -0x80000000LL) &&
12778 (C->getSExtValue() <= 0x7fffffffLL))
12779 weight = CW_Constant;
12780 }
12781 break;
12782 case 'Z':
12783 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12784 if (C->getZExtValue() <= 0xffffffff)
12785 weight = CW_Constant;
12786 }
12787 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012788 }
12789 return weight;
12790}
12791
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012792/// LowerXConstraint - try to replace an X constraint, which matches anything,
12793/// with another that has more specific requirements based on the type of the
12794/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012795const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012796LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012797 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12798 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012799 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012800 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012801 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012802 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012803 return "x";
12804 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012805
Chris Lattner5e764232008-04-26 23:02:14 +000012806 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012807}
12808
Chris Lattner48884cd2007-08-25 00:47:38 +000012809/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12810/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012811void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012812 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012813 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012814 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012815 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012816
Eric Christopher100c8332011-06-02 23:16:42 +000012817 // Only support length 1 constraints for now.
12818 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012819
Eric Christopher100c8332011-06-02 23:16:42 +000012820 char ConstraintLetter = Constraint[0];
12821 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012822 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012823 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012824 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012825 if (C->getZExtValue() <= 31) {
12826 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012827 break;
12828 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012829 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012830 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012831 case 'J':
12832 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012833 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012834 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12835 break;
12836 }
12837 }
12838 return;
12839 case 'K':
12840 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012841 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012842 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12843 break;
12844 }
12845 }
12846 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012847 case 'N':
12848 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012849 if (C->getZExtValue() <= 255) {
12850 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012851 break;
12852 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012853 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012854 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012855 case 'e': {
12856 // 32-bit signed value
12857 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012858 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12859 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012860 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012861 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012862 break;
12863 }
12864 // FIXME gcc accepts some relocatable values here too, but only in certain
12865 // memory models; it's complicated.
12866 }
12867 return;
12868 }
12869 case 'Z': {
12870 // 32-bit unsigned value
12871 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012872 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12873 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012874 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12875 break;
12876 }
12877 }
12878 // FIXME gcc accepts some relocatable values here too, but only in certain
12879 // memory models; it's complicated.
12880 return;
12881 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012882 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012883 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012884 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012885 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012886 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012887 break;
12888 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012889
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012890 // In any sort of PIC mode addresses need to be computed at runtime by
12891 // adding in a register or some sort of table lookup. These can't
12892 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012893 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012894 return;
12895
Chris Lattnerdc43a882007-05-03 16:52:29 +000012896 // If we are in non-pic codegen mode, we allow the address of a global (with
12897 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012898 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012899 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012900
Chris Lattner49921962009-05-08 18:23:14 +000012901 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12902 while (1) {
12903 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12904 Offset += GA->getOffset();
12905 break;
12906 } else if (Op.getOpcode() == ISD::ADD) {
12907 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12908 Offset += C->getZExtValue();
12909 Op = Op.getOperand(0);
12910 continue;
12911 }
12912 } else if (Op.getOpcode() == ISD::SUB) {
12913 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12914 Offset += -C->getZExtValue();
12915 Op = Op.getOperand(0);
12916 continue;
12917 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012918 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012919
Chris Lattner49921962009-05-08 18:23:14 +000012920 // Otherwise, this isn't something we can handle, reject it.
12921 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012922 }
Eric Christopherfd179292009-08-27 18:07:15 +000012923
Dan Gohman46510a72010-04-15 01:51:59 +000012924 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012925 // If we require an extra load to get this address, as in PIC mode, we
12926 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012927 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12928 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012929 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012930
Devang Patel0d881da2010-07-06 22:08:15 +000012931 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12932 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012933 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012934 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012935 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012936
Gabor Greifba36cb52008-08-28 21:40:38 +000012937 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012938 Ops.push_back(Result);
12939 return;
12940 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012941 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012942}
12943
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012944std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012945X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012946 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012947 // First, see if this is a constraint that directly corresponds to an LLVM
12948 // register class.
12949 if (Constraint.size() == 1) {
12950 // GCC Constraint Letters
12951 switch (Constraint[0]) {
12952 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000012953 // TODO: Slight differences here in allocation order and leaving
12954 // RIP in the class. Do they matter any more here than they do
12955 // in the normal allocation?
12956 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12957 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012958 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012959 return std::make_pair(0U, X86::GR32RegisterClass);
12960 else if (VT == MVT::i16)
12961 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000012962 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000012963 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012964 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000012965 return std::make_pair(0U, X86::GR64RegisterClass);
12966 break;
12967 }
12968 // 32-bit fallthrough
12969 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012970 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012971 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
12972 else if (VT == MVT::i16)
12973 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000012974 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000012975 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
12976 else if (VT == MVT::i64)
12977 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
12978 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012979 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012980 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000012981 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012982 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012983 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012984 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000012985 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012986 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012987 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012988 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000012989 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012990 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12991 if (VT == MVT::i16)
12992 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12993 if (VT == MVT::i32 || !Subtarget->is64Bit())
12994 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12995 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012996 case 'f': // FP Stack registers.
12997 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12998 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012999 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013000 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013001 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013002 return std::make_pair(0U, X86::RFP64RegisterClass);
13003 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000013004 case 'y': // MMX_REGS if MMX allowed.
13005 if (!Subtarget->hasMMX()) break;
13006 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013007 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013008 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000013009 // FALL THROUGH.
13010 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013011 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000013012
Owen Anderson825b72b2009-08-11 20:47:22 +000013013 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000013014 default: break;
13015 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013016 case MVT::f32:
13017 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000013018 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013019 case MVT::f64:
13020 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000013021 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013022 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013023 case MVT::v16i8:
13024 case MVT::v8i16:
13025 case MVT::v4i32:
13026 case MVT::v2i64:
13027 case MVT::v4f32:
13028 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000013029 return std::make_pair(0U, X86::VR128RegisterClass);
13030 }
Chris Lattnerad043e82007-04-09 05:11:28 +000013031 break;
13032 }
13033 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013034
Chris Lattnerf76d1802006-07-31 23:26:50 +000013035 // Use the default implementation in TargetLowering to convert the register
13036 // constraint into a member of a register class.
13037 std::pair<unsigned, const TargetRegisterClass*> Res;
13038 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000013039
13040 // Not found as a standard register?
13041 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013042 // Map st(0) -> st(7) -> ST0
13043 if (Constraint.size() == 7 && Constraint[0] == '{' &&
13044 tolower(Constraint[1]) == 's' &&
13045 tolower(Constraint[2]) == 't' &&
13046 Constraint[3] == '(' &&
13047 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
13048 Constraint[5] == ')' &&
13049 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000013050
Chris Lattner56d77c72009-09-13 22:41:48 +000013051 Res.first = X86::ST0+Constraint[4]-'0';
13052 Res.second = X86::RFP80RegisterClass;
13053 return Res;
13054 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013055
Chris Lattner56d77c72009-09-13 22:41:48 +000013056 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013057 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000013058 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000013059 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013060 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013061 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013062
13063 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013064 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013065 Res.first = X86::EFLAGS;
13066 Res.second = X86::CCRRegisterClass;
13067 return Res;
13068 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013069
Dale Johannesen330169f2008-11-13 21:52:36 +000013070 // 'A' means EAX + EDX.
13071 if (Constraint == "A") {
13072 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013073 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013074 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013075 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013076 return Res;
13077 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013078
Chris Lattnerf76d1802006-07-31 23:26:50 +000013079 // Otherwise, check to see if this is a register class of the wrong value
13080 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13081 // turn into {ax},{dx}.
13082 if (Res.second->hasType(VT))
13083 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013084
Chris Lattnerf76d1802006-07-31 23:26:50 +000013085 // All of the single-register GCC register classes map their values onto
13086 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13087 // really want an 8-bit or 32-bit register, map to the appropriate register
13088 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013089 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013090 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013091 unsigned DestReg = 0;
13092 switch (Res.first) {
13093 default: break;
13094 case X86::AX: DestReg = X86::AL; break;
13095 case X86::DX: DestReg = X86::DL; break;
13096 case X86::CX: DestReg = X86::CL; break;
13097 case X86::BX: DestReg = X86::BL; break;
13098 }
13099 if (DestReg) {
13100 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013101 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013102 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013103 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013104 unsigned DestReg = 0;
13105 switch (Res.first) {
13106 default: break;
13107 case X86::AX: DestReg = X86::EAX; break;
13108 case X86::DX: DestReg = X86::EDX; break;
13109 case X86::CX: DestReg = X86::ECX; break;
13110 case X86::BX: DestReg = X86::EBX; break;
13111 case X86::SI: DestReg = X86::ESI; break;
13112 case X86::DI: DestReg = X86::EDI; break;
13113 case X86::BP: DestReg = X86::EBP; break;
13114 case X86::SP: DestReg = X86::ESP; break;
13115 }
13116 if (DestReg) {
13117 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013118 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013119 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013120 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013121 unsigned DestReg = 0;
13122 switch (Res.first) {
13123 default: break;
13124 case X86::AX: DestReg = X86::RAX; break;
13125 case X86::DX: DestReg = X86::RDX; break;
13126 case X86::CX: DestReg = X86::RCX; break;
13127 case X86::BX: DestReg = X86::RBX; break;
13128 case X86::SI: DestReg = X86::RSI; break;
13129 case X86::DI: DestReg = X86::RDI; break;
13130 case X86::BP: DestReg = X86::RBP; break;
13131 case X86::SP: DestReg = X86::RSP; break;
13132 }
13133 if (DestReg) {
13134 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013135 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013136 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013137 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013138 } else if (Res.second == X86::FR32RegisterClass ||
13139 Res.second == X86::FR64RegisterClass ||
13140 Res.second == X86::VR128RegisterClass) {
13141 // Handle references to XMM physical registers that got mapped into the
13142 // wrong class. This can happen with constraints like {xmm0} where the
13143 // target independent register mapper will just pick the first match it can
13144 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013145 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013146 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013147 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013148 Res.second = X86::FR64RegisterClass;
13149 else if (X86::VR128RegisterClass->hasType(VT))
13150 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013151 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013152
Chris Lattnerf76d1802006-07-31 23:26:50 +000013153 return Res;
13154}