blob: 27ee327eff59e6e135bb65afcd12ff7af2011fb5 [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"
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000019#include "X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.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"
Mon P Wang3c81d352008-11-23 04:37:22 +000048#include "llvm/Support/CommandLine.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);
68static SDValue Extract128BitVector(SDValue Vec,
69 SDValue Idx,
70 SelectionDAG &DAG,
71 DebugLoc dl);
72
73/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
74/// sets things up to match to an AVX VEXTRACTF128 instruction or a
75/// simple subregister reference.
76static SDValue Extract128BitVector(SDValue Vec,
77 SDValue Idx,
78 SelectionDAG &DAG,
79 DebugLoc dl) {
80 EVT VT = Vec.getValueType();
81 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
82
83 EVT ElVT = VT.getVectorElementType();
84
85 int Factor = VT.getSizeInBits() / 128;
86
87 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
88 ElVT,
89 VT.getVectorNumElements() / Factor);
90
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
108
109 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
110 VecIdx);
111
112 return Result;
113 }
114
115 return SDValue();
116}
117
118/// Generate a DAG to put 128-bits into a vector > 128 bits. This
119/// sets things up to match to an AVX VINSERTF128 instruction or a
120/// simple superregister reference.
121static SDValue Insert128BitVector(SDValue Result,
122 SDValue Vec,
123 SDValue Idx,
124 SelectionDAG &DAG,
125 DebugLoc dl) {
126 if (isa<ConstantSDNode>(Idx)) {
127 EVT VT = Vec.getValueType();
128 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
129
130 EVT ElVT = VT.getVectorElementType();
131
132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
133
134 EVT ResultVT = Result.getValueType();
135
136 // Insert the relevant 128 bits.
137 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
138
139 // This is the index of the first element of the 128-bit chunk
140 // we want.
141 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
142 * ElemsPerChunk);
143
144 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
145
146 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
147 VecIdx);
148 return Result;
149 }
150
151 return SDValue();
152}
153
Chris Lattnerf0144122009-07-28 03:13:23 +0000154static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000155 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
156 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000157
Evan Cheng2bffee22011-02-01 01:14:13 +0000158 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000159 if (is64Bit)
160 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000161 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000162 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000163
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 if (is64Bit)
166 return new X8664_ELFTargetObjectFile(TM);
167 return new X8632_ELFTargetObjectFile(TM);
168 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000169 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000170 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000171 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000172}
173
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000174X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000175 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000176 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000177 X86ScalarSSEf64 = Subtarget->hasXMMInt();
178 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000179 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000180
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000181 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000182 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000183
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000184 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000185 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000186
187 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000189 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +0000190 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000191 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000192
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000193 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000194 // Setup Windows compiler runtime calls.
195 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000196 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
197 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000198 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000199 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000200 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000201 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
202 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 }
204
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000205 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000206 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000207 setUseUnderscoreSetJmp(false);
208 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000209 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000210 // MS runtime is weird: it exports _setjmp, but longjmp!
211 setUseUnderscoreSetJmp(true);
212 setUseUnderscoreLongJmp(false);
213 } else {
214 setUseUnderscoreSetJmp(true);
215 setUseUnderscoreLongJmp(true);
216 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000217
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000218 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000220 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000222 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000224
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000226
Scott Michelfdc40a02009-02-17 22:15:04 +0000227 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000229 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000231 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
233 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000234
235 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
237 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
238 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
239 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
240 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
241 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000242
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000243 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
244 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
246 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
247 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000248
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000252 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000253 // We have an algorithm for SSE2->double, and we turn this into a
254 // 64-bit FILD followed by conditional FADD for other targets.
255 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000256 // We have an algorithm for SSE2, and we turn this into a 64-bit
257 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000259 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000260
261 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
262 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
264 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000265
Devang Patel6a784892009-06-05 18:48:29 +0000266 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000267 // SSE has no i16 to fp conversion, only i32
268 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000270 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000272 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
274 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000276 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000279 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000280
Dale Johannesen73328d12007-09-19 23:55:34 +0000281 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
282 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
284 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000285
Evan Cheng02568ff2006-01-30 22:13:22 +0000286 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
287 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
289 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000290
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000291 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000293 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000295 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000298 }
299
300 // Handle FP_TO_UINT by promoting the destination to a larger signed
301 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
303 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
304 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305
Evan Cheng25ab6902006-09-08 06:48:29 +0000306 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000309 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000310 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000311 // Expand FP_TO_UINT into a select.
312 // FIXME: We would like to use a Custom expander here eventually to do
313 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000315 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000316 // With SSE3 we can use fisttpll to convert to a signed i64; without
317 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000320
Chris Lattner399610a2006-12-05 18:22:22 +0000321 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000322 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000323 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
324 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000325 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000326 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000327 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000328 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000329 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000330 }
Chris Lattner21f66852005-12-23 05:15:23 +0000331
Dan Gohmanb00ee212008-02-18 19:34:53 +0000332 // Scalar integer divide and remainder are lowered to use operations that
333 // produce two results, to match the available instructions. This exposes
334 // the two-result form to trivial CSE, which is able to combine x/y and x%y
335 // into a single instruction.
336 //
337 // Scalar integer multiply-high is also lowered to use two-result
338 // operations, to match the available instructions. However, plain multiply
339 // (low) operations are left as Legal, as there are single-result
340 // instructions for this in x86. Using the two-result multiply instructions
341 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000342 for (unsigned i = 0, e = 4; i != e; ++i) {
343 MVT VT = IntVTs[i];
344 setOperationAction(ISD::MULHS, VT, Expand);
345 setOperationAction(ISD::MULHU, VT, Expand);
346 setOperationAction(ISD::SDIV, VT, Expand);
347 setOperationAction(ISD::UDIV, VT, Expand);
348 setOperationAction(ISD::SREM, VT, Expand);
349 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000350
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000351 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000352 setOperationAction(ISD::ADDC, VT, Custom);
353 setOperationAction(ISD::ADDE, VT, Custom);
354 setOperationAction(ISD::SUBC, VT, Custom);
355 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000356 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000357
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
359 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
360 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
361 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000362 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
364 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
365 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
366 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
367 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
368 setOperationAction(ISD::FREM , MVT::f32 , Expand);
369 setOperationAction(ISD::FREM , MVT::f64 , Expand);
370 setOperationAction(ISD::FREM , MVT::f80 , Expand);
371 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000372
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
374 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000375 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
376 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
378 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000379 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
381 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000382 }
383
Benjamin Kramer1292c222010-12-04 20:32:23 +0000384 if (Subtarget->hasPOPCNT()) {
385 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
386 } else {
387 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
388 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
389 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
390 if (Subtarget->is64Bit())
391 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
392 }
393
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
395 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000396
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000397 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000398 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000399 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000400 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000401 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
403 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
404 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
405 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
406 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000407 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
409 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
410 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
411 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000412 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
414 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000415 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000417
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000418 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
420 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
421 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
422 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000423 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
425 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000426 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000427 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
429 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
430 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
431 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000432 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000433 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000434 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
436 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
437 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000438 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
440 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
441 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000442 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000443
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000444 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000446
Eric Christopher9a9d2752010-07-22 02:48:34 +0000447 // We may not have a libcall for MEMBARRIER so we should lower this.
448 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000449
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000450 // On X86 and X86-64, atomic operations are lowered to locked instructions.
451 // Locked instructions, in turn, have implicit fence semantics (all memory
452 // operations are flushed before issuing the locked instruction, and they
453 // are not buffered), so we can fold away the common pattern of
454 // fence-atomic-fence.
455 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000456
Mon P Wang63307c32008-05-05 19:05:59 +0000457 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000458 for (unsigned i = 0, e = 4; i != e; ++i) {
459 MVT VT = IntVTs[i];
460 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
461 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
462 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000463
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000464 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
466 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
467 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
468 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
469 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
470 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
471 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000472 }
473
Evan Cheng3c992d22006-03-07 02:02:57 +0000474 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000475 if (!Subtarget->isTargetDarwin() &&
476 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000477 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000479 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000480
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
482 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
483 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
484 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000485 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000486 setExceptionPointerRegister(X86::RAX);
487 setExceptionSelectorRegister(X86::RDX);
488 } else {
489 setExceptionPointerRegister(X86::EAX);
490 setExceptionSelectorRegister(X86::EDX);
491 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
493 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000496
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000498
Nate Begemanacc398c2006-01-25 18:21:52 +0000499 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000500 setOperationAction(ISD::VASTART , MVT::Other, Custom);
501 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000502 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000503 setOperationAction(ISD::VAARG , MVT::Other, Custom);
504 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000505 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::VAARG , MVT::Other, Expand);
507 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000508 }
Evan Chengae642192007-03-02 23:16:35 +0000509
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
511 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000512 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000514 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000516 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000518
Evan Chengc7ce29b2009-02-13 22:36:38 +0000519 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000520 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000521 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
523 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000524
Evan Cheng223547a2006-01-31 22:28:30 +0000525 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::FABS , MVT::f64, Custom);
527 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000528
529 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::FNEG , MVT::f64, Custom);
531 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000532
Evan Cheng68c47cb2007-01-05 07:55:56 +0000533 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
535 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000536
Evan Chengd25e9e82006-02-02 00:28:23 +0000537 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::FSIN , MVT::f64, Expand);
539 setOperationAction(ISD::FCOS , MVT::f64, Expand);
540 setOperationAction(ISD::FSIN , MVT::f32, Expand);
541 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000542
Chris Lattnera54aa942006-01-29 06:26:08 +0000543 // Expand FP immediates into loads from the stack, except for the special
544 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000545 addLegalFPImmediate(APFloat(+0.0)); // xorpd
546 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000547 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000548 // Use SSE for f32, x87 for f64.
549 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
551 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000552
553 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000555
556 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000558
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000560
561 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
563 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000564
565 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FSIN , MVT::f32, Expand);
567 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000568
Nate Begemane1795842008-02-14 08:57:00 +0000569 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000570 addLegalFPImmediate(APFloat(+0.0f)); // xorps
571 addLegalFPImmediate(APFloat(+0.0)); // FLD0
572 addLegalFPImmediate(APFloat(+1.0)); // FLD1
573 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
574 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
575
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000576 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
578 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000579 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000580 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000582 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
584 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000585
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
587 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
588 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
589 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000590
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000591 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
593 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000594 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000595 addLegalFPImmediate(APFloat(+0.0)); // FLD0
596 addLegalFPImmediate(APFloat(+1.0)); // FLD1
597 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
598 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
600 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
601 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
602 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000603 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000604
Dale Johannesen59a58732007-08-05 18:49:15 +0000605 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000606 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
608 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
609 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000610 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000611 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000612 addLegalFPImmediate(TmpFlt); // FLD0
613 TmpFlt.changeSign();
614 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000615
616 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000617 APFloat TmpFlt2(+1.0);
618 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
619 &ignored);
620 addLegalFPImmediate(TmpFlt2); // FLD1
621 TmpFlt2.changeSign();
622 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
623 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000624
Evan Chengc7ce29b2009-02-13 22:36:38 +0000625 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
627 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000628 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000629 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000630
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000631 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
633 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
634 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000635
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FLOG, MVT::f80, Expand);
637 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
638 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
639 setOperationAction(ISD::FEXP, MVT::f80, Expand);
640 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000641
Mon P Wangf007a8b2008-11-06 05:31:54 +0000642 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000643 // (for widening) or expand (for scalarization). Then we will selectively
644 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
646 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
647 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
648 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
649 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
650 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
651 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
652 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
653 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
654 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
655 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
656 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
657 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
658 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
659 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
660 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
661 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000663 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
664 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
666 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
667 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
668 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
669 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
670 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
671 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
672 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
673 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
674 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
675 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
676 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
677 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000696 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000697 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
701 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
702 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
703 setTruncStoreAction((MVT::SimpleValueType)VT,
704 (MVT::SimpleValueType)InnerVT, Expand);
705 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
706 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
707 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000708 }
709
Evan Chengc7ce29b2009-02-13 22:36:38 +0000710 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
711 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000712 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000713 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000714 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000715 }
716
Dale Johannesen0488fb62010-09-30 23:57:10 +0000717 // MMX-sized vectors (other than x86mmx) are expected to be expanded
718 // into smaller operations.
719 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
720 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
721 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
722 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
723 setOperationAction(ISD::AND, MVT::v8i8, Expand);
724 setOperationAction(ISD::AND, MVT::v4i16, Expand);
725 setOperationAction(ISD::AND, MVT::v2i32, Expand);
726 setOperationAction(ISD::AND, MVT::v1i64, Expand);
727 setOperationAction(ISD::OR, MVT::v8i8, Expand);
728 setOperationAction(ISD::OR, MVT::v4i16, Expand);
729 setOperationAction(ISD::OR, MVT::v2i32, Expand);
730 setOperationAction(ISD::OR, MVT::v1i64, Expand);
731 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
732 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
733 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
734 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
735 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
736 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
737 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
738 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
739 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
740 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
741 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
742 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
743 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000744 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
745 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
746 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
747 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000748
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000749 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000751
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
753 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
754 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
755 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
756 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
757 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
758 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
759 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
760 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
761 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
762 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
763 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000764 }
765
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000766 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000768
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000769 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
770 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
772 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
773 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
774 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000775
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
777 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
778 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
779 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
780 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
781 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
782 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
783 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
784 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
785 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
786 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
787 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
788 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
789 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
790 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
791 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000792
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
794 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
795 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
796 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000797
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
799 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
800 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
801 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
802 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000803
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000804 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
805 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
806 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
807 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
808 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
809
Evan Cheng2c3ae372006-04-12 21:21:57 +0000810 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
812 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000813 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000814 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000815 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000816 // Do not attempt to custom lower non-128-bit vectors
817 if (!VT.is128BitVector())
818 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 setOperationAction(ISD::BUILD_VECTOR,
820 VT.getSimpleVT().SimpleTy, Custom);
821 setOperationAction(ISD::VECTOR_SHUFFLE,
822 VT.getSimpleVT().SimpleTy, Custom);
823 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
824 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000825 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000826
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
828 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
829 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
830 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
832 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000833
Nate Begemancdd1eec2008-02-12 22:51:28 +0000834 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
836 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000837 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000838
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000839 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
841 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000842 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000843
844 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000845 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000846 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000847
Owen Andersond6662ad2009-08-10 20:46:15 +0000848 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000850 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000852 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000854 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000856 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000858 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000859
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000861
Evan Cheng2c3ae372006-04-12 21:21:57 +0000862 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
864 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
865 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
866 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000867
Owen Anderson825b72b2009-08-11 20:47:22 +0000868 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
869 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000870 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000871
Nate Begeman14d12ca2008-02-11 04:19:36 +0000872 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000873 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
874 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
875 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
876 setOperationAction(ISD::FRINT, MVT::f32, Legal);
877 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
878 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
879 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
880 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
881 setOperationAction(ISD::FRINT, MVT::f64, Legal);
882 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
883
Nate Begeman14d12ca2008-02-11 04:19:36 +0000884 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000886
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000887 // Can turn SHL into an integer multiply.
888 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000889 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000890
Nate Begeman14d12ca2008-02-11 04:19:36 +0000891 // i8 and i16 vectors are custom , because the source register and source
892 // source memory operand types are not the same width. f32 vectors are
893 // custom since the immediate controlling the insert encodes additional
894 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
896 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
898 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
901 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
902 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
903 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000904
905 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
907 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000908 }
909 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000911 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000913
David Greene9b9838d2009-06-29 16:47:10 +0000914 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
916 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
917 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
918 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000919 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000920
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
922 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
923 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
924 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000925
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
927 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
928 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
929 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
930 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
931 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000932
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
934 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
935 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
936 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
937 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
938 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000939
David Greene54d8eba2011-01-27 22:38:56 +0000940 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
941 // insert_vector_elt extract_subvector and extract_vector_elt for
942 // 256-bit types.
943 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
944 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
945 ++i) {
946 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
947 // Do not attempt to custom lower non-256-bit vectors
948 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
949 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000950 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000951 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
952 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000953 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000954 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000955 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000956 }
David Greene54d8eba2011-01-27 22:38:56 +0000957 // Custom-lower insert_subvector and extract_subvector based on
958 // the result type.
959 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
960 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
961 ++i) {
962 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
963 // Do not attempt to custom lower non-256-bit vectors
964 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +0000965 continue;
David Greene54d8eba2011-01-27 22:38:56 +0000966
967 if (MVT(VT).getSizeInBits() == 128) {
968 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000969 }
David Greene54d8eba2011-01-27 22:38:56 +0000970 else if (MVT(VT).getSizeInBits() == 256) {
971 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
972 }
David Greene9b9838d2009-06-29 16:47:10 +0000973 }
974
David Greene54d8eba2011-01-27 22:38:56 +0000975 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
976 // Don't promote loads because we need them for VPERM vector index versions.
977
978 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
979 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
980 VT++) {
981 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
982 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
983 continue;
984 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
985 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
986 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
987 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
988 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
989 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
990 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
991 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
992 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
993 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
994 }
David Greene9b9838d2009-06-29 16:47:10 +0000995 }
996
Evan Cheng6be2c582006-04-05 23:38:46 +0000997 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000998 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000999
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001000
Eli Friedman962f5492010-06-02 19:35:46 +00001001 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1002 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001003 //
Eli Friedman962f5492010-06-02 19:35:46 +00001004 // FIXME: We really should do custom legalization for addition and
1005 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1006 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001007 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1008 // Add/Sub/Mul with overflow operations are custom lowered.
1009 MVT VT = IntVTs[i];
1010 setOperationAction(ISD::SADDO, VT, Custom);
1011 setOperationAction(ISD::UADDO, VT, Custom);
1012 setOperationAction(ISD::SSUBO, VT, Custom);
1013 setOperationAction(ISD::USUBO, VT, Custom);
1014 setOperationAction(ISD::SMULO, VT, Custom);
1015 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001016 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001017
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001018 // There are no 8-bit 3-address imul/mul instructions
1019 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1020 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001021
Evan Chengd54f2d52009-03-31 19:38:51 +00001022 if (!Subtarget->is64Bit()) {
1023 // These libcalls are not available in 32-bit.
1024 setLibcallName(RTLIB::SHL_I128, 0);
1025 setLibcallName(RTLIB::SRL_I128, 0);
1026 setLibcallName(RTLIB::SRA_I128, 0);
1027 }
1028
Evan Cheng206ee9d2006-07-07 08:33:52 +00001029 // We have target-specific dag combine patterns for the following nodes:
1030 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001031 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001032 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001033 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001034 setTargetDAGCombine(ISD::SHL);
1035 setTargetDAGCombine(ISD::SRA);
1036 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001037 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001038 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001039 setTargetDAGCombine(ISD::ADD);
1040 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001041 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001042 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001043 if (Subtarget->is64Bit())
1044 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001045
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001046 computeRegisterProperties();
1047
Evan Cheng05219282011-01-06 06:52:41 +00001048 // On Darwin, -Os means optimize for size without hurting performance,
1049 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001050 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001051 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001052 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001053 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1054 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1055 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001056 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001057 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001058}
1059
Scott Michel5b8f82e2008-03-10 15:42:14 +00001060
Owen Anderson825b72b2009-08-11 20:47:22 +00001061MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1062 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001063}
1064
1065
Evan Cheng29286502008-01-23 23:17:41 +00001066/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1067/// the desired ByVal argument alignment.
1068static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1069 if (MaxAlign == 16)
1070 return;
1071 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1072 if (VTy->getBitWidth() == 128)
1073 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001074 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1075 unsigned EltAlign = 0;
1076 getMaxByValAlign(ATy->getElementType(), EltAlign);
1077 if (EltAlign > MaxAlign)
1078 MaxAlign = EltAlign;
1079 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1080 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1081 unsigned EltAlign = 0;
1082 getMaxByValAlign(STy->getElementType(i), EltAlign);
1083 if (EltAlign > MaxAlign)
1084 MaxAlign = EltAlign;
1085 if (MaxAlign == 16)
1086 break;
1087 }
1088 }
1089 return;
1090}
1091
1092/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1093/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001094/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1095/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001096unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001097 if (Subtarget->is64Bit()) {
1098 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001099 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001100 if (TyAlign > 8)
1101 return TyAlign;
1102 return 8;
1103 }
1104
Evan Cheng29286502008-01-23 23:17:41 +00001105 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001106 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001107 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001108 return Align;
1109}
Chris Lattner2b02a442007-02-25 08:29:00 +00001110
Evan Chengf0df0312008-05-15 08:39:06 +00001111/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001112/// and store operations as a result of memset, memcpy, and memmove
1113/// lowering. If DstAlign is zero that means it's safe to destination
1114/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1115/// means there isn't a need to check it against alignment requirement,
1116/// probably because the source does not need to be loaded. If
1117/// 'NonScalarIntSafe' is true, that means it's safe to return a
1118/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1119/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1120/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001121/// It returns EVT::Other if the type should be determined using generic
1122/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001123EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001124X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1125 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001126 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001127 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001128 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001129 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1130 // linux. This is because the stack realignment code can't handle certain
1131 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001132 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001133 if (NonScalarIntSafe &&
1134 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001135 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001136 (Subtarget->isUnalignedMemAccessFast() ||
1137 ((DstAlign == 0 || DstAlign >= 16) &&
1138 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001139 Subtarget->getStackAlignment() >= 16) {
1140 if (Subtarget->hasSSE2())
1141 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001142 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001143 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001144 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001145 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001146 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001147 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001148 // Do not use f64 to lower memcpy if source is string constant. It's
1149 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001150 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001151 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001152 }
Evan Chengf0df0312008-05-15 08:39:06 +00001153 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 return MVT::i64;
1155 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001156}
1157
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001158/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1159/// current function. The returned value is a member of the
1160/// MachineJumpTableInfo::JTEntryKind enum.
1161unsigned X86TargetLowering::getJumpTableEncoding() const {
1162 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1163 // symbol.
1164 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1165 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001166 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001167
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001168 // Otherwise, use the normal jump table encoding heuristics.
1169 return TargetLowering::getJumpTableEncoding();
1170}
1171
Chris Lattnerc64daab2010-01-26 05:02:42 +00001172const MCExpr *
1173X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1174 const MachineBasicBlock *MBB,
1175 unsigned uid,MCContext &Ctx) const{
1176 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1177 Subtarget->isPICStyleGOT());
1178 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1179 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001180 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1181 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001182}
1183
Evan Chengcc415862007-11-09 01:32:10 +00001184/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1185/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001186SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001187 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001188 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001189 // This doesn't have DebugLoc associated with it, but is not really the
1190 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001191 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001192 return Table;
1193}
1194
Chris Lattner589c6f62010-01-26 06:28:43 +00001195/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1196/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1197/// MCExpr.
1198const MCExpr *X86TargetLowering::
1199getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1200 MCContext &Ctx) const {
1201 // X86-64 uses RIP relative addressing based on the jump table label.
1202 if (Subtarget->isPICStyleRIPRel())
1203 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1204
1205 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001206 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001207}
1208
Bill Wendlingb4202b82009-07-01 18:50:55 +00001209/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001210unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001211 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001212}
1213
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001214// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001215std::pair<const TargetRegisterClass*, uint8_t>
1216X86TargetLowering::findRepresentativeClass(EVT VT) const{
1217 const TargetRegisterClass *RRC = 0;
1218 uint8_t Cost = 1;
1219 switch (VT.getSimpleVT().SimpleTy) {
1220 default:
1221 return TargetLowering::findRepresentativeClass(VT);
1222 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1223 RRC = (Subtarget->is64Bit()
1224 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1225 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001226 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001227 RRC = X86::VR64RegisterClass;
1228 break;
1229 case MVT::f32: case MVT::f64:
1230 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1231 case MVT::v4f32: case MVT::v2f64:
1232 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1233 case MVT::v4f64:
1234 RRC = X86::VR128RegisterClass;
1235 break;
1236 }
1237 return std::make_pair(RRC, Cost);
1238}
1239
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001240// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001241unsigned
1242X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1243 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001244 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001245
1246 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001247 switch (RC->getID()) {
1248 default:
1249 return 0;
1250 case X86::GR32RegClassID:
1251 return 4 - FPDiff;
1252 case X86::GR64RegClassID:
1253 return 8 - FPDiff;
1254 case X86::VR128RegClassID:
1255 return Subtarget->is64Bit() ? 10 : 4;
1256 case X86::VR64RegClassID:
1257 return 4;
1258 }
1259}
1260
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001261bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1262 unsigned &Offset) const {
1263 if (!Subtarget->isTargetLinux())
1264 return false;
1265
1266 if (Subtarget->is64Bit()) {
1267 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1268 Offset = 0x28;
1269 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1270 AddressSpace = 256;
1271 else
1272 AddressSpace = 257;
1273 } else {
1274 // %gs:0x14 on i386
1275 Offset = 0x14;
1276 AddressSpace = 256;
1277 }
1278 return true;
1279}
1280
1281
Chris Lattner2b02a442007-02-25 08:29:00 +00001282//===----------------------------------------------------------------------===//
1283// Return Value Calling Convention Implementation
1284//===----------------------------------------------------------------------===//
1285
Chris Lattner59ed56b2007-02-28 04:55:35 +00001286#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001287
Michael J. Spencerec38de22010-10-10 22:04:20 +00001288bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001289X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001290 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001291 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001292 SmallVector<CCValAssign, 16> RVLocs;
1293 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001294 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001295 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001296}
1297
Dan Gohman98ca4f22009-08-05 01:29:28 +00001298SDValue
1299X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001300 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001301 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001302 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001303 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001304 MachineFunction &MF = DAG.getMachineFunction();
1305 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001306
Chris Lattner9774c912007-02-27 05:28:59 +00001307 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001308 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1309 RVLocs, *DAG.getContext());
1310 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001311
Evan Chengdcea1632010-02-04 02:40:39 +00001312 // Add the regs to the liveout set for the function.
1313 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1314 for (unsigned i = 0; i != RVLocs.size(); ++i)
1315 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1316 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001317
Dan Gohman475871a2008-07-27 21:46:04 +00001318 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001319
Dan Gohman475871a2008-07-27 21:46:04 +00001320 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001321 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1322 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001323 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1324 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001325
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001326 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001327 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1328 CCValAssign &VA = RVLocs[i];
1329 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001330 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001331 EVT ValVT = ValToCopy.getValueType();
1332
Dale Johannesenc4510512010-09-24 19:05:48 +00001333 // If this is x86-64, and we disabled SSE, we can't return FP values,
1334 // or SSE or MMX vectors.
1335 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1336 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001337 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001338 report_fatal_error("SSE register return with SSE disabled");
1339 }
1340 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1341 // llvm-gcc has never done it right and no one has noticed, so this
1342 // should be OK for now.
1343 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001344 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001345 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001346
Chris Lattner447ff682008-03-11 03:23:40 +00001347 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1348 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001349 if (VA.getLocReg() == X86::ST0 ||
1350 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001351 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1352 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001353 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001354 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001355 RetOps.push_back(ValToCopy);
1356 // Don't emit a copytoreg.
1357 continue;
1358 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001359
Evan Cheng242b38b2009-02-23 09:03:22 +00001360 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1361 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001362 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001363 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001364 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001365 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001366 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1367 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001368 // If we don't have SSE2 available, convert to v4f32 so the generated
1369 // register is legal.
1370 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001371 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001372 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001373 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001374 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001375
Dale Johannesendd64c412009-02-04 00:33:20 +00001376 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001377 Flag = Chain.getValue(1);
1378 }
Dan Gohman61a92132008-04-21 23:59:07 +00001379
1380 // The x86-64 ABI for returning structs by value requires that we copy
1381 // the sret argument into %rax for the return. We saved the argument into
1382 // a virtual register in the entry block, so now we copy the value out
1383 // and into %rax.
1384 if (Subtarget->is64Bit() &&
1385 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1386 MachineFunction &MF = DAG.getMachineFunction();
1387 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1388 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001389 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001390 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001391 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001392
Dale Johannesendd64c412009-02-04 00:33:20 +00001393 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001394 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001395
1396 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001397 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001398 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001399
Chris Lattner447ff682008-03-11 03:23:40 +00001400 RetOps[0] = Chain; // Update chain.
1401
1402 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001403 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001404 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001405
1406 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001407 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001408}
1409
Evan Cheng3d2125c2010-11-30 23:55:39 +00001410bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1411 if (N->getNumValues() != 1)
1412 return false;
1413 if (!N->hasNUsesOfValue(1, 0))
1414 return false;
1415
1416 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001417 if (Copy->getOpcode() != ISD::CopyToReg &&
1418 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001419 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001420
1421 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001422 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001423 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001424 if (UI->getOpcode() != X86ISD::RET_FLAG)
1425 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001426 HasRet = true;
1427 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001428
Evan Cheng1bf891a2010-12-01 22:59:46 +00001429 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001430}
1431
Dan Gohman98ca4f22009-08-05 01:29:28 +00001432/// LowerCallResult - Lower the result values of a call into the
1433/// appropriate copies out of appropriate physical registers.
1434///
1435SDValue
1436X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001437 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001438 const SmallVectorImpl<ISD::InputArg> &Ins,
1439 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001440 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001441
Chris Lattnere32bbf62007-02-28 07:09:55 +00001442 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001443 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001444 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001446 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001447 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001448
Chris Lattner3085e152007-02-25 08:59:22 +00001449 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001450 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001451 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001452 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001453
Torok Edwin3f142c32009-02-01 18:15:56 +00001454 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001455 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001456 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001457 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001458 }
1459
Evan Cheng79fb3b42009-02-20 20:43:02 +00001460 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001461
1462 // If this is a call to a function that returns an fp value on the floating
1463 // point stack, we must guarantee the the value is popped from the stack, so
1464 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1465 // if the return value is not used. We use the FpGET_ST0 instructions
1466 // instead.
1467 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1468 // If we prefer to use the value in xmm registers, copy it out as f80 and
1469 // use a truncate to move it from fp stack reg to xmm reg.
1470 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1471 bool isST0 = VA.getLocReg() == X86::ST0;
1472 unsigned Opc = 0;
1473 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1474 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1475 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1476 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001477 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001478 Ops, 2), 1);
1479 Val = Chain.getValue(0);
1480
1481 // Round the f80 to the right size, which also moves it to the appropriate
1482 // xmm register.
1483 if (CopyVT != VA.getValVT())
1484 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1485 // This truncation won't change the value.
1486 DAG.getIntPtrConstant(1));
1487 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001488 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1489 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1490 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001491 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001492 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1494 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001495 } else {
1496 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001497 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001498 Val = Chain.getValue(0);
1499 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001500 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001501 } else {
1502 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1503 CopyVT, InFlag).getValue(1);
1504 Val = Chain.getValue(0);
1505 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001506 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001507 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001508 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001509
Dan Gohman98ca4f22009-08-05 01:29:28 +00001510 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001511}
1512
1513
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001514//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001515// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001516//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001517// StdCall calling convention seems to be standard for many Windows' API
1518// routines and around. It differs from C calling convention just a little:
1519// callee should clean up the stack, not caller. Symbols should be also
1520// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001521// For info on fast calling convention see Fast Calling Convention (tail call)
1522// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001523
Dan Gohman98ca4f22009-08-05 01:29:28 +00001524/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001525/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001526static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1527 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001529
Dan Gohman98ca4f22009-08-05 01:29:28 +00001530 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001531}
1532
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001533/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001534/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001535static bool
1536ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1537 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001538 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001539
Dan Gohman98ca4f22009-08-05 01:29:28 +00001540 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001541}
1542
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001543/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1544/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001545/// the specific parameter attribute. The copy will be passed as a byval
1546/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001547static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001548CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001549 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1550 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001551 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001552
Dale Johannesendd64c412009-02-04 00:33:20 +00001553 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001554 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001555 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001556}
1557
Chris Lattner29689432010-03-11 00:22:57 +00001558/// IsTailCallConvention - Return true if the calling convention is one that
1559/// supports tail call optimization.
1560static bool IsTailCallConvention(CallingConv::ID CC) {
1561 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1562}
1563
Evan Cheng0c439eb2010-01-27 00:07:07 +00001564/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1565/// a tailcall target by changing its ABI.
1566static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001567 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001568}
1569
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570SDValue
1571X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001572 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001573 const SmallVectorImpl<ISD::InputArg> &Ins,
1574 DebugLoc dl, SelectionDAG &DAG,
1575 const CCValAssign &VA,
1576 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001577 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001578 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001580 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001581 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001582 EVT ValVT;
1583
1584 // If value is passed by pointer we have address passed instead of the value
1585 // itself.
1586 if (VA.getLocInfo() == CCValAssign::Indirect)
1587 ValVT = VA.getLocVT();
1588 else
1589 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001590
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001591 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001592 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001593 // In case of tail call optimization mark all arguments mutable. Since they
1594 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001595 if (Flags.isByVal()) {
1596 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001597 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001598 return DAG.getFrameIndex(FI, getPointerTy());
1599 } else {
1600 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001601 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001602 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1603 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001604 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001605 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001606 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001607}
1608
Dan Gohman475871a2008-07-27 21:46:04 +00001609SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001611 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 bool isVarArg,
1613 const SmallVectorImpl<ISD::InputArg> &Ins,
1614 DebugLoc dl,
1615 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001616 SmallVectorImpl<SDValue> &InVals)
1617 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001618 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001619 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001620
Gordon Henriksen86737662008-01-05 16:56:59 +00001621 const Function* Fn = MF.getFunction();
1622 if (Fn->hasExternalLinkage() &&
1623 Subtarget->isTargetCygMing() &&
1624 Fn->getName() == "main")
1625 FuncInfo->setForceFramePointer(true);
1626
Evan Cheng1bc78042006-04-26 01:20:17 +00001627 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001628 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001629 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001630
Chris Lattner29689432010-03-11 00:22:57 +00001631 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1632 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001633
Chris Lattner638402b2007-02-28 07:00:42 +00001634 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001635 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001636 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1637 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001638
1639 // Allocate shadow area for Win64
1640 if (IsWin64) {
1641 CCInfo.AllocateStack(32, 8);
1642 }
1643
Duncan Sands45907662010-10-31 13:21:44 +00001644 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001645
Chris Lattnerf39f7712007-02-28 05:46:49 +00001646 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001647 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001648 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1649 CCValAssign &VA = ArgLocs[i];
1650 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1651 // places.
1652 assert(VA.getValNo() != LastVal &&
1653 "Don't support value assigned to multiple locs yet");
1654 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001655
Chris Lattnerf39f7712007-02-28 05:46:49 +00001656 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001657 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001658 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001659 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001660 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001661 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001663 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001665 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001667 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1668 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001669 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001670 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001671 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001672 RC = X86::VR64RegisterClass;
1673 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001674 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001675
Devang Patele9a7ea62011-01-31 21:38:14 +00001676 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001677 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001678
Chris Lattnerf39f7712007-02-28 05:46:49 +00001679 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1680 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1681 // right size.
1682 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001683 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001684 DAG.getValueType(VA.getValVT()));
1685 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001686 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001687 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001688 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001689 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001690
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001691 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001692 // Handle MMX values passed in XMM regs.
1693 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001694 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1695 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001696 } else
1697 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001698 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001699 } else {
1700 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001701 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001702 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001703
1704 // If value is passed via pointer - do a load.
1705 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001706 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1707 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001708
Dan Gohman98ca4f22009-08-05 01:29:28 +00001709 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001710 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001711
Dan Gohman61a92132008-04-21 23:59:07 +00001712 // The x86-64 ABI for returning structs by value requires that we copy
1713 // the sret argument into %rax for the return. Save the argument into
1714 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001715 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001716 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1717 unsigned Reg = FuncInfo->getSRetReturnReg();
1718 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001719 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001720 FuncInfo->setSRetReturnReg(Reg);
1721 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001724 }
1725
Chris Lattnerf39f7712007-02-28 05:46:49 +00001726 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001727 // Align stack specially for tail calls.
1728 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001729 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001730
Evan Cheng1bc78042006-04-26 01:20:17 +00001731 // If the function takes variable number of arguments, make a frame index for
1732 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001733 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001734 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1735 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001736 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001737 }
1738 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001739 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1740
1741 // FIXME: We should really autogenerate these arrays
1742 static const unsigned GPR64ArgRegsWin64[] = {
1743 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001744 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001745 static const unsigned GPR64ArgRegs64Bit[] = {
1746 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1747 };
1748 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001749 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1750 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1751 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001752 const unsigned *GPR64ArgRegs;
1753 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001754
1755 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001756 // The XMM registers which might contain var arg parameters are shadowed
1757 // in their paired GPR. So we only need to save the GPR to their home
1758 // slots.
1759 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001760 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001761 } else {
1762 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1763 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001764
1765 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001766 }
1767 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1768 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001769
Devang Patel578efa92009-06-05 21:57:13 +00001770 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001771 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001772 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001773 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001774 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001775 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001776 // Kernel mode asks for SSE to be disabled, so don't push them
1777 // on the stack.
1778 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001779
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001780 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001781 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001782 // Get to the caller-allocated home save location. Add 8 to account
1783 // for the return address.
1784 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001785 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001786 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001787 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1788 } else {
1789 // For X86-64, if there are vararg parameters that are passed via
1790 // registers, then we must store them to their spots on the stack so they
1791 // may be loaded by deferencing the result of va_next.
1792 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1793 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1794 FuncInfo->setRegSaveFrameIndex(
1795 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001796 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001797 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001798
Gordon Henriksen86737662008-01-05 16:56:59 +00001799 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001800 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001801 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1802 getPointerTy());
1803 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001804 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001805 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1806 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001807 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001808 X86::GR64RegisterClass, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001810 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001811 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001812 MachinePointerInfo::getFixedStack(
1813 FuncInfo->getRegSaveFrameIndex(), Offset),
1814 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001815 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001816 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001817 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001818
Dan Gohmanface41a2009-08-16 21:24:25 +00001819 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1820 // Now store the XMM (fp + vector) parameter registers.
1821 SmallVector<SDValue, 11> SaveXMMOps;
1822 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001823
Devang Patele9a7ea62011-01-31 21:38:14 +00001824 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001825 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1826 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001827
Dan Gohman1e93df62010-04-17 14:41:14 +00001828 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1829 FuncInfo->getRegSaveFrameIndex()));
1830 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1831 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001832
Dan Gohmanface41a2009-08-16 21:24:25 +00001833 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001834 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001835 X86::VR128RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001836 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1837 SaveXMMOps.push_back(Val);
1838 }
1839 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1840 MVT::Other,
1841 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001842 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001843
1844 if (!MemOps.empty())
1845 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1846 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001848 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001849
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001851 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001852 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001853 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001854 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001855 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001856 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001857 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001858 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001859
Gordon Henriksen86737662008-01-05 16:56:59 +00001860 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001861 // RegSaveFrameIndex is X86-64 only.
1862 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001863 if (CallConv == CallingConv::X86_FastCall ||
1864 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001865 // fastcc functions can't have varargs.
1866 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001867 }
Evan Cheng25caf632006-05-23 21:06:34 +00001868
Dan Gohman98ca4f22009-08-05 01:29:28 +00001869 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001870}
1871
Dan Gohman475871a2008-07-27 21:46:04 +00001872SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001873X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1874 SDValue StackPtr, SDValue Arg,
1875 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001876 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001877 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001878 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001879 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001880 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001881 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001882 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001883
1884 return DAG.getStore(Chain, dl, Arg, PtrOff,
1885 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001886 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001887}
1888
Bill Wendling64e87322009-01-16 19:25:27 +00001889/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001890/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001891SDValue
1892X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001893 SDValue &OutRetAddr, SDValue Chain,
1894 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001895 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001896 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001897 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001898 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001899
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001900 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001901 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1902 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001903 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001904}
1905
1906/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1907/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001908static SDValue
1909EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001911 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001912 // Store the return address to the appropriate stack slot.
1913 if (!FPDiff) return Chain;
1914 // Calculate the new stack slot for the return address.
1915 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001916 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001917 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001918 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001919 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001920 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001921 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001922 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001923 return Chain;
1924}
1925
Dan Gohman98ca4f22009-08-05 01:29:28 +00001926SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001927X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001928 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001929 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001930 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001931 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001932 const SmallVectorImpl<ISD::InputArg> &Ins,
1933 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001934 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001935 MachineFunction &MF = DAG.getMachineFunction();
1936 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001937 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001939 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001940
Evan Cheng5f941932010-02-05 02:21:12 +00001941 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001942 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001943 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1944 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001945 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001946
1947 // Sibcalls are automatically detected tailcalls which do not require
1948 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001949 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001950 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001951
1952 if (isTailCall)
1953 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001954 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001955
Chris Lattner29689432010-03-11 00:22:57 +00001956 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1957 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001958
Chris Lattner638402b2007-02-28 07:00:42 +00001959 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001960 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001961 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1962 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001963
1964 // Allocate shadow area for Win64
1965 if (IsWin64) {
1966 CCInfo.AllocateStack(32, 8);
1967 }
1968
Duncan Sands45907662010-10-31 13:21:44 +00001969 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001970
Chris Lattner423c5f42007-02-28 05:31:48 +00001971 // Get a count of how many bytes are to be pushed on the stack.
1972 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001973 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001974 // This is a sibcall. The memory operands are available in caller's
1975 // own caller's stack.
1976 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001977 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001978 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001979
Gordon Henriksen86737662008-01-05 16:56:59 +00001980 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001981 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001982 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001983 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001984 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1985 FPDiff = NumBytesCallerPushed - NumBytes;
1986
1987 // Set the delta of movement of the returnaddr stackslot.
1988 // But only set if delta is greater than previous delta.
1989 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1990 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1991 }
1992
Evan Chengf22f9b32010-02-06 03:28:46 +00001993 if (!IsSibcall)
1994 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001995
Dan Gohman475871a2008-07-27 21:46:04 +00001996 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001997 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001998 if (isTailCall && FPDiff)
1999 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2000 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002001
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2003 SmallVector<SDValue, 8> MemOpChains;
2004 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002005
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002006 // Walk the register/memloc assignments, inserting copies/loads. In the case
2007 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002008 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2009 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002010 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002011 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002012 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002013 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002014
Chris Lattner423c5f42007-02-28 05:31:48 +00002015 // Promote the value if needed.
2016 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002017 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002018 case CCValAssign::Full: break;
2019 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002020 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002021 break;
2022 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002023 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002024 break;
2025 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002026 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2027 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002028 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002029 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2030 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002031 } else
2032 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2033 break;
2034 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002035 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002036 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002037 case CCValAssign::Indirect: {
2038 // Store the argument.
2039 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002040 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002041 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002042 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002043 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002044 Arg = SpillSlot;
2045 break;
2046 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002047 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002048
Chris Lattner423c5f42007-02-28 05:31:48 +00002049 if (VA.isRegLoc()) {
2050 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002051 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002052 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2053 // shadow reg if callee is a varargs function.
2054 unsigned ShadowReg = 0;
2055 switch (VA.getLocReg()) {
2056 case X86::XMM0: ShadowReg = X86::RCX; break;
2057 case X86::XMM1: ShadowReg = X86::RDX; break;
2058 case X86::XMM2: ShadowReg = X86::R8; break;
2059 case X86::XMM3: ShadowReg = X86::R9; break;
2060 }
2061 if (ShadowReg)
2062 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2063 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002064 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002065 assert(VA.isMemLoc());
2066 if (StackPtr.getNode() == 0)
2067 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2068 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2069 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002070 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002071 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002072
Evan Cheng32fe1032006-05-25 00:59:30 +00002073 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002075 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002076
Evan Cheng347d5f72006-04-28 21:29:37 +00002077 // Build a sequence of copy-to-reg nodes chained together with token chain
2078 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002079 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002080 // Tail call byval lowering might overwrite argument registers so in case of
2081 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002082 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002083 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002084 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002085 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002086 InFlag = Chain.getValue(1);
2087 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002088
Chris Lattner88e1fd52009-07-09 04:24:46 +00002089 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002090 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2091 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002092 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002093 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2094 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002095 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002096 InFlag);
2097 InFlag = Chain.getValue(1);
2098 } else {
2099 // If we are tail calling and generating PIC/GOT style code load the
2100 // address of the callee into ECX. The value in ecx is used as target of
2101 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2102 // for tail calls on PIC/GOT architectures. Normally we would just put the
2103 // address of GOT into ebx and then call target@PLT. But for tail calls
2104 // ebx would be restored (since ebx is callee saved) before jumping to the
2105 // target@PLT.
2106
2107 // Note: The actual moving to ECX is done further down.
2108 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2109 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2110 !G->getGlobal()->hasProtectedVisibility())
2111 Callee = LowerGlobalAddress(Callee, DAG);
2112 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002113 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002114 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002115 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002116
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002117 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002118 // From AMD64 ABI document:
2119 // For calls that may call functions that use varargs or stdargs
2120 // (prototype-less calls or calls to functions containing ellipsis (...) in
2121 // the declaration) %al is used as hidden argument to specify the number
2122 // of SSE registers used. The contents of %al do not need to match exactly
2123 // the number of registers, but must be an ubound on the number of SSE
2124 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002125
Gordon Henriksen86737662008-01-05 16:56:59 +00002126 // Count the number of XMM registers allocated.
2127 static const unsigned XMMArgRegs[] = {
2128 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2129 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2130 };
2131 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002132 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002133 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002134
Dale Johannesendd64c412009-02-04 00:33:20 +00002135 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002136 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002137 InFlag = Chain.getValue(1);
2138 }
2139
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002140
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002141 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002142 if (isTailCall) {
2143 // Force all the incoming stack arguments to be loaded from the stack
2144 // before any new outgoing arguments are stored to the stack, because the
2145 // outgoing stack slots may alias the incoming argument stack slots, and
2146 // the alias isn't otherwise explicit. This is slightly more conservative
2147 // than necessary, because it means that each store effectively depends
2148 // on every argument instead of just those arguments it would clobber.
2149 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2150
Dan Gohman475871a2008-07-27 21:46:04 +00002151 SmallVector<SDValue, 8> MemOpChains2;
2152 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002153 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002154 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002155 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002156 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002157 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2158 CCValAssign &VA = ArgLocs[i];
2159 if (VA.isRegLoc())
2160 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002161 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002162 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002163 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002164 // Create frame index.
2165 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002166 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002167 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002168 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002169
Duncan Sands276dcbd2008-03-21 09:14:45 +00002170 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002171 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002172 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002173 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002174 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002175 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002176 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002177
Dan Gohman98ca4f22009-08-05 01:29:28 +00002178 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2179 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002180 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002181 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002182 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002183 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002184 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002185 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002186 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002187 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002188 }
2189 }
2190
2191 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002192 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002193 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002194
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002195 // Copy arguments to their registers.
2196 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002197 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002198 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002199 InFlag = Chain.getValue(1);
2200 }
Dan Gohman475871a2008-07-27 21:46:04 +00002201 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002202
Gordon Henriksen86737662008-01-05 16:56:59 +00002203 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002204 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002205 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002206 }
2207
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002208 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2209 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2210 // In the 64-bit large code model, we have to make all calls
2211 // through a register, since the call instruction's 32-bit
2212 // pc-relative offset may not be large enough to hold the whole
2213 // address.
2214 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002215 // If the callee is a GlobalAddress node (quite common, every direct call
2216 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2217 // it.
2218
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002219 // We should use extra load for direct calls to dllimported functions in
2220 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002221 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002222 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002223 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002224
Chris Lattner48a7d022009-07-09 05:02:21 +00002225 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2226 // external symbols most go through the PLT in PIC mode. If the symbol
2227 // has hidden or protected visibility, or if it is static or local, then
2228 // we don't need to use the PLT - we can directly call it.
2229 if (Subtarget->isTargetELF() &&
2230 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002231 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002232 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002233 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002234 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2235 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002236 // PC-relative references to external symbols should go through $stub,
2237 // unless we're building with the leopard linker or later, which
2238 // automatically synthesizes these stubs.
2239 OpFlags = X86II::MO_DARWIN_STUB;
2240 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002241
Devang Patel0d881da2010-07-06 22:08:15 +00002242 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002243 G->getOffset(), OpFlags);
2244 }
Bill Wendling056292f2008-09-16 21:48:12 +00002245 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002246 unsigned char OpFlags = 0;
2247
Evan Cheng1bf891a2010-12-01 22:59:46 +00002248 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2249 // external symbols should go through the PLT.
2250 if (Subtarget->isTargetELF() &&
2251 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2252 OpFlags = X86II::MO_PLT;
2253 } else if (Subtarget->isPICStyleStubAny() &&
2254 Subtarget->getDarwinVers() < 9) {
2255 // PC-relative references to external symbols should go through $stub,
2256 // unless we're building with the leopard linker or later, which
2257 // automatically synthesizes these stubs.
2258 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002259 }
Eric Christopherfd179292009-08-27 18:07:15 +00002260
Chris Lattner48a7d022009-07-09 05:02:21 +00002261 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2262 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002263 }
2264
Chris Lattnerd96d0722007-02-25 06:40:16 +00002265 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002266 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002267 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002268
Evan Chengf22f9b32010-02-06 03:28:46 +00002269 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002270 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2271 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002272 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002273 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002274
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002275 Ops.push_back(Chain);
2276 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002277
Dan Gohman98ca4f22009-08-05 01:29:28 +00002278 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002279 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002280
Gordon Henriksen86737662008-01-05 16:56:59 +00002281 // Add argument registers to the end of the list so that they are known live
2282 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002283 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2284 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2285 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002286
Evan Cheng586ccac2008-03-18 23:36:35 +00002287 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002288 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002289 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2290
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002291 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002292 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002293 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002294
Gabor Greifba36cb52008-08-28 21:40:38 +00002295 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002296 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002297
Dan Gohman98ca4f22009-08-05 01:29:28 +00002298 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002299 // We used to do:
2300 //// If this is the first return lowered for this function, add the regs
2301 //// to the liveout set for the function.
2302 // This isn't right, although it's probably harmless on x86; liveouts
2303 // should be computed from returns not tail calls. Consider a void
2304 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002305 return DAG.getNode(X86ISD::TC_RETURN, dl,
2306 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002307 }
2308
Dale Johannesenace16102009-02-03 19:33:06 +00002309 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002310 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002311
Chris Lattner2d297092006-05-23 18:50:38 +00002312 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002313 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002314 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002315 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002316 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002317 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002318 // pops the hidden struct pointer, so we have to push it back.
2319 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002320 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002321 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002322 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002323
Gordon Henriksenae636f82008-01-03 16:47:34 +00002324 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002325 if (!IsSibcall) {
2326 Chain = DAG.getCALLSEQ_END(Chain,
2327 DAG.getIntPtrConstant(NumBytes, true),
2328 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2329 true),
2330 InFlag);
2331 InFlag = Chain.getValue(1);
2332 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002333
Chris Lattner3085e152007-02-25 08:59:22 +00002334 // Handle result values, copying them out of physregs into vregs that we
2335 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002336 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2337 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002338}
2339
Evan Cheng25ab6902006-09-08 06:48:29 +00002340
2341//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002342// Fast Calling Convention (tail call) implementation
2343//===----------------------------------------------------------------------===//
2344
2345// Like std call, callee cleans arguments, convention except that ECX is
2346// reserved for storing the tail called function address. Only 2 registers are
2347// free for argument passing (inreg). Tail call optimization is performed
2348// provided:
2349// * tailcallopt is enabled
2350// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002351// On X86_64 architecture with GOT-style position independent code only local
2352// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002353// To keep the stack aligned according to platform abi the function
2354// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2355// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002356// If a tail called function callee has more arguments than the caller the
2357// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002358// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002359// original REtADDR, but before the saved framepointer or the spilled registers
2360// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2361// stack layout:
2362// arg1
2363// arg2
2364// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002365// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002366// move area ]
2367// (possible EBP)
2368// ESI
2369// EDI
2370// local1 ..
2371
2372/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2373/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002374unsigned
2375X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2376 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002377 MachineFunction &MF = DAG.getMachineFunction();
2378 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002379 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002380 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002381 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002382 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002383 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002384 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2385 // Number smaller than 12 so just add the difference.
2386 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2387 } else {
2388 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002389 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002390 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002391 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002392 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002393}
2394
Evan Cheng5f941932010-02-05 02:21:12 +00002395/// MatchingStackOffset - Return true if the given stack call argument is
2396/// already available in the same position (relatively) of the caller's
2397/// incoming argument stack.
2398static
2399bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2400 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2401 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002402 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2403 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002404 if (Arg.getOpcode() == ISD::CopyFromReg) {
2405 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002406 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002407 return false;
2408 MachineInstr *Def = MRI->getVRegDef(VR);
2409 if (!Def)
2410 return false;
2411 if (!Flags.isByVal()) {
2412 if (!TII->isLoadFromStackSlot(Def, FI))
2413 return false;
2414 } else {
2415 unsigned Opcode = Def->getOpcode();
2416 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2417 Def->getOperand(1).isFI()) {
2418 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002419 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002420 } else
2421 return false;
2422 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002423 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2424 if (Flags.isByVal())
2425 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002426 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002427 // define @foo(%struct.X* %A) {
2428 // tail call @bar(%struct.X* byval %A)
2429 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002430 return false;
2431 SDValue Ptr = Ld->getBasePtr();
2432 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2433 if (!FINode)
2434 return false;
2435 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002436 } else
2437 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002438
Evan Cheng4cae1332010-03-05 08:38:04 +00002439 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002440 if (!MFI->isFixedObjectIndex(FI))
2441 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002442 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002443}
2444
Dan Gohman98ca4f22009-08-05 01:29:28 +00002445/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2446/// for tail call optimization. Targets which want to do tail call
2447/// optimization should implement this function.
2448bool
2449X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002450 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002451 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002452 bool isCalleeStructRet,
2453 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002454 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002455 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002456 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002457 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002458 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002459 CalleeCC != CallingConv::C)
2460 return false;
2461
Evan Cheng7096ae42010-01-29 06:45:59 +00002462 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002463 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002464 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002465 CallingConv::ID CallerCC = CallerF->getCallingConv();
2466 bool CCMatch = CallerCC == CalleeCC;
2467
Dan Gohman1797ed52010-02-08 20:27:50 +00002468 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002469 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002470 return true;
2471 return false;
2472 }
2473
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002474 // Look for obvious safe cases to perform tail call optimization that do not
2475 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002476
Evan Cheng2c12cb42010-03-26 16:26:03 +00002477 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2478 // emit a special epilogue.
2479 if (RegInfo->needsStackRealignment(MF))
2480 return false;
2481
Eric Christopher90eb4022010-07-22 00:26:08 +00002482 // Do not sibcall optimize vararg calls unless the call site is not passing
2483 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002484 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002485 return false;
2486
Evan Chenga375d472010-03-15 18:54:48 +00002487 // Also avoid sibcall optimization if either caller or callee uses struct
2488 // return semantics.
2489 if (isCalleeStructRet || isCallerStructRet)
2490 return false;
2491
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002492 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2493 // Therefore if it's not used by the call it is not safe to optimize this into
2494 // a sibcall.
2495 bool Unused = false;
2496 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2497 if (!Ins[i].Used) {
2498 Unused = true;
2499 break;
2500 }
2501 }
2502 if (Unused) {
2503 SmallVector<CCValAssign, 16> RVLocs;
2504 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2505 RVLocs, *DAG.getContext());
2506 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002507 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002508 CCValAssign &VA = RVLocs[i];
2509 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2510 return false;
2511 }
2512 }
2513
Evan Cheng13617962010-04-30 01:12:32 +00002514 // If the calling conventions do not match, then we'd better make sure the
2515 // results are returned in the same way as what the caller expects.
2516 if (!CCMatch) {
2517 SmallVector<CCValAssign, 16> RVLocs1;
2518 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2519 RVLocs1, *DAG.getContext());
2520 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2521
2522 SmallVector<CCValAssign, 16> RVLocs2;
2523 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2524 RVLocs2, *DAG.getContext());
2525 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2526
2527 if (RVLocs1.size() != RVLocs2.size())
2528 return false;
2529 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2530 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2531 return false;
2532 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2533 return false;
2534 if (RVLocs1[i].isRegLoc()) {
2535 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2536 return false;
2537 } else {
2538 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2539 return false;
2540 }
2541 }
2542 }
2543
Evan Chenga6bff982010-01-30 01:22:00 +00002544 // If the callee takes no arguments then go on to check the results of the
2545 // call.
2546 if (!Outs.empty()) {
2547 // Check if stack adjustment is needed. For now, do not do this if any
2548 // argument is passed on the stack.
2549 SmallVector<CCValAssign, 16> ArgLocs;
2550 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2551 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002552
2553 // Allocate shadow area for Win64
2554 if (Subtarget->isTargetWin64()) {
2555 CCInfo.AllocateStack(32, 8);
2556 }
2557
Duncan Sands45907662010-10-31 13:21:44 +00002558 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002559 if (CCInfo.getNextStackOffset()) {
2560 MachineFunction &MF = DAG.getMachineFunction();
2561 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2562 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002563
2564 // Check if the arguments are already laid out in the right way as
2565 // the caller's fixed stack objects.
2566 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002567 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2568 const X86InstrInfo *TII =
2569 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002570 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2571 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002572 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002573 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002574 if (VA.getLocInfo() == CCValAssign::Indirect)
2575 return false;
2576 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002577 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2578 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002579 return false;
2580 }
2581 }
2582 }
Evan Cheng9c044672010-05-29 01:35:22 +00002583
2584 // If the tailcall address may be in a register, then make sure it's
2585 // possible to register allocate for it. In 32-bit, the call address can
2586 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002587 // callee-saved registers are restored. These happen to be the same
2588 // registers used to pass 'inreg' arguments so watch out for those.
2589 if (!Subtarget->is64Bit() &&
2590 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002591 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002592 unsigned NumInRegs = 0;
2593 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2594 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002595 if (!VA.isRegLoc())
2596 continue;
2597 unsigned Reg = VA.getLocReg();
2598 switch (Reg) {
2599 default: break;
2600 case X86::EAX: case X86::EDX: case X86::ECX:
2601 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002602 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002603 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002604 }
2605 }
2606 }
Evan Chenga6bff982010-01-30 01:22:00 +00002607 }
Evan Chengb1712452010-01-27 06:25:16 +00002608
Dale Johannesend155d7e2010-10-25 22:17:05 +00002609 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002610 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002611 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2612 return false;
2613
Evan Cheng86809cc2010-02-03 03:28:02 +00002614 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002615}
2616
Dan Gohman3df24e62008-09-03 23:12:08 +00002617FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002618X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2619 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002620}
2621
2622
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002623//===----------------------------------------------------------------------===//
2624// Other Lowering Hooks
2625//===----------------------------------------------------------------------===//
2626
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002627static bool MayFoldLoad(SDValue Op) {
2628 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2629}
2630
2631static bool MayFoldIntoStore(SDValue Op) {
2632 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2633}
2634
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002635static bool isTargetShuffle(unsigned Opcode) {
2636 switch(Opcode) {
2637 default: return false;
2638 case X86ISD::PSHUFD:
2639 case X86ISD::PSHUFHW:
2640 case X86ISD::PSHUFLW:
2641 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002642 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002643 case X86ISD::SHUFPS:
2644 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002645 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002646 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002647 case X86ISD::MOVLPS:
2648 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002649 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002650 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002651 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002652 case X86ISD::MOVSS:
2653 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002654 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002655 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002656 case X86ISD::PUNPCKLWD:
2657 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002658 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002659 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002660 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002661 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002662 case X86ISD::PUNPCKHWD:
2663 case X86ISD::PUNPCKHBW:
2664 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002665 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002666 return true;
2667 }
2668 return false;
2669}
2670
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002671static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002672 SDValue V1, SelectionDAG &DAG) {
2673 switch(Opc) {
2674 default: llvm_unreachable("Unknown x86 shuffle node");
2675 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002676 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002677 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002678 return DAG.getNode(Opc, dl, VT, V1);
2679 }
2680
2681 return SDValue();
2682}
2683
2684static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002685 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002686 switch(Opc) {
2687 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002688 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002689 case X86ISD::PSHUFHW:
2690 case X86ISD::PSHUFLW:
2691 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2692 }
2693
2694 return SDValue();
2695}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002696
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002697static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2698 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2699 switch(Opc) {
2700 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002701 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002702 case X86ISD::SHUFPD:
2703 case X86ISD::SHUFPS:
2704 return DAG.getNode(Opc, dl, VT, V1, V2,
2705 DAG.getConstant(TargetMask, MVT::i8));
2706 }
2707 return SDValue();
2708}
2709
2710static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2711 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2712 switch(Opc) {
2713 default: llvm_unreachable("Unknown x86 shuffle node");
2714 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002715 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002716 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002717 case X86ISD::MOVLPS:
2718 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002719 case X86ISD::MOVSS:
2720 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002721 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002722 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002723 case X86ISD::PUNPCKLWD:
2724 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002725 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002726 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002727 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002728 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002729 case X86ISD::PUNPCKHWD:
2730 case X86ISD::PUNPCKHBW:
2731 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002732 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002733 return DAG.getNode(Opc, dl, VT, V1, V2);
2734 }
2735 return SDValue();
2736}
2737
Dan Gohmand858e902010-04-17 15:26:15 +00002738SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002739 MachineFunction &MF = DAG.getMachineFunction();
2740 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2741 int ReturnAddrIndex = FuncInfo->getRAIndex();
2742
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002743 if (ReturnAddrIndex == 0) {
2744 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002745 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002746 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002747 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002748 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002749 }
2750
Evan Cheng25ab6902006-09-08 06:48:29 +00002751 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002752}
2753
2754
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002755bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2756 bool hasSymbolicDisplacement) {
2757 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002758 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002759 return false;
2760
2761 // If we don't have a symbolic displacement - we don't have any extra
2762 // restrictions.
2763 if (!hasSymbolicDisplacement)
2764 return true;
2765
2766 // FIXME: Some tweaks might be needed for medium code model.
2767 if (M != CodeModel::Small && M != CodeModel::Kernel)
2768 return false;
2769
2770 // For small code model we assume that latest object is 16MB before end of 31
2771 // bits boundary. We may also accept pretty large negative constants knowing
2772 // that all objects are in the positive half of address space.
2773 if (M == CodeModel::Small && Offset < 16*1024*1024)
2774 return true;
2775
2776 // For kernel code model we know that all object resist in the negative half
2777 // of 32bits address space. We may not accept negative offsets, since they may
2778 // be just off and we may accept pretty large positive ones.
2779 if (M == CodeModel::Kernel && Offset > 0)
2780 return true;
2781
2782 return false;
2783}
2784
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002785/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2786/// specific condition code, returning the condition code and the LHS/RHS of the
2787/// comparison to make.
2788static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2789 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002790 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002791 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2792 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2793 // X > -1 -> X == 0, jump !sign.
2794 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002795 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002796 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2797 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002798 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002799 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002800 // X < 1 -> X <= 0
2801 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002802 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002803 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002804 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002805
Evan Chengd9558e02006-01-06 00:43:03 +00002806 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002807 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002808 case ISD::SETEQ: return X86::COND_E;
2809 case ISD::SETGT: return X86::COND_G;
2810 case ISD::SETGE: return X86::COND_GE;
2811 case ISD::SETLT: return X86::COND_L;
2812 case ISD::SETLE: return X86::COND_LE;
2813 case ISD::SETNE: return X86::COND_NE;
2814 case ISD::SETULT: return X86::COND_B;
2815 case ISD::SETUGT: return X86::COND_A;
2816 case ISD::SETULE: return X86::COND_BE;
2817 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002818 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002819 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002820
Chris Lattner4c78e022008-12-23 23:42:27 +00002821 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002822
Chris Lattner4c78e022008-12-23 23:42:27 +00002823 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002824 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2825 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002826 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2827 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002828 }
2829
Chris Lattner4c78e022008-12-23 23:42:27 +00002830 switch (SetCCOpcode) {
2831 default: break;
2832 case ISD::SETOLT:
2833 case ISD::SETOLE:
2834 case ISD::SETUGT:
2835 case ISD::SETUGE:
2836 std::swap(LHS, RHS);
2837 break;
2838 }
2839
2840 // On a floating point condition, the flags are set as follows:
2841 // ZF PF CF op
2842 // 0 | 0 | 0 | X > Y
2843 // 0 | 0 | 1 | X < Y
2844 // 1 | 0 | 0 | X == Y
2845 // 1 | 1 | 1 | unordered
2846 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002847 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002848 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002849 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002850 case ISD::SETOLT: // flipped
2851 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002852 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002853 case ISD::SETOLE: // flipped
2854 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002855 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002856 case ISD::SETUGT: // flipped
2857 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002858 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002859 case ISD::SETUGE: // flipped
2860 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002861 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002862 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002863 case ISD::SETNE: return X86::COND_NE;
2864 case ISD::SETUO: return X86::COND_P;
2865 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002866 case ISD::SETOEQ:
2867 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002868 }
Evan Chengd9558e02006-01-06 00:43:03 +00002869}
2870
Evan Cheng4a460802006-01-11 00:33:36 +00002871/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2872/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002873/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002874static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002875 switch (X86CC) {
2876 default:
2877 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002878 case X86::COND_B:
2879 case X86::COND_BE:
2880 case X86::COND_E:
2881 case X86::COND_P:
2882 case X86::COND_A:
2883 case X86::COND_AE:
2884 case X86::COND_NE:
2885 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002886 return true;
2887 }
2888}
2889
Evan Chengeb2f9692009-10-27 19:56:55 +00002890/// isFPImmLegal - Returns true if the target can instruction select the
2891/// specified FP immediate natively. If false, the legalizer will
2892/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002893bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002894 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2895 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2896 return true;
2897 }
2898 return false;
2899}
2900
Nate Begeman9008ca62009-04-27 18:41:29 +00002901/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2902/// the specified range (L, H].
2903static bool isUndefOrInRange(int Val, int Low, int Hi) {
2904 return (Val < 0) || (Val >= Low && Val < Hi);
2905}
2906
2907/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2908/// specified value.
2909static bool isUndefOrEqual(int Val, int CmpVal) {
2910 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002911 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002912 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002913}
2914
Nate Begeman9008ca62009-04-27 18:41:29 +00002915/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2916/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2917/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002918static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002919 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002920 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002921 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002922 return (Mask[0] < 2 && Mask[1] < 2);
2923 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002924}
2925
Nate Begeman9008ca62009-04-27 18:41:29 +00002926bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002927 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002928 N->getMask(M);
2929 return ::isPSHUFDMask(M, N->getValueType(0));
2930}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002931
Nate Begeman9008ca62009-04-27 18:41:29 +00002932/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2933/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002934static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002935 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002936 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002937
Nate Begeman9008ca62009-04-27 18:41:29 +00002938 // Lower quadword copied in order or undef.
2939 for (int i = 0; i != 4; ++i)
2940 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002941 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002942
Evan Cheng506d3df2006-03-29 23:07:14 +00002943 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 for (int i = 4; i != 8; ++i)
2945 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002946 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002947
Evan Cheng506d3df2006-03-29 23:07:14 +00002948 return true;
2949}
2950
Nate Begeman9008ca62009-04-27 18:41:29 +00002951bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002952 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002953 N->getMask(M);
2954 return ::isPSHUFHWMask(M, N->getValueType(0));
2955}
Evan Cheng506d3df2006-03-29 23:07:14 +00002956
Nate Begeman9008ca62009-04-27 18:41:29 +00002957/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2958/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002959static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002961 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002962
Rafael Espindola15684b22009-04-24 12:40:33 +00002963 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002964 for (int i = 4; i != 8; ++i)
2965 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002966 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002967
Rafael Espindola15684b22009-04-24 12:40:33 +00002968 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002969 for (int i = 0; i != 4; ++i)
2970 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002971 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002972
Rafael Espindola15684b22009-04-24 12:40:33 +00002973 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002974}
2975
Nate Begeman9008ca62009-04-27 18:41:29 +00002976bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002977 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002978 N->getMask(M);
2979 return ::isPSHUFLWMask(M, N->getValueType(0));
2980}
2981
Nate Begemana09008b2009-10-19 02:17:23 +00002982/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2983/// is suitable for input to PALIGNR.
2984static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2985 bool hasSSSE3) {
2986 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002987
Nate Begemana09008b2009-10-19 02:17:23 +00002988 // Do not handle v2i64 / v2f64 shuffles with palignr.
2989 if (e < 4 || !hasSSSE3)
2990 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002991
Nate Begemana09008b2009-10-19 02:17:23 +00002992 for (i = 0; i != e; ++i)
2993 if (Mask[i] >= 0)
2994 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002995
Nate Begemana09008b2009-10-19 02:17:23 +00002996 // All undef, not a palignr.
2997 if (i == e)
2998 return false;
2999
3000 // Determine if it's ok to perform a palignr with only the LHS, since we
3001 // don't have access to the actual shuffle elements to see if RHS is undef.
3002 bool Unary = Mask[i] < (int)e;
3003 bool NeedsUnary = false;
3004
3005 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003006
Nate Begemana09008b2009-10-19 02:17:23 +00003007 // Check the rest of the elements to see if they are consecutive.
3008 for (++i; i != e; ++i) {
3009 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003010 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003011 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003012
Nate Begemana09008b2009-10-19 02:17:23 +00003013 Unary = Unary && (m < (int)e);
3014 NeedsUnary = NeedsUnary || (m < s);
3015
3016 if (NeedsUnary && !Unary)
3017 return false;
3018 if (Unary && m != ((s+i) & (e-1)))
3019 return false;
3020 if (!Unary && m != (s+i))
3021 return false;
3022 }
3023 return true;
3024}
3025
3026bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3027 SmallVector<int, 8> M;
3028 N->getMask(M);
3029 return ::isPALIGNRMask(M, N->getValueType(0), true);
3030}
3031
Evan Cheng14aed5e2006-03-24 01:18:28 +00003032/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3033/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003034static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003035 int NumElems = VT.getVectorNumElements();
3036 if (NumElems != 2 && NumElems != 4)
3037 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003038
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 int Half = NumElems / 2;
3040 for (int i = 0; i < Half; ++i)
3041 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003042 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 for (int i = Half; i < NumElems; ++i)
3044 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003045 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003046
Evan Cheng14aed5e2006-03-24 01:18:28 +00003047 return true;
3048}
3049
Nate Begeman9008ca62009-04-27 18:41:29 +00003050bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3051 SmallVector<int, 8> M;
3052 N->getMask(M);
3053 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003054}
3055
Evan Cheng213d2cf2007-05-17 18:45:50 +00003056/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003057/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3058/// half elements to come from vector 1 (which would equal the dest.) and
3059/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003060static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003061 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003062
3063 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003065
Nate Begeman9008ca62009-04-27 18:41:29 +00003066 int Half = NumElems / 2;
3067 for (int i = 0; i < Half; ++i)
3068 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003069 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003070 for (int i = Half; i < NumElems; ++i)
3071 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003072 return false;
3073 return true;
3074}
3075
Nate Begeman9008ca62009-04-27 18:41:29 +00003076static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3077 SmallVector<int, 8> M;
3078 N->getMask(M);
3079 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003080}
3081
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003082/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3083/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003084bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3085 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003086 return false;
3087
Evan Cheng2064a2b2006-03-28 06:50:32 +00003088 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003089 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3090 isUndefOrEqual(N->getMaskElt(1), 7) &&
3091 isUndefOrEqual(N->getMaskElt(2), 2) &&
3092 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003093}
3094
Nate Begeman0b10b912009-11-07 23:17:15 +00003095/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3096/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3097/// <2, 3, 2, 3>
3098bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3099 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003100
Nate Begeman0b10b912009-11-07 23:17:15 +00003101 if (NumElems != 4)
3102 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003103
Nate Begeman0b10b912009-11-07 23:17:15 +00003104 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3105 isUndefOrEqual(N->getMaskElt(1), 3) &&
3106 isUndefOrEqual(N->getMaskElt(2), 2) &&
3107 isUndefOrEqual(N->getMaskElt(3), 3);
3108}
3109
Evan Cheng5ced1d82006-04-06 23:23:56 +00003110/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3111/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003112bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3113 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003114
Evan Cheng5ced1d82006-04-06 23:23:56 +00003115 if (NumElems != 2 && NumElems != 4)
3116 return false;
3117
Evan Chengc5cdff22006-04-07 21:53:05 +00003118 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003119 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003120 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003121
Evan Chengc5cdff22006-04-07 21:53:05 +00003122 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003123 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003124 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003125
3126 return true;
3127}
3128
Nate Begeman0b10b912009-11-07 23:17:15 +00003129/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3130/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3131bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003133
Evan Cheng5ced1d82006-04-06 23:23:56 +00003134 if (NumElems != 2 && NumElems != 4)
3135 return false;
3136
Evan Chengc5cdff22006-04-07 21:53:05 +00003137 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003138 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003139 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003140
Nate Begeman9008ca62009-04-27 18:41:29 +00003141 for (unsigned i = 0; i < NumElems/2; ++i)
3142 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003143 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003144
3145 return true;
3146}
3147
Evan Cheng0038e592006-03-28 00:39:58 +00003148/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3149/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003150static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003151 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003153 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003154 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003155
Nate Begeman9008ca62009-04-27 18:41:29 +00003156 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3157 int BitI = Mask[i];
3158 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003159 if (!isUndefOrEqual(BitI, j))
3160 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003161 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003162 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003163 return false;
3164 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003165 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003166 return false;
3167 }
Evan Cheng0038e592006-03-28 00:39:58 +00003168 }
Evan Cheng0038e592006-03-28 00:39:58 +00003169 return true;
3170}
3171
Nate Begeman9008ca62009-04-27 18:41:29 +00003172bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3173 SmallVector<int, 8> M;
3174 N->getMask(M);
3175 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003176}
3177
Evan Cheng4fcb9222006-03-28 02:43:26 +00003178/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3179/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003180static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003181 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003183 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003184 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3187 int BitI = Mask[i];
3188 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003189 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003190 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003191 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003192 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003193 return false;
3194 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003195 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003196 return false;
3197 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003198 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003199 return true;
3200}
3201
Nate Begeman9008ca62009-04-27 18:41:29 +00003202bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3203 SmallVector<int, 8> M;
3204 N->getMask(M);
3205 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003206}
3207
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003208/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3209/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3210/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003211static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003212 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003213 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003214 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003215
Nate Begeman9008ca62009-04-27 18:41:29 +00003216 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3217 int BitI = Mask[i];
3218 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003219 if (!isUndefOrEqual(BitI, j))
3220 return false;
3221 if (!isUndefOrEqual(BitI1, j))
3222 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003223 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003224 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003225}
3226
Nate Begeman9008ca62009-04-27 18:41:29 +00003227bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3228 SmallVector<int, 8> M;
3229 N->getMask(M);
3230 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3231}
3232
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003233/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3234/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3235/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003236static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003238 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3239 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003240
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3242 int BitI = Mask[i];
3243 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003244 if (!isUndefOrEqual(BitI, j))
3245 return false;
3246 if (!isUndefOrEqual(BitI1, j))
3247 return false;
3248 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003249 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003250}
3251
Nate Begeman9008ca62009-04-27 18:41:29 +00003252bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3253 SmallVector<int, 8> M;
3254 N->getMask(M);
3255 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3256}
3257
Evan Cheng017dcc62006-04-21 01:05:10 +00003258/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3259/// specifies a shuffle of elements that is suitable for input to MOVSS,
3260/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003261static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003262 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003263 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003264
3265 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003266
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003268 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003269
Nate Begeman9008ca62009-04-27 18:41:29 +00003270 for (int i = 1; i < NumElts; ++i)
3271 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003272 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003273
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003274 return true;
3275}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003276
Nate Begeman9008ca62009-04-27 18:41:29 +00003277bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3278 SmallVector<int, 8> M;
3279 N->getMask(M);
3280 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003281}
3282
Evan Cheng017dcc62006-04-21 01:05:10 +00003283/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3284/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003285/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003286static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 bool V2IsSplat = false, bool V2IsUndef = false) {
3288 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003289 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003290 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003291
Nate Begeman9008ca62009-04-27 18:41:29 +00003292 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003293 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003294
Nate Begeman9008ca62009-04-27 18:41:29 +00003295 for (int i = 1; i < NumOps; ++i)
3296 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3297 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3298 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003299 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003300
Evan Cheng39623da2006-04-20 08:58:49 +00003301 return true;
3302}
3303
Nate Begeman9008ca62009-04-27 18:41:29 +00003304static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003305 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 SmallVector<int, 8> M;
3307 N->getMask(M);
3308 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003309}
3310
Evan Chengd9539472006-04-14 21:59:03 +00003311/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3312/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003313bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3314 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003315 return false;
3316
3317 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003318 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003319 int Elt = N->getMaskElt(i);
3320 if (Elt >= 0 && Elt != 1)
3321 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003322 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003323
3324 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003325 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003326 int Elt = N->getMaskElt(i);
3327 if (Elt >= 0 && Elt != 3)
3328 return false;
3329 if (Elt == 3)
3330 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003331 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003332 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003333 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003334 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003335}
3336
3337/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3338/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003339bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3340 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003341 return false;
3342
3343 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 for (unsigned i = 0; i < 2; ++i)
3345 if (N->getMaskElt(i) > 0)
3346 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003347
3348 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003349 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003350 int Elt = N->getMaskElt(i);
3351 if (Elt >= 0 && Elt != 2)
3352 return false;
3353 if (Elt == 2)
3354 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003355 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003356 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003357 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003358}
3359
Evan Cheng0b457f02008-09-25 20:50:48 +00003360/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3361/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003362bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3363 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003364
Nate Begeman9008ca62009-04-27 18:41:29 +00003365 for (int i = 0; i < e; ++i)
3366 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003367 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003368 for (int i = 0; i < e; ++i)
3369 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003370 return false;
3371 return true;
3372}
3373
David Greenec38a03e2011-02-03 15:50:00 +00003374/// isVEXTRACTF128Index - Return true if the specified
3375/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3376/// suitable for input to VEXTRACTF128.
3377bool X86::isVEXTRACTF128Index(SDNode *N) {
3378 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3379 return false;
3380
3381 // The index should be aligned on a 128-bit boundary.
3382 uint64_t Index =
3383 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3384
3385 unsigned VL = N->getValueType(0).getVectorNumElements();
3386 unsigned VBits = N->getValueType(0).getSizeInBits();
3387 unsigned ElSize = VBits / VL;
3388 bool Result = (Index * ElSize) % 128 == 0;
3389
3390 return Result;
3391}
3392
David Greeneccacdc12011-02-04 16:08:29 +00003393/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3394/// operand specifies a subvector insert that is suitable for input to
3395/// VINSERTF128.
3396bool X86::isVINSERTF128Index(SDNode *N) {
3397 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3398 return false;
3399
3400 // The index should be aligned on a 128-bit boundary.
3401 uint64_t Index =
3402 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3403
3404 unsigned VL = N->getValueType(0).getVectorNumElements();
3405 unsigned VBits = N->getValueType(0).getSizeInBits();
3406 unsigned ElSize = VBits / VL;
3407 bool Result = (Index * ElSize) % 128 == 0;
3408
3409 return Result;
3410}
3411
Evan Cheng63d33002006-03-22 08:01:21 +00003412/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003413/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003414unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3416 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3417
Evan Chengb9df0ca2006-03-22 02:53:00 +00003418 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3419 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 for (int i = 0; i < NumOperands; ++i) {
3421 int Val = SVOp->getMaskElt(NumOperands-i-1);
3422 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003423 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003424 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003425 if (i != NumOperands - 1)
3426 Mask <<= Shift;
3427 }
Evan Cheng63d33002006-03-22 08:01:21 +00003428 return Mask;
3429}
3430
Evan Cheng506d3df2006-03-29 23:07:14 +00003431/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003432/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003433unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003435 unsigned Mask = 0;
3436 // 8 nodes, but we only care about the last 4.
3437 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003438 int Val = SVOp->getMaskElt(i);
3439 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003440 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003441 if (i != 4)
3442 Mask <<= 2;
3443 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003444 return Mask;
3445}
3446
3447/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003448/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003449unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003450 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003451 unsigned Mask = 0;
3452 // 8 nodes, but we only care about the first 4.
3453 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 int Val = SVOp->getMaskElt(i);
3455 if (Val >= 0)
3456 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003457 if (i != 0)
3458 Mask <<= 2;
3459 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003460 return Mask;
3461}
3462
Nate Begemana09008b2009-10-19 02:17:23 +00003463/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3464/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3465unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3466 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3467 EVT VVT = N->getValueType(0);
3468 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3469 int Val = 0;
3470
3471 unsigned i, e;
3472 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3473 Val = SVOp->getMaskElt(i);
3474 if (Val >= 0)
3475 break;
3476 }
3477 return (Val - i) * EltSize;
3478}
3479
David Greenec38a03e2011-02-03 15:50:00 +00003480/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3481/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3482/// instructions.
3483unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3484 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3485 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3486
3487 uint64_t Index =
3488 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3489
3490 EVT VecVT = N->getOperand(0).getValueType();
3491 EVT ElVT = VecVT.getVectorElementType();
3492
3493 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3494
3495 return Index / NumElemsPerChunk;
3496}
3497
David Greeneccacdc12011-02-04 16:08:29 +00003498/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3499/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3500/// instructions.
3501unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3502 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3503 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3504
3505 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003506 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003507
3508 EVT VecVT = N->getValueType(0);
3509 EVT ElVT = VecVT.getVectorElementType();
3510
3511 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3512
3513 return Index / NumElemsPerChunk;
3514}
3515
Evan Cheng37b73872009-07-30 08:33:02 +00003516/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3517/// constant +0.0.
3518bool X86::isZeroNode(SDValue Elt) {
3519 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003520 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003521 (isa<ConstantFPSDNode>(Elt) &&
3522 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3523}
3524
Nate Begeman9008ca62009-04-27 18:41:29 +00003525/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3526/// their permute mask.
3527static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3528 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003529 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003530 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003531 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003532
Nate Begeman5a5ca152009-04-29 05:20:52 +00003533 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003534 int idx = SVOp->getMaskElt(i);
3535 if (idx < 0)
3536 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003537 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003538 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003539 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003540 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003541 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003542 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3543 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003544}
3545
Evan Cheng779ccea2007-12-07 21:30:01 +00003546/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3547/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003548static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003549 unsigned NumElems = VT.getVectorNumElements();
3550 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003551 int idx = Mask[i];
3552 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003553 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003554 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003555 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003556 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003557 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003558 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003559}
3560
Evan Cheng533a0aa2006-04-19 20:35:22 +00003561/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3562/// match movhlps. The lower half elements should come from upper half of
3563/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003564/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003565static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3566 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003567 return false;
3568 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003569 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003570 return false;
3571 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003572 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003573 return false;
3574 return true;
3575}
3576
Evan Cheng5ced1d82006-04-06 23:23:56 +00003577/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003578/// is promoted to a vector. It also returns the LoadSDNode by reference if
3579/// required.
3580static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003581 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3582 return false;
3583 N = N->getOperand(0).getNode();
3584 if (!ISD::isNON_EXTLoad(N))
3585 return false;
3586 if (LD)
3587 *LD = cast<LoadSDNode>(N);
3588 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003589}
3590
Evan Cheng533a0aa2006-04-19 20:35:22 +00003591/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3592/// match movlp{s|d}. The lower half elements should come from lower half of
3593/// V1 (and in order), and the upper half elements should come from the upper
3594/// half of V2 (and in order). And since V1 will become the source of the
3595/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003596static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3597 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003598 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003599 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003600 // Is V2 is a vector load, don't do this transformation. We will try to use
3601 // load folding shufps op.
3602 if (ISD::isNON_EXTLoad(V2))
3603 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003604
Nate Begeman5a5ca152009-04-29 05:20:52 +00003605 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003606
Evan Cheng533a0aa2006-04-19 20:35:22 +00003607 if (NumElems != 2 && NumElems != 4)
3608 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003609 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003610 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003611 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003612 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003613 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003614 return false;
3615 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003616}
3617
Evan Cheng39623da2006-04-20 08:58:49 +00003618/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3619/// all the same.
3620static bool isSplatVector(SDNode *N) {
3621 if (N->getOpcode() != ISD::BUILD_VECTOR)
3622 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003623
Dan Gohman475871a2008-07-27 21:46:04 +00003624 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003625 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3626 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003627 return false;
3628 return true;
3629}
3630
Evan Cheng213d2cf2007-05-17 18:45:50 +00003631/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003632/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003633/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003634static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003635 SDValue V1 = N->getOperand(0);
3636 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003637 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3638 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003639 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003640 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003641 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003642 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3643 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003644 if (Opc != ISD::BUILD_VECTOR ||
3645 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003646 return false;
3647 } else if (Idx >= 0) {
3648 unsigned Opc = V1.getOpcode();
3649 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3650 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003651 if (Opc != ISD::BUILD_VECTOR ||
3652 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003653 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003654 }
3655 }
3656 return true;
3657}
3658
3659/// getZeroVector - Returns a vector of specified type with all zero elements.
3660///
Owen Andersone50ed302009-08-10 22:56:29 +00003661static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003662 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003663 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003664
Dale Johannesen0488fb62010-09-30 23:57:10 +00003665 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003666 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003667 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003668 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003669 if (HasSSE2) { // SSE2
3670 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3671 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3672 } else { // SSE1
3673 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3674 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3675 }
3676 } else if (VT.getSizeInBits() == 256) { // AVX
3677 // 256-bit logic and arithmetic instructions in AVX are
3678 // all floating-point, no support for integer ops. Default
3679 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003681 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3682 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003683 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003684 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003685}
3686
Chris Lattner8a594482007-11-25 00:24:49 +00003687/// getOnesVector - Returns a vector of specified type with all bits set.
3688///
Owen Andersone50ed302009-08-10 22:56:29 +00003689static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003690 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003691
Chris Lattner8a594482007-11-25 00:24:49 +00003692 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3693 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003694 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003695 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003696 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003697 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003698}
3699
3700
Evan Cheng39623da2006-04-20 08:58:49 +00003701/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3702/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003703static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003704 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003705 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003706
Evan Cheng39623da2006-04-20 08:58:49 +00003707 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003708 SmallVector<int, 8> MaskVec;
3709 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003710
Nate Begeman5a5ca152009-04-29 05:20:52 +00003711 for (unsigned i = 0; i != NumElems; ++i) {
3712 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003713 MaskVec[i] = NumElems;
3714 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003715 }
Evan Cheng39623da2006-04-20 08:58:49 +00003716 }
Evan Cheng39623da2006-04-20 08:58:49 +00003717 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003718 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3719 SVOp->getOperand(1), &MaskVec[0]);
3720 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003721}
3722
Evan Cheng017dcc62006-04-21 01:05:10 +00003723/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3724/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003725static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003726 SDValue V2) {
3727 unsigned NumElems = VT.getVectorNumElements();
3728 SmallVector<int, 8> Mask;
3729 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003730 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003731 Mask.push_back(i);
3732 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003733}
3734
Nate Begeman9008ca62009-04-27 18:41:29 +00003735/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003736static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003737 SDValue V2) {
3738 unsigned NumElems = VT.getVectorNumElements();
3739 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003740 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003741 Mask.push_back(i);
3742 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003743 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003744 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003745}
3746
Nate Begeman9008ca62009-04-27 18:41:29 +00003747/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003748static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003749 SDValue V2) {
3750 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003751 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003752 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003753 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003754 Mask.push_back(i + Half);
3755 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003756 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003757 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003758}
3759
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003760/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3761static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003762 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003763 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003764 DebugLoc dl = SV->getDebugLoc();
3765 SDValue V1 = SV->getOperand(0);
3766 int NumElems = VT.getVectorNumElements();
3767 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003768
Nate Begeman9008ca62009-04-27 18:41:29 +00003769 // unpack elements to the correct location
3770 while (NumElems > 4) {
3771 if (EltNo < NumElems/2) {
3772 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3773 } else {
3774 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3775 EltNo -= NumElems/2;
3776 }
3777 NumElems >>= 1;
3778 }
Eric Christopherfd179292009-08-27 18:07:15 +00003779
Nate Begeman9008ca62009-04-27 18:41:29 +00003780 // Perform the splat.
3781 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003782 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003783 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003784 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003785}
3786
Evan Chengba05f722006-04-21 23:03:30 +00003787/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003788/// vector of zero or undef vector. This produces a shuffle where the low
3789/// element of V2 is swizzled into the zero/undef vector, landing at element
3790/// 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 +00003791static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003792 bool isZero, bool HasSSE2,
3793 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003794 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003795 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3797 unsigned NumElems = VT.getVectorNumElements();
3798 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003799 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 // If this is the insertion idx, put the low elt of V2 here.
3801 MaskVec.push_back(i == Idx ? NumElems : i);
3802 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003803}
3804
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003805/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3806/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003807SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3808 unsigned Depth) {
3809 if (Depth == 6)
3810 return SDValue(); // Limit search depth.
3811
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003812 SDValue V = SDValue(N, 0);
3813 EVT VT = V.getValueType();
3814 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003815
3816 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3817 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3818 Index = SV->getMaskElt(Index);
3819
3820 if (Index < 0)
3821 return DAG.getUNDEF(VT.getVectorElementType());
3822
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003823 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003824 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003825 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003826 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003827
3828 // Recurse into target specific vector shuffles to find scalars.
3829 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003830 int NumElems = VT.getVectorNumElements();
3831 SmallVector<unsigned, 16> ShuffleMask;
3832 SDValue ImmN;
3833
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003834 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003835 case X86ISD::SHUFPS:
3836 case X86ISD::SHUFPD:
3837 ImmN = N->getOperand(N->getNumOperands()-1);
3838 DecodeSHUFPSMask(NumElems,
3839 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3840 ShuffleMask);
3841 break;
3842 case X86ISD::PUNPCKHBW:
3843 case X86ISD::PUNPCKHWD:
3844 case X86ISD::PUNPCKHDQ:
3845 case X86ISD::PUNPCKHQDQ:
3846 DecodePUNPCKHMask(NumElems, ShuffleMask);
3847 break;
3848 case X86ISD::UNPCKHPS:
3849 case X86ISD::UNPCKHPD:
3850 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3851 break;
3852 case X86ISD::PUNPCKLBW:
3853 case X86ISD::PUNPCKLWD:
3854 case X86ISD::PUNPCKLDQ:
3855 case X86ISD::PUNPCKLQDQ:
3856 DecodePUNPCKLMask(NumElems, ShuffleMask);
3857 break;
3858 case X86ISD::UNPCKLPS:
3859 case X86ISD::UNPCKLPD:
3860 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3861 break;
3862 case X86ISD::MOVHLPS:
3863 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3864 break;
3865 case X86ISD::MOVLHPS:
3866 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3867 break;
3868 case X86ISD::PSHUFD:
3869 ImmN = N->getOperand(N->getNumOperands()-1);
3870 DecodePSHUFMask(NumElems,
3871 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3872 ShuffleMask);
3873 break;
3874 case X86ISD::PSHUFHW:
3875 ImmN = N->getOperand(N->getNumOperands()-1);
3876 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3877 ShuffleMask);
3878 break;
3879 case X86ISD::PSHUFLW:
3880 ImmN = N->getOperand(N->getNumOperands()-1);
3881 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3882 ShuffleMask);
3883 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003884 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003885 case X86ISD::MOVSD: {
3886 // The index 0 always comes from the first element of the second source,
3887 // this is why MOVSS and MOVSD are used in the first place. The other
3888 // elements come from the other positions of the first source vector.
3889 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003890 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3891 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003892 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003893 default:
3894 assert("not implemented for target shuffle node");
3895 return SDValue();
3896 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003897
3898 Index = ShuffleMask[Index];
3899 if (Index < 0)
3900 return DAG.getUNDEF(VT.getVectorElementType());
3901
3902 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3903 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3904 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003905 }
3906
3907 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003908 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003909 V = V.getOperand(0);
3910 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003911 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003912
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003913 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003914 return SDValue();
3915 }
3916
3917 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3918 return (Index == 0) ? V.getOperand(0)
3919 : DAG.getUNDEF(VT.getVectorElementType());
3920
3921 if (V.getOpcode() == ISD::BUILD_VECTOR)
3922 return V.getOperand(Index);
3923
3924 return SDValue();
3925}
3926
3927/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3928/// shuffle operation which come from a consecutively from a zero. The
3929/// search can start in two diferent directions, from left or right.
3930static
3931unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3932 bool ZerosFromLeft, SelectionDAG &DAG) {
3933 int i = 0;
3934
3935 while (i < NumElems) {
3936 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003937 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003938 if (!(Elt.getNode() &&
3939 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3940 break;
3941 ++i;
3942 }
3943
3944 return i;
3945}
3946
3947/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3948/// MaskE correspond consecutively to elements from one of the vector operands,
3949/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3950static
3951bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3952 int OpIdx, int NumElems, unsigned &OpNum) {
3953 bool SeenV1 = false;
3954 bool SeenV2 = false;
3955
3956 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3957 int Idx = SVOp->getMaskElt(i);
3958 // Ignore undef indicies
3959 if (Idx < 0)
3960 continue;
3961
3962 if (Idx < NumElems)
3963 SeenV1 = true;
3964 else
3965 SeenV2 = true;
3966
3967 // Only accept consecutive elements from the same vector
3968 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3969 return false;
3970 }
3971
3972 OpNum = SeenV1 ? 0 : 1;
3973 return true;
3974}
3975
3976/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3977/// logical left shift of a vector.
3978static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3979 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3980 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3981 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3982 false /* check zeros from right */, DAG);
3983 unsigned OpSrc;
3984
3985 if (!NumZeros)
3986 return false;
3987
3988 // Considering the elements in the mask that are not consecutive zeros,
3989 // check if they consecutively come from only one of the source vectors.
3990 //
3991 // V1 = {X, A, B, C} 0
3992 // \ \ \ /
3993 // vector_shuffle V1, V2 <1, 2, 3, X>
3994 //
3995 if (!isShuffleMaskConsecutive(SVOp,
3996 0, // Mask Start Index
3997 NumElems-NumZeros-1, // Mask End Index
3998 NumZeros, // Where to start looking in the src vector
3999 NumElems, // Number of elements in vector
4000 OpSrc)) // Which source operand ?
4001 return false;
4002
4003 isLeft = false;
4004 ShAmt = NumZeros;
4005 ShVal = SVOp->getOperand(OpSrc);
4006 return true;
4007}
4008
4009/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4010/// logical left shift of a vector.
4011static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4012 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4013 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4014 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4015 true /* check zeros from left */, DAG);
4016 unsigned OpSrc;
4017
4018 if (!NumZeros)
4019 return false;
4020
4021 // Considering the elements in the mask that are not consecutive zeros,
4022 // check if they consecutively come from only one of the source vectors.
4023 //
4024 // 0 { A, B, X, X } = V2
4025 // / \ / /
4026 // vector_shuffle V1, V2 <X, X, 4, 5>
4027 //
4028 if (!isShuffleMaskConsecutive(SVOp,
4029 NumZeros, // Mask Start Index
4030 NumElems-1, // Mask End Index
4031 0, // Where to start looking in the src vector
4032 NumElems, // Number of elements in vector
4033 OpSrc)) // Which source operand ?
4034 return false;
4035
4036 isLeft = true;
4037 ShAmt = NumZeros;
4038 ShVal = SVOp->getOperand(OpSrc);
4039 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004040}
4041
4042/// isVectorShift - Returns true if the shuffle can be implemented as a
4043/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004044static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004045 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004046 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4047 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4048 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004049
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004050 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004051}
4052
Evan Chengc78d3b42006-04-24 18:01:45 +00004053/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4054///
Dan Gohman475871a2008-07-27 21:46:04 +00004055static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004056 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004057 SelectionDAG &DAG,
4058 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004059 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004060 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004061
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004062 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004063 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004064 bool First = true;
4065 for (unsigned i = 0; i < 16; ++i) {
4066 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4067 if (ThisIsNonZero && First) {
4068 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004069 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004070 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004071 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004072 First = false;
4073 }
4074
4075 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004076 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004077 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4078 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004079 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004080 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004081 }
4082 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4084 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4085 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004086 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004087 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004088 } else
4089 ThisElt = LastElt;
4090
Gabor Greifba36cb52008-08-28 21:40:38 +00004091 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004092 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004093 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004094 }
4095 }
4096
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004097 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004098}
4099
Bill Wendlinga348c562007-03-22 18:42:45 +00004100/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004101///
Dan Gohman475871a2008-07-27 21:46:04 +00004102static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004103 unsigned NumNonZero, unsigned NumZero,
4104 SelectionDAG &DAG,
4105 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004106 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004107 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004108
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004109 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004110 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004111 bool First = true;
4112 for (unsigned i = 0; i < 8; ++i) {
4113 bool isNonZero = (NonZeros & (1 << i)) != 0;
4114 if (isNonZero) {
4115 if (First) {
4116 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004117 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004118 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004119 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004120 First = false;
4121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004122 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004123 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004124 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004125 }
4126 }
4127
4128 return V;
4129}
4130
Evan Chengf26ffe92008-05-29 08:22:04 +00004131/// getVShift - Return a vector logical shift node.
4132///
Owen Andersone50ed302009-08-10 22:56:29 +00004133static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004134 unsigned NumBits, SelectionDAG &DAG,
4135 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004136 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004137 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004138 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4139 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004140 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00004141 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00004142}
4143
Dan Gohman475871a2008-07-27 21:46:04 +00004144SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004145X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004146 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004147
Evan Chengc3630942009-12-09 21:00:30 +00004148 // Check if the scalar load can be widened into a vector load. And if
4149 // the address is "base + cst" see if the cst can be "absorbed" into
4150 // the shuffle mask.
4151 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4152 SDValue Ptr = LD->getBasePtr();
4153 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4154 return SDValue();
4155 EVT PVT = LD->getValueType(0);
4156 if (PVT != MVT::i32 && PVT != MVT::f32)
4157 return SDValue();
4158
4159 int FI = -1;
4160 int64_t Offset = 0;
4161 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4162 FI = FINode->getIndex();
4163 Offset = 0;
4164 } else if (Ptr.getOpcode() == ISD::ADD &&
4165 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4166 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4167 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4168 Offset = Ptr.getConstantOperandVal(1);
4169 Ptr = Ptr.getOperand(0);
4170 } else {
4171 return SDValue();
4172 }
4173
4174 SDValue Chain = LD->getChain();
4175 // Make sure the stack object alignment is at least 16.
4176 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4177 if (DAG.InferPtrAlignment(Ptr) < 16) {
4178 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004179 // Can't change the alignment. FIXME: It's possible to compute
4180 // the exact stack offset and reference FI + adjust offset instead.
4181 // If someone *really* cares about this. That's the way to implement it.
4182 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004183 } else {
4184 MFI->setObjectAlignment(FI, 16);
4185 }
4186 }
4187
4188 // (Offset % 16) must be multiple of 4. Then address is then
4189 // Ptr + (Offset & ~15).
4190 if (Offset < 0)
4191 return SDValue();
4192 if ((Offset % 16) & 3)
4193 return SDValue();
4194 int64_t StartOffset = Offset & ~15;
4195 if (StartOffset)
4196 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4197 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4198
4199 int EltNo = (Offset - StartOffset) >> 2;
4200 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4201 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004202 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4203 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004204 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004205 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004206 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4207 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004208 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004209 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004210 }
4211
4212 return SDValue();
4213}
4214
Michael J. Spencerec38de22010-10-10 22:04:20 +00004215/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4216/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004217/// load which has the same value as a build_vector whose operands are 'elts'.
4218///
4219/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004220///
Nate Begeman1449f292010-03-24 22:19:06 +00004221/// FIXME: we'd also like to handle the case where the last elements are zero
4222/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4223/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004224static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004225 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004226 EVT EltVT = VT.getVectorElementType();
4227 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004228
Nate Begemanfdea31a2010-03-24 20:49:50 +00004229 LoadSDNode *LDBase = NULL;
4230 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004231
Nate Begeman1449f292010-03-24 22:19:06 +00004232 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004233 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004234 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004235 for (unsigned i = 0; i < NumElems; ++i) {
4236 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004237
Nate Begemanfdea31a2010-03-24 20:49:50 +00004238 if (!Elt.getNode() ||
4239 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4240 return SDValue();
4241 if (!LDBase) {
4242 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4243 return SDValue();
4244 LDBase = cast<LoadSDNode>(Elt.getNode());
4245 LastLoadedElt = i;
4246 continue;
4247 }
4248 if (Elt.getOpcode() == ISD::UNDEF)
4249 continue;
4250
4251 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4252 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4253 return SDValue();
4254 LastLoadedElt = i;
4255 }
Nate Begeman1449f292010-03-24 22:19:06 +00004256
4257 // If we have found an entire vector of loads and undefs, then return a large
4258 // load of the entire vector width starting at the base pointer. If we found
4259 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004260 if (LastLoadedElt == NumElems - 1) {
4261 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004262 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004263 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004264 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004265 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004266 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004267 LDBase->isVolatile(), LDBase->isNonTemporal(),
4268 LDBase->getAlignment());
4269 } else if (NumElems == 4 && LastLoadedElt == 1) {
4270 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4271 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004272 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4273 Ops, 2, MVT::i32,
4274 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004275 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004276 }
4277 return SDValue();
4278}
4279
Evan Chengc3630942009-12-09 21:00:30 +00004280SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004281X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004282 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004283
Chris Lattner6e80e442010-08-28 17:15:43 +00004284 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4285 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004286 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4287 // is present, so AllOnes is ignored.
4288 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4289 (Op.getValueType().getSizeInBits() != 256 &&
4290 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004291 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004292 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4293 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004294 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004295 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004296
Gabor Greifba36cb52008-08-28 21:40:38 +00004297 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004298 return getOnesVector(Op.getValueType(), DAG, dl);
4299 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004300 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004301
Owen Andersone50ed302009-08-10 22:56:29 +00004302 EVT VT = Op.getValueType();
4303 EVT ExtVT = VT.getVectorElementType();
4304 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004305
4306 unsigned NumElems = Op.getNumOperands();
4307 unsigned NumZero = 0;
4308 unsigned NumNonZero = 0;
4309 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004310 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004311 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004312 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004313 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004314 if (Elt.getOpcode() == ISD::UNDEF)
4315 continue;
4316 Values.insert(Elt);
4317 if (Elt.getOpcode() != ISD::Constant &&
4318 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004319 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004320 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004321 NumZero++;
4322 else {
4323 NonZeros |= (1 << i);
4324 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004325 }
4326 }
4327
Chris Lattner97a2a562010-08-26 05:24:29 +00004328 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4329 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004330 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004331
Chris Lattner67f453a2008-03-09 05:42:06 +00004332 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004333 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004334 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004335 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004336
Chris Lattner62098042008-03-09 01:05:04 +00004337 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4338 // the value are obviously zero, truncate the value to i32 and do the
4339 // insertion that way. Only do this if the value is non-constant or if the
4340 // value is a constant being inserted into element 0. It is cheaper to do
4341 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004343 (!IsAllConstants || Idx == 0)) {
4344 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004345 // Handle SSE only.
4346 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4347 EVT VecVT = MVT::v4i32;
4348 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004349
Chris Lattner62098042008-03-09 01:05:04 +00004350 // Truncate the value (which may itself be a constant) to i32, and
4351 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004352 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004353 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004354 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4355 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004356
Chris Lattner62098042008-03-09 01:05:04 +00004357 // Now we have our 32-bit value zero extended in the low element of
4358 // a vector. If Idx != 0, swizzle it into place.
4359 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 SmallVector<int, 4> Mask;
4361 Mask.push_back(Idx);
4362 for (unsigned i = 1; i != VecElts; ++i)
4363 Mask.push_back(i);
4364 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004365 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004367 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004368 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004369 }
4370 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004371
Chris Lattner19f79692008-03-08 22:59:52 +00004372 // If we have a constant or non-constant insertion into the low element of
4373 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4374 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004375 // depending on what the source datatype is.
4376 if (Idx == 0) {
4377 if (NumZero == 0) {
4378 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004379 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4380 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004381 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4382 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4383 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4384 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004385 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4386 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004387 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4388 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004389 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4390 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4391 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004392 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004393 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004394 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004395
4396 // Is it a vector logical left shift?
4397 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004398 X86::isZeroNode(Op.getOperand(0)) &&
4399 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004400 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004401 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004402 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004403 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004404 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004406
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004407 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004408 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004409
Chris Lattner19f79692008-03-08 22:59:52 +00004410 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4411 // is a non-constant being inserted into an element other than the low one,
4412 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4413 // movd/movss) to move this into the low element, then shuffle it into
4414 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004415 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004416 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004417
Evan Cheng0db9fe62006-04-25 20:13:52 +00004418 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004419 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4420 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004421 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 MaskVec.push_back(i == Idx ? 0 : 1);
4424 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004425 }
4426 }
4427
Chris Lattner67f453a2008-03-09 05:42:06 +00004428 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004429 if (Values.size() == 1) {
4430 if (EVTBits == 32) {
4431 // Instead of a shuffle like this:
4432 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4433 // Check if it's possible to issue this instead.
4434 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4435 unsigned Idx = CountTrailingZeros_32(NonZeros);
4436 SDValue Item = Op.getOperand(Idx);
4437 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4438 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4439 }
Dan Gohman475871a2008-07-27 21:46:04 +00004440 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004441 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004442
Dan Gohmana3941172007-07-24 22:55:08 +00004443 // A vector full of immediates; various special cases are already
4444 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004445 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004446 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004447
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004448 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004449 if (EVTBits == 64) {
4450 if (NumNonZero == 1) {
4451 // One half is zero or undef.
4452 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004453 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004454 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004455 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4456 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004457 }
Dan Gohman475871a2008-07-27 21:46:04 +00004458 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004459 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004460
4461 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004462 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004463 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004464 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004465 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004466 }
4467
Bill Wendling826f36f2007-03-28 00:57:11 +00004468 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004469 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004470 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004471 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004472 }
4473
4474 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004475 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004476 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004477 if (NumElems == 4 && NumZero > 0) {
4478 for (unsigned i = 0; i < 4; ++i) {
4479 bool isZero = !(NonZeros & (1 << i));
4480 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004481 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004482 else
Dale Johannesenace16102009-02-03 19:33:06 +00004483 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004484 }
4485
4486 for (unsigned i = 0; i < 2; ++i) {
4487 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4488 default: break;
4489 case 0:
4490 V[i] = V[i*2]; // Must be a zero vector.
4491 break;
4492 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004494 break;
4495 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004496 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004497 break;
4498 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004499 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004500 break;
4501 }
4502 }
4503
Nate Begeman9008ca62009-04-27 18:41:29 +00004504 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004505 bool Reverse = (NonZeros & 0x3) == 2;
4506 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004507 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004508 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4509 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004510 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4511 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004512 }
4513
Nate Begemanfdea31a2010-03-24 20:49:50 +00004514 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4515 // Check for a build vector of consecutive loads.
4516 for (unsigned i = 0; i < NumElems; ++i)
4517 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004518
Nate Begemanfdea31a2010-03-24 20:49:50 +00004519 // Check for elements which are consecutive loads.
4520 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4521 if (LD.getNode())
4522 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004523
4524 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004525 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004526 SDValue Result;
4527 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4528 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4529 else
4530 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004531
Chris Lattner24faf612010-08-28 17:59:08 +00004532 for (unsigned i = 1; i < NumElems; ++i) {
4533 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4534 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004535 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004536 }
4537 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004538 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004539
Chris Lattner6e80e442010-08-28 17:15:43 +00004540 // Otherwise, expand into a number of unpckl*, start by extending each of
4541 // our (non-undef) elements to the full vector width with the element in the
4542 // bottom slot of the vector (which generates no code for SSE).
4543 for (unsigned i = 0; i < NumElems; ++i) {
4544 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4545 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4546 else
4547 V[i] = DAG.getUNDEF(VT);
4548 }
4549
4550 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004551 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4552 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4553 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004554 unsigned EltStride = NumElems >> 1;
4555 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004556 for (unsigned i = 0; i < EltStride; ++i) {
4557 // If V[i+EltStride] is undef and this is the first round of mixing,
4558 // then it is safe to just drop this shuffle: V[i] is already in the
4559 // right place, the one element (since it's the first round) being
4560 // inserted as undef can be dropped. This isn't safe for successive
4561 // rounds because they will permute elements within both vectors.
4562 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4563 EltStride == NumElems/2)
4564 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004565
Chris Lattner6e80e442010-08-28 17:15:43 +00004566 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004567 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004568 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004569 }
4570 return V[0];
4571 }
Dan Gohman475871a2008-07-27 21:46:04 +00004572 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004573}
4574
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004575SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004576X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004577 // We support concatenate two MMX registers and place them in a MMX
4578 // register. This is better than doing a stack convert.
4579 DebugLoc dl = Op.getDebugLoc();
4580 EVT ResVT = Op.getValueType();
4581 assert(Op.getNumOperands() == 2);
4582 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4583 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4584 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004585 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004586 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4587 InVec = Op.getOperand(1);
4588 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4589 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004590 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004591 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4592 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4593 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004594 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004595 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4596 Mask[0] = 0; Mask[1] = 2;
4597 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4598 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004599 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004600}
4601
Nate Begemanb9a47b82009-02-23 08:49:38 +00004602// v8i16 shuffles - Prefer shuffles in the following order:
4603// 1. [all] pshuflw, pshufhw, optional move
4604// 2. [ssse3] 1 x pshufb
4605// 3. [ssse3] 2 x pshufb + 1 x por
4606// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004607SDValue
4608X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4609 SelectionDAG &DAG) const {
4610 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004611 SDValue V1 = SVOp->getOperand(0);
4612 SDValue V2 = SVOp->getOperand(1);
4613 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004614 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004615
Nate Begemanb9a47b82009-02-23 08:49:38 +00004616 // Determine if more than 1 of the words in each of the low and high quadwords
4617 // of the result come from the same quadword of one of the two inputs. Undef
4618 // mask values count as coming from any quadword, for better codegen.
4619 SmallVector<unsigned, 4> LoQuad(4);
4620 SmallVector<unsigned, 4> HiQuad(4);
4621 BitVector InputQuads(4);
4622 for (unsigned i = 0; i < 8; ++i) {
4623 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004624 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004625 MaskVals.push_back(EltIdx);
4626 if (EltIdx < 0) {
4627 ++Quad[0];
4628 ++Quad[1];
4629 ++Quad[2];
4630 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004631 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004632 }
4633 ++Quad[EltIdx / 4];
4634 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004635 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004636
Nate Begemanb9a47b82009-02-23 08:49:38 +00004637 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004638 unsigned MaxQuad = 1;
4639 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004640 if (LoQuad[i] > MaxQuad) {
4641 BestLoQuad = i;
4642 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004643 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004644 }
4645
Nate Begemanb9a47b82009-02-23 08:49:38 +00004646 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004647 MaxQuad = 1;
4648 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004649 if (HiQuad[i] > MaxQuad) {
4650 BestHiQuad = i;
4651 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004652 }
4653 }
4654
Nate Begemanb9a47b82009-02-23 08:49:38 +00004655 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004656 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004657 // single pshufb instruction is necessary. If There are more than 2 input
4658 // quads, disable the next transformation since it does not help SSSE3.
4659 bool V1Used = InputQuads[0] || InputQuads[1];
4660 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004661 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004662 if (InputQuads.count() == 2 && V1Used && V2Used) {
4663 BestLoQuad = InputQuads.find_first();
4664 BestHiQuad = InputQuads.find_next(BestLoQuad);
4665 }
4666 if (InputQuads.count() > 2) {
4667 BestLoQuad = -1;
4668 BestHiQuad = -1;
4669 }
4670 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004671
Nate Begemanb9a47b82009-02-23 08:49:38 +00004672 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4673 // the shuffle mask. If a quad is scored as -1, that means that it contains
4674 // words from all 4 input quadwords.
4675 SDValue NewV;
4676 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 SmallVector<int, 8> MaskV;
4678 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4679 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004680 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004681 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4682 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4683 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004684
Nate Begemanb9a47b82009-02-23 08:49:38 +00004685 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4686 // source words for the shuffle, to aid later transformations.
4687 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004688 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004689 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004690 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004691 if (idx != (int)i)
4692 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004693 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004694 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004695 AllWordsInNewV = false;
4696 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004697 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004698
Nate Begemanb9a47b82009-02-23 08:49:38 +00004699 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4700 if (AllWordsInNewV) {
4701 for (int i = 0; i != 8; ++i) {
4702 int idx = MaskVals[i];
4703 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004704 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004705 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004706 if ((idx != i) && idx < 4)
4707 pshufhw = false;
4708 if ((idx != i) && idx > 3)
4709 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004710 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004711 V1 = NewV;
4712 V2Used = false;
4713 BestLoQuad = 0;
4714 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004715 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004716
Nate Begemanb9a47b82009-02-23 08:49:38 +00004717 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4718 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004719 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004720 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4721 unsigned TargetMask = 0;
4722 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004723 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004724 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4725 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4726 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004727 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004728 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004729 }
Eric Christopherfd179292009-08-27 18:07:15 +00004730
Nate Begemanb9a47b82009-02-23 08:49:38 +00004731 // If we have SSSE3, and all words of the result are from 1 input vector,
4732 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4733 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004734 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004735 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004736
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004738 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004739 // mask, and elements that come from V1 in the V2 mask, so that the two
4740 // results can be OR'd together.
4741 bool TwoInputs = V1Used && V2Used;
4742 for (unsigned i = 0; i != 8; ++i) {
4743 int EltIdx = MaskVals[i] * 2;
4744 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4746 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004747 continue;
4748 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004749 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4750 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004751 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004752 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004753 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004754 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004755 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004756 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004757 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004758
Nate Begemanb9a47b82009-02-23 08:49:38 +00004759 // Calculate the shuffle mask for the second input, shuffle it, and
4760 // OR it with the first shuffled input.
4761 pshufbMask.clear();
4762 for (unsigned i = 0; i != 8; ++i) {
4763 int EltIdx = MaskVals[i] * 2;
4764 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004765 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4766 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004767 continue;
4768 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004769 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4770 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004771 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004772 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004773 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004774 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004775 MVT::v16i8, &pshufbMask[0], 16));
4776 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004777 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004778 }
4779
4780 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4781 // and update MaskVals with new element order.
4782 BitVector InOrder(8);
4783 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004784 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004785 for (int i = 0; i != 4; ++i) {
4786 int idx = MaskVals[i];
4787 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004788 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 InOrder.set(i);
4790 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004791 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004792 InOrder.set(i);
4793 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004794 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004795 }
4796 }
4797 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004798 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004799 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004800 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004801
4802 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4803 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4804 NewV.getOperand(0),
4805 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4806 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004807 }
Eric Christopherfd179292009-08-27 18:07:15 +00004808
Nate Begemanb9a47b82009-02-23 08:49:38 +00004809 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4810 // and update MaskVals with the new element order.
4811 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004812 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004814 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004815 for (unsigned i = 4; i != 8; ++i) {
4816 int idx = MaskVals[i];
4817 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004818 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 InOrder.set(i);
4820 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004821 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004822 InOrder.set(i);
4823 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004824 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004825 }
4826 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004828 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004829
4830 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4831 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4832 NewV.getOperand(0),
4833 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4834 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 }
Eric Christopherfd179292009-08-27 18:07:15 +00004836
Nate Begemanb9a47b82009-02-23 08:49:38 +00004837 // In case BestHi & BestLo were both -1, which means each quadword has a word
4838 // from each of the four input quadwords, calculate the InOrder bitvector now
4839 // before falling through to the insert/extract cleanup.
4840 if (BestLoQuad == -1 && BestHiQuad == -1) {
4841 NewV = V1;
4842 for (int i = 0; i != 8; ++i)
4843 if (MaskVals[i] < 0 || MaskVals[i] == i)
4844 InOrder.set(i);
4845 }
Eric Christopherfd179292009-08-27 18:07:15 +00004846
Nate Begemanb9a47b82009-02-23 08:49:38 +00004847 // The other elements are put in the right place using pextrw and pinsrw.
4848 for (unsigned i = 0; i != 8; ++i) {
4849 if (InOrder[i])
4850 continue;
4851 int EltIdx = MaskVals[i];
4852 if (EltIdx < 0)
4853 continue;
4854 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004855 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004856 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004857 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004858 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004859 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004860 DAG.getIntPtrConstant(i));
4861 }
4862 return NewV;
4863}
4864
4865// v16i8 shuffles - Prefer shuffles in the following order:
4866// 1. [ssse3] 1 x pshufb
4867// 2. [ssse3] 2 x pshufb + 1 x por
4868// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4869static
Nate Begeman9008ca62009-04-27 18:41:29 +00004870SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004871 SelectionDAG &DAG,
4872 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004873 SDValue V1 = SVOp->getOperand(0);
4874 SDValue V2 = SVOp->getOperand(1);
4875 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004876 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004877 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004878
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004880 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004881 // present, fall back to case 3.
4882 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4883 bool V1Only = true;
4884 bool V2Only = true;
4885 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004886 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004887 if (EltIdx < 0)
4888 continue;
4889 if (EltIdx < 16)
4890 V2Only = false;
4891 else
4892 V1Only = false;
4893 }
Eric Christopherfd179292009-08-27 18:07:15 +00004894
Nate Begemanb9a47b82009-02-23 08:49:38 +00004895 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4896 if (TLI.getSubtarget()->hasSSSE3()) {
4897 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004898
Nate Begemanb9a47b82009-02-23 08:49:38 +00004899 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004900 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004901 //
4902 // Otherwise, we have elements from both input vectors, and must zero out
4903 // elements that come from V2 in the first mask, and V1 in the second mask
4904 // so that we can OR them together.
4905 bool TwoInputs = !(V1Only || V2Only);
4906 for (unsigned i = 0; i != 16; ++i) {
4907 int EltIdx = MaskVals[i];
4908 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004909 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 continue;
4911 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004912 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004913 }
4914 // If all the elements are from V2, assign it to V1 and return after
4915 // building the first pshufb.
4916 if (V2Only)
4917 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004918 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004919 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004920 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004921 if (!TwoInputs)
4922 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004923
Nate Begemanb9a47b82009-02-23 08:49:38 +00004924 // Calculate the shuffle mask for the second input, shuffle it, and
4925 // OR it with the first shuffled input.
4926 pshufbMask.clear();
4927 for (unsigned i = 0; i != 16; ++i) {
4928 int EltIdx = MaskVals[i];
4929 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004930 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004931 continue;
4932 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004933 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004934 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004935 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004936 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 MVT::v16i8, &pshufbMask[0], 16));
4938 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004939 }
Eric Christopherfd179292009-08-27 18:07:15 +00004940
Nate Begemanb9a47b82009-02-23 08:49:38 +00004941 // No SSSE3 - Calculate in place words and then fix all out of place words
4942 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4943 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004944 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4945 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004946 SDValue NewV = V2Only ? V2 : V1;
4947 for (int i = 0; i != 8; ++i) {
4948 int Elt0 = MaskVals[i*2];
4949 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004950
Nate Begemanb9a47b82009-02-23 08:49:38 +00004951 // This word of the result is all undef, skip it.
4952 if (Elt0 < 0 && Elt1 < 0)
4953 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004954
Nate Begemanb9a47b82009-02-23 08:49:38 +00004955 // This word of the result is already in the correct place, skip it.
4956 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4957 continue;
4958 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4959 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004960
Nate Begemanb9a47b82009-02-23 08:49:38 +00004961 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4962 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4963 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004964
4965 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4966 // using a single extract together, load it and store it.
4967 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004968 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004969 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004970 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004971 DAG.getIntPtrConstant(i));
4972 continue;
4973 }
4974
Nate Begemanb9a47b82009-02-23 08:49:38 +00004975 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004976 // source byte is not also odd, shift the extracted word left 8 bits
4977 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004978 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004979 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004980 DAG.getIntPtrConstant(Elt1 / 2));
4981 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004982 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004983 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004984 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004985 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4986 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004987 }
4988 // If Elt0 is defined, extract it from the appropriate source. If the
4989 // source byte is not also even, shift the extracted word right 8 bits. If
4990 // Elt1 was also defined, OR the extracted values together before
4991 // inserting them in the result.
4992 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004993 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004994 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4995 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004996 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004997 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004998 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004999 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5000 DAG.getConstant(0x00FF, MVT::i16));
5001 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005002 : InsElt0;
5003 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005004 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005005 DAG.getIntPtrConstant(i));
5006 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005007 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005008}
5009
Evan Cheng7a831ce2007-12-15 03:00:47 +00005010/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005011/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005012/// done when every pair / quad of shuffle mask elements point to elements in
5013/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005014/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005015static
Nate Begeman9008ca62009-04-27 18:41:29 +00005016SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005017 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005018 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005019 SDValue V1 = SVOp->getOperand(0);
5020 SDValue V2 = SVOp->getOperand(1);
5021 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005022 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005023 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005024 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005025 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005026 case MVT::v4f32: NewVT = MVT::v2f64; break;
5027 case MVT::v4i32: NewVT = MVT::v2i64; break;
5028 case MVT::v8i16: NewVT = MVT::v4i32; break;
5029 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005030 }
5031
Nate Begeman9008ca62009-04-27 18:41:29 +00005032 int Scale = NumElems / NewWidth;
5033 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005034 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005035 int StartIdx = -1;
5036 for (int j = 0; j < Scale; ++j) {
5037 int EltIdx = SVOp->getMaskElt(i+j);
5038 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005039 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005040 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005041 StartIdx = EltIdx - (EltIdx % Scale);
5042 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005043 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005044 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005045 if (StartIdx == -1)
5046 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005047 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005048 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005049 }
5050
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005051 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5052 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005053 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005054}
5055
Evan Chengd880b972008-05-09 21:53:03 +00005056/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005057///
Owen Andersone50ed302009-08-10 22:56:29 +00005058static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005059 SDValue SrcOp, SelectionDAG &DAG,
5060 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005061 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005062 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005063 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005064 LD = dyn_cast<LoadSDNode>(SrcOp);
5065 if (!LD) {
5066 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5067 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005068 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005069 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005070 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005071 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005072 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005073 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005074 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005075 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005076 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5077 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5078 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005079 SrcOp.getOperand(0)
5080 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005081 }
5082 }
5083 }
5084
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005085 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005086 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005087 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005088 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005089}
5090
Evan Chengace3c172008-07-22 21:13:36 +00005091/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5092/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005093static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005094LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5095 SDValue V1 = SVOp->getOperand(0);
5096 SDValue V2 = SVOp->getOperand(1);
5097 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005098 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005099
Evan Chengace3c172008-07-22 21:13:36 +00005100 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005101 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005102 SmallVector<int, 8> Mask1(4U, -1);
5103 SmallVector<int, 8> PermMask;
5104 SVOp->getMask(PermMask);
5105
Evan Chengace3c172008-07-22 21:13:36 +00005106 unsigned NumHi = 0;
5107 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005108 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005109 int Idx = PermMask[i];
5110 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005111 Locs[i] = std::make_pair(-1, -1);
5112 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005113 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5114 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005115 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005116 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005117 NumLo++;
5118 } else {
5119 Locs[i] = std::make_pair(1, NumHi);
5120 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005121 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005122 NumHi++;
5123 }
5124 }
5125 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005126
Evan Chengace3c172008-07-22 21:13:36 +00005127 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005128 // If no more than two elements come from either vector. This can be
5129 // implemented with two shuffles. First shuffle gather the elements.
5130 // The second shuffle, which takes the first shuffle as both of its
5131 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005132 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005133
Nate Begeman9008ca62009-04-27 18:41:29 +00005134 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005135
Evan Chengace3c172008-07-22 21:13:36 +00005136 for (unsigned i = 0; i != 4; ++i) {
5137 if (Locs[i].first == -1)
5138 continue;
5139 else {
5140 unsigned Idx = (i < 2) ? 0 : 4;
5141 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005142 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005143 }
5144 }
5145
Nate Begeman9008ca62009-04-27 18:41:29 +00005146 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005147 } else if (NumLo == 3 || NumHi == 3) {
5148 // Otherwise, we must have three elements from one vector, call it X, and
5149 // one element from the other, call it Y. First, use a shufps to build an
5150 // intermediate vector with the one element from Y and the element from X
5151 // that will be in the same half in the final destination (the indexes don't
5152 // matter). Then, use a shufps to build the final vector, taking the half
5153 // containing the element from Y from the intermediate, and the other half
5154 // from X.
5155 if (NumHi == 3) {
5156 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005157 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005158 std::swap(V1, V2);
5159 }
5160
5161 // Find the element from V2.
5162 unsigned HiIndex;
5163 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005164 int Val = PermMask[HiIndex];
5165 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005166 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005167 if (Val >= 4)
5168 break;
5169 }
5170
Nate Begeman9008ca62009-04-27 18:41:29 +00005171 Mask1[0] = PermMask[HiIndex];
5172 Mask1[1] = -1;
5173 Mask1[2] = PermMask[HiIndex^1];
5174 Mask1[3] = -1;
5175 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005176
5177 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005178 Mask1[0] = PermMask[0];
5179 Mask1[1] = PermMask[1];
5180 Mask1[2] = HiIndex & 1 ? 6 : 4;
5181 Mask1[3] = HiIndex & 1 ? 4 : 6;
5182 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005183 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005184 Mask1[0] = HiIndex & 1 ? 2 : 0;
5185 Mask1[1] = HiIndex & 1 ? 0 : 2;
5186 Mask1[2] = PermMask[2];
5187 Mask1[3] = PermMask[3];
5188 if (Mask1[2] >= 0)
5189 Mask1[2] += 4;
5190 if (Mask1[3] >= 0)
5191 Mask1[3] += 4;
5192 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005193 }
Evan Chengace3c172008-07-22 21:13:36 +00005194 }
5195
5196 // Break it into (shuffle shuffle_hi, shuffle_lo).
5197 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005198 SmallVector<int,8> LoMask(4U, -1);
5199 SmallVector<int,8> HiMask(4U, -1);
5200
5201 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005202 unsigned MaskIdx = 0;
5203 unsigned LoIdx = 0;
5204 unsigned HiIdx = 2;
5205 for (unsigned i = 0; i != 4; ++i) {
5206 if (i == 2) {
5207 MaskPtr = &HiMask;
5208 MaskIdx = 1;
5209 LoIdx = 0;
5210 HiIdx = 2;
5211 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005212 int Idx = PermMask[i];
5213 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005214 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005215 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005216 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005217 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005218 LoIdx++;
5219 } else {
5220 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005221 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005222 HiIdx++;
5223 }
5224 }
5225
Nate Begeman9008ca62009-04-27 18:41:29 +00005226 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5227 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5228 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005229 for (unsigned i = 0; i != 4; ++i) {
5230 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005231 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005232 } else {
5233 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005234 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005235 }
5236 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005237 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005238}
5239
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005240static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005241 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005242 V = V.getOperand(0);
5243 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5244 V = V.getOperand(0);
5245 if (MayFoldLoad(V))
5246 return true;
5247 return false;
5248}
5249
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005250// FIXME: the version above should always be used. Since there's
5251// a bug where several vector shuffles can't be folded because the
5252// DAG is not updated during lowering and a node claims to have two
5253// uses while it only has one, use this version, and let isel match
5254// another instruction if the load really happens to have more than
5255// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005256// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005257static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005258 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005259 V = V.getOperand(0);
5260 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5261 V = V.getOperand(0);
5262 if (ISD::isNormalLoad(V.getNode()))
5263 return true;
5264 return false;
5265}
5266
5267/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5268/// a vector extract, and if both can be later optimized into a single load.
5269/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5270/// here because otherwise a target specific shuffle node is going to be
5271/// emitted for this shuffle, and the optimization not done.
5272/// FIXME: This is probably not the best approach, but fix the problem
5273/// until the right path is decided.
5274static
5275bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5276 const TargetLowering &TLI) {
5277 EVT VT = V.getValueType();
5278 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5279
5280 // Be sure that the vector shuffle is present in a pattern like this:
5281 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5282 if (!V.hasOneUse())
5283 return false;
5284
5285 SDNode *N = *V.getNode()->use_begin();
5286 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5287 return false;
5288
5289 SDValue EltNo = N->getOperand(1);
5290 if (!isa<ConstantSDNode>(EltNo))
5291 return false;
5292
5293 // If the bit convert changed the number of elements, it is unsafe
5294 // to examine the mask.
5295 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005296 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005297 EVT SrcVT = V.getOperand(0).getValueType();
5298 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5299 return false;
5300 V = V.getOperand(0);
5301 HasShuffleIntoBitcast = true;
5302 }
5303
5304 // Select the input vector, guarding against out of range extract vector.
5305 unsigned NumElems = VT.getVectorNumElements();
5306 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5307 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5308 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5309
5310 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005311 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005312 V = V.getOperand(0);
5313
5314 if (ISD::isNormalLoad(V.getNode())) {
5315 // Is the original load suitable?
5316 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5317
5318 // FIXME: avoid the multi-use bug that is preventing lots of
5319 // of foldings to be detected, this is still wrong of course, but
5320 // give the temporary desired behavior, and if it happens that
5321 // the load has real more uses, during isel it will not fold, and
5322 // will generate poor code.
5323 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5324 return false;
5325
5326 if (!HasShuffleIntoBitcast)
5327 return true;
5328
5329 // If there's a bitcast before the shuffle, check if the load type and
5330 // alignment is valid.
5331 unsigned Align = LN0->getAlignment();
5332 unsigned NewAlign =
5333 TLI.getTargetData()->getABITypeAlignment(
5334 VT.getTypeForEVT(*DAG.getContext()));
5335
5336 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5337 return false;
5338 }
5339
5340 return true;
5341}
5342
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005343static
Evan Cheng835580f2010-10-07 20:50:20 +00005344SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5345 EVT VT = Op.getValueType();
5346
5347 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005348 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5349 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005350 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5351 V1, DAG));
5352}
5353
5354static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005355SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5356 bool HasSSE2) {
5357 SDValue V1 = Op.getOperand(0);
5358 SDValue V2 = Op.getOperand(1);
5359 EVT VT = Op.getValueType();
5360
5361 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5362
5363 if (HasSSE2 && VT == MVT::v2f64)
5364 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5365
5366 // v4f32 or v4i32
5367 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5368}
5369
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005370static
5371SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5372 SDValue V1 = Op.getOperand(0);
5373 SDValue V2 = Op.getOperand(1);
5374 EVT VT = Op.getValueType();
5375
5376 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5377 "unsupported shuffle type");
5378
5379 if (V2.getOpcode() == ISD::UNDEF)
5380 V2 = V1;
5381
5382 // v4i32 or v4f32
5383 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5384}
5385
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005386static
5387SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5388 SDValue V1 = Op.getOperand(0);
5389 SDValue V2 = Op.getOperand(1);
5390 EVT VT = Op.getValueType();
5391 unsigned NumElems = VT.getVectorNumElements();
5392
5393 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5394 // operand of these instructions is only memory, so check if there's a
5395 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5396 // same masks.
5397 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005398
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005399 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005400 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005401 CanFoldLoad = true;
5402
5403 // When V1 is a load, it can be folded later into a store in isel, example:
5404 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5405 // turns into:
5406 // (MOVLPSmr addr:$src1, VR128:$src2)
5407 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005408 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005409 CanFoldLoad = true;
5410
5411 if (CanFoldLoad) {
5412 if (HasSSE2 && NumElems == 2)
5413 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5414
5415 if (NumElems == 4)
5416 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5417 }
5418
5419 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5420 // movl and movlp will both match v2i64, but v2i64 is never matched by
5421 // movl earlier because we make it strict to avoid messing with the movlp load
5422 // folding logic (see the code above getMOVLP call). Match it here then,
5423 // this is horrible, but will stay like this until we move all shuffle
5424 // matching to x86 specific nodes. Note that for the 1st condition all
5425 // types are matched with movsd.
5426 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5427 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5428 else if (HasSSE2)
5429 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5430
5431
5432 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5433
5434 // Invert the operand order and use SHUFPS to match it.
5435 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5436 X86::getShuffleSHUFImmediate(SVOp), DAG);
5437}
5438
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005439static inline unsigned getUNPCKLOpcode(EVT VT) {
5440 switch(VT.getSimpleVT().SimpleTy) {
5441 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5442 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5443 case MVT::v4f32: return X86ISD::UNPCKLPS;
5444 case MVT::v2f64: return X86ISD::UNPCKLPD;
5445 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5446 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5447 default:
5448 llvm_unreachable("Unknow type for unpckl");
5449 }
5450 return 0;
5451}
5452
5453static inline unsigned getUNPCKHOpcode(EVT VT) {
5454 switch(VT.getSimpleVT().SimpleTy) {
5455 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5456 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5457 case MVT::v4f32: return X86ISD::UNPCKHPS;
5458 case MVT::v2f64: return X86ISD::UNPCKHPD;
5459 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5460 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5461 default:
5462 llvm_unreachable("Unknow type for unpckh");
5463 }
5464 return 0;
5465}
5466
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005467static
5468SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005469 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005470 const X86Subtarget *Subtarget) {
5471 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5472 EVT VT = Op.getValueType();
5473 DebugLoc dl = Op.getDebugLoc();
5474 SDValue V1 = Op.getOperand(0);
5475 SDValue V2 = Op.getOperand(1);
5476
5477 if (isZeroShuffle(SVOp))
5478 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5479
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005480 // Handle splat operations
5481 if (SVOp->isSplat()) {
5482 // Special case, this is the only place now where it's
5483 // allowed to return a vector_shuffle operation without
5484 // using a target specific node, because *hopefully* it
5485 // will be optimized away by the dag combiner.
5486 if (VT.getVectorNumElements() <= 4 &&
5487 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5488 return Op;
5489
5490 // Handle splats by matching through known masks
5491 if (VT.getVectorNumElements() <= 4)
5492 return SDValue();
5493
Evan Cheng835580f2010-10-07 20:50:20 +00005494 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005495 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005496 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005497
5498 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5499 // do it!
5500 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5501 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5502 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005503 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005504 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5505 // FIXME: Figure out a cleaner way to do this.
5506 // Try to make use of movq to zero out the top part.
5507 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5508 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5509 if (NewOp.getNode()) {
5510 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5511 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5512 DAG, Subtarget, dl);
5513 }
5514 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5515 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5516 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5517 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5518 DAG, Subtarget, dl);
5519 }
5520 }
5521 return SDValue();
5522}
5523
Dan Gohman475871a2008-07-27 21:46:04 +00005524SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005525X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005526 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005527 SDValue V1 = Op.getOperand(0);
5528 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005529 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005530 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005531 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005532 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005533 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5534 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005535 bool V1IsSplat = false;
5536 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005537 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005538 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005539 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005540 MachineFunction &MF = DAG.getMachineFunction();
5541 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005542
Dale Johannesen0488fb62010-09-30 23:57:10 +00005543 // Shuffle operations on MMX not supported.
5544 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005545 return Op;
5546
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005547 // Vector shuffle lowering takes 3 steps:
5548 //
5549 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5550 // narrowing and commutation of operands should be handled.
5551 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5552 // shuffle nodes.
5553 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5554 // so the shuffle can be broken into other shuffles and the legalizer can
5555 // try the lowering again.
5556 //
5557 // The general ideia is that no vector_shuffle operation should be left to
5558 // be matched during isel, all of them must be converted to a target specific
5559 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005560
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005561 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5562 // narrowing and commutation of operands should be handled. The actual code
5563 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005564 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005565 if (NewOp.getNode())
5566 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005567
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005568 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5569 // unpckh_undef). Only use pshufd if speed is more important than size.
5570 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5571 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5572 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5573 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5574 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5575 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005576
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005577 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005578 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005579 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005580
Dale Johannesen0488fb62010-09-30 23:57:10 +00005581 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005582 return getMOVHighToLow(Op, dl, DAG);
5583
5584 // Use to match splats
5585 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5586 (VT == MVT::v2f64 || VT == MVT::v2i64))
5587 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5588
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005589 if (X86::isPSHUFDMask(SVOp)) {
5590 // The actual implementation will match the mask in the if above and then
5591 // during isel it can match several different instructions, not only pshufd
5592 // as its name says, sad but true, emulate the behavior for now...
5593 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5594 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5595
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005596 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5597
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005598 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005599 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5600
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005601 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005602 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5603 TargetMask, DAG);
5604
5605 if (VT == MVT::v4f32)
5606 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5607 TargetMask, DAG);
5608 }
Eric Christopherfd179292009-08-27 18:07:15 +00005609
Evan Chengf26ffe92008-05-29 08:22:04 +00005610 // Check if this can be converted into a logical shift.
5611 bool isLeft = false;
5612 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005613 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005614 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005615 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005616 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005617 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005618 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005619 EVT EltVT = VT.getVectorElementType();
5620 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005621 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005622 }
Eric Christopherfd179292009-08-27 18:07:15 +00005623
Nate Begeman9008ca62009-04-27 18:41:29 +00005624 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005625 if (V1IsUndef)
5626 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005627 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005628 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005629 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005630 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005631 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5632
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005633 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005634 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5635 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005636 }
Eric Christopherfd179292009-08-27 18:07:15 +00005637
Nate Begeman9008ca62009-04-27 18:41:29 +00005638 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005639 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5640 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005641
Dale Johannesen0488fb62010-09-30 23:57:10 +00005642 if (X86::isMOVHLPSMask(SVOp))
5643 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005644
Dale Johannesen0488fb62010-09-30 23:57:10 +00005645 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5646 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005647
Dale Johannesen0488fb62010-09-30 23:57:10 +00005648 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5649 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005650
Dale Johannesen0488fb62010-09-30 23:57:10 +00005651 if (X86::isMOVLPMask(SVOp))
5652 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005653
Nate Begeman9008ca62009-04-27 18:41:29 +00005654 if (ShouldXformToMOVHLPS(SVOp) ||
5655 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5656 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005657
Evan Chengf26ffe92008-05-29 08:22:04 +00005658 if (isShift) {
5659 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005660 EVT EltVT = VT.getVectorElementType();
5661 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005662 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005663 }
Eric Christopherfd179292009-08-27 18:07:15 +00005664
Evan Cheng9eca5e82006-10-25 21:49:50 +00005665 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005666 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5667 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005668 V1IsSplat = isSplatVector(V1.getNode());
5669 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005670
Chris Lattner8a594482007-11-25 00:24:49 +00005671 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005672 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005673 Op = CommuteVectorShuffle(SVOp, DAG);
5674 SVOp = cast<ShuffleVectorSDNode>(Op);
5675 V1 = SVOp->getOperand(0);
5676 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005677 std::swap(V1IsSplat, V2IsSplat);
5678 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005679 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005680 }
5681
Nate Begeman9008ca62009-04-27 18:41:29 +00005682 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5683 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005684 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005685 return V1;
5686 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5687 // the instruction selector will not match, so get a canonical MOVL with
5688 // swapped operands to undo the commute.
5689 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005690 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005691
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005692 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005693 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005694
5695 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005696 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005697
Evan Cheng9bbbb982006-10-25 20:48:19 +00005698 if (V2IsSplat) {
5699 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005700 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005701 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005702 SDValue NewMask = NormalizeMask(SVOp, DAG);
5703 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5704 if (NSVOp != SVOp) {
5705 if (X86::isUNPCKLMask(NSVOp, true)) {
5706 return NewMask;
5707 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5708 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005709 }
5710 }
5711 }
5712
Evan Cheng9eca5e82006-10-25 21:49:50 +00005713 if (Commuted) {
5714 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005715 // FIXME: this seems wrong.
5716 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5717 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005718
5719 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005720 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005721
5722 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005723 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005724 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005725
Nate Begeman9008ca62009-04-27 18:41:29 +00005726 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005727 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005728 return CommuteVectorShuffle(SVOp, DAG);
5729
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005730 // The checks below are all present in isShuffleMaskLegal, but they are
5731 // inlined here right now to enable us to directly emit target specific
5732 // nodes, and remove one by one until they don't return Op anymore.
5733 SmallVector<int, 16> M;
5734 SVOp->getMask(M);
5735
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005736 if (isPALIGNRMask(M, VT, HasSSSE3))
5737 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5738 X86::getShufflePALIGNRImmediate(SVOp),
5739 DAG);
5740
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005741 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5742 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5743 if (VT == MVT::v2f64)
5744 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5745 if (VT == MVT::v2i64)
5746 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5747 }
5748
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005749 if (isPSHUFHWMask(M, VT))
5750 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5751 X86::getShufflePSHUFHWImmediate(SVOp),
5752 DAG);
5753
5754 if (isPSHUFLWMask(M, VT))
5755 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5756 X86::getShufflePSHUFLWImmediate(SVOp),
5757 DAG);
5758
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005759 if (isSHUFPMask(M, VT)) {
5760 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5761 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5762 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5763 TargetMask, DAG);
5764 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5765 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5766 TargetMask, DAG);
5767 }
5768
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005769 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5770 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5771 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5772 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5773 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5774 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5775
Evan Cheng14b32e12007-12-11 01:46:18 +00005776 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005777 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005778 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005779 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005780 return NewOp;
5781 }
5782
Owen Anderson825b72b2009-08-11 20:47:22 +00005783 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005784 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 if (NewOp.getNode())
5786 return NewOp;
5787 }
Eric Christopherfd179292009-08-27 18:07:15 +00005788
Dale Johannesen0488fb62010-09-30 23:57:10 +00005789 // Handle all 4 wide cases with a number of shuffles.
5790 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005791 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005792
Dan Gohman475871a2008-07-27 21:46:04 +00005793 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005794}
5795
Dan Gohman475871a2008-07-27 21:46:04 +00005796SDValue
5797X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005798 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005799 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005800 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005801 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005802 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005803 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005805 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005806 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005807 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005808 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5809 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5810 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005811 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5812 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005813 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005814 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005815 Op.getOperand(0)),
5816 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005817 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005818 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005819 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005820 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005821 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005822 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005823 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5824 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005825 // result has a single use which is a store or a bitcast to i32. And in
5826 // the case of a store, it's not worth it if the index is a constant 0,
5827 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005828 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005829 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005830 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005831 if ((User->getOpcode() != ISD::STORE ||
5832 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5833 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005834 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005835 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005836 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005837 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005838 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005839 Op.getOperand(0)),
5840 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005841 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005842 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005843 // ExtractPS works with constant index.
5844 if (isa<ConstantSDNode>(Op.getOperand(1)))
5845 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005846 }
Dan Gohman475871a2008-07-27 21:46:04 +00005847 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005848}
5849
5850
Dan Gohman475871a2008-07-27 21:46:04 +00005851SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005852X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5853 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005854 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005855 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005856
Evan Cheng62a3f152008-03-24 21:52:23 +00005857 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005858 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005859 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005860 return Res;
5861 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005862
Owen Andersone50ed302009-08-10 22:56:29 +00005863 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005864 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005865 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005866 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005867 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005868 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005869 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005870 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5871 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005872 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005873 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005874 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005875 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005876 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005877 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005878 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005879 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005880 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005881 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005882 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005883 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005884 if (Idx == 0)
5885 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005886
Evan Cheng0db9fe62006-04-25 20:13:52 +00005887 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005888 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005889 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005890 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005891 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005892 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005893 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005894 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005895 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5896 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5897 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005898 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005899 if (Idx == 0)
5900 return Op;
5901
5902 // UNPCKHPD the element to the lowest double word, then movsd.
5903 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5904 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005905 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005906 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005907 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005908 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005909 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005910 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005911 }
5912
Dan Gohman475871a2008-07-27 21:46:04 +00005913 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005914}
5915
Dan Gohman475871a2008-07-27 21:46:04 +00005916SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005917X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5918 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005919 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005920 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005921 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005922
Dan Gohman475871a2008-07-27 21:46:04 +00005923 SDValue N0 = Op.getOperand(0);
5924 SDValue N1 = Op.getOperand(1);
5925 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005926
Dan Gohman8a55ce42009-09-23 21:02:20 +00005927 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005928 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005929 unsigned Opc;
5930 if (VT == MVT::v8i16)
5931 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005932 else if (VT == MVT::v16i8)
5933 Opc = X86ISD::PINSRB;
5934 else
5935 Opc = X86ISD::PINSRB;
5936
Nate Begeman14d12ca2008-02-11 04:19:36 +00005937 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5938 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005939 if (N1.getValueType() != MVT::i32)
5940 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5941 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005942 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005943 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005944 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005945 // Bits [7:6] of the constant are the source select. This will always be
5946 // zero here. The DAG Combiner may combine an extract_elt index into these
5947 // bits. For example (insert (extract, 3), 2) could be matched by putting
5948 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005949 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005950 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005951 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005952 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005953 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005954 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005955 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005956 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005957 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005958 // PINSR* works with constant index.
5959 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005960 }
Dan Gohman475871a2008-07-27 21:46:04 +00005961 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005962}
5963
Dan Gohman475871a2008-07-27 21:46:04 +00005964SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005965X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005966 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005967 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005968
5969 if (Subtarget->hasSSE41())
5970 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5971
Dan Gohman8a55ce42009-09-23 21:02:20 +00005972 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005973 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005974
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005975 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005976 SDValue N0 = Op.getOperand(0);
5977 SDValue N1 = Op.getOperand(1);
5978 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005979
Dan Gohman8a55ce42009-09-23 21:02:20 +00005980 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005981 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5982 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005983 if (N1.getValueType() != MVT::i32)
5984 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5985 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005986 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005987 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005988 }
Dan Gohman475871a2008-07-27 21:46:04 +00005989 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005990}
5991
Dan Gohman475871a2008-07-27 21:46:04 +00005992SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005993X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005994 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005995
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005996 if (Op.getValueType() == MVT::v1i64 &&
5997 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005998 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005999
Owen Anderson825b72b2009-08-11 20:47:22 +00006000 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006001 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6002 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006003 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006004 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006005}
6006
David Greene91585092011-01-26 15:38:49 +00006007// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6008// a simple subregister reference or explicit instructions to grab
6009// upper bits of a vector.
6010SDValue
6011X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6012 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006013 DebugLoc dl = Op.getNode()->getDebugLoc();
6014 SDValue Vec = Op.getNode()->getOperand(0);
6015 SDValue Idx = Op.getNode()->getOperand(1);
6016
6017 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6018 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6019 return Extract128BitVector(Vec, Idx, DAG, dl);
6020 }
David Greene91585092011-01-26 15:38:49 +00006021 }
6022 return SDValue();
6023}
6024
David Greenecfe33c42011-01-26 19:13:22 +00006025// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6026// simple superregister reference or explicit instructions to insert
6027// the upper bits of a vector.
6028SDValue
6029X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6030 if (Subtarget->hasAVX()) {
6031 DebugLoc dl = Op.getNode()->getDebugLoc();
6032 SDValue Vec = Op.getNode()->getOperand(0);
6033 SDValue SubVec = Op.getNode()->getOperand(1);
6034 SDValue Idx = Op.getNode()->getOperand(2);
6035
6036 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6037 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006038 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006039 }
6040 }
6041 return SDValue();
6042}
6043
Bill Wendling056292f2008-09-16 21:48:12 +00006044// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6045// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6046// one of the above mentioned nodes. It has to be wrapped because otherwise
6047// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6048// be used to form addressing mode. These wrapped nodes will be selected
6049// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006050SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006051X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006052 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006053
Chris Lattner41621a22009-06-26 19:22:52 +00006054 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6055 // global base reg.
6056 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006057 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006058 CodeModel::Model M = getTargetMachine().getCodeModel();
6059
Chris Lattner4f066492009-07-11 20:29:19 +00006060 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006061 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006062 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006063 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006064 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006065 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006066 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006067
Evan Cheng1606e8e2009-03-13 07:51:59 +00006068 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006069 CP->getAlignment(),
6070 CP->getOffset(), OpFlag);
6071 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006072 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006073 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006074 if (OpFlag) {
6075 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006076 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006077 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006078 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006079 }
6080
6081 return Result;
6082}
6083
Dan Gohmand858e902010-04-17 15:26:15 +00006084SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006085 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006086
Chris Lattner18c59872009-06-27 04:16:01 +00006087 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6088 // global base reg.
6089 unsigned char OpFlag = 0;
6090 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006091 CodeModel::Model M = getTargetMachine().getCodeModel();
6092
Chris Lattner4f066492009-07-11 20:29:19 +00006093 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006094 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006095 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006096 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006097 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006098 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006099 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006100
Chris Lattner18c59872009-06-27 04:16:01 +00006101 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6102 OpFlag);
6103 DebugLoc DL = JT->getDebugLoc();
6104 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006105
Chris Lattner18c59872009-06-27 04:16:01 +00006106 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006107 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006108 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6109 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006110 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006111 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006112
Chris Lattner18c59872009-06-27 04:16:01 +00006113 return Result;
6114}
6115
6116SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006117X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006118 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006119
Chris Lattner18c59872009-06-27 04:16:01 +00006120 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6121 // global base reg.
6122 unsigned char OpFlag = 0;
6123 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006124 CodeModel::Model M = getTargetMachine().getCodeModel();
6125
Chris Lattner4f066492009-07-11 20:29:19 +00006126 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006127 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006128 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006129 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006130 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006131 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006132 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006133
Chris Lattner18c59872009-06-27 04:16:01 +00006134 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006135
Chris Lattner18c59872009-06-27 04:16:01 +00006136 DebugLoc DL = Op.getDebugLoc();
6137 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006138
6139
Chris Lattner18c59872009-06-27 04:16:01 +00006140 // With PIC, the address is actually $g + Offset.
6141 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006142 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006143 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6144 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006145 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006146 Result);
6147 }
Eric Christopherfd179292009-08-27 18:07:15 +00006148
Chris Lattner18c59872009-06-27 04:16:01 +00006149 return Result;
6150}
6151
Dan Gohman475871a2008-07-27 21:46:04 +00006152SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006153X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006154 // Create the TargetBlockAddressAddress node.
6155 unsigned char OpFlags =
6156 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006157 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006158 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006159 DebugLoc dl = Op.getDebugLoc();
6160 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6161 /*isTarget=*/true, OpFlags);
6162
Dan Gohmanf705adb2009-10-30 01:28:02 +00006163 if (Subtarget->isPICStyleRIPRel() &&
6164 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006165 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6166 else
6167 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006168
Dan Gohman29cbade2009-11-20 23:18:13 +00006169 // With PIC, the address is actually $g + Offset.
6170 if (isGlobalRelativeToPICBase(OpFlags)) {
6171 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6172 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6173 Result);
6174 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006175
6176 return Result;
6177}
6178
6179SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006180X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006181 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006182 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006183 // Create the TargetGlobalAddress node, folding in the constant
6184 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006185 unsigned char OpFlags =
6186 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006187 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006188 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006189 if (OpFlags == X86II::MO_NO_FLAG &&
6190 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006191 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006192 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006193 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006194 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006195 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006196 }
Eric Christopherfd179292009-08-27 18:07:15 +00006197
Chris Lattner4f066492009-07-11 20:29:19 +00006198 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006199 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006200 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6201 else
6202 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006203
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006204 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006205 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006206 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6207 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006208 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006209 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006210
Chris Lattner36c25012009-07-10 07:34:39 +00006211 // For globals that require a load from a stub to get the address, emit the
6212 // load.
6213 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006214 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006215 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006216
Dan Gohman6520e202008-10-18 02:06:02 +00006217 // If there was a non-zero offset that we didn't fold, create an explicit
6218 // addition for it.
6219 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006220 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006221 DAG.getConstant(Offset, getPointerTy()));
6222
Evan Cheng0db9fe62006-04-25 20:13:52 +00006223 return Result;
6224}
6225
Evan Chengda43bcf2008-09-24 00:05:32 +00006226SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006227X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006228 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006229 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006230 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006231}
6232
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006233static SDValue
6234GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006235 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006236 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006237 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006238 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006239 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006240 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006241 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006242 GA->getOffset(),
6243 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006244 if (InFlag) {
6245 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006246 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006247 } else {
6248 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006249 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006250 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006251
6252 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006253 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006254
Rafael Espindola15f1b662009-04-24 12:59:40 +00006255 SDValue Flag = Chain.getValue(1);
6256 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006257}
6258
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006259// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006260static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006261LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006262 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006263 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006264 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6265 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006266 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006267 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006268 InFlag = Chain.getValue(1);
6269
Chris Lattnerb903bed2009-06-26 21:20:29 +00006270 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006271}
6272
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006273// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006274static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006275LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006276 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006277 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6278 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006279}
6280
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006281// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6282// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006283static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006284 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006285 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006286 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006287
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006288 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6289 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6290 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006291
Michael J. Spencerec38de22010-10-10 22:04:20 +00006292 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006293 DAG.getIntPtrConstant(0),
6294 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006295
Chris Lattnerb903bed2009-06-26 21:20:29 +00006296 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006297 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6298 // initialexec.
6299 unsigned WrapperKind = X86ISD::Wrapper;
6300 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006301 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006302 } else if (is64Bit) {
6303 assert(model == TLSModel::InitialExec);
6304 OperandFlags = X86II::MO_GOTTPOFF;
6305 WrapperKind = X86ISD::WrapperRIP;
6306 } else {
6307 assert(model == TLSModel::InitialExec);
6308 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006309 }
Eric Christopherfd179292009-08-27 18:07:15 +00006310
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006311 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6312 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006313 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006314 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006315 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006316 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006317
Rafael Espindola9a580232009-02-27 13:37:18 +00006318 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006319 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006320 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006321
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006322 // The address of the thread local variable is the add of the thread
6323 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006324 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006325}
6326
Dan Gohman475871a2008-07-27 21:46:04 +00006327SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006328X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006329
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006330 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006331 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006332
Eric Christopher30ef0e52010-06-03 04:07:48 +00006333 if (Subtarget->isTargetELF()) {
6334 // TODO: implement the "local dynamic" model
6335 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006336
Eric Christopher30ef0e52010-06-03 04:07:48 +00006337 // If GV is an alias then use the aliasee for determining
6338 // thread-localness.
6339 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6340 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006341
6342 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006343 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006344
Eric Christopher30ef0e52010-06-03 04:07:48 +00006345 switch (model) {
6346 case TLSModel::GeneralDynamic:
6347 case TLSModel::LocalDynamic: // not implemented
6348 if (Subtarget->is64Bit())
6349 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6350 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006351
Eric Christopher30ef0e52010-06-03 04:07:48 +00006352 case TLSModel::InitialExec:
6353 case TLSModel::LocalExec:
6354 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6355 Subtarget->is64Bit());
6356 }
6357 } else if (Subtarget->isTargetDarwin()) {
6358 // Darwin only has one model of TLS. Lower to that.
6359 unsigned char OpFlag = 0;
6360 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6361 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006362
Eric Christopher30ef0e52010-06-03 04:07:48 +00006363 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6364 // global base reg.
6365 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6366 !Subtarget->is64Bit();
6367 if (PIC32)
6368 OpFlag = X86II::MO_TLVP_PIC_BASE;
6369 else
6370 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006371 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006372 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006373 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006374 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006375 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006376
Eric Christopher30ef0e52010-06-03 04:07:48 +00006377 // With PIC32, the address is actually $g + Offset.
6378 if (PIC32)
6379 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6380 DAG.getNode(X86ISD::GlobalBaseReg,
6381 DebugLoc(), getPointerTy()),
6382 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006383
Eric Christopher30ef0e52010-06-03 04:07:48 +00006384 // Lowering the machine isd will make sure everything is in the right
6385 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006386 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006387 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006388 SDValue Args[] = { Chain, Offset };
6389 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006390
Eric Christopher30ef0e52010-06-03 04:07:48 +00006391 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6392 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6393 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006394
Eric Christopher30ef0e52010-06-03 04:07:48 +00006395 // And our return value (tls address) is in the standard call return value
6396 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006397 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6398 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006399 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006400
Eric Christopher30ef0e52010-06-03 04:07:48 +00006401 assert(false &&
6402 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006403
Torok Edwinc23197a2009-07-14 16:55:14 +00006404 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006405 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006406}
6407
Evan Cheng0db9fe62006-04-25 20:13:52 +00006408
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006409/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006410/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006411SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006412 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006413 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006414 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006415 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006416 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006417 SDValue ShOpLo = Op.getOperand(0);
6418 SDValue ShOpHi = Op.getOperand(1);
6419 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006420 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006421 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006422 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006423
Dan Gohman475871a2008-07-27 21:46:04 +00006424 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006425 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006426 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6427 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006428 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006429 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6430 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006431 }
Evan Chenge3413162006-01-09 18:33:28 +00006432
Owen Anderson825b72b2009-08-11 20:47:22 +00006433 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6434 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006435 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006436 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006437
Dan Gohman475871a2008-07-27 21:46:04 +00006438 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006439 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006440 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6441 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006442
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006443 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006444 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6445 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006446 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006447 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6448 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006449 }
6450
Dan Gohman475871a2008-07-27 21:46:04 +00006451 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006452 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006453}
Evan Chenga3195e82006-01-12 22:54:21 +00006454
Dan Gohmand858e902010-04-17 15:26:15 +00006455SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6456 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006457 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006458
Dale Johannesen0488fb62010-09-30 23:57:10 +00006459 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006460 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006461
Owen Anderson825b72b2009-08-11 20:47:22 +00006462 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006463 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006464
Eli Friedman36df4992009-05-27 00:47:34 +00006465 // These are really Legal; return the operand so the caller accepts it as
6466 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006467 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006468 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006469 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006470 Subtarget->is64Bit()) {
6471 return Op;
6472 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006473
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006474 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006475 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006476 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006477 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006478 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006479 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006480 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006481 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006482 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006483 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6484}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006485
Owen Andersone50ed302009-08-10 22:56:29 +00006486SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006487 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006488 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006489 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006490 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006491 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006492 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006493 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006494 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006495 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006496 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006497
Chris Lattner492a43e2010-09-22 01:28:21 +00006498 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006499
Chris Lattner492a43e2010-09-22 01:28:21 +00006500 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6501 MachineMemOperand *MMO =
6502 DAG.getMachineFunction()
6503 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6504 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006505
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006506 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006507 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6508 X86ISD::FILD, DL,
6509 Tys, Ops, array_lengthof(Ops),
6510 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006511
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006512 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006513 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006514 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006515
6516 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6517 // shouldn't be necessary except that RFP cannot be live across
6518 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006519 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006520 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6521 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006522 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006523 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006524 SDValue Ops[] = {
6525 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6526 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006527 MachineMemOperand *MMO =
6528 DAG.getMachineFunction()
6529 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006530 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006531
Chris Lattner492a43e2010-09-22 01:28:21 +00006532 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6533 Ops, array_lengthof(Ops),
6534 Op.getValueType(), MMO);
6535 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006536 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006537 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006538 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006539
Evan Cheng0db9fe62006-04-25 20:13:52 +00006540 return Result;
6541}
6542
Bill Wendling8b8a6362009-01-17 03:56:04 +00006543// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006544SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6545 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006546 // This algorithm is not obvious. Here it is in C code, more or less:
6547 /*
6548 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6549 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6550 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006551
Bill Wendling8b8a6362009-01-17 03:56:04 +00006552 // Copy ints to xmm registers.
6553 __m128i xh = _mm_cvtsi32_si128( hi );
6554 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006555
Bill Wendling8b8a6362009-01-17 03:56:04 +00006556 // Combine into low half of a single xmm register.
6557 __m128i x = _mm_unpacklo_epi32( xh, xl );
6558 __m128d d;
6559 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006560
Bill Wendling8b8a6362009-01-17 03:56:04 +00006561 // Merge in appropriate exponents to give the integer bits the right
6562 // magnitude.
6563 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006564
Bill Wendling8b8a6362009-01-17 03:56:04 +00006565 // Subtract away the biases to deal with the IEEE-754 double precision
6566 // implicit 1.
6567 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006568
Bill Wendling8b8a6362009-01-17 03:56:04 +00006569 // All conversions up to here are exact. The correctly rounded result is
6570 // calculated using the current rounding mode using the following
6571 // horizontal add.
6572 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6573 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6574 // store doesn't really need to be here (except
6575 // maybe to zero the other double)
6576 return sd;
6577 }
6578 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006579
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006580 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006581 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006582
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006583 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006584 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006585 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6586 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6587 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6588 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006589 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006590 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006591
Bill Wendling8b8a6362009-01-17 03:56:04 +00006592 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006593 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006594 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006595 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006596 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006597 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006598 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006599
Owen Anderson825b72b2009-08-11 20:47:22 +00006600 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6601 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006602 Op.getOperand(0),
6603 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006604 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6605 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006606 Op.getOperand(0),
6607 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006608 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6609 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006610 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006611 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006612 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006613 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006614 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006615 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006616 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006617 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006618
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006619 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006620 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006621 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6622 DAG.getUNDEF(MVT::v2f64), ShufMask);
6623 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6624 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006625 DAG.getIntPtrConstant(0));
6626}
6627
Bill Wendling8b8a6362009-01-17 03:56:04 +00006628// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006629SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6630 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006631 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006632 // FP constant to bias correct the final result.
6633 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006634 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006635
6636 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006637 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6638 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006639 Op.getOperand(0),
6640 DAG.getIntPtrConstant(0)));
6641
Owen Anderson825b72b2009-08-11 20:47:22 +00006642 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006643 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006644 DAG.getIntPtrConstant(0));
6645
6646 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006647 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006648 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006649 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006650 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006651 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006652 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006653 MVT::v2f64, Bias)));
6654 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006655 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006656 DAG.getIntPtrConstant(0));
6657
6658 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006659 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006660
6661 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006662 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006663
Owen Anderson825b72b2009-08-11 20:47:22 +00006664 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006665 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006666 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006667 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006668 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006669 }
6670
6671 // Handle final rounding.
6672 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006673}
6674
Dan Gohmand858e902010-04-17 15:26:15 +00006675SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6676 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006677 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006678 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006679
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006680 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006681 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6682 // the optimization here.
6683 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006684 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006685
Owen Andersone50ed302009-08-10 22:56:29 +00006686 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006687 EVT DstVT = Op.getValueType();
6688 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006689 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006690 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006691 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006692
6693 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006694 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006695 if (SrcVT == MVT::i32) {
6696 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6697 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6698 getPointerTy(), StackSlot, WordOff);
6699 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006700 StackSlot, MachinePointerInfo(),
6701 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006702 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006703 OffsetSlot, MachinePointerInfo(),
6704 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006705 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6706 return Fild;
6707 }
6708
6709 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6710 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006711 StackSlot, MachinePointerInfo(),
6712 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006713 // For i64 source, we need to add the appropriate power of 2 if the input
6714 // was negative. This is the same as the optimization in
6715 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6716 // we must be careful to do the computation in x87 extended precision, not
6717 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006718 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6719 MachineMemOperand *MMO =
6720 DAG.getMachineFunction()
6721 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6722 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006723
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006724 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6725 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006726 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6727 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006728
6729 APInt FF(32, 0x5F800000ULL);
6730
6731 // Check whether the sign bit is set.
6732 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6733 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6734 ISD::SETLT);
6735
6736 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6737 SDValue FudgePtr = DAG.getConstantPool(
6738 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6739 getPointerTy());
6740
6741 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6742 SDValue Zero = DAG.getIntPtrConstant(0);
6743 SDValue Four = DAG.getIntPtrConstant(4);
6744 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6745 Zero, Four);
6746 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6747
6748 // Load the value out, extending it from f32 to f80.
6749 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006750 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006751 FudgePtr, MachinePointerInfo::getConstantPool(),
6752 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006753 // Extend everything to 80 bits to force it to be done on x87.
6754 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6755 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006756}
6757
Dan Gohman475871a2008-07-27 21:46:04 +00006758std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006759FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006760 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006761
Owen Andersone50ed302009-08-10 22:56:29 +00006762 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006763
6764 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006765 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6766 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006767 }
6768
Owen Anderson825b72b2009-08-11 20:47:22 +00006769 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6770 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006771 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006772
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006773 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006774 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006775 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006776 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006777 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006778 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006779 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006780 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006781
Evan Cheng87c89352007-10-15 20:11:21 +00006782 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6783 // stack slot.
6784 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006785 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006786 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006787 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006788
Michael J. Spencerec38de22010-10-10 22:04:20 +00006789
6790
Evan Cheng0db9fe62006-04-25 20:13:52 +00006791 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006792 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006793 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006794 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6795 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6796 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006797 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006798
Dan Gohman475871a2008-07-27 21:46:04 +00006799 SDValue Chain = DAG.getEntryNode();
6800 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006801 EVT TheVT = Op.getOperand(0).getValueType();
6802 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006803 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006804 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006805 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006806 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006807 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006808 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006809 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006810 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006811
Chris Lattner492a43e2010-09-22 01:28:21 +00006812 MachineMemOperand *MMO =
6813 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6814 MachineMemOperand::MOLoad, MemSize, MemSize);
6815 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6816 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006817 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006818 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006819 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6820 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006821
Chris Lattner07290932010-09-22 01:05:16 +00006822 MachineMemOperand *MMO =
6823 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6824 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006825
Evan Cheng0db9fe62006-04-25 20:13:52 +00006826 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006827 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006828 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6829 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006830
Chris Lattner27a6c732007-11-24 07:07:01 +00006831 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006832}
6833
Dan Gohmand858e902010-04-17 15:26:15 +00006834SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6835 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006836 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006837 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006838
Eli Friedman948e95a2009-05-23 09:59:16 +00006839 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006840 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006841 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6842 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006843
Chris Lattner27a6c732007-11-24 07:07:01 +00006844 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006845 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006846 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006847}
6848
Dan Gohmand858e902010-04-17 15:26:15 +00006849SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6850 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006851 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6852 SDValue FIST = Vals.first, StackSlot = Vals.second;
6853 assert(FIST.getNode() && "Unexpected failure");
6854
6855 // Load the result.
6856 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006857 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006858}
6859
Dan Gohmand858e902010-04-17 15:26:15 +00006860SDValue X86TargetLowering::LowerFABS(SDValue Op,
6861 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006862 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006863 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006864 EVT VT = Op.getValueType();
6865 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006866 if (VT.isVector())
6867 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006868 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006869 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006870 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006871 CV.push_back(C);
6872 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006873 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006874 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006875 CV.push_back(C);
6876 CV.push_back(C);
6877 CV.push_back(C);
6878 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006879 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006880 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006881 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006882 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006883 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006884 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006885 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006886}
6887
Dan Gohmand858e902010-04-17 15:26:15 +00006888SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006889 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006890 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006891 EVT VT = Op.getValueType();
6892 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006893 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006894 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006895 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006896 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006897 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006898 CV.push_back(C);
6899 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006900 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006901 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006902 CV.push_back(C);
6903 CV.push_back(C);
6904 CV.push_back(C);
6905 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006906 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006907 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006908 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006909 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006910 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006911 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006912 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006913 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006914 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006915 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006916 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006917 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006918 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006919 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006920 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006921}
6922
Dan Gohmand858e902010-04-17 15:26:15 +00006923SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006924 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006925 SDValue Op0 = Op.getOperand(0);
6926 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006927 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006928 EVT VT = Op.getValueType();
6929 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006930
6931 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006932 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006933 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006934 SrcVT = VT;
6935 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006936 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006937 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006938 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006939 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006940 }
6941
6942 // At this point the operands and the result should have the same
6943 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006944
Evan Cheng68c47cb2007-01-05 07:55:56 +00006945 // First get the sign bit of second operand.
6946 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006947 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006948 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6949 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006950 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006951 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6952 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6953 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6954 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006955 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006956 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006957 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006958 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006959 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006960 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006961 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006962
6963 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006964 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006965 // Op0 is MVT::f32, Op1 is MVT::f64.
6966 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6967 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6968 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006969 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006970 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006971 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006972 }
6973
Evan Cheng73d6cf12007-01-05 21:37:56 +00006974 // Clear first operand sign bit.
6975 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006976 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006977 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6978 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006979 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006980 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6981 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6982 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6983 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006984 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006985 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006986 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006987 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006988 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006989 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006990 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006991
6992 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006993 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006994}
6995
Dan Gohman076aee32009-03-04 19:44:21 +00006996/// Emit nodes that will be selected as "test Op0,Op0", or something
6997/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006998SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006999 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007000 DebugLoc dl = Op.getDebugLoc();
7001
Dan Gohman31125812009-03-07 01:58:32 +00007002 // CF and OF aren't always set the way we want. Determine which
7003 // of these we need.
7004 bool NeedCF = false;
7005 bool NeedOF = false;
7006 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007007 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007008 case X86::COND_A: case X86::COND_AE:
7009 case X86::COND_B: case X86::COND_BE:
7010 NeedCF = true;
7011 break;
7012 case X86::COND_G: case X86::COND_GE:
7013 case X86::COND_L: case X86::COND_LE:
7014 case X86::COND_O: case X86::COND_NO:
7015 NeedOF = true;
7016 break;
Dan Gohman31125812009-03-07 01:58:32 +00007017 }
7018
Dan Gohman076aee32009-03-04 19:44:21 +00007019 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007020 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7021 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007022 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7023 // Emit a CMP with 0, which is the TEST pattern.
7024 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7025 DAG.getConstant(0, Op.getValueType()));
7026
7027 unsigned Opcode = 0;
7028 unsigned NumOperands = 0;
7029 switch (Op.getNode()->getOpcode()) {
7030 case ISD::ADD:
7031 // Due to an isel shortcoming, be conservative if this add is likely to be
7032 // selected as part of a load-modify-store instruction. When the root node
7033 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7034 // uses of other nodes in the match, such as the ADD in this case. This
7035 // leads to the ADD being left around and reselected, with the result being
7036 // two adds in the output. Alas, even if none our users are stores, that
7037 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7038 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7039 // climbing the DAG back to the root, and it doesn't seem to be worth the
7040 // effort.
7041 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007042 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007043 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7044 goto default_case;
7045
7046 if (ConstantSDNode *C =
7047 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7048 // An add of one will be selected as an INC.
7049 if (C->getAPIntValue() == 1) {
7050 Opcode = X86ISD::INC;
7051 NumOperands = 1;
7052 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007053 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007054
7055 // An add of negative one (subtract of one) will be selected as a DEC.
7056 if (C->getAPIntValue().isAllOnesValue()) {
7057 Opcode = X86ISD::DEC;
7058 NumOperands = 1;
7059 break;
7060 }
Dan Gohman076aee32009-03-04 19:44:21 +00007061 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007062
7063 // Otherwise use a regular EFLAGS-setting add.
7064 Opcode = X86ISD::ADD;
7065 NumOperands = 2;
7066 break;
7067 case ISD::AND: {
7068 // If the primary and result isn't used, don't bother using X86ISD::AND,
7069 // because a TEST instruction will be better.
7070 bool NonFlagUse = false;
7071 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7072 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7073 SDNode *User = *UI;
7074 unsigned UOpNo = UI.getOperandNo();
7075 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7076 // Look pass truncate.
7077 UOpNo = User->use_begin().getOperandNo();
7078 User = *User->use_begin();
7079 }
7080
7081 if (User->getOpcode() != ISD::BRCOND &&
7082 User->getOpcode() != ISD::SETCC &&
7083 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7084 NonFlagUse = true;
7085 break;
7086 }
Dan Gohman076aee32009-03-04 19:44:21 +00007087 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007088
7089 if (!NonFlagUse)
7090 break;
7091 }
7092 // FALL THROUGH
7093 case ISD::SUB:
7094 case ISD::OR:
7095 case ISD::XOR:
7096 // Due to the ISEL shortcoming noted above, be conservative if this op is
7097 // likely to be selected as part of a load-modify-store instruction.
7098 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7099 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7100 if (UI->getOpcode() == ISD::STORE)
7101 goto default_case;
7102
7103 // Otherwise use a regular EFLAGS-setting instruction.
7104 switch (Op.getNode()->getOpcode()) {
7105 default: llvm_unreachable("unexpected operator!");
7106 case ISD::SUB: Opcode = X86ISD::SUB; break;
7107 case ISD::OR: Opcode = X86ISD::OR; break;
7108 case ISD::XOR: Opcode = X86ISD::XOR; break;
7109 case ISD::AND: Opcode = X86ISD::AND; break;
7110 }
7111
7112 NumOperands = 2;
7113 break;
7114 case X86ISD::ADD:
7115 case X86ISD::SUB:
7116 case X86ISD::INC:
7117 case X86ISD::DEC:
7118 case X86ISD::OR:
7119 case X86ISD::XOR:
7120 case X86ISD::AND:
7121 return SDValue(Op.getNode(), 1);
7122 default:
7123 default_case:
7124 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007125 }
7126
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007127 if (Opcode == 0)
7128 // Emit a CMP with 0, which is the TEST pattern.
7129 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7130 DAG.getConstant(0, Op.getValueType()));
7131
7132 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7133 SmallVector<SDValue, 4> Ops;
7134 for (unsigned i = 0; i != NumOperands; ++i)
7135 Ops.push_back(Op.getOperand(i));
7136
7137 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7138 DAG.ReplaceAllUsesWith(Op, New);
7139 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007140}
7141
7142/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7143/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007144SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007145 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007146 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7147 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007148 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007149
7150 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007151 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007152}
7153
Evan Chengd40d03e2010-01-06 19:38:29 +00007154/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7155/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007156SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7157 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007158 SDValue Op0 = And.getOperand(0);
7159 SDValue Op1 = And.getOperand(1);
7160 if (Op0.getOpcode() == ISD::TRUNCATE)
7161 Op0 = Op0.getOperand(0);
7162 if (Op1.getOpcode() == ISD::TRUNCATE)
7163 Op1 = Op1.getOperand(0);
7164
Evan Chengd40d03e2010-01-06 19:38:29 +00007165 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007166 if (Op1.getOpcode() == ISD::SHL)
7167 std::swap(Op0, Op1);
7168 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007169 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7170 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007171 // If we looked past a truncate, check that it's only truncating away
7172 // known zeros.
7173 unsigned BitWidth = Op0.getValueSizeInBits();
7174 unsigned AndBitWidth = And.getValueSizeInBits();
7175 if (BitWidth > AndBitWidth) {
7176 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7177 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7178 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7179 return SDValue();
7180 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007181 LHS = Op1;
7182 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007183 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007184 } else if (Op1.getOpcode() == ISD::Constant) {
7185 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7186 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007187 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7188 LHS = AndLHS.getOperand(0);
7189 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007190 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007191 }
Evan Cheng0488db92007-09-25 01:57:46 +00007192
Evan Chengd40d03e2010-01-06 19:38:29 +00007193 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007194 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007195 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007196 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007197 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007198 // Also promote i16 to i32 for performance / code size reason.
7199 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007200 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007201 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007202
Evan Chengd40d03e2010-01-06 19:38:29 +00007203 // If the operand types disagree, extend the shift amount to match. Since
7204 // BT ignores high bits (like shifts) we can use anyextend.
7205 if (LHS.getValueType() != RHS.getValueType())
7206 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007207
Evan Chengd40d03e2010-01-06 19:38:29 +00007208 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7209 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7210 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7211 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007212 }
7213
Evan Cheng54de3ea2010-01-05 06:52:31 +00007214 return SDValue();
7215}
7216
Dan Gohmand858e902010-04-17 15:26:15 +00007217SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007218 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7219 SDValue Op0 = Op.getOperand(0);
7220 SDValue Op1 = Op.getOperand(1);
7221 DebugLoc dl = Op.getDebugLoc();
7222 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7223
7224 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007225 // Lower (X & (1 << N)) == 0 to BT(X, N).
7226 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7227 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007228 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007229 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007230 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007231 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7232 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7233 if (NewSetCC.getNode())
7234 return NewSetCC;
7235 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007236
Chris Lattner481eebc2010-12-19 21:23:48 +00007237 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7238 // these.
7239 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007240 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7241 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7242 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007243
Chris Lattner481eebc2010-12-19 21:23:48 +00007244 // If the input is a setcc, then reuse the input setcc or use a new one with
7245 // the inverted condition.
7246 if (Op0.getOpcode() == X86ISD::SETCC) {
7247 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7248 bool Invert = (CC == ISD::SETNE) ^
7249 cast<ConstantSDNode>(Op1)->isNullValue();
7250 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007251
Evan Cheng2c755ba2010-02-27 07:36:59 +00007252 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007253 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7254 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7255 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007256 }
7257
Evan Chenge5b51ac2010-04-17 06:13:15 +00007258 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007259 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007260 if (X86CC == X86::COND_INVALID)
7261 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007262
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007263 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007264 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007265 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007266}
7267
Dan Gohmand858e902010-04-17 15:26:15 +00007268SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007269 SDValue Cond;
7270 SDValue Op0 = Op.getOperand(0);
7271 SDValue Op1 = Op.getOperand(1);
7272 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007273 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007274 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7275 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007276 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007277
7278 if (isFP) {
7279 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007280 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007281 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7282 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007283 bool Swap = false;
7284
7285 switch (SetCCOpcode) {
7286 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007287 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007288 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007289 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007290 case ISD::SETGT: Swap = true; // Fallthrough
7291 case ISD::SETLT:
7292 case ISD::SETOLT: SSECC = 1; break;
7293 case ISD::SETOGE:
7294 case ISD::SETGE: Swap = true; // Fallthrough
7295 case ISD::SETLE:
7296 case ISD::SETOLE: SSECC = 2; break;
7297 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007298 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007299 case ISD::SETNE: SSECC = 4; break;
7300 case ISD::SETULE: Swap = true;
7301 case ISD::SETUGE: SSECC = 5; break;
7302 case ISD::SETULT: Swap = true;
7303 case ISD::SETUGT: SSECC = 6; break;
7304 case ISD::SETO: SSECC = 7; break;
7305 }
7306 if (Swap)
7307 std::swap(Op0, Op1);
7308
Nate Begemanfb8ead02008-07-25 19:05:58 +00007309 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007310 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007311 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007312 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007313 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7314 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007315 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007316 }
7317 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007318 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007319 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7320 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007321 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007322 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007323 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007324 }
7325 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007326 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007327 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007328
Nate Begeman30a0de92008-07-17 16:51:19 +00007329 // We are handling one of the integer comparisons here. Since SSE only has
7330 // GT and EQ comparisons for integer, swapping operands and multiple
7331 // operations may be required for some comparisons.
7332 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7333 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007334
Owen Anderson825b72b2009-08-11 20:47:22 +00007335 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007336 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007337 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007338 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007339 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7340 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007341 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007342
Nate Begeman30a0de92008-07-17 16:51:19 +00007343 switch (SetCCOpcode) {
7344 default: break;
7345 case ISD::SETNE: Invert = true;
7346 case ISD::SETEQ: Opc = EQOpc; break;
7347 case ISD::SETLT: Swap = true;
7348 case ISD::SETGT: Opc = GTOpc; break;
7349 case ISD::SETGE: Swap = true;
7350 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7351 case ISD::SETULT: Swap = true;
7352 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7353 case ISD::SETUGE: Swap = true;
7354 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7355 }
7356 if (Swap)
7357 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007358
Nate Begeman30a0de92008-07-17 16:51:19 +00007359 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7360 // bits of the inputs before performing those operations.
7361 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007362 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007363 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7364 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007365 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007366 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7367 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007368 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7369 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007370 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007371
Dale Johannesenace16102009-02-03 19:33:06 +00007372 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007373
7374 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007375 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007376 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007377
Nate Begeman30a0de92008-07-17 16:51:19 +00007378 return Result;
7379}
Evan Cheng0488db92007-09-25 01:57:46 +00007380
Evan Cheng370e5342008-12-03 08:38:43 +00007381// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007382static bool isX86LogicalCmp(SDValue Op) {
7383 unsigned Opc = Op.getNode()->getOpcode();
7384 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7385 return true;
7386 if (Op.getResNo() == 1 &&
7387 (Opc == X86ISD::ADD ||
7388 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007389 Opc == X86ISD::ADC ||
7390 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007391 Opc == X86ISD::SMUL ||
7392 Opc == X86ISD::UMUL ||
7393 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007394 Opc == X86ISD::DEC ||
7395 Opc == X86ISD::OR ||
7396 Opc == X86ISD::XOR ||
7397 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007398 return true;
7399
Chris Lattner9637d5b2010-12-05 07:49:54 +00007400 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7401 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007402
Dan Gohman076aee32009-03-04 19:44:21 +00007403 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007404}
7405
Chris Lattnera2b56002010-12-05 01:23:24 +00007406static bool isZero(SDValue V) {
7407 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7408 return C && C->isNullValue();
7409}
7410
Chris Lattner96908b12010-12-05 02:00:51 +00007411static bool isAllOnes(SDValue V) {
7412 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7413 return C && C->isAllOnesValue();
7414}
7415
Dan Gohmand858e902010-04-17 15:26:15 +00007416SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007417 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007418 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007419 SDValue Op1 = Op.getOperand(1);
7420 SDValue Op2 = Op.getOperand(2);
7421 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007422 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007423
Dan Gohman1a492952009-10-20 16:22:37 +00007424 if (Cond.getOpcode() == ISD::SETCC) {
7425 SDValue NewCond = LowerSETCC(Cond, DAG);
7426 if (NewCond.getNode())
7427 Cond = NewCond;
7428 }
Evan Cheng734503b2006-09-11 02:19:56 +00007429
Chris Lattnera2b56002010-12-05 01:23:24 +00007430 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007431 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007432 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007433 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007434 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007435 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7436 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007437 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007438
Chris Lattnera2b56002010-12-05 01:23:24 +00007439 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007440
7441 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007442 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7443 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007444
7445 SDValue CmpOp0 = Cmp.getOperand(0);
7446 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7447 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007448
Chris Lattner96908b12010-12-05 02:00:51 +00007449 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007450 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7451 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007452
Chris Lattner96908b12010-12-05 02:00:51 +00007453 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7454 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007455
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007456 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007457 if (N2C == 0 || !N2C->isNullValue())
7458 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7459 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007460 }
7461 }
7462
Chris Lattnera2b56002010-12-05 01:23:24 +00007463 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007464 if (Cond.getOpcode() == ISD::AND &&
7465 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7466 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007467 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007468 Cond = Cond.getOperand(0);
7469 }
7470
Evan Cheng3f41d662007-10-08 22:16:29 +00007471 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7472 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007473 if (Cond.getOpcode() == X86ISD::SETCC ||
7474 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007475 CC = Cond.getOperand(0);
7476
Dan Gohman475871a2008-07-27 21:46:04 +00007477 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007478 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007479 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007480
Evan Cheng3f41d662007-10-08 22:16:29 +00007481 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007482 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007483 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007484 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007485
Chris Lattnerd1980a52009-03-12 06:52:53 +00007486 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7487 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007488 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007489 addTest = false;
7490 }
7491 }
7492
7493 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007494 // Look pass the truncate.
7495 if (Cond.getOpcode() == ISD::TRUNCATE)
7496 Cond = Cond.getOperand(0);
7497
7498 // We know the result of AND is compared against zero. Try to match
7499 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007500 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007501 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007502 if (NewSetCC.getNode()) {
7503 CC = NewSetCC.getOperand(0);
7504 Cond = NewSetCC.getOperand(1);
7505 addTest = false;
7506 }
7507 }
7508 }
7509
7510 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007511 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007512 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007513 }
7514
Benjamin Kramere915ff32010-12-22 23:09:28 +00007515 // a < b ? -1 : 0 -> RES = ~setcc_carry
7516 // a < b ? 0 : -1 -> RES = setcc_carry
7517 // a >= b ? -1 : 0 -> RES = setcc_carry
7518 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7519 if (Cond.getOpcode() == X86ISD::CMP) {
7520 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7521
7522 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7523 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7524 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7525 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7526 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7527 return DAG.getNOT(DL, Res, Res.getValueType());
7528 return Res;
7529 }
7530 }
7531
Evan Cheng0488db92007-09-25 01:57:46 +00007532 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7533 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007534 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007535 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007536 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007537}
7538
Evan Cheng370e5342008-12-03 08:38:43 +00007539// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7540// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7541// from the AND / OR.
7542static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7543 Opc = Op.getOpcode();
7544 if (Opc != ISD::OR && Opc != ISD::AND)
7545 return false;
7546 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7547 Op.getOperand(0).hasOneUse() &&
7548 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7549 Op.getOperand(1).hasOneUse());
7550}
7551
Evan Cheng961d6d42009-02-02 08:19:07 +00007552// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7553// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007554static bool isXor1OfSetCC(SDValue Op) {
7555 if (Op.getOpcode() != ISD::XOR)
7556 return false;
7557 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7558 if (N1C && N1C->getAPIntValue() == 1) {
7559 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7560 Op.getOperand(0).hasOneUse();
7561 }
7562 return false;
7563}
7564
Dan Gohmand858e902010-04-17 15:26:15 +00007565SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007566 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007567 SDValue Chain = Op.getOperand(0);
7568 SDValue Cond = Op.getOperand(1);
7569 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007570 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007571 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007572
Dan Gohman1a492952009-10-20 16:22:37 +00007573 if (Cond.getOpcode() == ISD::SETCC) {
7574 SDValue NewCond = LowerSETCC(Cond, DAG);
7575 if (NewCond.getNode())
7576 Cond = NewCond;
7577 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007578#if 0
7579 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007580 else if (Cond.getOpcode() == X86ISD::ADD ||
7581 Cond.getOpcode() == X86ISD::SUB ||
7582 Cond.getOpcode() == X86ISD::SMUL ||
7583 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007584 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007585#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007586
Evan Chengad9c0a32009-12-15 00:53:42 +00007587 // Look pass (and (setcc_carry (cmp ...)), 1).
7588 if (Cond.getOpcode() == ISD::AND &&
7589 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7590 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007591 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007592 Cond = Cond.getOperand(0);
7593 }
7594
Evan Cheng3f41d662007-10-08 22:16:29 +00007595 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7596 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007597 if (Cond.getOpcode() == X86ISD::SETCC ||
7598 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007599 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007600
Dan Gohman475871a2008-07-27 21:46:04 +00007601 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007602 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007603 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007604 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007605 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007606 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007607 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007608 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007609 default: break;
7610 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007611 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007612 // These can only come from an arithmetic instruction with overflow,
7613 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007614 Cond = Cond.getNode()->getOperand(1);
7615 addTest = false;
7616 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007617 }
Evan Cheng0488db92007-09-25 01:57:46 +00007618 }
Evan Cheng370e5342008-12-03 08:38:43 +00007619 } else {
7620 unsigned CondOpc;
7621 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7622 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007623 if (CondOpc == ISD::OR) {
7624 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7625 // two branches instead of an explicit OR instruction with a
7626 // separate test.
7627 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007628 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007629 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007630 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007631 Chain, Dest, CC, Cmp);
7632 CC = Cond.getOperand(1).getOperand(0);
7633 Cond = Cmp;
7634 addTest = false;
7635 }
7636 } else { // ISD::AND
7637 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7638 // two branches instead of an explicit AND instruction with a
7639 // separate test. However, we only do this if this block doesn't
7640 // have a fall-through edge, because this requires an explicit
7641 // jmp when the condition is false.
7642 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007643 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007644 Op.getNode()->hasOneUse()) {
7645 X86::CondCode CCode =
7646 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7647 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007648 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007649 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007650 // Look for an unconditional branch following this conditional branch.
7651 // We need this because we need to reverse the successors in order
7652 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007653 if (User->getOpcode() == ISD::BR) {
7654 SDValue FalseBB = User->getOperand(1);
7655 SDNode *NewBR =
7656 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007657 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007658 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007659 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007660
Dale Johannesene4d209d2009-02-03 20:21:25 +00007661 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007662 Chain, Dest, CC, Cmp);
7663 X86::CondCode CCode =
7664 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7665 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007666 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007667 Cond = Cmp;
7668 addTest = false;
7669 }
7670 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007671 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007672 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7673 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7674 // It should be transformed during dag combiner except when the condition
7675 // is set by a arithmetics with overflow node.
7676 X86::CondCode CCode =
7677 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7678 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007679 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007680 Cond = Cond.getOperand(0).getOperand(1);
7681 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007682 }
Evan Cheng0488db92007-09-25 01:57:46 +00007683 }
7684
7685 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007686 // Look pass the truncate.
7687 if (Cond.getOpcode() == ISD::TRUNCATE)
7688 Cond = Cond.getOperand(0);
7689
7690 // We know the result of AND is compared against zero. Try to match
7691 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007692 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007693 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7694 if (NewSetCC.getNode()) {
7695 CC = NewSetCC.getOperand(0);
7696 Cond = NewSetCC.getOperand(1);
7697 addTest = false;
7698 }
7699 }
7700 }
7701
7702 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007703 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007704 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007705 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007706 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007707 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007708}
7709
Anton Korobeynikove060b532007-04-17 19:34:00 +00007710
7711// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7712// Calls to _alloca is needed to probe the stack when allocating more than 4k
7713// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7714// that the guard pages used by the OS virtual memory manager are allocated in
7715// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007716SDValue
7717X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007718 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007719 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007720 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007721 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007722
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007723 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007724 SDValue Chain = Op.getOperand(0);
7725 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007726 // FIXME: Ensure alignment here
7727
Dan Gohman475871a2008-07-27 21:46:04 +00007728 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007729
Owen Anderson825b72b2009-08-11 20:47:22 +00007730 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007731
Dale Johannesendd64c412009-02-04 00:33:20 +00007732 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007733 Flag = Chain.getValue(1);
7734
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007735 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007736
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007737 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007738 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007739
Dale Johannesendd64c412009-02-04 00:33:20 +00007740 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007741
Dan Gohman475871a2008-07-27 21:46:04 +00007742 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007743 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007744}
7745
Dan Gohmand858e902010-04-17 15:26:15 +00007746SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007747 MachineFunction &MF = DAG.getMachineFunction();
7748 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7749
Dan Gohman69de1932008-02-06 22:27:42 +00007750 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007751 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007752
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007753 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007754 // vastart just stores the address of the VarArgsFrameIndex slot into the
7755 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007756 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7757 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007758 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7759 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007760 }
7761
7762 // __va_list_tag:
7763 // gp_offset (0 - 6 * 8)
7764 // fp_offset (48 - 48 + 8 * 16)
7765 // overflow_arg_area (point to parameters coming in memory).
7766 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007767 SmallVector<SDValue, 8> MemOps;
7768 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007769 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007770 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007771 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7772 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007773 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007774 MemOps.push_back(Store);
7775
7776 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007777 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007778 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007779 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007780 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7781 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007782 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007783 MemOps.push_back(Store);
7784
7785 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007786 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007787 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007788 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7789 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007790 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7791 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007792 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007793 MemOps.push_back(Store);
7794
7795 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007796 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007797 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007798 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7799 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007800 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7801 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007802 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007803 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007804 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007805}
7806
Dan Gohmand858e902010-04-17 15:26:15 +00007807SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007808 assert(Subtarget->is64Bit() &&
7809 "LowerVAARG only handles 64-bit va_arg!");
7810 assert((Subtarget->isTargetLinux() ||
7811 Subtarget->isTargetDarwin()) &&
7812 "Unhandled target in LowerVAARG");
7813 assert(Op.getNode()->getNumOperands() == 4);
7814 SDValue Chain = Op.getOperand(0);
7815 SDValue SrcPtr = Op.getOperand(1);
7816 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7817 unsigned Align = Op.getConstantOperandVal(3);
7818 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007819
Dan Gohman320afb82010-10-12 18:00:49 +00007820 EVT ArgVT = Op.getNode()->getValueType(0);
7821 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7822 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7823 uint8_t ArgMode;
7824
7825 // Decide which area this value should be read from.
7826 // TODO: Implement the AMD64 ABI in its entirety. This simple
7827 // selection mechanism works only for the basic types.
7828 if (ArgVT == MVT::f80) {
7829 llvm_unreachable("va_arg for f80 not yet implemented");
7830 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7831 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7832 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7833 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7834 } else {
7835 llvm_unreachable("Unhandled argument type in LowerVAARG");
7836 }
7837
7838 if (ArgMode == 2) {
7839 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007840 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007841 !(DAG.getMachineFunction()
7842 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007843 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007844 }
7845
7846 // Insert VAARG_64 node into the DAG
7847 // VAARG_64 returns two values: Variable Argument Address, Chain
7848 SmallVector<SDValue, 11> InstOps;
7849 InstOps.push_back(Chain);
7850 InstOps.push_back(SrcPtr);
7851 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7852 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7853 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7854 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7855 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7856 VTs, &InstOps[0], InstOps.size(),
7857 MVT::i64,
7858 MachinePointerInfo(SV),
7859 /*Align=*/0,
7860 /*Volatile=*/false,
7861 /*ReadMem=*/true,
7862 /*WriteMem=*/true);
7863 Chain = VAARG.getValue(1);
7864
7865 // Load the next argument and return it
7866 return DAG.getLoad(ArgVT, dl,
7867 Chain,
7868 VAARG,
7869 MachinePointerInfo(),
7870 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007871}
7872
Dan Gohmand858e902010-04-17 15:26:15 +00007873SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007874 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007875 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007876 SDValue Chain = Op.getOperand(0);
7877 SDValue DstPtr = Op.getOperand(1);
7878 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007879 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7880 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007881 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007882
Chris Lattnere72f2022010-09-21 05:40:29 +00007883 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007884 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007885 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007886 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007887}
7888
Dan Gohman475871a2008-07-27 21:46:04 +00007889SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007890X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007891 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007892 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007893 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007894 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007895 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007896 case Intrinsic::x86_sse_comieq_ss:
7897 case Intrinsic::x86_sse_comilt_ss:
7898 case Intrinsic::x86_sse_comile_ss:
7899 case Intrinsic::x86_sse_comigt_ss:
7900 case Intrinsic::x86_sse_comige_ss:
7901 case Intrinsic::x86_sse_comineq_ss:
7902 case Intrinsic::x86_sse_ucomieq_ss:
7903 case Intrinsic::x86_sse_ucomilt_ss:
7904 case Intrinsic::x86_sse_ucomile_ss:
7905 case Intrinsic::x86_sse_ucomigt_ss:
7906 case Intrinsic::x86_sse_ucomige_ss:
7907 case Intrinsic::x86_sse_ucomineq_ss:
7908 case Intrinsic::x86_sse2_comieq_sd:
7909 case Intrinsic::x86_sse2_comilt_sd:
7910 case Intrinsic::x86_sse2_comile_sd:
7911 case Intrinsic::x86_sse2_comigt_sd:
7912 case Intrinsic::x86_sse2_comige_sd:
7913 case Intrinsic::x86_sse2_comineq_sd:
7914 case Intrinsic::x86_sse2_ucomieq_sd:
7915 case Intrinsic::x86_sse2_ucomilt_sd:
7916 case Intrinsic::x86_sse2_ucomile_sd:
7917 case Intrinsic::x86_sse2_ucomigt_sd:
7918 case Intrinsic::x86_sse2_ucomige_sd:
7919 case Intrinsic::x86_sse2_ucomineq_sd: {
7920 unsigned Opc = 0;
7921 ISD::CondCode CC = ISD::SETCC_INVALID;
7922 switch (IntNo) {
7923 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007924 case Intrinsic::x86_sse_comieq_ss:
7925 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007926 Opc = X86ISD::COMI;
7927 CC = ISD::SETEQ;
7928 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007929 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007930 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007931 Opc = X86ISD::COMI;
7932 CC = ISD::SETLT;
7933 break;
7934 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007935 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007936 Opc = X86ISD::COMI;
7937 CC = ISD::SETLE;
7938 break;
7939 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007940 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007941 Opc = X86ISD::COMI;
7942 CC = ISD::SETGT;
7943 break;
7944 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007945 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007946 Opc = X86ISD::COMI;
7947 CC = ISD::SETGE;
7948 break;
7949 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007950 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007951 Opc = X86ISD::COMI;
7952 CC = ISD::SETNE;
7953 break;
7954 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007955 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007956 Opc = X86ISD::UCOMI;
7957 CC = ISD::SETEQ;
7958 break;
7959 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007960 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007961 Opc = X86ISD::UCOMI;
7962 CC = ISD::SETLT;
7963 break;
7964 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007965 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007966 Opc = X86ISD::UCOMI;
7967 CC = ISD::SETLE;
7968 break;
7969 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007970 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007971 Opc = X86ISD::UCOMI;
7972 CC = ISD::SETGT;
7973 break;
7974 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007975 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007976 Opc = X86ISD::UCOMI;
7977 CC = ISD::SETGE;
7978 break;
7979 case Intrinsic::x86_sse_ucomineq_ss:
7980 case Intrinsic::x86_sse2_ucomineq_sd:
7981 Opc = X86ISD::UCOMI;
7982 CC = ISD::SETNE;
7983 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007984 }
Evan Cheng734503b2006-09-11 02:19:56 +00007985
Dan Gohman475871a2008-07-27 21:46:04 +00007986 SDValue LHS = Op.getOperand(1);
7987 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007988 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007989 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007990 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7991 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7992 DAG.getConstant(X86CC, MVT::i8), Cond);
7993 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007994 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007995 // ptest and testp intrinsics. The intrinsic these come from are designed to
7996 // return an integer value, not just an instruction so lower it to the ptest
7997 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007998 case Intrinsic::x86_sse41_ptestz:
7999 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008000 case Intrinsic::x86_sse41_ptestnzc:
8001 case Intrinsic::x86_avx_ptestz_256:
8002 case Intrinsic::x86_avx_ptestc_256:
8003 case Intrinsic::x86_avx_ptestnzc_256:
8004 case Intrinsic::x86_avx_vtestz_ps:
8005 case Intrinsic::x86_avx_vtestc_ps:
8006 case Intrinsic::x86_avx_vtestnzc_ps:
8007 case Intrinsic::x86_avx_vtestz_pd:
8008 case Intrinsic::x86_avx_vtestc_pd:
8009 case Intrinsic::x86_avx_vtestnzc_pd:
8010 case Intrinsic::x86_avx_vtestz_ps_256:
8011 case Intrinsic::x86_avx_vtestc_ps_256:
8012 case Intrinsic::x86_avx_vtestnzc_ps_256:
8013 case Intrinsic::x86_avx_vtestz_pd_256:
8014 case Intrinsic::x86_avx_vtestc_pd_256:
8015 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8016 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008017 unsigned X86CC = 0;
8018 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008019 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008020 case Intrinsic::x86_avx_vtestz_ps:
8021 case Intrinsic::x86_avx_vtestz_pd:
8022 case Intrinsic::x86_avx_vtestz_ps_256:
8023 case Intrinsic::x86_avx_vtestz_pd_256:
8024 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008025 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008026 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008027 // ZF = 1
8028 X86CC = X86::COND_E;
8029 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008030 case Intrinsic::x86_avx_vtestc_ps:
8031 case Intrinsic::x86_avx_vtestc_pd:
8032 case Intrinsic::x86_avx_vtestc_ps_256:
8033 case Intrinsic::x86_avx_vtestc_pd_256:
8034 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008035 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008036 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008037 // CF = 1
8038 X86CC = X86::COND_B;
8039 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008040 case Intrinsic::x86_avx_vtestnzc_ps:
8041 case Intrinsic::x86_avx_vtestnzc_pd:
8042 case Intrinsic::x86_avx_vtestnzc_ps_256:
8043 case Intrinsic::x86_avx_vtestnzc_pd_256:
8044 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008045 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008046 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008047 // ZF and CF = 0
8048 X86CC = X86::COND_A;
8049 break;
8050 }
Eric Christopherfd179292009-08-27 18:07:15 +00008051
Eric Christopher71c67532009-07-29 00:28:05 +00008052 SDValue LHS = Op.getOperand(1);
8053 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008054 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8055 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008056 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8057 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8058 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008059 }
Evan Cheng5759f972008-05-04 09:15:50 +00008060
8061 // Fix vector shift instructions where the last operand is a non-immediate
8062 // i32 value.
8063 case Intrinsic::x86_sse2_pslli_w:
8064 case Intrinsic::x86_sse2_pslli_d:
8065 case Intrinsic::x86_sse2_pslli_q:
8066 case Intrinsic::x86_sse2_psrli_w:
8067 case Intrinsic::x86_sse2_psrli_d:
8068 case Intrinsic::x86_sse2_psrli_q:
8069 case Intrinsic::x86_sse2_psrai_w:
8070 case Intrinsic::x86_sse2_psrai_d:
8071 case Intrinsic::x86_mmx_pslli_w:
8072 case Intrinsic::x86_mmx_pslli_d:
8073 case Intrinsic::x86_mmx_pslli_q:
8074 case Intrinsic::x86_mmx_psrli_w:
8075 case Intrinsic::x86_mmx_psrli_d:
8076 case Intrinsic::x86_mmx_psrli_q:
8077 case Intrinsic::x86_mmx_psrai_w:
8078 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008079 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008080 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008081 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008082
8083 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008084 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008085 switch (IntNo) {
8086 case Intrinsic::x86_sse2_pslli_w:
8087 NewIntNo = Intrinsic::x86_sse2_psll_w;
8088 break;
8089 case Intrinsic::x86_sse2_pslli_d:
8090 NewIntNo = Intrinsic::x86_sse2_psll_d;
8091 break;
8092 case Intrinsic::x86_sse2_pslli_q:
8093 NewIntNo = Intrinsic::x86_sse2_psll_q;
8094 break;
8095 case Intrinsic::x86_sse2_psrli_w:
8096 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8097 break;
8098 case Intrinsic::x86_sse2_psrli_d:
8099 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8100 break;
8101 case Intrinsic::x86_sse2_psrli_q:
8102 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8103 break;
8104 case Intrinsic::x86_sse2_psrai_w:
8105 NewIntNo = Intrinsic::x86_sse2_psra_w;
8106 break;
8107 case Intrinsic::x86_sse2_psrai_d:
8108 NewIntNo = Intrinsic::x86_sse2_psra_d;
8109 break;
8110 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008111 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008112 switch (IntNo) {
8113 case Intrinsic::x86_mmx_pslli_w:
8114 NewIntNo = Intrinsic::x86_mmx_psll_w;
8115 break;
8116 case Intrinsic::x86_mmx_pslli_d:
8117 NewIntNo = Intrinsic::x86_mmx_psll_d;
8118 break;
8119 case Intrinsic::x86_mmx_pslli_q:
8120 NewIntNo = Intrinsic::x86_mmx_psll_q;
8121 break;
8122 case Intrinsic::x86_mmx_psrli_w:
8123 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8124 break;
8125 case Intrinsic::x86_mmx_psrli_d:
8126 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8127 break;
8128 case Intrinsic::x86_mmx_psrli_q:
8129 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8130 break;
8131 case Intrinsic::x86_mmx_psrai_w:
8132 NewIntNo = Intrinsic::x86_mmx_psra_w;
8133 break;
8134 case Intrinsic::x86_mmx_psrai_d:
8135 NewIntNo = Intrinsic::x86_mmx_psra_d;
8136 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008137 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008138 }
8139 break;
8140 }
8141 }
Mon P Wangefa42202009-09-03 19:56:25 +00008142
8143 // The vector shift intrinsics with scalars uses 32b shift amounts but
8144 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8145 // to be zero.
8146 SDValue ShOps[4];
8147 ShOps[0] = ShAmt;
8148 ShOps[1] = DAG.getConstant(0, MVT::i32);
8149 if (ShAmtVT == MVT::v4i32) {
8150 ShOps[2] = DAG.getUNDEF(MVT::i32);
8151 ShOps[3] = DAG.getUNDEF(MVT::i32);
8152 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8153 } else {
8154 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008155// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008156 }
8157
Owen Andersone50ed302009-08-10 22:56:29 +00008158 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008159 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008160 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008161 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008162 Op.getOperand(1), ShAmt);
8163 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008164 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008165}
Evan Cheng72261582005-12-20 06:22:03 +00008166
Dan Gohmand858e902010-04-17 15:26:15 +00008167SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8168 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008169 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8170 MFI->setReturnAddressIsTaken(true);
8171
Bill Wendling64e87322009-01-16 19:25:27 +00008172 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008173 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008174
8175 if (Depth > 0) {
8176 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8177 SDValue Offset =
8178 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008179 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008180 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008181 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008182 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008183 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008184 }
8185
8186 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008187 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008188 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008189 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008190}
8191
Dan Gohmand858e902010-04-17 15:26:15 +00008192SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008193 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8194 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008195
Owen Andersone50ed302009-08-10 22:56:29 +00008196 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008197 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008198 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8199 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008200 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008201 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008202 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8203 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008204 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008205 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008206}
8207
Dan Gohman475871a2008-07-27 21:46:04 +00008208SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008209 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008210 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008211}
8212
Dan Gohmand858e902010-04-17 15:26:15 +00008213SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008214 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008215 SDValue Chain = Op.getOperand(0);
8216 SDValue Offset = Op.getOperand(1);
8217 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008218 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008219
Dan Gohmand8816272010-08-11 18:14:00 +00008220 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8221 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8222 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008223 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008224
Dan Gohmand8816272010-08-11 18:14:00 +00008225 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8226 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008227 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008228 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8229 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008230 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008231 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008232
Dale Johannesene4d209d2009-02-03 20:21:25 +00008233 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008234 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008235 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008236}
8237
Dan Gohman475871a2008-07-27 21:46:04 +00008238SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008239 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008240 SDValue Root = Op.getOperand(0);
8241 SDValue Trmp = Op.getOperand(1); // trampoline
8242 SDValue FPtr = Op.getOperand(2); // nested function
8243 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008244 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008245
Dan Gohman69de1932008-02-06 22:27:42 +00008246 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008247
8248 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008249 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008250
8251 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008252 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8253 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008254
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008255 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8256 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008257
8258 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8259
8260 // Load the pointer to the nested function into R11.
8261 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008262 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008263 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008264 Addr, MachinePointerInfo(TrmpAddr),
8265 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008266
Owen Anderson825b72b2009-08-11 20:47:22 +00008267 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8268 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008269 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8270 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008271 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008272
8273 // Load the 'nest' parameter value into R10.
8274 // R10 is specified in X86CallingConv.td
8275 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8277 DAG.getConstant(10, MVT::i64));
8278 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008279 Addr, MachinePointerInfo(TrmpAddr, 10),
8280 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008281
Owen Anderson825b72b2009-08-11 20:47:22 +00008282 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8283 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008284 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8285 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008286 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008287
8288 // Jump to the nested function.
8289 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008290 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8291 DAG.getConstant(20, MVT::i64));
8292 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008293 Addr, MachinePointerInfo(TrmpAddr, 20),
8294 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008295
8296 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008297 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8298 DAG.getConstant(22, MVT::i64));
8299 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008300 MachinePointerInfo(TrmpAddr, 22),
8301 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008302
Dan Gohman475871a2008-07-27 21:46:04 +00008303 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008304 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008305 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008306 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008307 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008308 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008309 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008310 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008311
8312 switch (CC) {
8313 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008314 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008315 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008316 case CallingConv::X86_StdCall: {
8317 // Pass 'nest' parameter in ECX.
8318 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008319 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008320
8321 // Check that ECX wasn't needed by an 'inreg' parameter.
8322 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008323 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008324
Chris Lattner58d74912008-03-12 17:45:29 +00008325 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008326 unsigned InRegCount = 0;
8327 unsigned Idx = 1;
8328
8329 for (FunctionType::param_iterator I = FTy->param_begin(),
8330 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008331 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008332 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008333 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008334
8335 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008336 report_fatal_error("Nest register in use - reduce number of inreg"
8337 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008338 }
8339 }
8340 break;
8341 }
8342 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008343 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008344 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008345 // Pass 'nest' parameter in EAX.
8346 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008347 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008348 break;
8349 }
8350
Dan Gohman475871a2008-07-27 21:46:04 +00008351 SDValue OutChains[4];
8352 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008353
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8355 DAG.getConstant(10, MVT::i32));
8356 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008357
Chris Lattnera62fe662010-02-05 19:20:30 +00008358 // This is storing the opcode for MOV32ri.
8359 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008360 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008361 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008362 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008363 Trmp, MachinePointerInfo(TrmpAddr),
8364 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008365
Owen Anderson825b72b2009-08-11 20:47:22 +00008366 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8367 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008368 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8369 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008370 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008371
Chris Lattnera62fe662010-02-05 19:20:30 +00008372 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008373 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8374 DAG.getConstant(5, MVT::i32));
8375 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008376 MachinePointerInfo(TrmpAddr, 5),
8377 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008378
Owen Anderson825b72b2009-08-11 20:47:22 +00008379 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8380 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008381 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8382 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008383 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008384
Dan Gohman475871a2008-07-27 21:46:04 +00008385 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008386 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008387 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008388 }
8389}
8390
Dan Gohmand858e902010-04-17 15:26:15 +00008391SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8392 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008393 /*
8394 The rounding mode is in bits 11:10 of FPSR, and has the following
8395 settings:
8396 00 Round to nearest
8397 01 Round to -inf
8398 10 Round to +inf
8399 11 Round to 0
8400
8401 FLT_ROUNDS, on the other hand, expects the following:
8402 -1 Undefined
8403 0 Round to 0
8404 1 Round to nearest
8405 2 Round to +inf
8406 3 Round to -inf
8407
8408 To perform the conversion, we do:
8409 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8410 */
8411
8412 MachineFunction &MF = DAG.getMachineFunction();
8413 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008414 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008415 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008416 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008417 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008418
8419 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008420 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008421 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008422
Michael J. Spencerec38de22010-10-10 22:04:20 +00008423
Chris Lattner2156b792010-09-22 01:11:26 +00008424 MachineMemOperand *MMO =
8425 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8426 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008427
Chris Lattner2156b792010-09-22 01:11:26 +00008428 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8429 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8430 DAG.getVTList(MVT::Other),
8431 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008432
8433 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008434 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008435 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008436
8437 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008438 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008439 DAG.getNode(ISD::SRL, DL, MVT::i16,
8440 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008441 CWD, DAG.getConstant(0x800, MVT::i16)),
8442 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008443 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008444 DAG.getNode(ISD::SRL, DL, MVT::i16,
8445 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008446 CWD, DAG.getConstant(0x400, MVT::i16)),
8447 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008448
Dan Gohman475871a2008-07-27 21:46:04 +00008449 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008450 DAG.getNode(ISD::AND, DL, MVT::i16,
8451 DAG.getNode(ISD::ADD, DL, MVT::i16,
8452 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008453 DAG.getConstant(1, MVT::i16)),
8454 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008455
8456
Duncan Sands83ec4b62008-06-06 12:08:01 +00008457 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008458 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008459}
8460
Dan Gohmand858e902010-04-17 15:26:15 +00008461SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008462 EVT VT = Op.getValueType();
8463 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008464 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008465 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008466
8467 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008468 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008469 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008470 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008471 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008472 }
Evan Cheng18efe262007-12-14 02:13:44 +00008473
Evan Cheng152804e2007-12-14 08:30:15 +00008474 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008475 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008476 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008477
8478 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008479 SDValue Ops[] = {
8480 Op,
8481 DAG.getConstant(NumBits+NumBits-1, OpVT),
8482 DAG.getConstant(X86::COND_E, MVT::i8),
8483 Op.getValue(1)
8484 };
8485 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008486
8487 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008488 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008489
Owen Anderson825b72b2009-08-11 20:47:22 +00008490 if (VT == MVT::i8)
8491 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008492 return Op;
8493}
8494
Dan Gohmand858e902010-04-17 15:26:15 +00008495SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008496 EVT VT = Op.getValueType();
8497 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008498 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008499 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008500
8501 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008502 if (VT == MVT::i8) {
8503 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008504 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008505 }
Evan Cheng152804e2007-12-14 08:30:15 +00008506
8507 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008508 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008509 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008510
8511 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008512 SDValue Ops[] = {
8513 Op,
8514 DAG.getConstant(NumBits, OpVT),
8515 DAG.getConstant(X86::COND_E, MVT::i8),
8516 Op.getValue(1)
8517 };
8518 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008519
Owen Anderson825b72b2009-08-11 20:47:22 +00008520 if (VT == MVT::i8)
8521 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008522 return Op;
8523}
8524
Dan Gohmand858e902010-04-17 15:26:15 +00008525SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008526 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008527 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008528 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008529
Mon P Wangaf9b9522008-12-18 21:42:19 +00008530 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8531 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8532 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8533 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8534 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8535 //
8536 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8537 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8538 // return AloBlo + AloBhi + AhiBlo;
8539
8540 SDValue A = Op.getOperand(0);
8541 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008542
Dale Johannesene4d209d2009-02-03 20:21:25 +00008543 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008544 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8545 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008546 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008547 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8548 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008549 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008550 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008551 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008552 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008553 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008554 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008555 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008556 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008557 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008558 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008559 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8560 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008561 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008562 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8563 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008564 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8565 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008566 return Res;
8567}
8568
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008569SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8570 EVT VT = Op.getValueType();
8571 DebugLoc dl = Op.getDebugLoc();
8572 SDValue R = Op.getOperand(0);
8573
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008574 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008575
Nate Begeman51409212010-07-28 00:21:48 +00008576 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8577
8578 if (VT == MVT::v4i32) {
8579 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8580 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8581 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8582
8583 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008584
Nate Begeman51409212010-07-28 00:21:48 +00008585 std::vector<Constant*> CV(4, CI);
8586 Constant *C = ConstantVector::get(CV);
8587 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8588 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008589 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008590 false, false, 16);
8591
8592 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008593 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008594 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8595 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8596 }
8597 if (VT == MVT::v16i8) {
8598 // a = a << 5;
8599 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8600 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8601 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8602
8603 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8604 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8605
8606 std::vector<Constant*> CVM1(16, CM1);
8607 std::vector<Constant*> CVM2(16, CM2);
8608 Constant *C = ConstantVector::get(CVM1);
8609 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8610 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008611 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008612 false, false, 16);
8613
8614 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8615 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8616 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8617 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8618 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008619 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008620 // a += a
8621 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008622
Nate Begeman51409212010-07-28 00:21:48 +00008623 C = ConstantVector::get(CVM2);
8624 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8625 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008626 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008627 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008628
Nate Begeman51409212010-07-28 00:21:48 +00008629 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8630 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8631 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8632 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8633 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008634 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008635 // a += a
8636 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008637
Nate Begeman51409212010-07-28 00:21:48 +00008638 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008639 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008640 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8641 return R;
8642 }
8643 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008644}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008645
Dan Gohmand858e902010-04-17 15:26:15 +00008646SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008647 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8648 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008649 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8650 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008651 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008652 SDValue LHS = N->getOperand(0);
8653 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008654 unsigned BaseOp = 0;
8655 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008656 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008657 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008658 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008659 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008660 // A subtract of one will be selected as a INC. Note that INC doesn't
8661 // set CF, so we can't do this for UADDO.
8662 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8663 if (C->getAPIntValue() == 1) {
8664 BaseOp = X86ISD::INC;
8665 Cond = X86::COND_O;
8666 break;
8667 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008668 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008669 Cond = X86::COND_O;
8670 break;
8671 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008672 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008673 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008674 break;
8675 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008676 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8677 // set CF, so we can't do this for USUBO.
8678 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8679 if (C->getAPIntValue() == 1) {
8680 BaseOp = X86ISD::DEC;
8681 Cond = X86::COND_O;
8682 break;
8683 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008684 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008685 Cond = X86::COND_O;
8686 break;
8687 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008688 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008689 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008690 break;
8691 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008692 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008693 Cond = X86::COND_O;
8694 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008695 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8696 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8697 MVT::i32);
8698 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008699
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008700 SDValue SetCC =
8701 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8702 DAG.getConstant(X86::COND_O, MVT::i32),
8703 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008704
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008705 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8706 return Sum;
8707 }
Bill Wendling74c37652008-12-09 22:08:41 +00008708 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008709
Bill Wendling61edeb52008-12-02 01:06:39 +00008710 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008711 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008712 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008713
Bill Wendling61edeb52008-12-02 01:06:39 +00008714 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008715 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8716 DAG.getConstant(Cond, MVT::i32),
8717 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008718
Bill Wendling61edeb52008-12-02 01:06:39 +00008719 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8720 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008721}
8722
Eric Christopher9a9d2752010-07-22 02:48:34 +00008723SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8724 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008725
Eric Christopherb6729dc2010-08-04 23:03:04 +00008726 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008727 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008728 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008729 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008730 SDValue Ops[] = {
8731 DAG.getRegister(X86::ESP, MVT::i32), // Base
8732 DAG.getTargetConstant(1, MVT::i8), // Scale
8733 DAG.getRegister(0, MVT::i32), // Index
8734 DAG.getTargetConstant(0, MVT::i32), // Disp
8735 DAG.getRegister(0, MVT::i32), // Segment.
8736 Zero,
8737 Chain
8738 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008739 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008740 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8741 array_lengthof(Ops));
8742 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008743 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008744
Eric Christopher9a9d2752010-07-22 02:48:34 +00008745 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008746 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008747 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008748
Chris Lattner132929a2010-08-14 17:26:09 +00008749 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8750 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8751 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8752 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008753
Chris Lattner132929a2010-08-14 17:26:09 +00008754 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8755 if (!Op1 && !Op2 && !Op3 && Op4)
8756 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008757
Chris Lattner132929a2010-08-14 17:26:09 +00008758 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8759 if (Op1 && !Op2 && !Op3 && !Op4)
8760 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008761
8762 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008763 // (MFENCE)>;
8764 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008765}
8766
Dan Gohmand858e902010-04-17 15:26:15 +00008767SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008768 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008769 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008770 unsigned Reg = 0;
8771 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008772 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008773 default:
8774 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008775 case MVT::i8: Reg = X86::AL; size = 1; break;
8776 case MVT::i16: Reg = X86::AX; size = 2; break;
8777 case MVT::i32: Reg = X86::EAX; size = 4; break;
8778 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008779 assert(Subtarget->is64Bit() && "Node not type legal!");
8780 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008781 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008782 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008783 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008784 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008785 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008786 Op.getOperand(1),
8787 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008788 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008789 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008790 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008791 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8792 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8793 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008794 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008795 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008796 return cpOut;
8797}
8798
Duncan Sands1607f052008-12-01 11:39:25 +00008799SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008800 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008801 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008802 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008803 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008804 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008805 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008806 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8807 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008808 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008809 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8810 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008811 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008812 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008813 rdx.getValue(1)
8814 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008815 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008816}
8817
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008818SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008819 SelectionDAG &DAG) const {
8820 EVT SrcVT = Op.getOperand(0).getValueType();
8821 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008822 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8823 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008824 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008825 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008826 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008827 // i64 <=> MMX conversions are Legal.
8828 if (SrcVT==MVT::i64 && DstVT.isVector())
8829 return Op;
8830 if (DstVT==MVT::i64 && SrcVT.isVector())
8831 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008832 // MMX <=> MMX conversions are Legal.
8833 if (SrcVT.isVector() && DstVT.isVector())
8834 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008835 // All other conversions need to be expanded.
8836 return SDValue();
8837}
Chris Lattner5b856542010-12-20 00:59:46 +00008838
Dan Gohmand858e902010-04-17 15:26:15 +00008839SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008840 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008841 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008842 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008843 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008844 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008845 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008846 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008847 Node->getOperand(0),
8848 Node->getOperand(1), negOp,
8849 cast<AtomicSDNode>(Node)->getSrcValue(),
8850 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008851}
8852
Chris Lattner5b856542010-12-20 00:59:46 +00008853static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8854 EVT VT = Op.getNode()->getValueType(0);
8855
8856 // Let legalize expand this if it isn't a legal type yet.
8857 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8858 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008859
Chris Lattner5b856542010-12-20 00:59:46 +00008860 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008861
Chris Lattner5b856542010-12-20 00:59:46 +00008862 unsigned Opc;
8863 bool ExtraOp = false;
8864 switch (Op.getOpcode()) {
8865 default: assert(0 && "Invalid code");
8866 case ISD::ADDC: Opc = X86ISD::ADD; break;
8867 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8868 case ISD::SUBC: Opc = X86ISD::SUB; break;
8869 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8870 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008871
Chris Lattner5b856542010-12-20 00:59:46 +00008872 if (!ExtraOp)
8873 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8874 Op.getOperand(1));
8875 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8876 Op.getOperand(1), Op.getOperand(2));
8877}
8878
Evan Cheng0db9fe62006-04-25 20:13:52 +00008879/// LowerOperation - Provide custom lowering hooks for some operations.
8880///
Dan Gohmand858e902010-04-17 15:26:15 +00008881SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008882 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008883 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008884 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008885 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8886 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008887 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008888 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008889 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8890 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8891 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00008892 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00008893 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008894 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8895 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8896 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008897 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008898 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008899 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008900 case ISD::SHL_PARTS:
8901 case ISD::SRA_PARTS:
8902 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8903 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008904 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008905 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008906 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008907 case ISD::FABS: return LowerFABS(Op, DAG);
8908 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008909 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008910 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008911 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008912 case ISD::SELECT: return LowerSELECT(Op, DAG);
8913 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008914 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008915 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008916 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008917 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008918 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008919 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8920 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008921 case ISD::FRAME_TO_ARGS_OFFSET:
8922 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008923 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008924 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008925 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008926 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008927 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8928 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008929 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008930 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008931 case ISD::SADDO:
8932 case ISD::UADDO:
8933 case ISD::SSUBO:
8934 case ISD::USUBO:
8935 case ISD::SMULO:
8936 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008937 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008938 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00008939 case ISD::ADDC:
8940 case ISD::ADDE:
8941 case ISD::SUBC:
8942 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008943 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008944}
8945
Duncan Sands1607f052008-12-01 11:39:25 +00008946void X86TargetLowering::
8947ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008948 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008949 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008950 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008951 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008952
8953 SDValue Chain = Node->getOperand(0);
8954 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008955 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008956 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008957 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008958 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008959 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008960 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008961 SDValue Result =
8962 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8963 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008964 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008965 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008966 Results.push_back(Result.getValue(2));
8967}
8968
Duncan Sands126d9072008-07-04 11:47:58 +00008969/// ReplaceNodeResults - Replace a node with an illegal result type
8970/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008971void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8972 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008973 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008974 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008975 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008976 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008977 assert(false && "Do not know how to custom type legalize this operation!");
8978 return;
Chris Lattner5b856542010-12-20 00:59:46 +00008979 case ISD::ADDC:
8980 case ISD::ADDE:
8981 case ISD::SUBC:
8982 case ISD::SUBE:
8983 // We don't want to expand or promote these.
8984 return;
Duncan Sands1607f052008-12-01 11:39:25 +00008985 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008986 std::pair<SDValue,SDValue> Vals =
8987 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008988 SDValue FIST = Vals.first, StackSlot = Vals.second;
8989 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008990 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008991 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008992 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8993 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008994 }
8995 return;
8996 }
8997 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008998 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008999 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009000 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009001 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009002 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009003 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009004 eax.getValue(2));
9005 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9006 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009007 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009008 Results.push_back(edx.getValue(1));
9009 return;
9010 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009011 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009012 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009013 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009014 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009015 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9016 DAG.getConstant(0, MVT::i32));
9017 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9018 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009019 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9020 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009021 cpInL.getValue(1));
9022 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009023 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9024 DAG.getConstant(0, MVT::i32));
9025 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9026 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009027 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009028 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009029 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009030 swapInL.getValue(1));
9031 SDValue Ops[] = { swapInH.getValue(0),
9032 N->getOperand(1),
9033 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009034 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009035 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9036 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9037 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009038 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009039 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009040 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009041 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009042 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009043 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009044 Results.push_back(cpOutH.getValue(1));
9045 return;
9046 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009047 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009048 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9049 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009050 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009051 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9052 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009053 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009054 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9055 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009056 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009057 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9058 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009059 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009060 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9061 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009062 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009063 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9064 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009065 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009066 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9067 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009068 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009069}
9070
Evan Cheng72261582005-12-20 06:22:03 +00009071const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9072 switch (Opcode) {
9073 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009074 case X86ISD::BSF: return "X86ISD::BSF";
9075 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009076 case X86ISD::SHLD: return "X86ISD::SHLD";
9077 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009078 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009079 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009080 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009081 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009082 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009083 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009084 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9085 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9086 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009087 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009088 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009089 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009090 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009091 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009092 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009093 case X86ISD::COMI: return "X86ISD::COMI";
9094 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009095 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009096 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009097 case X86ISD::CMOV: return "X86ISD::CMOV";
9098 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009099 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009100 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9101 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009102 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009103 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009104 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009105 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009106 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009107 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9108 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009109 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009110 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009111 case X86ISD::PANDN: return "X86ISD::PANDN";
9112 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9113 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9114 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009115 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009116 case X86ISD::FMAX: return "X86ISD::FMAX";
9117 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009118 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9119 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009120 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009121 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009122 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009123 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009124 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009125 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9126 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009127 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9128 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9129 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9130 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9131 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9132 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009133 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9134 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009135 case X86ISD::VSHL: return "X86ISD::VSHL";
9136 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009137 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9138 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9139 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9140 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9141 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9142 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9143 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9144 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9145 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9146 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009147 case X86ISD::ADD: return "X86ISD::ADD";
9148 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009149 case X86ISD::ADC: return "X86ISD::ADC";
9150 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009151 case X86ISD::SMUL: return "X86ISD::SMUL";
9152 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009153 case X86ISD::INC: return "X86ISD::INC";
9154 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009155 case X86ISD::OR: return "X86ISD::OR";
9156 case X86ISD::XOR: return "X86ISD::XOR";
9157 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009158 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009159 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009160 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009161 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9162 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9163 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9164 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9165 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9166 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9167 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9168 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9169 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009170 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009171 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009172 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009173 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9174 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009175 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9176 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9177 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9178 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9179 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9180 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9181 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9182 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9183 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
9184 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9185 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9186 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9187 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9188 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9189 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9190 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9191 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9192 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9193 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009194 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009195 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009196 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009197 }
9198}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009199
Chris Lattnerc9addb72007-03-30 23:15:24 +00009200// isLegalAddressingMode - Return true if the addressing mode represented
9201// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009202bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009203 const Type *Ty) const {
9204 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009205 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009206 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009207
Chris Lattnerc9addb72007-03-30 23:15:24 +00009208 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009209 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009210 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009211
Chris Lattnerc9addb72007-03-30 23:15:24 +00009212 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009213 unsigned GVFlags =
9214 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009215
Chris Lattnerdfed4132009-07-10 07:38:24 +00009216 // If a reference to this global requires an extra load, we can't fold it.
9217 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009218 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009219
Chris Lattnerdfed4132009-07-10 07:38:24 +00009220 // If BaseGV requires a register for the PIC base, we cannot also have a
9221 // BaseReg specified.
9222 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009223 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009224
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009225 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009226 if ((M != CodeModel::Small || R != Reloc::Static) &&
9227 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009228 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009229 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009230
Chris Lattnerc9addb72007-03-30 23:15:24 +00009231 switch (AM.Scale) {
9232 case 0:
9233 case 1:
9234 case 2:
9235 case 4:
9236 case 8:
9237 // These scales always work.
9238 break;
9239 case 3:
9240 case 5:
9241 case 9:
9242 // These scales are formed with basereg+scalereg. Only accept if there is
9243 // no basereg yet.
9244 if (AM.HasBaseReg)
9245 return false;
9246 break;
9247 default: // Other stuff never works.
9248 return false;
9249 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009250
Chris Lattnerc9addb72007-03-30 23:15:24 +00009251 return true;
9252}
9253
9254
Evan Cheng2bd122c2007-10-26 01:56:11 +00009255bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009256 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009257 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009258 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9259 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009260 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009261 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009262 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009263}
9264
Owen Andersone50ed302009-08-10 22:56:29 +00009265bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009266 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009267 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009268 unsigned NumBits1 = VT1.getSizeInBits();
9269 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009270 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009271 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009272 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009273}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009274
Dan Gohman97121ba2009-04-08 00:15:30 +00009275bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009276 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009277 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009278}
9279
Owen Andersone50ed302009-08-10 22:56:29 +00009280bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009281 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009282 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009283}
9284
Owen Andersone50ed302009-08-10 22:56:29 +00009285bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009286 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009287 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009288}
9289
Evan Cheng60c07e12006-07-05 22:17:51 +00009290/// isShuffleMaskLegal - Targets can use this to indicate that they only
9291/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9292/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9293/// are assumed to be legal.
9294bool
Eric Christopherfd179292009-08-27 18:07:15 +00009295X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009296 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009297 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009298 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009299 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009300
Nate Begemana09008b2009-10-19 02:17:23 +00009301 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009302 return (VT.getVectorNumElements() == 2 ||
9303 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9304 isMOVLMask(M, VT) ||
9305 isSHUFPMask(M, VT) ||
9306 isPSHUFDMask(M, VT) ||
9307 isPSHUFHWMask(M, VT) ||
9308 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009309 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009310 isUNPCKLMask(M, VT) ||
9311 isUNPCKHMask(M, VT) ||
9312 isUNPCKL_v_undef_Mask(M, VT) ||
9313 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009314}
9315
Dan Gohman7d8143f2008-04-09 20:09:42 +00009316bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009317X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009318 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009319 unsigned NumElts = VT.getVectorNumElements();
9320 // FIXME: This collection of masks seems suspect.
9321 if (NumElts == 2)
9322 return true;
9323 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9324 return (isMOVLMask(Mask, VT) ||
9325 isCommutedMOVLMask(Mask, VT, true) ||
9326 isSHUFPMask(Mask, VT) ||
9327 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009328 }
9329 return false;
9330}
9331
9332//===----------------------------------------------------------------------===//
9333// X86 Scheduler Hooks
9334//===----------------------------------------------------------------------===//
9335
Mon P Wang63307c32008-05-05 19:05:59 +00009336// private utility function
9337MachineBasicBlock *
9338X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9339 MachineBasicBlock *MBB,
9340 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009341 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009342 unsigned LoadOpc,
9343 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009344 unsigned notOpc,
9345 unsigned EAXreg,
9346 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009347 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009348 // For the atomic bitwise operator, we generate
9349 // thisMBB:
9350 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009351 // ld t1 = [bitinstr.addr]
9352 // op t2 = t1, [bitinstr.val]
9353 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009354 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9355 // bz newMBB
9356 // fallthrough -->nextMBB
9357 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9358 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009359 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009360 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009361
Mon P Wang63307c32008-05-05 19:05:59 +00009362 /// First build the CFG
9363 MachineFunction *F = MBB->getParent();
9364 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009365 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9366 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9367 F->insert(MBBIter, newMBB);
9368 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009369
Dan Gohman14152b42010-07-06 20:24:04 +00009370 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9371 nextMBB->splice(nextMBB->begin(), thisMBB,
9372 llvm::next(MachineBasicBlock::iterator(bInstr)),
9373 thisMBB->end());
9374 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009375
Mon P Wang63307c32008-05-05 19:05:59 +00009376 // Update thisMBB to fall through to newMBB
9377 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009378
Mon P Wang63307c32008-05-05 19:05:59 +00009379 // newMBB jumps to itself and fall through to nextMBB
9380 newMBB->addSuccessor(nextMBB);
9381 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009382
Mon P Wang63307c32008-05-05 19:05:59 +00009383 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009384 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009385 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009386 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009387 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009388 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009389 int numArgs = bInstr->getNumOperands() - 1;
9390 for (int i=0; i < numArgs; ++i)
9391 argOpers[i] = &bInstr->getOperand(i+1);
9392
9393 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009394 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009395 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009396
Dale Johannesen140be2d2008-08-19 18:47:28 +00009397 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009398 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009399 for (int i=0; i <= lastAddrIndx; ++i)
9400 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009401
Dale Johannesen140be2d2008-08-19 18:47:28 +00009402 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009403 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009404 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009406 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009407 tt = t1;
9408
Dale Johannesen140be2d2008-08-19 18:47:28 +00009409 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009410 assert((argOpers[valArgIndx]->isReg() ||
9411 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009412 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009413 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009414 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009415 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009416 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009417 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009418 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009419
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009420 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009421 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009422
Dale Johannesene4d209d2009-02-03 20:21:25 +00009423 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009424 for (int i=0; i <= lastAddrIndx; ++i)
9425 (*MIB).addOperand(*argOpers[i]);
9426 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009427 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009428 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9429 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009430
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009431 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009432 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009433
Mon P Wang63307c32008-05-05 19:05:59 +00009434 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009435 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009436
Dan Gohman14152b42010-07-06 20:24:04 +00009437 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009438 return nextMBB;
9439}
9440
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009441// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009442MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009443X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9444 MachineBasicBlock *MBB,
9445 unsigned regOpcL,
9446 unsigned regOpcH,
9447 unsigned immOpcL,
9448 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009449 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009450 // For the atomic bitwise operator, we generate
9451 // thisMBB (instructions are in pairs, except cmpxchg8b)
9452 // ld t1,t2 = [bitinstr.addr]
9453 // newMBB:
9454 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9455 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009456 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009457 // mov ECX, EBX <- t5, t6
9458 // mov EAX, EDX <- t1, t2
9459 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9460 // mov t3, t4 <- EAX, EDX
9461 // bz newMBB
9462 // result in out1, out2
9463 // fallthrough -->nextMBB
9464
9465 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9466 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009467 const unsigned NotOpc = X86::NOT32r;
9468 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9469 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9470 MachineFunction::iterator MBBIter = MBB;
9471 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009472
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009473 /// First build the CFG
9474 MachineFunction *F = MBB->getParent();
9475 MachineBasicBlock *thisMBB = MBB;
9476 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9477 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9478 F->insert(MBBIter, newMBB);
9479 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009480
Dan Gohman14152b42010-07-06 20:24:04 +00009481 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9482 nextMBB->splice(nextMBB->begin(), thisMBB,
9483 llvm::next(MachineBasicBlock::iterator(bInstr)),
9484 thisMBB->end());
9485 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009486
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009487 // Update thisMBB to fall through to newMBB
9488 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009489
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009490 // newMBB jumps to itself and fall through to nextMBB
9491 newMBB->addSuccessor(nextMBB);
9492 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009493
Dale Johannesene4d209d2009-02-03 20:21:25 +00009494 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009495 // Insert instructions into newMBB based on incoming instruction
9496 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009497 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009498 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009499 MachineOperand& dest1Oper = bInstr->getOperand(0);
9500 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009501 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9502 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009503 argOpers[i] = &bInstr->getOperand(i+2);
9504
Dan Gohman71ea4e52010-05-14 21:01:44 +00009505 // We use some of the operands multiple times, so conservatively just
9506 // clear any kill flags that might be present.
9507 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9508 argOpers[i]->setIsKill(false);
9509 }
9510
Evan Chengad5b52f2010-01-08 19:14:57 +00009511 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009512 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009513
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009514 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009515 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009516 for (int i=0; i <= lastAddrIndx; ++i)
9517 (*MIB).addOperand(*argOpers[i]);
9518 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009519 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009520 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009521 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009522 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009523 MachineOperand newOp3 = *(argOpers[3]);
9524 if (newOp3.isImm())
9525 newOp3.setImm(newOp3.getImm()+4);
9526 else
9527 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009528 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009529 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009530
9531 // t3/4 are defined later, at the bottom of the loop
9532 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9533 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009534 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009535 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009536 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009537 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9538
Evan Cheng306b4ca2010-01-08 23:41:50 +00009539 // The subsequent operations should be using the destination registers of
9540 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009541 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009542 t1 = F->getRegInfo().createVirtualRegister(RC);
9543 t2 = F->getRegInfo().createVirtualRegister(RC);
9544 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9545 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009546 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009547 t1 = dest1Oper.getReg();
9548 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009549 }
9550
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009551 int valArgIndx = lastAddrIndx + 1;
9552 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009553 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009554 "invalid operand");
9555 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9556 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009557 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009558 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009559 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009560 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009561 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009562 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009563 (*MIB).addOperand(*argOpers[valArgIndx]);
9564 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009565 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009566 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009567 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009568 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009569 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009570 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009571 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009572 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009573 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009574 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009575
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009576 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009577 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009578 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009579 MIB.addReg(t2);
9580
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009581 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009582 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009583 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009584 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009585
Dale Johannesene4d209d2009-02-03 20:21:25 +00009586 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009587 for (int i=0; i <= lastAddrIndx; ++i)
9588 (*MIB).addOperand(*argOpers[i]);
9589
9590 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009591 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9592 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009593
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009594 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009595 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009596 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009597 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009598
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009599 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009600 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009601
Dan Gohman14152b42010-07-06 20:24:04 +00009602 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009603 return nextMBB;
9604}
9605
9606// private utility function
9607MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009608X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9609 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009610 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009611 // For the atomic min/max operator, we generate
9612 // thisMBB:
9613 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009614 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009615 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009616 // cmp t1, t2
9617 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009618 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009619 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9620 // bz newMBB
9621 // fallthrough -->nextMBB
9622 //
9623 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9624 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009625 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009626 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009627
Mon P Wang63307c32008-05-05 19:05:59 +00009628 /// First build the CFG
9629 MachineFunction *F = MBB->getParent();
9630 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009631 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9632 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9633 F->insert(MBBIter, newMBB);
9634 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009635
Dan Gohman14152b42010-07-06 20:24:04 +00009636 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9637 nextMBB->splice(nextMBB->begin(), thisMBB,
9638 llvm::next(MachineBasicBlock::iterator(mInstr)),
9639 thisMBB->end());
9640 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009641
Mon P Wang63307c32008-05-05 19:05:59 +00009642 // Update thisMBB to fall through to newMBB
9643 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009644
Mon P Wang63307c32008-05-05 19:05:59 +00009645 // newMBB jumps to newMBB and fall through to nextMBB
9646 newMBB->addSuccessor(nextMBB);
9647 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009648
Dale Johannesene4d209d2009-02-03 20:21:25 +00009649 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009650 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009651 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009652 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009653 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009654 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009655 int numArgs = mInstr->getNumOperands() - 1;
9656 for (int i=0; i < numArgs; ++i)
9657 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009658
Mon P Wang63307c32008-05-05 19:05:59 +00009659 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009660 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009661 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009662
Mon P Wangab3e7472008-05-05 22:56:23 +00009663 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009664 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009665 for (int i=0; i <= lastAddrIndx; ++i)
9666 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009667
Mon P Wang63307c32008-05-05 19:05:59 +00009668 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009669 assert((argOpers[valArgIndx]->isReg() ||
9670 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009671 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009672
9673 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009674 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009675 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009676 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009677 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009678 (*MIB).addOperand(*argOpers[valArgIndx]);
9679
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009680 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009681 MIB.addReg(t1);
9682
Dale Johannesene4d209d2009-02-03 20:21:25 +00009683 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009684 MIB.addReg(t1);
9685 MIB.addReg(t2);
9686
9687 // Generate movc
9688 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009689 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009690 MIB.addReg(t2);
9691 MIB.addReg(t1);
9692
9693 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009694 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009695 for (int i=0; i <= lastAddrIndx; ++i)
9696 (*MIB).addOperand(*argOpers[i]);
9697 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009698 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009699 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9700 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009701
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009702 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009703 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009704
Mon P Wang63307c32008-05-05 19:05:59 +00009705 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009706 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009707
Dan Gohman14152b42010-07-06 20:24:04 +00009708 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009709 return nextMBB;
9710}
9711
Eric Christopherf83a5de2009-08-27 18:08:16 +00009712// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009713// or XMM0_V32I8 in AVX all of this code can be replaced with that
9714// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009715MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009716X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009717 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009718 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9719 "Target must have SSE4.2 or AVX features enabled");
9720
Eric Christopherb120ab42009-08-18 22:50:32 +00009721 DebugLoc dl = MI->getDebugLoc();
9722 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009723 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009724 if (!Subtarget->hasAVX()) {
9725 if (memArg)
9726 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9727 else
9728 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9729 } else {
9730 if (memArg)
9731 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9732 else
9733 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9734 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009735
Eric Christopher41c902f2010-11-30 08:20:21 +00009736 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009737 for (unsigned i = 0; i < numArgs; ++i) {
9738 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009739 if (!(Op.isReg() && Op.isImplicit()))
9740 MIB.addOperand(Op);
9741 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009742 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009743 .addReg(X86::XMM0);
9744
Dan Gohman14152b42010-07-06 20:24:04 +00009745 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009746 return BB;
9747}
9748
9749MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009750X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009751 DebugLoc dl = MI->getDebugLoc();
9752 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009753
Eric Christopher228232b2010-11-30 07:20:12 +00009754 // Address into RAX/EAX, other two args into ECX, EDX.
9755 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9756 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9757 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9758 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009759 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009760
Eric Christopher228232b2010-11-30 07:20:12 +00009761 unsigned ValOps = X86::AddrNumOperands;
9762 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9763 .addReg(MI->getOperand(ValOps).getReg());
9764 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9765 .addReg(MI->getOperand(ValOps+1).getReg());
9766
9767 // The instruction doesn't actually take any operands though.
9768 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009769
Eric Christopher228232b2010-11-30 07:20:12 +00009770 MI->eraseFromParent(); // The pseudo is gone now.
9771 return BB;
9772}
9773
9774MachineBasicBlock *
9775X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009776 DebugLoc dl = MI->getDebugLoc();
9777 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009778
Eric Christopher228232b2010-11-30 07:20:12 +00009779 // First arg in ECX, the second in EAX.
9780 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9781 .addReg(MI->getOperand(0).getReg());
9782 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9783 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009784
Eric Christopher228232b2010-11-30 07:20:12 +00009785 // The instruction doesn't actually take any operands though.
9786 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009787
Eric Christopher228232b2010-11-30 07:20:12 +00009788 MI->eraseFromParent(); // The pseudo is gone now.
9789 return BB;
9790}
9791
9792MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009793X86TargetLowering::EmitVAARG64WithCustomInserter(
9794 MachineInstr *MI,
9795 MachineBasicBlock *MBB) const {
9796 // Emit va_arg instruction on X86-64.
9797
9798 // Operands to this pseudo-instruction:
9799 // 0 ) Output : destination address (reg)
9800 // 1-5) Input : va_list address (addr, i64mem)
9801 // 6 ) ArgSize : Size (in bytes) of vararg type
9802 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9803 // 8 ) Align : Alignment of type
9804 // 9 ) EFLAGS (implicit-def)
9805
9806 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9807 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9808
9809 unsigned DestReg = MI->getOperand(0).getReg();
9810 MachineOperand &Base = MI->getOperand(1);
9811 MachineOperand &Scale = MI->getOperand(2);
9812 MachineOperand &Index = MI->getOperand(3);
9813 MachineOperand &Disp = MI->getOperand(4);
9814 MachineOperand &Segment = MI->getOperand(5);
9815 unsigned ArgSize = MI->getOperand(6).getImm();
9816 unsigned ArgMode = MI->getOperand(7).getImm();
9817 unsigned Align = MI->getOperand(8).getImm();
9818
9819 // Memory Reference
9820 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9821 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9822 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9823
9824 // Machine Information
9825 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9826 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9827 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9828 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9829 DebugLoc DL = MI->getDebugLoc();
9830
9831 // struct va_list {
9832 // i32 gp_offset
9833 // i32 fp_offset
9834 // i64 overflow_area (address)
9835 // i64 reg_save_area (address)
9836 // }
9837 // sizeof(va_list) = 24
9838 // alignment(va_list) = 8
9839
9840 unsigned TotalNumIntRegs = 6;
9841 unsigned TotalNumXMMRegs = 8;
9842 bool UseGPOffset = (ArgMode == 1);
9843 bool UseFPOffset = (ArgMode == 2);
9844 unsigned MaxOffset = TotalNumIntRegs * 8 +
9845 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9846
9847 /* Align ArgSize to a multiple of 8 */
9848 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9849 bool NeedsAlign = (Align > 8);
9850
9851 MachineBasicBlock *thisMBB = MBB;
9852 MachineBasicBlock *overflowMBB;
9853 MachineBasicBlock *offsetMBB;
9854 MachineBasicBlock *endMBB;
9855
9856 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9857 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9858 unsigned OffsetReg = 0;
9859
9860 if (!UseGPOffset && !UseFPOffset) {
9861 // If we only pull from the overflow region, we don't create a branch.
9862 // We don't need to alter control flow.
9863 OffsetDestReg = 0; // unused
9864 OverflowDestReg = DestReg;
9865
9866 offsetMBB = NULL;
9867 overflowMBB = thisMBB;
9868 endMBB = thisMBB;
9869 } else {
9870 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9871 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9872 // If not, pull from overflow_area. (branch to overflowMBB)
9873 //
9874 // thisMBB
9875 // | .
9876 // | .
9877 // offsetMBB overflowMBB
9878 // | .
9879 // | .
9880 // endMBB
9881
9882 // Registers for the PHI in endMBB
9883 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9884 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9885
9886 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9887 MachineFunction *MF = MBB->getParent();
9888 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9889 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9890 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9891
9892 MachineFunction::iterator MBBIter = MBB;
9893 ++MBBIter;
9894
9895 // Insert the new basic blocks
9896 MF->insert(MBBIter, offsetMBB);
9897 MF->insert(MBBIter, overflowMBB);
9898 MF->insert(MBBIter, endMBB);
9899
9900 // Transfer the remainder of MBB and its successor edges to endMBB.
9901 endMBB->splice(endMBB->begin(), thisMBB,
9902 llvm::next(MachineBasicBlock::iterator(MI)),
9903 thisMBB->end());
9904 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9905
9906 // Make offsetMBB and overflowMBB successors of thisMBB
9907 thisMBB->addSuccessor(offsetMBB);
9908 thisMBB->addSuccessor(overflowMBB);
9909
9910 // endMBB is a successor of both offsetMBB and overflowMBB
9911 offsetMBB->addSuccessor(endMBB);
9912 overflowMBB->addSuccessor(endMBB);
9913
9914 // Load the offset value into a register
9915 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9916 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9917 .addOperand(Base)
9918 .addOperand(Scale)
9919 .addOperand(Index)
9920 .addDisp(Disp, UseFPOffset ? 4 : 0)
9921 .addOperand(Segment)
9922 .setMemRefs(MMOBegin, MMOEnd);
9923
9924 // Check if there is enough room left to pull this argument.
9925 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9926 .addReg(OffsetReg)
9927 .addImm(MaxOffset + 8 - ArgSizeA8);
9928
9929 // Branch to "overflowMBB" if offset >= max
9930 // Fall through to "offsetMBB" otherwise
9931 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9932 .addMBB(overflowMBB);
9933 }
9934
9935 // In offsetMBB, emit code to use the reg_save_area.
9936 if (offsetMBB) {
9937 assert(OffsetReg != 0);
9938
9939 // Read the reg_save_area address.
9940 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9941 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9942 .addOperand(Base)
9943 .addOperand(Scale)
9944 .addOperand(Index)
9945 .addDisp(Disp, 16)
9946 .addOperand(Segment)
9947 .setMemRefs(MMOBegin, MMOEnd);
9948
9949 // Zero-extend the offset
9950 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9951 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9952 .addImm(0)
9953 .addReg(OffsetReg)
9954 .addImm(X86::sub_32bit);
9955
9956 // Add the offset to the reg_save_area to get the final address.
9957 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9958 .addReg(OffsetReg64)
9959 .addReg(RegSaveReg);
9960
9961 // Compute the offset for the next argument
9962 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9963 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9964 .addReg(OffsetReg)
9965 .addImm(UseFPOffset ? 16 : 8);
9966
9967 // Store it back into the va_list.
9968 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9969 .addOperand(Base)
9970 .addOperand(Scale)
9971 .addOperand(Index)
9972 .addDisp(Disp, UseFPOffset ? 4 : 0)
9973 .addOperand(Segment)
9974 .addReg(NextOffsetReg)
9975 .setMemRefs(MMOBegin, MMOEnd);
9976
9977 // Jump to endMBB
9978 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9979 .addMBB(endMBB);
9980 }
9981
9982 //
9983 // Emit code to use overflow area
9984 //
9985
9986 // Load the overflow_area address into a register.
9987 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9988 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9989 .addOperand(Base)
9990 .addOperand(Scale)
9991 .addOperand(Index)
9992 .addDisp(Disp, 8)
9993 .addOperand(Segment)
9994 .setMemRefs(MMOBegin, MMOEnd);
9995
9996 // If we need to align it, do so. Otherwise, just copy the address
9997 // to OverflowDestReg.
9998 if (NeedsAlign) {
9999 // Align the overflow address
10000 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10001 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10002
10003 // aligned_addr = (addr + (align-1)) & ~(align-1)
10004 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10005 .addReg(OverflowAddrReg)
10006 .addImm(Align-1);
10007
10008 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10009 .addReg(TmpReg)
10010 .addImm(~(uint64_t)(Align-1));
10011 } else {
10012 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10013 .addReg(OverflowAddrReg);
10014 }
10015
10016 // Compute the next overflow address after this argument.
10017 // (the overflow address should be kept 8-byte aligned)
10018 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10019 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10020 .addReg(OverflowDestReg)
10021 .addImm(ArgSizeA8);
10022
10023 // Store the new overflow address.
10024 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10025 .addOperand(Base)
10026 .addOperand(Scale)
10027 .addOperand(Index)
10028 .addDisp(Disp, 8)
10029 .addOperand(Segment)
10030 .addReg(NextAddrReg)
10031 .setMemRefs(MMOBegin, MMOEnd);
10032
10033 // If we branched, emit the PHI to the front of endMBB.
10034 if (offsetMBB) {
10035 BuildMI(*endMBB, endMBB->begin(), DL,
10036 TII->get(X86::PHI), DestReg)
10037 .addReg(OffsetDestReg).addMBB(offsetMBB)
10038 .addReg(OverflowDestReg).addMBB(overflowMBB);
10039 }
10040
10041 // Erase the pseudo instruction
10042 MI->eraseFromParent();
10043
10044 return endMBB;
10045}
10046
10047MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010048X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10049 MachineInstr *MI,
10050 MachineBasicBlock *MBB) const {
10051 // Emit code to save XMM registers to the stack. The ABI says that the
10052 // number of registers to save is given in %al, so it's theoretically
10053 // possible to do an indirect jump trick to avoid saving all of them,
10054 // however this code takes a simpler approach and just executes all
10055 // of the stores if %al is non-zero. It's less code, and it's probably
10056 // easier on the hardware branch predictor, and stores aren't all that
10057 // expensive anyway.
10058
10059 // Create the new basic blocks. One block contains all the XMM stores,
10060 // and one block is the final destination regardless of whether any
10061 // stores were performed.
10062 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10063 MachineFunction *F = MBB->getParent();
10064 MachineFunction::iterator MBBIter = MBB;
10065 ++MBBIter;
10066 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10067 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10068 F->insert(MBBIter, XMMSaveMBB);
10069 F->insert(MBBIter, EndMBB);
10070
Dan Gohman14152b42010-07-06 20:24:04 +000010071 // Transfer the remainder of MBB and its successor edges to EndMBB.
10072 EndMBB->splice(EndMBB->begin(), MBB,
10073 llvm::next(MachineBasicBlock::iterator(MI)),
10074 MBB->end());
10075 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10076
Dan Gohmand6708ea2009-08-15 01:38:56 +000010077 // The original block will now fall through to the XMM save block.
10078 MBB->addSuccessor(XMMSaveMBB);
10079 // The XMMSaveMBB will fall through to the end block.
10080 XMMSaveMBB->addSuccessor(EndMBB);
10081
10082 // Now add the instructions.
10083 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10084 DebugLoc DL = MI->getDebugLoc();
10085
10086 unsigned CountReg = MI->getOperand(0).getReg();
10087 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10088 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10089
10090 if (!Subtarget->isTargetWin64()) {
10091 // If %al is 0, branch around the XMM save block.
10092 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010093 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010094 MBB->addSuccessor(EndMBB);
10095 }
10096
10097 // In the XMM save block, save all the XMM argument registers.
10098 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10099 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010100 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010101 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010102 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010103 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010104 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010105 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10106 .addFrameIndex(RegSaveFrameIndex)
10107 .addImm(/*Scale=*/1)
10108 .addReg(/*IndexReg=*/0)
10109 .addImm(/*Disp=*/Offset)
10110 .addReg(/*Segment=*/0)
10111 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010112 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010113 }
10114
Dan Gohman14152b42010-07-06 20:24:04 +000010115 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010116
10117 return EndMBB;
10118}
Mon P Wang63307c32008-05-05 19:05:59 +000010119
Evan Cheng60c07e12006-07-05 22:17:51 +000010120MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010121X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010122 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010123 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10124 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010125
Chris Lattner52600972009-09-02 05:57:00 +000010126 // To "insert" a SELECT_CC instruction, we actually have to insert the
10127 // diamond control-flow pattern. The incoming instruction knows the
10128 // destination vreg to set, the condition code register to branch on, the
10129 // true/false values to select between, and a branch opcode to use.
10130 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10131 MachineFunction::iterator It = BB;
10132 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010133
Chris Lattner52600972009-09-02 05:57:00 +000010134 // thisMBB:
10135 // ...
10136 // TrueVal = ...
10137 // cmpTY ccX, r1, r2
10138 // bCC copy1MBB
10139 // fallthrough --> copy0MBB
10140 MachineBasicBlock *thisMBB = BB;
10141 MachineFunction *F = BB->getParent();
10142 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10143 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010144 F->insert(It, copy0MBB);
10145 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010146
Bill Wendling730c07e2010-06-25 20:48:10 +000010147 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10148 // live into the sink and copy blocks.
10149 const MachineFunction *MF = BB->getParent();
10150 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10151 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010152
Dan Gohman14152b42010-07-06 20:24:04 +000010153 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10154 const MachineOperand &MO = MI->getOperand(I);
10155 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010156 unsigned Reg = MO.getReg();
10157 if (Reg != X86::EFLAGS) continue;
10158 copy0MBB->addLiveIn(Reg);
10159 sinkMBB->addLiveIn(Reg);
10160 }
10161
Dan Gohman14152b42010-07-06 20:24:04 +000010162 // Transfer the remainder of BB and its successor edges to sinkMBB.
10163 sinkMBB->splice(sinkMBB->begin(), BB,
10164 llvm::next(MachineBasicBlock::iterator(MI)),
10165 BB->end());
10166 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10167
10168 // Add the true and fallthrough blocks as its successors.
10169 BB->addSuccessor(copy0MBB);
10170 BB->addSuccessor(sinkMBB);
10171
10172 // Create the conditional branch instruction.
10173 unsigned Opc =
10174 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10175 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10176
Chris Lattner52600972009-09-02 05:57:00 +000010177 // copy0MBB:
10178 // %FalseValue = ...
10179 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010180 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010181
Chris Lattner52600972009-09-02 05:57:00 +000010182 // sinkMBB:
10183 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10184 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010185 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10186 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010187 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10188 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10189
Dan Gohman14152b42010-07-06 20:24:04 +000010190 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010191 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010192}
10193
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010194MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010195X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010196 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010197 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10198 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010199
10200 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10201 // non-trivial part is impdef of ESP.
10202 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10203 // mingw-w64.
10204
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010205 const char *StackProbeSymbol =
10206 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10207
Dan Gohman14152b42010-07-06 20:24:04 +000010208 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010209 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010210 .addReg(X86::EAX, RegState::Implicit)
10211 .addReg(X86::ESP, RegState::Implicit)
10212 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010213 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10214 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010215
Dan Gohman14152b42010-07-06 20:24:04 +000010216 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010217 return BB;
10218}
Chris Lattner52600972009-09-02 05:57:00 +000010219
10220MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010221X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10222 MachineBasicBlock *BB) const {
10223 // This is pretty easy. We're taking the value that we received from
10224 // our load from the relocation, sticking it in either RDI (x86-64)
10225 // or EAX and doing an indirect call. The return value will then
10226 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010227 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010228 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010229 DebugLoc DL = MI->getDebugLoc();
10230 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010231
10232 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010233 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010234
Eric Christopher30ef0e52010-06-03 04:07:48 +000010235 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010236 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10237 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010238 .addReg(X86::RIP)
10239 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010240 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010241 MI->getOperand(3).getTargetFlags())
10242 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010243 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010244 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010245 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010246 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10247 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010248 .addReg(0)
10249 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010250 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010251 MI->getOperand(3).getTargetFlags())
10252 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010253 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010254 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010255 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010256 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10257 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010258 .addReg(TII->getGlobalBaseReg(F))
10259 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010260 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010261 MI->getOperand(3).getTargetFlags())
10262 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010263 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010264 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010265 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010266
Dan Gohman14152b42010-07-06 20:24:04 +000010267 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010268 return BB;
10269}
10270
10271MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010272X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010273 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010274 switch (MI->getOpcode()) {
10275 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010276 case X86::TAILJMPd64:
10277 case X86::TAILJMPr64:
10278 case X86::TAILJMPm64:
10279 assert(!"TAILJMP64 would not be touched here.");
10280 case X86::TCRETURNdi64:
10281 case X86::TCRETURNri64:
10282 case X86::TCRETURNmi64:
10283 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10284 // On AMD64, additional defs should be added before register allocation.
10285 if (!Subtarget->isTargetWin64()) {
10286 MI->addRegisterDefined(X86::RSI);
10287 MI->addRegisterDefined(X86::RDI);
10288 MI->addRegisterDefined(X86::XMM6);
10289 MI->addRegisterDefined(X86::XMM7);
10290 MI->addRegisterDefined(X86::XMM8);
10291 MI->addRegisterDefined(X86::XMM9);
10292 MI->addRegisterDefined(X86::XMM10);
10293 MI->addRegisterDefined(X86::XMM11);
10294 MI->addRegisterDefined(X86::XMM12);
10295 MI->addRegisterDefined(X86::XMM13);
10296 MI->addRegisterDefined(X86::XMM14);
10297 MI->addRegisterDefined(X86::XMM15);
10298 }
10299 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010300 case X86::WIN_ALLOCA:
10301 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010302 case X86::TLSCall_32:
10303 case X86::TLSCall_64:
10304 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010305 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010306 case X86::CMOV_FR32:
10307 case X86::CMOV_FR64:
10308 case X86::CMOV_V4F32:
10309 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010310 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010311 case X86::CMOV_GR16:
10312 case X86::CMOV_GR32:
10313 case X86::CMOV_RFP32:
10314 case X86::CMOV_RFP64:
10315 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010316 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010317
Dale Johannesen849f2142007-07-03 00:53:03 +000010318 case X86::FP32_TO_INT16_IN_MEM:
10319 case X86::FP32_TO_INT32_IN_MEM:
10320 case X86::FP32_TO_INT64_IN_MEM:
10321 case X86::FP64_TO_INT16_IN_MEM:
10322 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010323 case X86::FP64_TO_INT64_IN_MEM:
10324 case X86::FP80_TO_INT16_IN_MEM:
10325 case X86::FP80_TO_INT32_IN_MEM:
10326 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010327 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10328 DebugLoc DL = MI->getDebugLoc();
10329
Evan Cheng60c07e12006-07-05 22:17:51 +000010330 // Change the floating point control register to use "round towards zero"
10331 // mode when truncating to an integer value.
10332 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010333 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010334 addFrameReference(BuildMI(*BB, MI, DL,
10335 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010336
10337 // Load the old value of the high byte of the control word...
10338 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010339 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010340 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010341 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010342
10343 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010344 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010345 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010346
10347 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010348 addFrameReference(BuildMI(*BB, MI, DL,
10349 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010350
10351 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010352 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010353 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010354
10355 // Get the X86 opcode to use.
10356 unsigned Opc;
10357 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010358 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010359 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10360 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10361 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10362 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10363 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10364 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010365 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10366 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10367 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010368 }
10369
10370 X86AddressMode AM;
10371 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010372 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010373 AM.BaseType = X86AddressMode::RegBase;
10374 AM.Base.Reg = Op.getReg();
10375 } else {
10376 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010377 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010378 }
10379 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010380 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010381 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010382 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010383 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010384 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010385 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010386 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010387 AM.GV = Op.getGlobal();
10388 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010389 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010390 }
Dan Gohman14152b42010-07-06 20:24:04 +000010391 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010392 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010393
10394 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010395 addFrameReference(BuildMI(*BB, MI, DL,
10396 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010397
Dan Gohman14152b42010-07-06 20:24:04 +000010398 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010399 return BB;
10400 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010401 // String/text processing lowering.
10402 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010403 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010404 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10405 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010406 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010407 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10408 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010409 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010410 return EmitPCMP(MI, BB, 5, false /* in mem */);
10411 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010412 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010413 return EmitPCMP(MI, BB, 5, true /* in mem */);
10414
Eric Christopher228232b2010-11-30 07:20:12 +000010415 // Thread synchronization.
10416 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010417 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010418 case X86::MWAIT:
10419 return EmitMwait(MI, BB);
10420
Eric Christopherb120ab42009-08-18 22:50:32 +000010421 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010422 case X86::ATOMAND32:
10423 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010424 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010425 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010426 X86::NOT32r, X86::EAX,
10427 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010428 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010429 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10430 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010431 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010432 X86::NOT32r, X86::EAX,
10433 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010434 case X86::ATOMXOR32:
10435 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010436 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010437 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010438 X86::NOT32r, X86::EAX,
10439 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010440 case X86::ATOMNAND32:
10441 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010442 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010443 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010444 X86::NOT32r, X86::EAX,
10445 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010446 case X86::ATOMMIN32:
10447 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10448 case X86::ATOMMAX32:
10449 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10450 case X86::ATOMUMIN32:
10451 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10452 case X86::ATOMUMAX32:
10453 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010454
10455 case X86::ATOMAND16:
10456 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10457 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010458 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010459 X86::NOT16r, X86::AX,
10460 X86::GR16RegisterClass);
10461 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010462 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010463 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010464 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010465 X86::NOT16r, X86::AX,
10466 X86::GR16RegisterClass);
10467 case X86::ATOMXOR16:
10468 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10469 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010470 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010471 X86::NOT16r, X86::AX,
10472 X86::GR16RegisterClass);
10473 case X86::ATOMNAND16:
10474 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10475 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010476 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010477 X86::NOT16r, X86::AX,
10478 X86::GR16RegisterClass, true);
10479 case X86::ATOMMIN16:
10480 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10481 case X86::ATOMMAX16:
10482 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10483 case X86::ATOMUMIN16:
10484 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10485 case X86::ATOMUMAX16:
10486 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10487
10488 case X86::ATOMAND8:
10489 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10490 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010491 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010492 X86::NOT8r, X86::AL,
10493 X86::GR8RegisterClass);
10494 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010495 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010496 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010497 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010498 X86::NOT8r, X86::AL,
10499 X86::GR8RegisterClass);
10500 case X86::ATOMXOR8:
10501 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10502 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010503 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010504 X86::NOT8r, X86::AL,
10505 X86::GR8RegisterClass);
10506 case X86::ATOMNAND8:
10507 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10508 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010509 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010510 X86::NOT8r, X86::AL,
10511 X86::GR8RegisterClass, true);
10512 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010513 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010514 case X86::ATOMAND64:
10515 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010516 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010517 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010518 X86::NOT64r, X86::RAX,
10519 X86::GR64RegisterClass);
10520 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010521 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10522 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010523 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010524 X86::NOT64r, X86::RAX,
10525 X86::GR64RegisterClass);
10526 case X86::ATOMXOR64:
10527 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010528 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010529 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010530 X86::NOT64r, X86::RAX,
10531 X86::GR64RegisterClass);
10532 case X86::ATOMNAND64:
10533 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10534 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010535 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010536 X86::NOT64r, X86::RAX,
10537 X86::GR64RegisterClass, true);
10538 case X86::ATOMMIN64:
10539 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10540 case X86::ATOMMAX64:
10541 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10542 case X86::ATOMUMIN64:
10543 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10544 case X86::ATOMUMAX64:
10545 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010546
10547 // This group does 64-bit operations on a 32-bit host.
10548 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010549 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010550 X86::AND32rr, X86::AND32rr,
10551 X86::AND32ri, X86::AND32ri,
10552 false);
10553 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010554 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010555 X86::OR32rr, X86::OR32rr,
10556 X86::OR32ri, X86::OR32ri,
10557 false);
10558 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010559 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010560 X86::XOR32rr, X86::XOR32rr,
10561 X86::XOR32ri, X86::XOR32ri,
10562 false);
10563 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010564 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010565 X86::AND32rr, X86::AND32rr,
10566 X86::AND32ri, X86::AND32ri,
10567 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010568 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010569 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010570 X86::ADD32rr, X86::ADC32rr,
10571 X86::ADD32ri, X86::ADC32ri,
10572 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010573 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010574 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010575 X86::SUB32rr, X86::SBB32rr,
10576 X86::SUB32ri, X86::SBB32ri,
10577 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010578 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010579 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010580 X86::MOV32rr, X86::MOV32rr,
10581 X86::MOV32ri, X86::MOV32ri,
10582 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010583 case X86::VASTART_SAVE_XMM_REGS:
10584 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010585
10586 case X86::VAARG_64:
10587 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010588 }
10589}
10590
10591//===----------------------------------------------------------------------===//
10592// X86 Optimization Hooks
10593//===----------------------------------------------------------------------===//
10594
Dan Gohman475871a2008-07-27 21:46:04 +000010595void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010596 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010597 APInt &KnownZero,
10598 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010599 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010600 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010601 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010602 assert((Opc >= ISD::BUILTIN_OP_END ||
10603 Opc == ISD::INTRINSIC_WO_CHAIN ||
10604 Opc == ISD::INTRINSIC_W_CHAIN ||
10605 Opc == ISD::INTRINSIC_VOID) &&
10606 "Should use MaskedValueIsZero if you don't know whether Op"
10607 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010608
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010609 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010610 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010611 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010612 case X86ISD::ADD:
10613 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010614 case X86ISD::ADC:
10615 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010616 case X86ISD::SMUL:
10617 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010618 case X86ISD::INC:
10619 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010620 case X86ISD::OR:
10621 case X86ISD::XOR:
10622 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010623 // These nodes' second result is a boolean.
10624 if (Op.getResNo() == 0)
10625 break;
10626 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010627 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010628 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10629 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010630 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010631 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010632}
Chris Lattner259e97c2006-01-31 19:43:35 +000010633
Owen Andersonbc146b02010-09-21 20:42:50 +000010634unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10635 unsigned Depth) const {
10636 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10637 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10638 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010639
Owen Andersonbc146b02010-09-21 20:42:50 +000010640 // Fallback case.
10641 return 1;
10642}
10643
Evan Cheng206ee9d2006-07-07 08:33:52 +000010644/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010645/// node is a GlobalAddress + offset.
10646bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010647 const GlobalValue* &GA,
10648 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010649 if (N->getOpcode() == X86ISD::Wrapper) {
10650 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010651 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010652 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010653 return true;
10654 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010655 }
Evan Chengad4196b2008-05-12 19:56:52 +000010656 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010657}
10658
Evan Cheng206ee9d2006-07-07 08:33:52 +000010659/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10660/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10661/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010662/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010663static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010664 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010665 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010666 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010667
Eli Friedman7a5e5552009-06-07 06:52:44 +000010668 if (VT.getSizeInBits() != 128)
10669 return SDValue();
10670
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010671 // Don't create instructions with illegal types after legalize types has run.
10672 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10673 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10674 return SDValue();
10675
Nate Begemanfdea31a2010-03-24 20:49:50 +000010676 SmallVector<SDValue, 16> Elts;
10677 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010678 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010679
Nate Begemanfdea31a2010-03-24 20:49:50 +000010680 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010681}
Evan Chengd880b972008-05-09 21:53:03 +000010682
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010683/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10684/// generation and convert it from being a bunch of shuffles and extracts
10685/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010686static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10687 const TargetLowering &TLI) {
10688 SDValue InputVector = N->getOperand(0);
10689
10690 // Only operate on vectors of 4 elements, where the alternative shuffling
10691 // gets to be more expensive.
10692 if (InputVector.getValueType() != MVT::v4i32)
10693 return SDValue();
10694
10695 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10696 // single use which is a sign-extend or zero-extend, and all elements are
10697 // used.
10698 SmallVector<SDNode *, 4> Uses;
10699 unsigned ExtractedElements = 0;
10700 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10701 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10702 if (UI.getUse().getResNo() != InputVector.getResNo())
10703 return SDValue();
10704
10705 SDNode *Extract = *UI;
10706 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10707 return SDValue();
10708
10709 if (Extract->getValueType(0) != MVT::i32)
10710 return SDValue();
10711 if (!Extract->hasOneUse())
10712 return SDValue();
10713 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10714 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10715 return SDValue();
10716 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10717 return SDValue();
10718
10719 // Record which element was extracted.
10720 ExtractedElements |=
10721 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10722
10723 Uses.push_back(Extract);
10724 }
10725
10726 // If not all the elements were used, this may not be worthwhile.
10727 if (ExtractedElements != 15)
10728 return SDValue();
10729
10730 // Ok, we've now decided to do the transformation.
10731 DebugLoc dl = InputVector.getDebugLoc();
10732
10733 // Store the value to a temporary stack slot.
10734 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010735 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10736 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010737
10738 // Replace each use (extract) with a load of the appropriate element.
10739 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10740 UE = Uses.end(); UI != UE; ++UI) {
10741 SDNode *Extract = *UI;
10742
10743 // Compute the element's address.
10744 SDValue Idx = Extract->getOperand(1);
10745 unsigned EltSize =
10746 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10747 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10748 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10749
Eric Christopher90eb4022010-07-22 00:26:08 +000010750 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010751 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010752
10753 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010754 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010755 ScalarAddr, MachinePointerInfo(),
10756 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010757
10758 // Replace the exact with the load.
10759 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10760 }
10761
10762 // The replacement was made in place; don't return anything.
10763 return SDValue();
10764}
10765
Chris Lattner83e6c992006-10-04 06:57:07 +000010766/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010767static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010768 const X86Subtarget *Subtarget) {
10769 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010770 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010771 // Get the LHS/RHS of the select.
10772 SDValue LHS = N->getOperand(1);
10773 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010774
Dan Gohman670e5392009-09-21 18:03:22 +000010775 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010776 // instructions match the semantics of the common C idiom x<y?x:y but not
10777 // x<=y?x:y, because of how they handle negative zero (which can be
10778 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010779 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010780 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010781 Cond.getOpcode() == ISD::SETCC) {
10782 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010783
Chris Lattner47b4ce82009-03-11 05:48:52 +000010784 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010785 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010786 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10787 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010788 switch (CC) {
10789 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010790 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010791 // Converting this to a min would handle NaNs incorrectly, and swapping
10792 // the operands would cause it to handle comparisons between positive
10793 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010794 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010795 if (!UnsafeFPMath &&
10796 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10797 break;
10798 std::swap(LHS, RHS);
10799 }
Dan Gohman670e5392009-09-21 18:03:22 +000010800 Opcode = X86ISD::FMIN;
10801 break;
10802 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010803 // Converting this to a min would handle comparisons between positive
10804 // and negative zero incorrectly.
10805 if (!UnsafeFPMath &&
10806 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10807 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010808 Opcode = X86ISD::FMIN;
10809 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010810 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010811 // Converting this to a min would handle both negative zeros and NaNs
10812 // incorrectly, but we can swap the operands to fix both.
10813 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010814 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010815 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010816 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010817 Opcode = X86ISD::FMIN;
10818 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010819
Dan Gohman670e5392009-09-21 18:03:22 +000010820 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010821 // Converting this to a max would handle comparisons between positive
10822 // and negative zero incorrectly.
10823 if (!UnsafeFPMath &&
10824 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10825 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010826 Opcode = X86ISD::FMAX;
10827 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010828 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010829 // Converting this to a max would handle NaNs incorrectly, and swapping
10830 // the operands would cause it to handle comparisons between positive
10831 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010832 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010833 if (!UnsafeFPMath &&
10834 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10835 break;
10836 std::swap(LHS, RHS);
10837 }
Dan Gohman670e5392009-09-21 18:03:22 +000010838 Opcode = X86ISD::FMAX;
10839 break;
10840 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010841 // Converting this to a max would handle both negative zeros and NaNs
10842 // incorrectly, but we can swap the operands to fix both.
10843 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010844 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010845 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010846 case ISD::SETGE:
10847 Opcode = X86ISD::FMAX;
10848 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010849 }
Dan Gohman670e5392009-09-21 18:03:22 +000010850 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010851 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10852 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010853 switch (CC) {
10854 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010855 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010856 // Converting this to a min would handle comparisons between positive
10857 // and negative zero incorrectly, and swapping the operands would
10858 // cause it to handle NaNs incorrectly.
10859 if (!UnsafeFPMath &&
10860 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010861 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010862 break;
10863 std::swap(LHS, RHS);
10864 }
Dan Gohman670e5392009-09-21 18:03:22 +000010865 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010866 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010867 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010868 // Converting this to a min would handle NaNs incorrectly.
10869 if (!UnsafeFPMath &&
10870 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10871 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010872 Opcode = X86ISD::FMIN;
10873 break;
10874 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010875 // Converting this to a min would handle both negative zeros and NaNs
10876 // incorrectly, but we can swap the operands to fix both.
10877 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010878 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010879 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010880 case ISD::SETGE:
10881 Opcode = X86ISD::FMIN;
10882 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010883
Dan Gohman670e5392009-09-21 18:03:22 +000010884 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010885 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010886 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010887 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010888 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010889 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010890 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010891 // Converting this to a max would handle comparisons between positive
10892 // and negative zero incorrectly, and swapping the operands would
10893 // cause it to handle NaNs incorrectly.
10894 if (!UnsafeFPMath &&
10895 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010896 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010897 break;
10898 std::swap(LHS, RHS);
10899 }
Dan Gohman670e5392009-09-21 18:03:22 +000010900 Opcode = X86ISD::FMAX;
10901 break;
10902 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010903 // Converting this to a max would handle both negative zeros and NaNs
10904 // incorrectly, but we can swap the operands to fix both.
10905 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010906 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010907 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010908 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010909 Opcode = X86ISD::FMAX;
10910 break;
10911 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010912 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010913
Chris Lattner47b4ce82009-03-11 05:48:52 +000010914 if (Opcode)
10915 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010916 }
Eric Christopherfd179292009-08-27 18:07:15 +000010917
Chris Lattnerd1980a52009-03-12 06:52:53 +000010918 // If this is a select between two integer constants, try to do some
10919 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010920 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10921 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010922 // Don't do this for crazy integer types.
10923 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10924 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010925 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010926 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010927
Chris Lattnercee56e72009-03-13 05:53:31 +000010928 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010929 // Efficiently invertible.
10930 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10931 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10932 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10933 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010934 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010935 }
Eric Christopherfd179292009-08-27 18:07:15 +000010936
Chris Lattnerd1980a52009-03-12 06:52:53 +000010937 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010938 if (FalseC->getAPIntValue() == 0 &&
10939 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010940 if (NeedsCondInvert) // Invert the condition if needed.
10941 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10942 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010943
Chris Lattnerd1980a52009-03-12 06:52:53 +000010944 // Zero extend the condition if needed.
10945 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010946
Chris Lattnercee56e72009-03-13 05:53:31 +000010947 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010948 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010949 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010950 }
Eric Christopherfd179292009-08-27 18:07:15 +000010951
Chris Lattner97a29a52009-03-13 05:22:11 +000010952 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010953 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010954 if (NeedsCondInvert) // Invert the condition if needed.
10955 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10956 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010957
Chris Lattner97a29a52009-03-13 05:22:11 +000010958 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010959 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10960 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010961 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010962 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010963 }
Eric Christopherfd179292009-08-27 18:07:15 +000010964
Chris Lattnercee56e72009-03-13 05:53:31 +000010965 // Optimize cases that will turn into an LEA instruction. This requires
10966 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010967 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010968 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010969 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010970
Chris Lattnercee56e72009-03-13 05:53:31 +000010971 bool isFastMultiplier = false;
10972 if (Diff < 10) {
10973 switch ((unsigned char)Diff) {
10974 default: break;
10975 case 1: // result = add base, cond
10976 case 2: // result = lea base( , cond*2)
10977 case 3: // result = lea base(cond, cond*2)
10978 case 4: // result = lea base( , cond*4)
10979 case 5: // result = lea base(cond, cond*4)
10980 case 8: // result = lea base( , cond*8)
10981 case 9: // result = lea base(cond, cond*8)
10982 isFastMultiplier = true;
10983 break;
10984 }
10985 }
Eric Christopherfd179292009-08-27 18:07:15 +000010986
Chris Lattnercee56e72009-03-13 05:53:31 +000010987 if (isFastMultiplier) {
10988 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10989 if (NeedsCondInvert) // Invert the condition if needed.
10990 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10991 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010992
Chris Lattnercee56e72009-03-13 05:53:31 +000010993 // Zero extend the condition if needed.
10994 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10995 Cond);
10996 // Scale the condition by the difference.
10997 if (Diff != 1)
10998 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10999 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011000
Chris Lattnercee56e72009-03-13 05:53:31 +000011001 // Add the base if non-zero.
11002 if (FalseC->getAPIntValue() != 0)
11003 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11004 SDValue(FalseC, 0));
11005 return Cond;
11006 }
Eric Christopherfd179292009-08-27 18:07:15 +000011007 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011008 }
11009 }
Eric Christopherfd179292009-08-27 18:07:15 +000011010
Dan Gohman475871a2008-07-27 21:46:04 +000011011 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011012}
11013
Chris Lattnerd1980a52009-03-12 06:52:53 +000011014/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11015static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11016 TargetLowering::DAGCombinerInfo &DCI) {
11017 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011018
Chris Lattnerd1980a52009-03-12 06:52:53 +000011019 // If the flag operand isn't dead, don't touch this CMOV.
11020 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11021 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011022
Chris Lattnerd1980a52009-03-12 06:52:53 +000011023 // If this is a select between two integer constants, try to do some
11024 // optimizations. Note that the operands are ordered the opposite of SELECT
11025 // operands.
11026 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11027 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11028 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11029 // larger than FalseC (the false value).
11030 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011031
Chris Lattnerd1980a52009-03-12 06:52:53 +000011032 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11033 CC = X86::GetOppositeBranchCondition(CC);
11034 std::swap(TrueC, FalseC);
11035 }
Eric Christopherfd179292009-08-27 18:07:15 +000011036
Chris Lattnerd1980a52009-03-12 06:52:53 +000011037 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011038 // This is efficient for any integer data type (including i8/i16) and
11039 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011040 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11041 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011042 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11043 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011044
Chris Lattnerd1980a52009-03-12 06:52:53 +000011045 // Zero extend the condition if needed.
11046 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011047
Chris Lattnerd1980a52009-03-12 06:52:53 +000011048 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11049 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011050 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011051 if (N->getNumValues() == 2) // Dead flag value?
11052 return DCI.CombineTo(N, Cond, SDValue());
11053 return Cond;
11054 }
Eric Christopherfd179292009-08-27 18:07:15 +000011055
Chris Lattnercee56e72009-03-13 05:53:31 +000011056 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11057 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011058 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11059 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011060 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11061 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011062
Chris Lattner97a29a52009-03-13 05:22:11 +000011063 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011064 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11065 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011066 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11067 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011068
Chris Lattner97a29a52009-03-13 05:22:11 +000011069 if (N->getNumValues() == 2) // Dead flag value?
11070 return DCI.CombineTo(N, Cond, SDValue());
11071 return Cond;
11072 }
Eric Christopherfd179292009-08-27 18:07:15 +000011073
Chris Lattnercee56e72009-03-13 05:53:31 +000011074 // Optimize cases that will turn into an LEA instruction. This requires
11075 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011076 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011077 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011078 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011079
Chris Lattnercee56e72009-03-13 05:53:31 +000011080 bool isFastMultiplier = false;
11081 if (Diff < 10) {
11082 switch ((unsigned char)Diff) {
11083 default: break;
11084 case 1: // result = add base, cond
11085 case 2: // result = lea base( , cond*2)
11086 case 3: // result = lea base(cond, cond*2)
11087 case 4: // result = lea base( , cond*4)
11088 case 5: // result = lea base(cond, cond*4)
11089 case 8: // result = lea base( , cond*8)
11090 case 9: // result = lea base(cond, cond*8)
11091 isFastMultiplier = true;
11092 break;
11093 }
11094 }
Eric Christopherfd179292009-08-27 18:07:15 +000011095
Chris Lattnercee56e72009-03-13 05:53:31 +000011096 if (isFastMultiplier) {
11097 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11098 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011099 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11100 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011101 // Zero extend the condition if needed.
11102 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11103 Cond);
11104 // Scale the condition by the difference.
11105 if (Diff != 1)
11106 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11107 DAG.getConstant(Diff, Cond.getValueType()));
11108
11109 // Add the base if non-zero.
11110 if (FalseC->getAPIntValue() != 0)
11111 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11112 SDValue(FalseC, 0));
11113 if (N->getNumValues() == 2) // Dead flag value?
11114 return DCI.CombineTo(N, Cond, SDValue());
11115 return Cond;
11116 }
Eric Christopherfd179292009-08-27 18:07:15 +000011117 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011118 }
11119 }
11120 return SDValue();
11121}
11122
11123
Evan Cheng0b0cd912009-03-28 05:57:29 +000011124/// PerformMulCombine - Optimize a single multiply with constant into two
11125/// in order to implement it with two cheaper instructions, e.g.
11126/// LEA + SHL, LEA + LEA.
11127static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11128 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011129 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11130 return SDValue();
11131
Owen Andersone50ed302009-08-10 22:56:29 +000011132 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011133 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011134 return SDValue();
11135
11136 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11137 if (!C)
11138 return SDValue();
11139 uint64_t MulAmt = C->getZExtValue();
11140 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11141 return SDValue();
11142
11143 uint64_t MulAmt1 = 0;
11144 uint64_t MulAmt2 = 0;
11145 if ((MulAmt % 9) == 0) {
11146 MulAmt1 = 9;
11147 MulAmt2 = MulAmt / 9;
11148 } else if ((MulAmt % 5) == 0) {
11149 MulAmt1 = 5;
11150 MulAmt2 = MulAmt / 5;
11151 } else if ((MulAmt % 3) == 0) {
11152 MulAmt1 = 3;
11153 MulAmt2 = MulAmt / 3;
11154 }
11155 if (MulAmt2 &&
11156 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11157 DebugLoc DL = N->getDebugLoc();
11158
11159 if (isPowerOf2_64(MulAmt2) &&
11160 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11161 // If second multiplifer is pow2, issue it first. We want the multiply by
11162 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11163 // is an add.
11164 std::swap(MulAmt1, MulAmt2);
11165
11166 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011167 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011168 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011169 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011170 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011171 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011172 DAG.getConstant(MulAmt1, VT));
11173
Eric Christopherfd179292009-08-27 18:07:15 +000011174 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011175 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011176 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011177 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011178 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011179 DAG.getConstant(MulAmt2, VT));
11180
11181 // Do not add new nodes to DAG combiner worklist.
11182 DCI.CombineTo(N, NewMul, false);
11183 }
11184 return SDValue();
11185}
11186
Evan Chengad9c0a32009-12-15 00:53:42 +000011187static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11188 SDValue N0 = N->getOperand(0);
11189 SDValue N1 = N->getOperand(1);
11190 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11191 EVT VT = N0.getValueType();
11192
11193 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11194 // since the result of setcc_c is all zero's or all ones.
11195 if (N1C && N0.getOpcode() == ISD::AND &&
11196 N0.getOperand(1).getOpcode() == ISD::Constant) {
11197 SDValue N00 = N0.getOperand(0);
11198 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11199 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11200 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11201 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11202 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11203 APInt ShAmt = N1C->getAPIntValue();
11204 Mask = Mask.shl(ShAmt);
11205 if (Mask != 0)
11206 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11207 N00, DAG.getConstant(Mask, VT));
11208 }
11209 }
11210
11211 return SDValue();
11212}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011213
Nate Begeman740ab032009-01-26 00:52:55 +000011214/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11215/// when possible.
11216static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11217 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011218 EVT VT = N->getValueType(0);
11219 if (!VT.isVector() && VT.isInteger() &&
11220 N->getOpcode() == ISD::SHL)
11221 return PerformSHLCombine(N, DAG);
11222
Nate Begeman740ab032009-01-26 00:52:55 +000011223 // On X86 with SSE2 support, we can transform this to a vector shift if
11224 // all elements are shifted by the same amount. We can't do this in legalize
11225 // because the a constant vector is typically transformed to a constant pool
11226 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011227 if (!Subtarget->hasSSE2())
11228 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011229
Owen Anderson825b72b2009-08-11 20:47:22 +000011230 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011231 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011232
Mon P Wang3becd092009-01-28 08:12:05 +000011233 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011234 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011235 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011236 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011237 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11238 unsigned NumElts = VT.getVectorNumElements();
11239 unsigned i = 0;
11240 for (; i != NumElts; ++i) {
11241 SDValue Arg = ShAmtOp.getOperand(i);
11242 if (Arg.getOpcode() == ISD::UNDEF) continue;
11243 BaseShAmt = Arg;
11244 break;
11245 }
11246 for (; i != NumElts; ++i) {
11247 SDValue Arg = ShAmtOp.getOperand(i);
11248 if (Arg.getOpcode() == ISD::UNDEF) continue;
11249 if (Arg != BaseShAmt) {
11250 return SDValue();
11251 }
11252 }
11253 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011254 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011255 SDValue InVec = ShAmtOp.getOperand(0);
11256 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11257 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11258 unsigned i = 0;
11259 for (; i != NumElts; ++i) {
11260 SDValue Arg = InVec.getOperand(i);
11261 if (Arg.getOpcode() == ISD::UNDEF) continue;
11262 BaseShAmt = Arg;
11263 break;
11264 }
11265 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11266 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011267 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011268 if (C->getZExtValue() == SplatIdx)
11269 BaseShAmt = InVec.getOperand(1);
11270 }
11271 }
11272 if (BaseShAmt.getNode() == 0)
11273 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11274 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011275 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011276 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011277
Mon P Wangefa42202009-09-03 19:56:25 +000011278 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011279 if (EltVT.bitsGT(MVT::i32))
11280 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11281 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011282 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011283
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011284 // The shift amount is identical so we can do a vector shift.
11285 SDValue ValOp = N->getOperand(0);
11286 switch (N->getOpcode()) {
11287 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011288 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011289 break;
11290 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011291 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011292 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011293 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011294 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011295 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011296 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011297 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011298 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011299 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011300 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011301 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011302 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011303 break;
11304 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011305 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011306 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011307 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011308 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011309 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011310 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011311 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011312 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011313 break;
11314 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011315 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011316 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011317 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011318 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011319 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011320 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011321 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011322 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011323 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011324 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011325 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011326 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011327 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011328 }
11329 return SDValue();
11330}
11331
Nate Begemanb65c1752010-12-17 22:55:37 +000011332
11333static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11334 TargetLowering::DAGCombinerInfo &DCI,
11335 const X86Subtarget *Subtarget) {
11336 if (DCI.isBeforeLegalizeOps())
11337 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011338
Nate Begemanb65c1752010-12-17 22:55:37 +000011339 // Want to form PANDN nodes, in the hopes of then easily combining them with
11340 // OR and AND nodes to form PBLEND/PSIGN.
11341 EVT VT = N->getValueType(0);
11342 if (VT != MVT::v2i64)
11343 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011344
Nate Begemanb65c1752010-12-17 22:55:37 +000011345 SDValue N0 = N->getOperand(0);
11346 SDValue N1 = N->getOperand(1);
11347 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011348
Nate Begemanb65c1752010-12-17 22:55:37 +000011349 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011350 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011351 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11352 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11353
11354 // Check RHS for vnot
11355 if (N1.getOpcode() == ISD::XOR &&
11356 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11357 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011358
Nate Begemanb65c1752010-12-17 22:55:37 +000011359 return SDValue();
11360}
11361
Evan Cheng760d1942010-01-04 21:22:48 +000011362static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011363 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011364 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011365 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011366 return SDValue();
11367
Evan Cheng760d1942010-01-04 21:22:48 +000011368 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011369 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011370 return SDValue();
11371
Evan Cheng760d1942010-01-04 21:22:48 +000011372 SDValue N0 = N->getOperand(0);
11373 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011374
Nate Begemanb65c1752010-12-17 22:55:37 +000011375 // look for psign/blend
11376 if (Subtarget->hasSSSE3()) {
11377 if (VT == MVT::v2i64) {
11378 // Canonicalize pandn to RHS
11379 if (N0.getOpcode() == X86ISD::PANDN)
11380 std::swap(N0, N1);
11381 // or (and (m, x), (pandn m, y))
11382 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11383 SDValue Mask = N1.getOperand(0);
11384 SDValue X = N1.getOperand(1);
11385 SDValue Y;
11386 if (N0.getOperand(0) == Mask)
11387 Y = N0.getOperand(1);
11388 if (N0.getOperand(1) == Mask)
11389 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011390
Nate Begemanb65c1752010-12-17 22:55:37 +000011391 // Check to see if the mask appeared in both the AND and PANDN and
11392 if (!Y.getNode())
11393 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011394
Nate Begemanb65c1752010-12-17 22:55:37 +000011395 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11396 if (Mask.getOpcode() != ISD::BITCAST ||
11397 X.getOpcode() != ISD::BITCAST ||
11398 Y.getOpcode() != ISD::BITCAST)
11399 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011400
Nate Begemanb65c1752010-12-17 22:55:37 +000011401 // Look through mask bitcast.
11402 Mask = Mask.getOperand(0);
11403 EVT MaskVT = Mask.getValueType();
11404
11405 // Validate that the Mask operand is a vector sra node. The sra node
11406 // will be an intrinsic.
11407 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11408 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011409
Nate Begemanb65c1752010-12-17 22:55:37 +000011410 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11411 // there is no psrai.b
11412 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11413 case Intrinsic::x86_sse2_psrai_w:
11414 case Intrinsic::x86_sse2_psrai_d:
11415 break;
11416 default: return SDValue();
11417 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011418
Nate Begemanb65c1752010-12-17 22:55:37 +000011419 // Check that the SRA is all signbits.
11420 SDValue SraC = Mask.getOperand(2);
11421 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11422 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11423 if ((SraAmt + 1) != EltBits)
11424 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011425
Nate Begemanb65c1752010-12-17 22:55:37 +000011426 DebugLoc DL = N->getDebugLoc();
11427
11428 // Now we know we at least have a plendvb with the mask val. See if
11429 // we can form a psignb/w/d.
11430 // psign = x.type == y.type == mask.type && y = sub(0, x);
11431 X = X.getOperand(0);
11432 Y = Y.getOperand(0);
11433 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11434 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11435 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11436 unsigned Opc = 0;
11437 switch (EltBits) {
11438 case 8: Opc = X86ISD::PSIGNB; break;
11439 case 16: Opc = X86ISD::PSIGNW; break;
11440 case 32: Opc = X86ISD::PSIGND; break;
11441 default: break;
11442 }
11443 if (Opc) {
11444 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11445 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11446 }
11447 }
11448 // PBLENDVB only available on SSE 4.1
11449 if (!Subtarget->hasSSE41())
11450 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011451
Nate Begemanb65c1752010-12-17 22:55:37 +000011452 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11453 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11454 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011455 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011456 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11457 }
11458 }
11459 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011460
Nate Begemanb65c1752010-12-17 22:55:37 +000011461 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011462 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11463 std::swap(N0, N1);
11464 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11465 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011466 if (!N0.hasOneUse() || !N1.hasOneUse())
11467 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011468
11469 SDValue ShAmt0 = N0.getOperand(1);
11470 if (ShAmt0.getValueType() != MVT::i8)
11471 return SDValue();
11472 SDValue ShAmt1 = N1.getOperand(1);
11473 if (ShAmt1.getValueType() != MVT::i8)
11474 return SDValue();
11475 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11476 ShAmt0 = ShAmt0.getOperand(0);
11477 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11478 ShAmt1 = ShAmt1.getOperand(0);
11479
11480 DebugLoc DL = N->getDebugLoc();
11481 unsigned Opc = X86ISD::SHLD;
11482 SDValue Op0 = N0.getOperand(0);
11483 SDValue Op1 = N1.getOperand(0);
11484 if (ShAmt0.getOpcode() == ISD::SUB) {
11485 Opc = X86ISD::SHRD;
11486 std::swap(Op0, Op1);
11487 std::swap(ShAmt0, ShAmt1);
11488 }
11489
Evan Cheng8b1190a2010-04-28 01:18:01 +000011490 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011491 if (ShAmt1.getOpcode() == ISD::SUB) {
11492 SDValue Sum = ShAmt1.getOperand(0);
11493 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011494 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11495 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11496 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11497 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011498 return DAG.getNode(Opc, DL, VT,
11499 Op0, Op1,
11500 DAG.getNode(ISD::TRUNCATE, DL,
11501 MVT::i8, ShAmt0));
11502 }
11503 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11504 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11505 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011506 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011507 return DAG.getNode(Opc, DL, VT,
11508 N0.getOperand(0), N1.getOperand(0),
11509 DAG.getNode(ISD::TRUNCATE, DL,
11510 MVT::i8, ShAmt0));
11511 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011512
Evan Cheng760d1942010-01-04 21:22:48 +000011513 return SDValue();
11514}
11515
Chris Lattner149a4e52008-02-22 02:09:43 +000011516/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011517static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011518 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011519 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11520 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011521 // A preferable solution to the general problem is to figure out the right
11522 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011523
11524 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011525 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011526 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011527 if (VT.getSizeInBits() != 64)
11528 return SDValue();
11529
Devang Patel578efa92009-06-05 21:57:13 +000011530 const Function *F = DAG.getMachineFunction().getFunction();
11531 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011532 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011533 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011534 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011535 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011536 isa<LoadSDNode>(St->getValue()) &&
11537 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11538 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011539 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011540 LoadSDNode *Ld = 0;
11541 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011542 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011543 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011544 // Must be a store of a load. We currently handle two cases: the load
11545 // is a direct child, and it's under an intervening TokenFactor. It is
11546 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011547 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011548 Ld = cast<LoadSDNode>(St->getChain());
11549 else if (St->getValue().hasOneUse() &&
11550 ChainVal->getOpcode() == ISD::TokenFactor) {
11551 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011552 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011553 TokenFactorIndex = i;
11554 Ld = cast<LoadSDNode>(St->getValue());
11555 } else
11556 Ops.push_back(ChainVal->getOperand(i));
11557 }
11558 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011559
Evan Cheng536e6672009-03-12 05:59:15 +000011560 if (!Ld || !ISD::isNormalLoad(Ld))
11561 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011562
Evan Cheng536e6672009-03-12 05:59:15 +000011563 // If this is not the MMX case, i.e. we are just turning i64 load/store
11564 // into f64 load/store, avoid the transformation if there are multiple
11565 // uses of the loaded value.
11566 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11567 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011568
Evan Cheng536e6672009-03-12 05:59:15 +000011569 DebugLoc LdDL = Ld->getDebugLoc();
11570 DebugLoc StDL = N->getDebugLoc();
11571 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11572 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11573 // pair instead.
11574 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011575 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011576 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11577 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011578 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011579 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011580 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011581 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011582 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011583 Ops.size());
11584 }
Evan Cheng536e6672009-03-12 05:59:15 +000011585 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011586 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011587 St->isVolatile(), St->isNonTemporal(),
11588 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011589 }
Evan Cheng536e6672009-03-12 05:59:15 +000011590
11591 // Otherwise, lower to two pairs of 32-bit loads / stores.
11592 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011593 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11594 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011595
Owen Anderson825b72b2009-08-11 20:47:22 +000011596 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011597 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011598 Ld->isVolatile(), Ld->isNonTemporal(),
11599 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011600 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011601 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011602 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011603 MinAlign(Ld->getAlignment(), 4));
11604
11605 SDValue NewChain = LoLd.getValue(1);
11606 if (TokenFactorIndex != -1) {
11607 Ops.push_back(LoLd);
11608 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011609 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011610 Ops.size());
11611 }
11612
11613 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011614 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11615 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011616
11617 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011618 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011619 St->isVolatile(), St->isNonTemporal(),
11620 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011621 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011622 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011623 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011624 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011625 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011626 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011627 }
Dan Gohman475871a2008-07-27 21:46:04 +000011628 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011629}
11630
Chris Lattner6cf73262008-01-25 06:14:17 +000011631/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11632/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011633static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011634 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11635 // F[X]OR(0.0, x) -> x
11636 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011637 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11638 if (C->getValueAPF().isPosZero())
11639 return N->getOperand(1);
11640 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11641 if (C->getValueAPF().isPosZero())
11642 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011643 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011644}
11645
11646/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011647static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011648 // FAND(0.0, x) -> 0.0
11649 // FAND(x, 0.0) -> 0.0
11650 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11651 if (C->getValueAPF().isPosZero())
11652 return N->getOperand(0);
11653 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11654 if (C->getValueAPF().isPosZero())
11655 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011656 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011657}
11658
Dan Gohmane5af2d32009-01-29 01:59:02 +000011659static SDValue PerformBTCombine(SDNode *N,
11660 SelectionDAG &DAG,
11661 TargetLowering::DAGCombinerInfo &DCI) {
11662 // BT ignores high bits in the bit index operand.
11663 SDValue Op1 = N->getOperand(1);
11664 if (Op1.hasOneUse()) {
11665 unsigned BitWidth = Op1.getValueSizeInBits();
11666 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11667 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011668 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11669 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011670 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011671 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11672 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11673 DCI.CommitTargetLoweringOpt(TLO);
11674 }
11675 return SDValue();
11676}
Chris Lattner83e6c992006-10-04 06:57:07 +000011677
Eli Friedman7a5e5552009-06-07 06:52:44 +000011678static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11679 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011680 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011681 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011682 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011683 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011684 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011685 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011686 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011687 }
11688 return SDValue();
11689}
11690
Evan Cheng2e489c42009-12-16 00:53:11 +000011691static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11692 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11693 // (and (i32 x86isd::setcc_carry), 1)
11694 // This eliminates the zext. This transformation is necessary because
11695 // ISD::SETCC is always legalized to i8.
11696 DebugLoc dl = N->getDebugLoc();
11697 SDValue N0 = N->getOperand(0);
11698 EVT VT = N->getValueType(0);
11699 if (N0.getOpcode() == ISD::AND &&
11700 N0.hasOneUse() &&
11701 N0.getOperand(0).hasOneUse()) {
11702 SDValue N00 = N0.getOperand(0);
11703 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11704 return SDValue();
11705 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11706 if (!C || C->getZExtValue() != 1)
11707 return SDValue();
11708 return DAG.getNode(ISD::AND, dl, VT,
11709 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11710 N00.getOperand(0), N00.getOperand(1)),
11711 DAG.getConstant(1, VT));
11712 }
11713
11714 return SDValue();
11715}
11716
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011717// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11718static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11719 unsigned X86CC = N->getConstantOperandVal(0);
11720 SDValue EFLAG = N->getOperand(1);
11721 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011722
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011723 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11724 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11725 // cases.
11726 if (X86CC == X86::COND_B)
11727 return DAG.getNode(ISD::AND, DL, MVT::i8,
11728 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11729 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11730 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011731
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011732 return SDValue();
11733}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011734
Chris Lattner23a01992010-12-20 01:37:09 +000011735// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11736static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11737 X86TargetLowering::DAGCombinerInfo &DCI) {
11738 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11739 // the result is either zero or one (depending on the input carry bit).
11740 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11741 if (X86::isZeroNode(N->getOperand(0)) &&
11742 X86::isZeroNode(N->getOperand(1)) &&
11743 // We don't have a good way to replace an EFLAGS use, so only do this when
11744 // dead right now.
11745 SDValue(N, 1).use_empty()) {
11746 DebugLoc DL = N->getDebugLoc();
11747 EVT VT = N->getValueType(0);
11748 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11749 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11750 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11751 DAG.getConstant(X86::COND_B,MVT::i8),
11752 N->getOperand(2)),
11753 DAG.getConstant(1, VT));
11754 return DCI.CombineTo(N, Res1, CarryOut);
11755 }
11756
11757 return SDValue();
11758}
11759
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011760// fold (add Y, (sete X, 0)) -> adc 0, Y
11761// (add Y, (setne X, 0)) -> sbb -1, Y
11762// (sub (sete X, 0), Y) -> sbb 0, Y
11763// (sub (setne X, 0), Y) -> adc -1, Y
11764static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11765 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011766
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011767 // Look through ZExts.
11768 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11769 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11770 return SDValue();
11771
11772 SDValue SetCC = Ext.getOperand(0);
11773 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11774 return SDValue();
11775
11776 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11777 if (CC != X86::COND_E && CC != X86::COND_NE)
11778 return SDValue();
11779
11780 SDValue Cmp = SetCC.getOperand(1);
11781 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011782 !X86::isZeroNode(Cmp.getOperand(1)) ||
11783 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011784 return SDValue();
11785
11786 SDValue CmpOp0 = Cmp.getOperand(0);
11787 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11788 DAG.getConstant(1, CmpOp0.getValueType()));
11789
11790 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11791 if (CC == X86::COND_NE)
11792 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11793 DL, OtherVal.getValueType(), OtherVal,
11794 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11795 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11796 DL, OtherVal.getValueType(), OtherVal,
11797 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11798}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011799
Dan Gohman475871a2008-07-27 21:46:04 +000011800SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011801 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011802 SelectionDAG &DAG = DCI.DAG;
11803 switch (N->getOpcode()) {
11804 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011805 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011806 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011807 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011808 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011809 case ISD::ADD:
11810 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011811 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011812 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011813 case ISD::SHL:
11814 case ISD::SRA:
11815 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011816 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011817 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011818 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011819 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011820 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11821 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011822 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011823 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011824 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011825 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011826 case X86ISD::SHUFPS: // Handle all target specific shuffles
11827 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011828 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011829 case X86ISD::PUNPCKHBW:
11830 case X86ISD::PUNPCKHWD:
11831 case X86ISD::PUNPCKHDQ:
11832 case X86ISD::PUNPCKHQDQ:
11833 case X86ISD::UNPCKHPS:
11834 case X86ISD::UNPCKHPD:
11835 case X86ISD::PUNPCKLBW:
11836 case X86ISD::PUNPCKLWD:
11837 case X86ISD::PUNPCKLDQ:
11838 case X86ISD::PUNPCKLQDQ:
11839 case X86ISD::UNPCKLPS:
11840 case X86ISD::UNPCKLPD:
11841 case X86ISD::MOVHLPS:
11842 case X86ISD::MOVLHPS:
11843 case X86ISD::PSHUFD:
11844 case X86ISD::PSHUFHW:
11845 case X86ISD::PSHUFLW:
11846 case X86ISD::MOVSS:
11847 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011848 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011849 }
11850
Dan Gohman475871a2008-07-27 21:46:04 +000011851 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011852}
11853
Evan Chenge5b51ac2010-04-17 06:13:15 +000011854/// isTypeDesirableForOp - Return true if the target has native support for
11855/// the specified value type and it is 'desirable' to use the type for the
11856/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11857/// instruction encodings are longer and some i16 instructions are slow.
11858bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11859 if (!isTypeLegal(VT))
11860 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011861 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011862 return true;
11863
11864 switch (Opc) {
11865 default:
11866 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011867 case ISD::LOAD:
11868 case ISD::SIGN_EXTEND:
11869 case ISD::ZERO_EXTEND:
11870 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011871 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011872 case ISD::SRL:
11873 case ISD::SUB:
11874 case ISD::ADD:
11875 case ISD::MUL:
11876 case ISD::AND:
11877 case ISD::OR:
11878 case ISD::XOR:
11879 return false;
11880 }
11881}
11882
11883/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011884/// beneficial for dag combiner to promote the specified node. If true, it
11885/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011886bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011887 EVT VT = Op.getValueType();
11888 if (VT != MVT::i16)
11889 return false;
11890
Evan Cheng4c26e932010-04-19 19:29:22 +000011891 bool Promote = false;
11892 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011893 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011894 default: break;
11895 case ISD::LOAD: {
11896 LoadSDNode *LD = cast<LoadSDNode>(Op);
11897 // If the non-extending load has a single use and it's not live out, then it
11898 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011899 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11900 Op.hasOneUse()*/) {
11901 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11902 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11903 // The only case where we'd want to promote LOAD (rather then it being
11904 // promoted as an operand is when it's only use is liveout.
11905 if (UI->getOpcode() != ISD::CopyToReg)
11906 return false;
11907 }
11908 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011909 Promote = true;
11910 break;
11911 }
11912 case ISD::SIGN_EXTEND:
11913 case ISD::ZERO_EXTEND:
11914 case ISD::ANY_EXTEND:
11915 Promote = true;
11916 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011917 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011918 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011919 SDValue N0 = Op.getOperand(0);
11920 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011921 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011922 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011923 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011924 break;
11925 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011926 case ISD::ADD:
11927 case ISD::MUL:
11928 case ISD::AND:
11929 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011930 case ISD::XOR:
11931 Commute = true;
11932 // fallthrough
11933 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011934 SDValue N0 = Op.getOperand(0);
11935 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011936 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011937 return false;
11938 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011939 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011940 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011941 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011942 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011943 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011944 }
11945 }
11946
11947 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011948 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011949}
11950
Evan Cheng60c07e12006-07-05 22:17:51 +000011951//===----------------------------------------------------------------------===//
11952// X86 Inline Assembly Support
11953//===----------------------------------------------------------------------===//
11954
Chris Lattnerb8105652009-07-20 17:51:36 +000011955bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11956 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000011957
11958 std::string AsmStr = IA->getAsmString();
11959
11960 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011961 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011962 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011963
11964 switch (AsmPieces.size()) {
11965 default: return false;
11966 case 1:
11967 AsmStr = AsmPieces[0];
11968 AsmPieces.clear();
11969 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11970
Evan Cheng55d42002011-01-08 01:24:27 +000011971 // FIXME: this should verify that we are targetting a 486 or better. If not,
11972 // we will turn this bswap into something that will be lowered to logical ops
11973 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11974 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000011975 // bswap $0
11976 if (AsmPieces.size() == 2 &&
11977 (AsmPieces[0] == "bswap" ||
11978 AsmPieces[0] == "bswapq" ||
11979 AsmPieces[0] == "bswapl") &&
11980 (AsmPieces[1] == "$0" ||
11981 AsmPieces[1] == "${0:q}")) {
11982 // No need to check constraints, nothing other than the equivalent of
11983 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000011984 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11985 if (!Ty || Ty->getBitWidth() % 16 != 0)
11986 return false;
11987 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000011988 }
11989 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011990 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011991 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011992 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011993 AsmPieces[1] == "$$8," &&
11994 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011995 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11996 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011997 const std::string &ConstraintsStr = IA->getConstraintString();
11998 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011999 std::sort(AsmPieces.begin(), AsmPieces.end());
12000 if (AsmPieces.size() == 4 &&
12001 AsmPieces[0] == "~{cc}" &&
12002 AsmPieces[1] == "~{dirflag}" &&
12003 AsmPieces[2] == "~{flags}" &&
12004 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012005 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12006 if (!Ty || Ty->getBitWidth() % 16 != 0)
12007 return false;
12008 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012009 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012010 }
12011 break;
12012 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012013 if (CI->getType()->isIntegerTy(32) &&
12014 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12015 SmallVector<StringRef, 4> Words;
12016 SplitString(AsmPieces[0], Words, " \t,");
12017 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12018 Words[2] == "${0:w}") {
12019 Words.clear();
12020 SplitString(AsmPieces[1], Words, " \t,");
12021 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12022 Words[2] == "$0") {
12023 Words.clear();
12024 SplitString(AsmPieces[2], Words, " \t,");
12025 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12026 Words[2] == "${0:w}") {
12027 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012028 const std::string &ConstraintsStr = IA->getConstraintString();
12029 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012030 std::sort(AsmPieces.begin(), AsmPieces.end());
12031 if (AsmPieces.size() == 4 &&
12032 AsmPieces[0] == "~{cc}" &&
12033 AsmPieces[1] == "~{dirflag}" &&
12034 AsmPieces[2] == "~{flags}" &&
12035 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012036 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12037 if (!Ty || Ty->getBitWidth() % 16 != 0)
12038 return false;
12039 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012040 }
12041 }
12042 }
12043 }
12044 }
Evan Cheng55d42002011-01-08 01:24:27 +000012045
12046 if (CI->getType()->isIntegerTy(64)) {
12047 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12048 if (Constraints.size() >= 2 &&
12049 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12050 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12051 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12052 SmallVector<StringRef, 4> Words;
12053 SplitString(AsmPieces[0], Words, " \t");
12054 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012055 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012056 SplitString(AsmPieces[1], Words, " \t");
12057 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12058 Words.clear();
12059 SplitString(AsmPieces[2], Words, " \t,");
12060 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12061 Words[2] == "%edx") {
12062 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12063 if (!Ty || Ty->getBitWidth() % 16 != 0)
12064 return false;
12065 return IntrinsicLowering::LowerToByteSwap(CI);
12066 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012067 }
12068 }
12069 }
12070 }
12071 break;
12072 }
12073 return false;
12074}
12075
12076
12077
Chris Lattnerf4dff842006-07-11 02:54:03 +000012078/// getConstraintType - Given a constraint letter, return the type of
12079/// constraint it is for this target.
12080X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012081X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12082 if (Constraint.size() == 1) {
12083 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012084 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012085 case 'q':
12086 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012087 case 'f':
12088 case 't':
12089 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012090 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012091 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012092 case 'Y':
12093 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012094 case 'a':
12095 case 'b':
12096 case 'c':
12097 case 'd':
12098 case 'S':
12099 case 'D':
12100 case 'A':
12101 return C_Register;
12102 case 'I':
12103 case 'J':
12104 case 'K':
12105 case 'L':
12106 case 'M':
12107 case 'N':
12108 case 'G':
12109 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012110 case 'e':
12111 case 'Z':
12112 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012113 default:
12114 break;
12115 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012116 }
Chris Lattner4234f572007-03-25 02:14:49 +000012117 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012118}
12119
John Thompson44ab89e2010-10-29 17:29:13 +000012120/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012121/// This object must already have been set up with the operand type
12122/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012123TargetLowering::ConstraintWeight
12124 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012125 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012126 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012127 Value *CallOperandVal = info.CallOperandVal;
12128 // If we don't have a value, we can't do a match,
12129 // but allow it at the lowest weight.
12130 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012131 return CW_Default;
12132 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012133 // Look at the constraint type.
12134 switch (*constraint) {
12135 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012136 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12137 case 'R':
12138 case 'q':
12139 case 'Q':
12140 case 'a':
12141 case 'b':
12142 case 'c':
12143 case 'd':
12144 case 'S':
12145 case 'D':
12146 case 'A':
12147 if (CallOperandVal->getType()->isIntegerTy())
12148 weight = CW_SpecificReg;
12149 break;
12150 case 'f':
12151 case 't':
12152 case 'u':
12153 if (type->isFloatingPointTy())
12154 weight = CW_SpecificReg;
12155 break;
12156 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012157 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012158 weight = CW_SpecificReg;
12159 break;
12160 case 'x':
12161 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012162 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012163 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012164 break;
12165 case 'I':
12166 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12167 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012168 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012169 }
12170 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012171 case 'J':
12172 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12173 if (C->getZExtValue() <= 63)
12174 weight = CW_Constant;
12175 }
12176 break;
12177 case 'K':
12178 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12179 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12180 weight = CW_Constant;
12181 }
12182 break;
12183 case 'L':
12184 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12185 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12186 weight = CW_Constant;
12187 }
12188 break;
12189 case 'M':
12190 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12191 if (C->getZExtValue() <= 3)
12192 weight = CW_Constant;
12193 }
12194 break;
12195 case 'N':
12196 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12197 if (C->getZExtValue() <= 0xff)
12198 weight = CW_Constant;
12199 }
12200 break;
12201 case 'G':
12202 case 'C':
12203 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12204 weight = CW_Constant;
12205 }
12206 break;
12207 case 'e':
12208 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12209 if ((C->getSExtValue() >= -0x80000000LL) &&
12210 (C->getSExtValue() <= 0x7fffffffLL))
12211 weight = CW_Constant;
12212 }
12213 break;
12214 case 'Z':
12215 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12216 if (C->getZExtValue() <= 0xffffffff)
12217 weight = CW_Constant;
12218 }
12219 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012220 }
12221 return weight;
12222}
12223
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012224/// LowerXConstraint - try to replace an X constraint, which matches anything,
12225/// with another that has more specific requirements based on the type of the
12226/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012227const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012228LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012229 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12230 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012231 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012232 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012233 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012234 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012235 return "x";
12236 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012237
Chris Lattner5e764232008-04-26 23:02:14 +000012238 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012239}
12240
Chris Lattner48884cd2007-08-25 00:47:38 +000012241/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12242/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012243void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012244 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012245 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012246 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012247 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012248
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012249 switch (Constraint) {
12250 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012251 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012252 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012253 if (C->getZExtValue() <= 31) {
12254 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012255 break;
12256 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012257 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012258 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012259 case 'J':
12260 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012261 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012262 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12263 break;
12264 }
12265 }
12266 return;
12267 case 'K':
12268 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012269 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012270 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12271 break;
12272 }
12273 }
12274 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012275 case 'N':
12276 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012277 if (C->getZExtValue() <= 255) {
12278 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012279 break;
12280 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012281 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012282 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012283 case 'e': {
12284 // 32-bit signed value
12285 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012286 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12287 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012288 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012289 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012290 break;
12291 }
12292 // FIXME gcc accepts some relocatable values here too, but only in certain
12293 // memory models; it's complicated.
12294 }
12295 return;
12296 }
12297 case 'Z': {
12298 // 32-bit unsigned value
12299 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012300 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12301 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012302 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12303 break;
12304 }
12305 }
12306 // FIXME gcc accepts some relocatable values here too, but only in certain
12307 // memory models; it's complicated.
12308 return;
12309 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012310 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012311 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012312 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012313 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012314 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012315 break;
12316 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012317
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012318 // In any sort of PIC mode addresses need to be computed at runtime by
12319 // adding in a register or some sort of table lookup. These can't
12320 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012321 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012322 return;
12323
Chris Lattnerdc43a882007-05-03 16:52:29 +000012324 // If we are in non-pic codegen mode, we allow the address of a global (with
12325 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012326 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012327 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012328
Chris Lattner49921962009-05-08 18:23:14 +000012329 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12330 while (1) {
12331 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12332 Offset += GA->getOffset();
12333 break;
12334 } else if (Op.getOpcode() == ISD::ADD) {
12335 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12336 Offset += C->getZExtValue();
12337 Op = Op.getOperand(0);
12338 continue;
12339 }
12340 } else if (Op.getOpcode() == ISD::SUB) {
12341 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12342 Offset += -C->getZExtValue();
12343 Op = Op.getOperand(0);
12344 continue;
12345 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012346 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012347
Chris Lattner49921962009-05-08 18:23:14 +000012348 // Otherwise, this isn't something we can handle, reject it.
12349 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012350 }
Eric Christopherfd179292009-08-27 18:07:15 +000012351
Dan Gohman46510a72010-04-15 01:51:59 +000012352 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012353 // If we require an extra load to get this address, as in PIC mode, we
12354 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012355 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12356 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012357 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012358
Devang Patel0d881da2010-07-06 22:08:15 +000012359 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12360 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012361 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012362 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012363 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012364
Gabor Greifba36cb52008-08-28 21:40:38 +000012365 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012366 Ops.push_back(Result);
12367 return;
12368 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012369 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012370}
12371
Chris Lattner259e97c2006-01-31 19:43:35 +000012372std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012373getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012374 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012375 if (Constraint.size() == 1) {
12376 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012377 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012378 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012379 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12380 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012381 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012382 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12383 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12384 X86::R10D,X86::R11D,X86::R12D,
12385 X86::R13D,X86::R14D,X86::R15D,
12386 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012387 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012388 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12389 X86::SI, X86::DI, X86::R8W,X86::R9W,
12390 X86::R10W,X86::R11W,X86::R12W,
12391 X86::R13W,X86::R14W,X86::R15W,
12392 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012393 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012394 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12395 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12396 X86::R10B,X86::R11B,X86::R12B,
12397 X86::R13B,X86::R14B,X86::R15B,
12398 X86::BPL, X86::SPL, 0);
12399
Owen Anderson825b72b2009-08-11 20:47:22 +000012400 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012401 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12402 X86::RSI, X86::RDI, X86::R8, X86::R9,
12403 X86::R10, X86::R11, X86::R12,
12404 X86::R13, X86::R14, X86::R15,
12405 X86::RBP, X86::RSP, 0);
12406
12407 break;
12408 }
Eric Christopherfd179292009-08-27 18:07:15 +000012409 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012410 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012411 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012412 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012413 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012414 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012415 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012416 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012417 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012418 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12419 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012420 }
12421 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012422
Chris Lattner1efa40f2006-02-22 00:56:39 +000012423 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012424}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012425
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012426std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012427X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012428 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012429 // First, see if this is a constraint that directly corresponds to an LLVM
12430 // register class.
12431 if (Constraint.size() == 1) {
12432 // GCC Constraint Letters
12433 switch (Constraint[0]) {
12434 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012435 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012436 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012437 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012438 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012439 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012440 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012441 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012442 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012443 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012444 case 'R': // LEGACY_REGS
12445 if (VT == MVT::i8)
12446 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12447 if (VT == MVT::i16)
12448 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12449 if (VT == MVT::i32 || !Subtarget->is64Bit())
12450 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12451 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012452 case 'f': // FP Stack registers.
12453 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12454 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012455 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012456 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012457 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012458 return std::make_pair(0U, X86::RFP64RegisterClass);
12459 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012460 case 'y': // MMX_REGS if MMX allowed.
12461 if (!Subtarget->hasMMX()) break;
12462 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012463 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012464 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012465 // FALL THROUGH.
12466 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012467 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012468
Owen Anderson825b72b2009-08-11 20:47:22 +000012469 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012470 default: break;
12471 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012472 case MVT::f32:
12473 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012474 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012475 case MVT::f64:
12476 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012477 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012478 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012479 case MVT::v16i8:
12480 case MVT::v8i16:
12481 case MVT::v4i32:
12482 case MVT::v2i64:
12483 case MVT::v4f32:
12484 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012485 return std::make_pair(0U, X86::VR128RegisterClass);
12486 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012487 break;
12488 }
12489 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012490
Chris Lattnerf76d1802006-07-31 23:26:50 +000012491 // Use the default implementation in TargetLowering to convert the register
12492 // constraint into a member of a register class.
12493 std::pair<unsigned, const TargetRegisterClass*> Res;
12494 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012495
12496 // Not found as a standard register?
12497 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012498 // Map st(0) -> st(7) -> ST0
12499 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12500 tolower(Constraint[1]) == 's' &&
12501 tolower(Constraint[2]) == 't' &&
12502 Constraint[3] == '(' &&
12503 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12504 Constraint[5] == ')' &&
12505 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012506
Chris Lattner56d77c72009-09-13 22:41:48 +000012507 Res.first = X86::ST0+Constraint[4]-'0';
12508 Res.second = X86::RFP80RegisterClass;
12509 return Res;
12510 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012511
Chris Lattner56d77c72009-09-13 22:41:48 +000012512 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012513 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012514 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012515 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012516 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012517 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012518
12519 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012520 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012521 Res.first = X86::EFLAGS;
12522 Res.second = X86::CCRRegisterClass;
12523 return Res;
12524 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012525
Dale Johannesen330169f2008-11-13 21:52:36 +000012526 // 'A' means EAX + EDX.
12527 if (Constraint == "A") {
12528 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012529 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012530 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012531 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012532 return Res;
12533 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012534
Chris Lattnerf76d1802006-07-31 23:26:50 +000012535 // Otherwise, check to see if this is a register class of the wrong value
12536 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12537 // turn into {ax},{dx}.
12538 if (Res.second->hasType(VT))
12539 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012540
Chris Lattnerf76d1802006-07-31 23:26:50 +000012541 // All of the single-register GCC register classes map their values onto
12542 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12543 // really want an 8-bit or 32-bit register, map to the appropriate register
12544 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012545 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012546 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012547 unsigned DestReg = 0;
12548 switch (Res.first) {
12549 default: break;
12550 case X86::AX: DestReg = X86::AL; break;
12551 case X86::DX: DestReg = X86::DL; break;
12552 case X86::CX: DestReg = X86::CL; break;
12553 case X86::BX: DestReg = X86::BL; break;
12554 }
12555 if (DestReg) {
12556 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012557 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012558 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012559 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012560 unsigned DestReg = 0;
12561 switch (Res.first) {
12562 default: break;
12563 case X86::AX: DestReg = X86::EAX; break;
12564 case X86::DX: DestReg = X86::EDX; break;
12565 case X86::CX: DestReg = X86::ECX; break;
12566 case X86::BX: DestReg = X86::EBX; break;
12567 case X86::SI: DestReg = X86::ESI; break;
12568 case X86::DI: DestReg = X86::EDI; break;
12569 case X86::BP: DestReg = X86::EBP; break;
12570 case X86::SP: DestReg = X86::ESP; break;
12571 }
12572 if (DestReg) {
12573 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012574 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012575 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012576 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012577 unsigned DestReg = 0;
12578 switch (Res.first) {
12579 default: break;
12580 case X86::AX: DestReg = X86::RAX; break;
12581 case X86::DX: DestReg = X86::RDX; break;
12582 case X86::CX: DestReg = X86::RCX; break;
12583 case X86::BX: DestReg = X86::RBX; break;
12584 case X86::SI: DestReg = X86::RSI; break;
12585 case X86::DI: DestReg = X86::RDI; break;
12586 case X86::BP: DestReg = X86::RBP; break;
12587 case X86::SP: DestReg = X86::RSP; break;
12588 }
12589 if (DestReg) {
12590 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012591 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012592 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012593 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012594 } else if (Res.second == X86::FR32RegisterClass ||
12595 Res.second == X86::FR64RegisterClass ||
12596 Res.second == X86::VR128RegisterClass) {
12597 // Handle references to XMM physical registers that got mapped into the
12598 // wrong class. This can happen with constraints like {xmm0} where the
12599 // target independent register mapper will just pick the first match it can
12600 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012601 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012602 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012603 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012604 Res.second = X86::FR64RegisterClass;
12605 else if (X86::VR128RegisterClass->hasType(VT))
12606 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012607 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012608
Chris Lattnerf76d1802006-07-31 23:26:50 +000012609 return Res;
12610}