blob: 3e3ab691bceb0ab14e37af450411699291fca5c8 [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000047#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000054using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055
Evan Chengb1712452010-01-27 06:25:16 +000056STATISTIC(NumTailCalls, "Number of tail calls");
57
Evan Cheng10e86422008-04-25 19:11:04 +000058// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000059static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000060 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000061
Chris Lattnerf0144122009-07-28 03:13:23 +000062static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Michael J. Spencerec38de22010-10-10 22:04:20 +000063
Eric Christopher62f35a22010-07-05 19:26:33 +000064 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Michael J. Spencerec38de22010-10-10 22:04:20 +000065
Eric Christopher62f35a22010-07-05 19:26:33 +000066 if (TM.getSubtarget<X86Subtarget>().isTargetDarwin()) {
Chris Lattnere019ec12010-12-19 20:07:10 +000067 if (is64Bit)
68 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000069 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +000070 }
Chris Lattnere019ec12010-12-19 20:07:10 +000071
72 if (TM.getSubtarget<X86Subtarget>().isTargetELF() ){
73 if (is64Bit)
74 return new X8664_ELFTargetObjectFile(TM);
75 return new X8632_ELFTargetObjectFile(TM);
76 }
77 if (TM.getSubtarget<X86Subtarget>().isTargetCOFF())
78 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000079 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000080}
81
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000082X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000083 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000084 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +000085 X86ScalarSSEf64 = Subtarget->hasXMMInt();
86 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +000087 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000088
Anton Korobeynikov2365f512007-07-14 14:06:15 +000089 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000090 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000091
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000092 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +000093 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094
95 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000096 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000097 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000098 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000099 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000100
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000101 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000102 // Setup Windows compiler runtime calls.
103 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000104 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
105 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000106 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000107 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000108 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000109 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
110 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000111 }
112
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000113 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000114 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000115 setUseUnderscoreSetJmp(false);
116 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000117 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000118 // MS runtime is weird: it exports _setjmp, but longjmp!
119 setUseUnderscoreSetJmp(true);
120 setUseUnderscoreLongJmp(false);
121 } else {
122 setUseUnderscoreSetJmp(true);
123 setUseUnderscoreLongJmp(true);
124 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000125
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000126 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000128 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000130 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000132
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000134
Scott Michelfdc40a02009-02-17 22:15:04 +0000135 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000137 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000139 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
141 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000142
143 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
145 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
146 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
147 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
148 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
149 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000150
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000151 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
152 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
154 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
155 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000156
Evan Cheng25ab6902006-09-08 06:48:29 +0000157 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
159 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000160 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000161 // We have an algorithm for SSE2->double, and we turn this into a
162 // 64-bit FILD followed by conditional FADD for other targets.
163 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000164 // We have an algorithm for SSE2, and we turn this into a 64-bit
165 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000166 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000167 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000168
169 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
170 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
172 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000173
Devang Patel6a784892009-06-05 18:48:29 +0000174 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000175 // SSE has no i16 to fp conversion, only i32
176 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000178 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000180 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
182 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000183 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000184 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000185 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
186 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000187 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000188
Dale Johannesen73328d12007-09-19 23:55:34 +0000189 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
190 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
192 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000193
Evan Cheng02568ff2006-01-30 22:13:22 +0000194 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
195 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
197 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000198
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000199 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000201 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000203 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
205 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000206 }
207
208 // Handle FP_TO_UINT by promoting the destination to a larger signed
209 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
211 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
212 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000213
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
216 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000217 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000218 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 // Expand FP_TO_UINT into a select.
220 // FIXME: We would like to use a Custom expander here eventually to do
221 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000223 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000224 // With SSE3 we can use fisttpll to convert to a signed i64; without
225 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228
Chris Lattner399610a2006-12-05 18:22:22 +0000229 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000230 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000231 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
232 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000233 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000234 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000235 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000236 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000237 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000238 }
Chris Lattner21f66852005-12-23 05:15:23 +0000239
Dan Gohmanb00ee212008-02-18 19:34:53 +0000240 // Scalar integer divide and remainder are lowered to use operations that
241 // produce two results, to match the available instructions. This exposes
242 // the two-result form to trivial CSE, which is able to combine x/y and x%y
243 // into a single instruction.
244 //
245 // Scalar integer multiply-high is also lowered to use two-result
246 // operations, to match the available instructions. However, plain multiply
247 // (low) operations are left as Legal, as there are single-result
248 // instructions for this in x86. Using the two-result multiply instructions
249 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000250 for (unsigned i = 0, e = 4; i != e; ++i) {
251 MVT VT = IntVTs[i];
252 setOperationAction(ISD::MULHS, VT, Expand);
253 setOperationAction(ISD::MULHU, VT, Expand);
254 setOperationAction(ISD::SDIV, VT, Expand);
255 setOperationAction(ISD::UDIV, VT, Expand);
256 setOperationAction(ISD::SREM, VT, Expand);
257 setOperationAction(ISD::UREM, VT, Expand);
258 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000259
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
261 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
262 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
263 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000264 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
267 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
268 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
269 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
270 setOperationAction(ISD::FREM , MVT::f32 , Expand);
271 setOperationAction(ISD::FREM , MVT::f64 , Expand);
272 setOperationAction(ISD::FREM , MVT::f80 , Expand);
273 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000274
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
276 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000277 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
278 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
280 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000281 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000284 }
285
Benjamin Kramer1292c222010-12-04 20:32:23 +0000286 if (Subtarget->hasPOPCNT()) {
287 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
288 } else {
289 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
290 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
291 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
292 if (Subtarget->is64Bit())
293 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
294 }
295
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
297 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000298
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000299 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000300 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000301 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000302 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000303 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
305 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
306 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
307 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
308 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000309 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
311 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
312 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
313 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
316 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000319
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000320 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
322 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
323 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
324 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000325 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
327 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000328 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000329 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
331 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
332 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
333 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000334 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000335 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000336 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
338 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
339 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000340 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
342 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
343 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000344 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000345
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000346 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000348
Eric Christopher9a9d2752010-07-22 02:48:34 +0000349 // We may not have a libcall for MEMBARRIER so we should lower this.
350 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000351
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000352 // On X86 and X86-64, atomic operations are lowered to locked instructions.
353 // Locked instructions, in turn, have implicit fence semantics (all memory
354 // operations are flushed before issuing the locked instruction, and they
355 // are not buffered), so we can fold away the common pattern of
356 // fence-atomic-fence.
357 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000358
Mon P Wang63307c32008-05-05 19:05:59 +0000359 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000360 for (unsigned i = 0, e = 4; i != e; ++i) {
361 MVT VT = IntVTs[i];
362 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
363 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
364 }
365
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000366 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
368 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000374 }
375
Evan Cheng3c992d22006-03-07 02:02:57 +0000376 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000377 if (!Subtarget->isTargetDarwin() &&
378 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000379 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000381 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000382
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
384 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
385 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
386 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000387 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000388 setExceptionPointerRegister(X86::RAX);
389 setExceptionSelectorRegister(X86::RDX);
390 } else {
391 setExceptionPointerRegister(X86::EAX);
392 setExceptionSelectorRegister(X86::EDX);
393 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
395 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000396
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000398
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000400
Nate Begemanacc398c2006-01-25 18:21:52 +0000401 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::VASTART , MVT::Other, Custom);
403 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000404 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::VAARG , MVT::Other, Custom);
406 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000407 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::VAARG , MVT::Other, Expand);
409 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000410 }
Evan Chengae642192007-03-02 23:16:35 +0000411
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
413 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000414 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000416 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000418 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000420
Evan Chengc7ce29b2009-02-13 22:36:38 +0000421 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000422 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000423 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
425 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000426
Evan Cheng223547a2006-01-31 22:28:30 +0000427 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::FABS , MVT::f64, Custom);
429 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000430
431 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::FNEG , MVT::f64, Custom);
433 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000434
Evan Cheng68c47cb2007-01-05 07:55:56 +0000435 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
437 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000438
Evan Chengd25e9e82006-02-02 00:28:23 +0000439 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FSIN , MVT::f64, Expand);
441 setOperationAction(ISD::FCOS , MVT::f64, Expand);
442 setOperationAction(ISD::FSIN , MVT::f32, Expand);
443 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000444
Chris Lattnera54aa942006-01-29 06:26:08 +0000445 // Expand FP immediates into loads from the stack, except for the special
446 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000447 addLegalFPImmediate(APFloat(+0.0)); // xorpd
448 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000449 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000450 // Use SSE for f32, x87 for f64.
451 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
453 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000454
455 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000457
458 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462
463 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
465 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000466
467 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::FSIN , MVT::f32, Expand);
469 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000470
Nate Begemane1795842008-02-14 08:57:00 +0000471 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000472 addLegalFPImmediate(APFloat(+0.0f)); // xorps
473 addLegalFPImmediate(APFloat(+0.0)); // FLD0
474 addLegalFPImmediate(APFloat(+1.0)); // FLD1
475 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
476 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
477
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000478 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
480 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000481 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000482 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000483 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000484 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
486 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000487
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
489 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
490 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
491 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000492
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
495 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000496 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000497 addLegalFPImmediate(APFloat(+0.0)); // FLD0
498 addLegalFPImmediate(APFloat(+1.0)); // FLD1
499 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
500 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000501 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
502 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
503 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
504 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000505 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000506
Dale Johannesen59a58732007-08-05 18:49:15 +0000507 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000508 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000509 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
510 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
511 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000512 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000513 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000514 addLegalFPImmediate(TmpFlt); // FLD0
515 TmpFlt.changeSign();
516 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000517
518 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000519 APFloat TmpFlt2(+1.0);
520 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
521 &ignored);
522 addLegalFPImmediate(TmpFlt2); // FLD1
523 TmpFlt2.changeSign();
524 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
525 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000526
Evan Chengc7ce29b2009-02-13 22:36:38 +0000527 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
529 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000530 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000531 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000532
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000533 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
535 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
536 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::FLOG, MVT::f80, Expand);
539 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
540 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
541 setOperationAction(ISD::FEXP, MVT::f80, Expand);
542 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000543
Mon P Wangf007a8b2008-11-06 05:31:54 +0000544 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000545 // (for widening) or expand (for scalarization). Then we will selectively
546 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
548 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
549 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
564 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
565 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000597 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000598 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
602 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
603 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
604 setTruncStoreAction((MVT::SimpleValueType)VT,
605 (MVT::SimpleValueType)InnerVT, Expand);
606 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
607 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
608 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000609 }
610
Evan Chengc7ce29b2009-02-13 22:36:38 +0000611 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
612 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000613 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000614 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000615 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000616 }
617
Dale Johannesen0488fb62010-09-30 23:57:10 +0000618 // MMX-sized vectors (other than x86mmx) are expected to be expanded
619 // into smaller operations.
620 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
621 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
622 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
623 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
624 setOperationAction(ISD::AND, MVT::v8i8, Expand);
625 setOperationAction(ISD::AND, MVT::v4i16, Expand);
626 setOperationAction(ISD::AND, MVT::v2i32, Expand);
627 setOperationAction(ISD::AND, MVT::v1i64, Expand);
628 setOperationAction(ISD::OR, MVT::v8i8, Expand);
629 setOperationAction(ISD::OR, MVT::v4i16, Expand);
630 setOperationAction(ISD::OR, MVT::v2i32, Expand);
631 setOperationAction(ISD::OR, MVT::v1i64, Expand);
632 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
633 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
634 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
635 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
636 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
637 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
638 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
639 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
640 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
641 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
642 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
643 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
644 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000645 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
646 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
647 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
648 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000649
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000650 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000652
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
654 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
655 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
656 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
657 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
658 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
659 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
660 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
661 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
662 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
663 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
664 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000665 }
666
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000667 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000669
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000670 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
671 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
673 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
674 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
675 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
678 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
679 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
680 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
681 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
682 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
683 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
684 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
685 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
686 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
687 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
688 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
689 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
690 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
691 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
692 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
695 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
696 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
697 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000698
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
700 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
701 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
702 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
703 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000704
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000705 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
706 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
707 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
708 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
709 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
710
Evan Cheng2c3ae372006-04-12 21:21:57 +0000711 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
713 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000714 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000715 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000716 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000717 // Do not attempt to custom lower non-128-bit vectors
718 if (!VT.is128BitVector())
719 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::BUILD_VECTOR,
721 VT.getSimpleVT().SimpleTy, Custom);
722 setOperationAction(ISD::VECTOR_SHUFFLE,
723 VT.getSimpleVT().SimpleTy, Custom);
724 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
725 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000726 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000727
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
729 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
730 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
731 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
733 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000734
Nate Begemancdd1eec2008-02-12 22:51:28 +0000735 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
737 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000738 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000739
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000740 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
742 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000743 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000744
745 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000746 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000747 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000748
Owen Andersond6662ad2009-08-10 20:46:15 +0000749 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000751 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000753 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000755 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000756 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000757 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000759 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000760
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000762
Evan Cheng2c3ae372006-04-12 21:21:57 +0000763 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
765 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
766 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
767 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
770 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000771 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000772
Nate Begeman14d12ca2008-02-11 04:19:36 +0000773 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000774 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
775 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
776 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
777 setOperationAction(ISD::FRINT, MVT::f32, Legal);
778 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
779 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
780 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
781 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
782 setOperationAction(ISD::FRINT, MVT::f64, Legal);
783 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
784
Nate Begeman14d12ca2008-02-11 04:19:36 +0000785 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000787
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000788 // Can turn SHL into an integer multiply.
789 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000790 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000791
Nate Begeman14d12ca2008-02-11 04:19:36 +0000792 // i8 and i16 vectors are custom , because the source register and source
793 // source memory operand types are not the same width. f32 vectors are
794 // custom since the immediate controlling the insert encodes additional
795 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
797 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
798 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
799 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000800
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
802 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
803 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
804 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000805
806 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
808 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000809 }
810 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000811
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000812 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000814
David Greene9b9838d2009-06-29 16:47:10 +0000815 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
817 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
818 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
819 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000820 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000821
Owen Anderson825b72b2009-08-11 20:47:22 +0000822 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
823 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
824 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
825 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
826 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
827 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
828 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
829 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
831 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +0000832 setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
834 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
835 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
836 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000837
838 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
840 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
841 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
842 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
843 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
844 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
845 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
846 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
847 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
848 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
849 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
850 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
851 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
852 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000853
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
855 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
856 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
857 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000858
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
860 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
861 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
863 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000864
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
866 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
867 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
868 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
869 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
870 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000871
872#if 0
873 // Not sure we want to do this since there are no 256-bit integer
874 // operations in AVX
875
876 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
877 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
879 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000880
881 // Do not attempt to custom lower non-power-of-2 vectors
882 if (!isPowerOf2_32(VT.getVectorNumElements()))
883 continue;
884
885 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
886 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
887 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
888 }
889
890 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
892 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000893 }
David Greene9b9838d2009-06-29 16:47:10 +0000894#endif
895
896#if 0
897 // Not sure we want to do this since there are no 256-bit integer
898 // operations in AVX
899
900 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
901 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
903 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000904
905 if (!VT.is256BitVector()) {
906 continue;
907 }
908 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000909 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000910 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000912 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000914 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000916 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000918 }
919
Owen Anderson825b72b2009-08-11 20:47:22 +0000920 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000921#endif
922 }
923
Evan Cheng6be2c582006-04-05 23:38:46 +0000924 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000926
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000927
Eli Friedman962f5492010-06-02 19:35:46 +0000928 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
929 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000930 //
Eli Friedman962f5492010-06-02 19:35:46 +0000931 // FIXME: We really should do custom legalization for addition and
932 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
933 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000934 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
935 // Add/Sub/Mul with overflow operations are custom lowered.
936 MVT VT = IntVTs[i];
937 setOperationAction(ISD::SADDO, VT, Custom);
938 setOperationAction(ISD::UADDO, VT, Custom);
939 setOperationAction(ISD::SSUBO, VT, Custom);
940 setOperationAction(ISD::USUBO, VT, Custom);
941 setOperationAction(ISD::SMULO, VT, Custom);
942 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +0000943 }
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000944
945 // There are no 8-bit 3-address imul/mul instructions
946 setOperationAction(ISD::SMULO, MVT::i8, Expand);
947 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000948
Evan Chengd54f2d52009-03-31 19:38:51 +0000949 if (!Subtarget->is64Bit()) {
950 // These libcalls are not available in 32-bit.
951 setLibcallName(RTLIB::SHL_I128, 0);
952 setLibcallName(RTLIB::SRL_I128, 0);
953 setLibcallName(RTLIB::SRA_I128, 0);
954 }
955
Evan Cheng206ee9d2006-07-07 08:33:52 +0000956 // We have target-specific dag combine patterns for the following nodes:
957 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000958 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000959 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000960 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000961 setTargetDAGCombine(ISD::SHL);
962 setTargetDAGCombine(ISD::SRA);
963 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000964 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +0000965 setTargetDAGCombine(ISD::AND);
Chris Lattner149a4e52008-02-22 02:09:43 +0000966 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000967 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000968 if (Subtarget->is64Bit())
969 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000970
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000971 computeRegisterProperties();
972
Evan Cheng87ed7162006-02-14 08:25:08 +0000973 // FIXME: These should be based on subtarget info. Plus, the values should
974 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000975 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000976 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000977 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000978 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000979 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000980}
981
Scott Michel5b8f82e2008-03-10 15:42:14 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
984 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000985}
986
987
Evan Cheng29286502008-01-23 23:17:41 +0000988/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
989/// the desired ByVal argument alignment.
990static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
991 if (MaxAlign == 16)
992 return;
993 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
994 if (VTy->getBitWidth() == 128)
995 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000996 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
997 unsigned EltAlign = 0;
998 getMaxByValAlign(ATy->getElementType(), EltAlign);
999 if (EltAlign > MaxAlign)
1000 MaxAlign = EltAlign;
1001 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1002 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1003 unsigned EltAlign = 0;
1004 getMaxByValAlign(STy->getElementType(i), EltAlign);
1005 if (EltAlign > MaxAlign)
1006 MaxAlign = EltAlign;
1007 if (MaxAlign == 16)
1008 break;
1009 }
1010 }
1011 return;
1012}
1013
1014/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1015/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001016/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1017/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001018unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001019 if (Subtarget->is64Bit()) {
1020 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001021 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001022 if (TyAlign > 8)
1023 return TyAlign;
1024 return 8;
1025 }
1026
Evan Cheng29286502008-01-23 23:17:41 +00001027 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001028 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001029 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001030 return Align;
1031}
Chris Lattner2b02a442007-02-25 08:29:00 +00001032
Evan Chengf0df0312008-05-15 08:39:06 +00001033/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001034/// and store operations as a result of memset, memcpy, and memmove
1035/// lowering. If DstAlign is zero that means it's safe to destination
1036/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1037/// means there isn't a need to check it against alignment requirement,
1038/// probably because the source does not need to be loaded. If
1039/// 'NonScalarIntSafe' is true, that means it's safe to return a
1040/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1041/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1042/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001043/// It returns EVT::Other if the type should be determined using generic
1044/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001045EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001046X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1047 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001048 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001049 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001050 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001051 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1052 // linux. This is because the stack realignment code can't handle certain
1053 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001054 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001055 if (NonScalarIntSafe &&
1056 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001057 if (Size >= 16 &&
1058 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001059 ((DstAlign == 0 || DstAlign >= 16) &&
1060 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001061 Subtarget->getStackAlignment() >= 16) {
1062 if (Subtarget->hasSSE2())
1063 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001064 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001065 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001066 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001067 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001068 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001069 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001070 // Do not use f64 to lower memcpy if source is string constant. It's
1071 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001072 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001073 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001074 }
Evan Chengf0df0312008-05-15 08:39:06 +00001075 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001076 return MVT::i64;
1077 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001078}
1079
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001080/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1081/// current function. The returned value is a member of the
1082/// MachineJumpTableInfo::JTEntryKind enum.
1083unsigned X86TargetLowering::getJumpTableEncoding() const {
1084 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1085 // symbol.
1086 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1087 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001088 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001089
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001090 // Otherwise, use the normal jump table encoding heuristics.
1091 return TargetLowering::getJumpTableEncoding();
1092}
1093
Chris Lattnerc64daab2010-01-26 05:02:42 +00001094const MCExpr *
1095X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1096 const MachineBasicBlock *MBB,
1097 unsigned uid,MCContext &Ctx) const{
1098 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1099 Subtarget->isPICStyleGOT());
1100 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1101 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001102 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1103 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001104}
1105
Evan Chengcc415862007-11-09 01:32:10 +00001106/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1107/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001108SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001109 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001110 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001111 // This doesn't have DebugLoc associated with it, but is not really the
1112 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001113 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001114 return Table;
1115}
1116
Chris Lattner589c6f62010-01-26 06:28:43 +00001117/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1118/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1119/// MCExpr.
1120const MCExpr *X86TargetLowering::
1121getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1122 MCContext &Ctx) const {
1123 // X86-64 uses RIP relative addressing based on the jump table label.
1124 if (Subtarget->isPICStyleRIPRel())
1125 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1126
1127 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001128 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001129}
1130
Bill Wendlingb4202b82009-07-01 18:50:55 +00001131/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001132unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001133 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001134}
1135
Evan Chengdee81012010-07-26 21:50:05 +00001136std::pair<const TargetRegisterClass*, uint8_t>
1137X86TargetLowering::findRepresentativeClass(EVT VT) const{
1138 const TargetRegisterClass *RRC = 0;
1139 uint8_t Cost = 1;
1140 switch (VT.getSimpleVT().SimpleTy) {
1141 default:
1142 return TargetLowering::findRepresentativeClass(VT);
1143 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1144 RRC = (Subtarget->is64Bit()
1145 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1146 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001147 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001148 RRC = X86::VR64RegisterClass;
1149 break;
1150 case MVT::f32: case MVT::f64:
1151 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1152 case MVT::v4f32: case MVT::v2f64:
1153 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1154 case MVT::v4f64:
1155 RRC = X86::VR128RegisterClass;
1156 break;
1157 }
1158 return std::make_pair(RRC, Cost);
1159}
1160
Evan Cheng70017e42010-07-24 00:39:05 +00001161unsigned
1162X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1163 MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001164 const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
1165
1166 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001167 switch (RC->getID()) {
1168 default:
1169 return 0;
1170 case X86::GR32RegClassID:
1171 return 4 - FPDiff;
1172 case X86::GR64RegClassID:
1173 return 8 - FPDiff;
1174 case X86::VR128RegClassID:
1175 return Subtarget->is64Bit() ? 10 : 4;
1176 case X86::VR64RegClassID:
1177 return 4;
1178 }
1179}
1180
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001181bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1182 unsigned &Offset) const {
1183 if (!Subtarget->isTargetLinux())
1184 return false;
1185
1186 if (Subtarget->is64Bit()) {
1187 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1188 Offset = 0x28;
1189 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1190 AddressSpace = 256;
1191 else
1192 AddressSpace = 257;
1193 } else {
1194 // %gs:0x14 on i386
1195 Offset = 0x14;
1196 AddressSpace = 256;
1197 }
1198 return true;
1199}
1200
1201
Chris Lattner2b02a442007-02-25 08:29:00 +00001202//===----------------------------------------------------------------------===//
1203// Return Value Calling Convention Implementation
1204//===----------------------------------------------------------------------===//
1205
Chris Lattner59ed56b2007-02-28 04:55:35 +00001206#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001207
Michael J. Spencerec38de22010-10-10 22:04:20 +00001208bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001209X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001210 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001211 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001212 SmallVector<CCValAssign, 16> RVLocs;
1213 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001214 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001215 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001216}
1217
Dan Gohman98ca4f22009-08-05 01:29:28 +00001218SDValue
1219X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001220 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001222 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001223 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001224 MachineFunction &MF = DAG.getMachineFunction();
1225 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001226
Chris Lattner9774c912007-02-27 05:28:59 +00001227 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001228 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1229 RVLocs, *DAG.getContext());
1230 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001231
Evan Chengdcea1632010-02-04 02:40:39 +00001232 // Add the regs to the liveout set for the function.
1233 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1234 for (unsigned i = 0; i != RVLocs.size(); ++i)
1235 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1236 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001237
Dan Gohman475871a2008-07-27 21:46:04 +00001238 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001239
Dan Gohman475871a2008-07-27 21:46:04 +00001240 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001241 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1242 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001243 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1244 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001245
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001246 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001247 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1248 CCValAssign &VA = RVLocs[i];
1249 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001250 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001251 EVT ValVT = ValToCopy.getValueType();
1252
Dale Johannesenc4510512010-09-24 19:05:48 +00001253 // If this is x86-64, and we disabled SSE, we can't return FP values,
1254 // or SSE or MMX vectors.
1255 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1256 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001257 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001258 report_fatal_error("SSE register return with SSE disabled");
1259 }
1260 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1261 // llvm-gcc has never done it right and no one has noticed, so this
1262 // should be OK for now.
1263 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001264 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001265 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001266
Chris Lattner447ff682008-03-11 03:23:40 +00001267 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1268 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001269 if (VA.getLocReg() == X86::ST0 ||
1270 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001271 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1272 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001273 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001274 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001275 RetOps.push_back(ValToCopy);
1276 // Don't emit a copytoreg.
1277 continue;
1278 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001279
Evan Cheng242b38b2009-02-23 09:03:22 +00001280 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1281 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001282 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001283 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001284 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001285 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001286 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1287 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001288 // If we don't have SSE2 available, convert to v4f32 so the generated
1289 // register is legal.
1290 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001291 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001292 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001293 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001294 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001295
Dale Johannesendd64c412009-02-04 00:33:20 +00001296 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001297 Flag = Chain.getValue(1);
1298 }
Dan Gohman61a92132008-04-21 23:59:07 +00001299
1300 // The x86-64 ABI for returning structs by value requires that we copy
1301 // the sret argument into %rax for the return. We saved the argument into
1302 // a virtual register in the entry block, so now we copy the value out
1303 // and into %rax.
1304 if (Subtarget->is64Bit() &&
1305 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1306 MachineFunction &MF = DAG.getMachineFunction();
1307 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1308 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001309 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001310 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001311 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001312
Dale Johannesendd64c412009-02-04 00:33:20 +00001313 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001314 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001315
1316 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001317 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001318 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001319
Chris Lattner447ff682008-03-11 03:23:40 +00001320 RetOps[0] = Chain; // Update chain.
1321
1322 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001323 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001324 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001325
1326 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001327 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001328}
1329
Evan Cheng3d2125c2010-11-30 23:55:39 +00001330bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1331 if (N->getNumValues() != 1)
1332 return false;
1333 if (!N->hasNUsesOfValue(1, 0))
1334 return false;
1335
1336 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001337 if (Copy->getOpcode() != ISD::CopyToReg &&
1338 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001339 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001340
1341 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001342 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001343 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001344 if (UI->getOpcode() != X86ISD::RET_FLAG)
1345 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001346 HasRet = true;
1347 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001348
Evan Cheng1bf891a2010-12-01 22:59:46 +00001349 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001350}
1351
Dan Gohman98ca4f22009-08-05 01:29:28 +00001352/// LowerCallResult - Lower the result values of a call into the
1353/// appropriate copies out of appropriate physical registers.
1354///
1355SDValue
1356X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001357 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001358 const SmallVectorImpl<ISD::InputArg> &Ins,
1359 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001360 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001361
Chris Lattnere32bbf62007-02-28 07:09:55 +00001362 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001363 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001364 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001365 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001366 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001367 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001368
Chris Lattner3085e152007-02-25 08:59:22 +00001369 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001370 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001371 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001372 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001373
Torok Edwin3f142c32009-02-01 18:15:56 +00001374 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001375 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001376 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001377 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001378 }
1379
Evan Cheng79fb3b42009-02-20 20:43:02 +00001380 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001381
1382 // If this is a call to a function that returns an fp value on the floating
1383 // point stack, we must guarantee the the value is popped from the stack, so
1384 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1385 // if the return value is not used. We use the FpGET_ST0 instructions
1386 // instead.
1387 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1388 // If we prefer to use the value in xmm registers, copy it out as f80 and
1389 // use a truncate to move it from fp stack reg to xmm reg.
1390 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1391 bool isST0 = VA.getLocReg() == X86::ST0;
1392 unsigned Opc = 0;
1393 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1394 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1395 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1396 SDValue Ops[] = { Chain, InFlag };
1397 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Flag,
1398 Ops, 2), 1);
1399 Val = Chain.getValue(0);
1400
1401 // Round the f80 to the right size, which also moves it to the appropriate
1402 // xmm register.
1403 if (CopyVT != VA.getValVT())
1404 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1405 // This truncation won't change the value.
1406 DAG.getIntPtrConstant(1));
1407 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001408 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1409 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1410 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001412 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001413 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1414 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001415 } else {
1416 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001417 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001418 Val = Chain.getValue(0);
1419 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001420 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001421 } else {
1422 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1423 CopyVT, InFlag).getValue(1);
1424 Val = Chain.getValue(0);
1425 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001426 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001427 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001428 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001429
Dan Gohman98ca4f22009-08-05 01:29:28 +00001430 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001431}
1432
1433
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001434//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001435// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001436//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001437// StdCall calling convention seems to be standard for many Windows' API
1438// routines and around. It differs from C calling convention just a little:
1439// callee should clean up the stack, not caller. Symbols should be also
1440// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001441// For info on fast calling convention see Fast Calling Convention (tail call)
1442// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001443
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001445/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001446static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1447 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001448 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001449
Dan Gohman98ca4f22009-08-05 01:29:28 +00001450 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001451}
1452
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001453/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001454/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001455static bool
1456ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1457 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001458 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001459
Dan Gohman98ca4f22009-08-05 01:29:28 +00001460 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001461}
1462
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001463/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1464/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001465/// the specific parameter attribute. The copy will be passed as a byval
1466/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001467static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001468CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001469 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1470 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001471 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001472
Dale Johannesendd64c412009-02-04 00:33:20 +00001473 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001474 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001475 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001476}
1477
Chris Lattner29689432010-03-11 00:22:57 +00001478/// IsTailCallConvention - Return true if the calling convention is one that
1479/// supports tail call optimization.
1480static bool IsTailCallConvention(CallingConv::ID CC) {
1481 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1482}
1483
Evan Cheng0c439eb2010-01-27 00:07:07 +00001484/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1485/// a tailcall target by changing its ABI.
1486static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001487 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001488}
1489
Dan Gohman98ca4f22009-08-05 01:29:28 +00001490SDValue
1491X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001492 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001493 const SmallVectorImpl<ISD::InputArg> &Ins,
1494 DebugLoc dl, SelectionDAG &DAG,
1495 const CCValAssign &VA,
1496 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001497 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001498 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001500 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001501 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001502 EVT ValVT;
1503
1504 // If value is passed by pointer we have address passed instead of the value
1505 // itself.
1506 if (VA.getLocInfo() == CCValAssign::Indirect)
1507 ValVT = VA.getLocVT();
1508 else
1509 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001510
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001511 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001512 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001513 // In case of tail call optimization mark all arguments mutable. Since they
1514 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001515 if (Flags.isByVal()) {
1516 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001517 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001518 return DAG.getFrameIndex(FI, getPointerTy());
1519 } else {
1520 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001521 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001522 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1523 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001524 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001525 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001526 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001527}
1528
Dan Gohman475871a2008-07-27 21:46:04 +00001529SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001530X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001531 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001532 bool isVarArg,
1533 const SmallVectorImpl<ISD::InputArg> &Ins,
1534 DebugLoc dl,
1535 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001536 SmallVectorImpl<SDValue> &InVals)
1537 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001538 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001539 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001540
Gordon Henriksen86737662008-01-05 16:56:59 +00001541 const Function* Fn = MF.getFunction();
1542 if (Fn->hasExternalLinkage() &&
1543 Subtarget->isTargetCygMing() &&
1544 Fn->getName() == "main")
1545 FuncInfo->setForceFramePointer(true);
1546
Evan Cheng1bc78042006-04-26 01:20:17 +00001547 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001548 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001549 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001550
Chris Lattner29689432010-03-11 00:22:57 +00001551 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1552 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001553
Chris Lattner638402b2007-02-28 07:00:42 +00001554 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001555 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001556 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1557 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001558 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Chris Lattnerf39f7712007-02-28 05:46:49 +00001560 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001561 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001562 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1563 CCValAssign &VA = ArgLocs[i];
1564 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1565 // places.
1566 assert(VA.getValNo() != LastVal &&
1567 "Don't support value assigned to multiple locs yet");
1568 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001569
Chris Lattnerf39f7712007-02-28 05:46:49 +00001570 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001571 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001572 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001573 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001574 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001576 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001577 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001578 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001580 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001581 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1582 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001583 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001584 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001585 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001586 RC = X86::VR64RegisterClass;
1587 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001588 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001589
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001590 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001592
Chris Lattnerf39f7712007-02-28 05:46:49 +00001593 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1594 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1595 // right size.
1596 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001597 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001598 DAG.getValueType(VA.getValVT()));
1599 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001600 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001601 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001602 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001603 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001604
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001605 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001606 // Handle MMX values passed in XMM regs.
1607 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001608 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1609 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001610 } else
1611 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001612 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001613 } else {
1614 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001616 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001617
1618 // If value is passed via pointer - do a load.
1619 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001620 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1621 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001622
Dan Gohman98ca4f22009-08-05 01:29:28 +00001623 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001624 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001625
Dan Gohman61a92132008-04-21 23:59:07 +00001626 // The x86-64 ABI for returning structs by value requires that we copy
1627 // the sret argument into %rax for the return. Save the argument into
1628 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001629 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001630 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1631 unsigned Reg = FuncInfo->getSRetReturnReg();
1632 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001633 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001634 FuncInfo->setSRetReturnReg(Reg);
1635 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001636 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001637 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001638 }
1639
Chris Lattnerf39f7712007-02-28 05:46:49 +00001640 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001641 // Align stack specially for tail calls.
1642 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001643 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001644
Evan Cheng1bc78042006-04-26 01:20:17 +00001645 // If the function takes variable number of arguments, make a frame index for
1646 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001647 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001648 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1649 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001650 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001651 }
1652 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001653 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1654
1655 // FIXME: We should really autogenerate these arrays
1656 static const unsigned GPR64ArgRegsWin64[] = {
1657 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001658 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001659 static const unsigned GPR64ArgRegs64Bit[] = {
1660 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1661 };
1662 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001663 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1664 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1665 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001666 const unsigned *GPR64ArgRegs;
1667 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001668
1669 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001670 // The XMM registers which might contain var arg parameters are shadowed
1671 // in their paired GPR. So we only need to save the GPR to their home
1672 // slots.
1673 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001674 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001675 } else {
1676 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1677 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001678
1679 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001680 }
1681 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1682 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001683
Devang Patel578efa92009-06-05 21:57:13 +00001684 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001685 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001686 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001687 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001688 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001689 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001690 // Kernel mode asks for SSE to be disabled, so don't push them
1691 // on the stack.
1692 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001693
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001694 if (IsWin64) {
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001695 const TargetFrameInfo &TFI = *getTargetMachine().getFrameInfo();
1696 // Get to the caller-allocated home save location. Add 8 to account
1697 // for the return address.
1698 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001699 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001700 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001701 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1702 } else {
1703 // For X86-64, if there are vararg parameters that are passed via
1704 // registers, then we must store them to their spots on the stack so they
1705 // may be loaded by deferencing the result of va_next.
1706 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1707 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1708 FuncInfo->setRegSaveFrameIndex(
1709 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001710 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001711 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001712
Gordon Henriksen86737662008-01-05 16:56:59 +00001713 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001714 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001715 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1716 getPointerTy());
1717 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001718 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001719 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1720 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001721 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1722 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001724 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001725 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001726 MachinePointerInfo::getFixedStack(
1727 FuncInfo->getRegSaveFrameIndex(), Offset),
1728 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001729 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001730 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001731 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001732
Dan Gohmanface41a2009-08-16 21:24:25 +00001733 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1734 // Now store the XMM (fp + vector) parameter registers.
1735 SmallVector<SDValue, 11> SaveXMMOps;
1736 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001737
Dan Gohmanface41a2009-08-16 21:24:25 +00001738 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1739 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1740 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001741
Dan Gohman1e93df62010-04-17 14:41:14 +00001742 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1743 FuncInfo->getRegSaveFrameIndex()));
1744 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1745 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001746
Dan Gohmanface41a2009-08-16 21:24:25 +00001747 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001748 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Dan Gohmanface41a2009-08-16 21:24:25 +00001749 X86::VR128RegisterClass);
1750 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1751 SaveXMMOps.push_back(Val);
1752 }
1753 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1754 MVT::Other,
1755 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001756 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001757
1758 if (!MemOps.empty())
1759 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1760 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001761 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001762 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Gordon Henriksen86737662008-01-05 16:56:59 +00001764 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001765 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001766 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001767 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001768 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001769 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001770 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001771 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001772 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001773
Gordon Henriksen86737662008-01-05 16:56:59 +00001774 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001775 // RegSaveFrameIndex is X86-64 only.
1776 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001777 if (CallConv == CallingConv::X86_FastCall ||
1778 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001779 // fastcc functions can't have varargs.
1780 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001781 }
Evan Cheng25caf632006-05-23 21:06:34 +00001782
Dan Gohman98ca4f22009-08-05 01:29:28 +00001783 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001784}
1785
Dan Gohman475871a2008-07-27 21:46:04 +00001786SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001787X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1788 SDValue StackPtr, SDValue Arg,
1789 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001790 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001791 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001792 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1793 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001794 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001795 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001796 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001797 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001798
1799 return DAG.getStore(Chain, dl, Arg, PtrOff,
1800 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001801 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001802}
1803
Bill Wendling64e87322009-01-16 19:25:27 +00001804/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001805/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001806SDValue
1807X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001808 SDValue &OutRetAddr, SDValue Chain,
1809 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001810 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001811 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001812 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001813 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001814
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001815 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001816 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1817 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001818 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001819}
1820
1821/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1822/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001823static SDValue
1824EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001825 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001826 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001827 // Store the return address to the appropriate stack slot.
1828 if (!FPDiff) return Chain;
1829 // Calculate the new stack slot for the return address.
1830 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001831 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001832 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001834 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001835 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001836 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001837 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001838 return Chain;
1839}
1840
Dan Gohman98ca4f22009-08-05 01:29:28 +00001841SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001842X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001843 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001844 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001846 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847 const SmallVectorImpl<ISD::InputArg> &Ins,
1848 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001849 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001850 MachineFunction &MF = DAG.getMachineFunction();
1851 bool Is64Bit = Subtarget->is64Bit();
1852 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001853 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001854
Evan Cheng5f941932010-02-05 02:21:12 +00001855 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001856 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001857 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1858 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001859 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001860
1861 // Sibcalls are automatically detected tailcalls which do not require
1862 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001863 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001864 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001865
1866 if (isTailCall)
1867 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001868 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001869
Chris Lattner29689432010-03-11 00:22:57 +00001870 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1871 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001872
Chris Lattner638402b2007-02-28 07:00:42 +00001873 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001874 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001875 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1876 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001877 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001878
Chris Lattner423c5f42007-02-28 05:31:48 +00001879 // Get a count of how many bytes are to be pushed on the stack.
1880 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001881 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001882 // This is a sibcall. The memory operands are available in caller's
1883 // own caller's stack.
1884 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001885 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001886 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001887
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001889 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001890 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001891 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1893 FPDiff = NumBytesCallerPushed - NumBytes;
1894
1895 // Set the delta of movement of the returnaddr stackslot.
1896 // But only set if delta is greater than previous delta.
1897 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1898 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1899 }
1900
Evan Chengf22f9b32010-02-06 03:28:46 +00001901 if (!IsSibcall)
1902 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001903
Dan Gohman475871a2008-07-27 21:46:04 +00001904 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001905 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001906 if (isTailCall && FPDiff)
1907 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1908 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001909
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1911 SmallVector<SDValue, 8> MemOpChains;
1912 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001913
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001914 // Walk the register/memloc assignments, inserting copies/loads. In the case
1915 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001916 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1917 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001918 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001919 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001920 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001921 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001922
Chris Lattner423c5f42007-02-28 05:31:48 +00001923 // Promote the value if needed.
1924 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001925 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001926 case CCValAssign::Full: break;
1927 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001928 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001929 break;
1930 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001931 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001932 break;
1933 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001934 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1935 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001936 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001937 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1938 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001939 } else
1940 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1941 break;
1942 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001943 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001944 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001945 case CCValAssign::Indirect: {
1946 // Store the argument.
1947 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001948 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001949 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001950 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001951 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001952 Arg = SpillSlot;
1953 break;
1954 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001955 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001956
Chris Lattner423c5f42007-02-28 05:31:48 +00001957 if (VA.isRegLoc()) {
1958 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001959 if (isVarArg && Subtarget->isTargetWin64()) {
1960 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1961 // shadow reg if callee is a varargs function.
1962 unsigned ShadowReg = 0;
1963 switch (VA.getLocReg()) {
1964 case X86::XMM0: ShadowReg = X86::RCX; break;
1965 case X86::XMM1: ShadowReg = X86::RDX; break;
1966 case X86::XMM2: ShadowReg = X86::R8; break;
1967 case X86::XMM3: ShadowReg = X86::R9; break;
1968 }
1969 if (ShadowReg)
1970 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1971 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001972 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001973 assert(VA.isMemLoc());
1974 if (StackPtr.getNode() == 0)
1975 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1976 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1977 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001978 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001979 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001980
Evan Cheng32fe1032006-05-25 00:59:30 +00001981 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001983 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001984
Evan Cheng347d5f72006-04-28 21:29:37 +00001985 // Build a sequence of copy-to-reg nodes chained together with token chain
1986 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001987 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001988 // Tail call byval lowering might overwrite argument registers so in case of
1989 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001991 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001992 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001993 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001994 InFlag = Chain.getValue(1);
1995 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001996
Chris Lattner88e1fd52009-07-09 04:24:46 +00001997 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001998 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1999 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002000 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002001 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2002 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002003 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002004 InFlag);
2005 InFlag = Chain.getValue(1);
2006 } else {
2007 // If we are tail calling and generating PIC/GOT style code load the
2008 // address of the callee into ECX. The value in ecx is used as target of
2009 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2010 // for tail calls on PIC/GOT architectures. Normally we would just put the
2011 // address of GOT into ebx and then call target@PLT. But for tail calls
2012 // ebx would be restored (since ebx is callee saved) before jumping to the
2013 // target@PLT.
2014
2015 // Note: The actual moving to ECX is done further down.
2016 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2017 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2018 !G->getGlobal()->hasProtectedVisibility())
2019 Callee = LowerGlobalAddress(Callee, DAG);
2020 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002021 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002022 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002023 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002024
Nate Begemanc8ea6732010-07-21 20:49:52 +00002025 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002026 // From AMD64 ABI document:
2027 // For calls that may call functions that use varargs or stdargs
2028 // (prototype-less calls or calls to functions containing ellipsis (...) in
2029 // the declaration) %al is used as hidden argument to specify the number
2030 // of SSE registers used. The contents of %al do not need to match exactly
2031 // the number of registers, but must be an ubound on the number of SSE
2032 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002033
Gordon Henriksen86737662008-01-05 16:56:59 +00002034 // Count the number of XMM registers allocated.
2035 static const unsigned XMMArgRegs[] = {
2036 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2037 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2038 };
2039 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002040 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002041 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002042
Dale Johannesendd64c412009-02-04 00:33:20 +00002043 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 InFlag = Chain.getValue(1);
2046 }
2047
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002048
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002049 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002050 if (isTailCall) {
2051 // Force all the incoming stack arguments to be loaded from the stack
2052 // before any new outgoing arguments are stored to the stack, because the
2053 // outgoing stack slots may alias the incoming argument stack slots, and
2054 // the alias isn't otherwise explicit. This is slightly more conservative
2055 // than necessary, because it means that each store effectively depends
2056 // on every argument instead of just those arguments it would clobber.
2057 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2058
Dan Gohman475871a2008-07-27 21:46:04 +00002059 SmallVector<SDValue, 8> MemOpChains2;
2060 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002061 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002062 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002063 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002064 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002065 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2066 CCValAssign &VA = ArgLocs[i];
2067 if (VA.isRegLoc())
2068 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002069 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002070 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002071 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002072 // Create frame index.
2073 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002074 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002075 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002076 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002077
Duncan Sands276dcbd2008-03-21 09:14:45 +00002078 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002079 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002080 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002081 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002082 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002083 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002084 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002085
Dan Gohman98ca4f22009-08-05 01:29:28 +00002086 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2087 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002088 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002089 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002090 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002091 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002092 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002093 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002094 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002095 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002096 }
2097 }
2098
2099 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002101 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002102
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002103 // Copy arguments to their registers.
2104 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002105 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002106 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002107 InFlag = Chain.getValue(1);
2108 }
Dan Gohman475871a2008-07-27 21:46:04 +00002109 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002110
Gordon Henriksen86737662008-01-05 16:56:59 +00002111 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002112 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002113 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002114 }
2115
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002116 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2117 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2118 // In the 64-bit large code model, we have to make all calls
2119 // through a register, since the call instruction's 32-bit
2120 // pc-relative offset may not be large enough to hold the whole
2121 // address.
2122 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002123 // If the callee is a GlobalAddress node (quite common, every direct call
2124 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2125 // it.
2126
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002127 // We should use extra load for direct calls to dllimported functions in
2128 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002129 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002130 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002131 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002132
Chris Lattner48a7d022009-07-09 05:02:21 +00002133 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2134 // external symbols most go through the PLT in PIC mode. If the symbol
2135 // has hidden or protected visibility, or if it is static or local, then
2136 // we don't need to use the PLT - we can directly call it.
2137 if (Subtarget->isTargetELF() &&
2138 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002139 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002140 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002141 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002142 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2143 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002144 // PC-relative references to external symbols should go through $stub,
2145 // unless we're building with the leopard linker or later, which
2146 // automatically synthesizes these stubs.
2147 OpFlags = X86II::MO_DARWIN_STUB;
2148 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002149
Devang Patel0d881da2010-07-06 22:08:15 +00002150 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002151 G->getOffset(), OpFlags);
2152 }
Bill Wendling056292f2008-09-16 21:48:12 +00002153 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002154 unsigned char OpFlags = 0;
2155
Evan Cheng1bf891a2010-12-01 22:59:46 +00002156 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2157 // external symbols should go through the PLT.
2158 if (Subtarget->isTargetELF() &&
2159 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2160 OpFlags = X86II::MO_PLT;
2161 } else if (Subtarget->isPICStyleStubAny() &&
2162 Subtarget->getDarwinVers() < 9) {
2163 // PC-relative references to external symbols should go through $stub,
2164 // unless we're building with the leopard linker or later, which
2165 // automatically synthesizes these stubs.
2166 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002167 }
Eric Christopherfd179292009-08-27 18:07:15 +00002168
Chris Lattner48a7d022009-07-09 05:02:21 +00002169 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2170 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002171 }
2172
Chris Lattnerd96d0722007-02-25 06:40:16 +00002173 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002174 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002175 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002176
Evan Chengf22f9b32010-02-06 03:28:46 +00002177 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002178 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2179 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002180 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002181 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002182
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002183 Ops.push_back(Chain);
2184 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002185
Dan Gohman98ca4f22009-08-05 01:29:28 +00002186 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002187 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002188
Gordon Henriksen86737662008-01-05 16:56:59 +00002189 // Add argument registers to the end of the list so that they are known live
2190 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002191 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2192 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2193 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002194
Evan Cheng586ccac2008-03-18 23:36:35 +00002195 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002196 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002197 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2198
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002199 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2200 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002201 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002202
Gabor Greifba36cb52008-08-28 21:40:38 +00002203 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002204 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002205
Dan Gohman98ca4f22009-08-05 01:29:28 +00002206 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002207 // We used to do:
2208 //// If this is the first return lowered for this function, add the regs
2209 //// to the liveout set for the function.
2210 // This isn't right, although it's probably harmless on x86; liveouts
2211 // should be computed from returns not tail calls. Consider a void
2212 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213 return DAG.getNode(X86ISD::TC_RETURN, dl,
2214 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002215 }
2216
Dale Johannesenace16102009-02-03 19:33:06 +00002217 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002218 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002219
Chris Lattner2d297092006-05-23 18:50:38 +00002220 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002221 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002222 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002223 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002224 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002225 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002226 // pops the hidden struct pointer, so we have to push it back.
2227 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002228 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002229 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002230 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002231
Gordon Henriksenae636f82008-01-03 16:47:34 +00002232 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002233 if (!IsSibcall) {
2234 Chain = DAG.getCALLSEQ_END(Chain,
2235 DAG.getIntPtrConstant(NumBytes, true),
2236 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2237 true),
2238 InFlag);
2239 InFlag = Chain.getValue(1);
2240 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002241
Chris Lattner3085e152007-02-25 08:59:22 +00002242 // Handle result values, copying them out of physregs into vregs that we
2243 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002244 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2245 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002246}
2247
Evan Cheng25ab6902006-09-08 06:48:29 +00002248
2249//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002250// Fast Calling Convention (tail call) implementation
2251//===----------------------------------------------------------------------===//
2252
2253// Like std call, callee cleans arguments, convention except that ECX is
2254// reserved for storing the tail called function address. Only 2 registers are
2255// free for argument passing (inreg). Tail call optimization is performed
2256// provided:
2257// * tailcallopt is enabled
2258// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002259// On X86_64 architecture with GOT-style position independent code only local
2260// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002261// To keep the stack aligned according to platform abi the function
2262// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2263// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002264// If a tail called function callee has more arguments than the caller the
2265// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002266// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002267// original REtADDR, but before the saved framepointer or the spilled registers
2268// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2269// stack layout:
2270// arg1
2271// arg2
2272// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002273// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002274// move area ]
2275// (possible EBP)
2276// ESI
2277// EDI
2278// local1 ..
2279
2280/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2281/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002282unsigned
2283X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2284 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002285 MachineFunction &MF = DAG.getMachineFunction();
2286 const TargetMachine &TM = MF.getTarget();
2287 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2288 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002289 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002290 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002291 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002292 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2293 // Number smaller than 12 so just add the difference.
2294 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2295 } else {
2296 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002297 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002298 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002299 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002300 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002301}
2302
Evan Cheng5f941932010-02-05 02:21:12 +00002303/// MatchingStackOffset - Return true if the given stack call argument is
2304/// already available in the same position (relatively) of the caller's
2305/// incoming argument stack.
2306static
2307bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2308 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2309 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002310 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2311 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002312 if (Arg.getOpcode() == ISD::CopyFromReg) {
2313 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2314 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2315 return false;
2316 MachineInstr *Def = MRI->getVRegDef(VR);
2317 if (!Def)
2318 return false;
2319 if (!Flags.isByVal()) {
2320 if (!TII->isLoadFromStackSlot(Def, FI))
2321 return false;
2322 } else {
2323 unsigned Opcode = Def->getOpcode();
2324 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2325 Def->getOperand(1).isFI()) {
2326 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002327 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002328 } else
2329 return false;
2330 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002331 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2332 if (Flags.isByVal())
2333 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002334 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002335 // define @foo(%struct.X* %A) {
2336 // tail call @bar(%struct.X* byval %A)
2337 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002338 return false;
2339 SDValue Ptr = Ld->getBasePtr();
2340 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2341 if (!FINode)
2342 return false;
2343 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002344 } else
2345 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002346
Evan Cheng4cae1332010-03-05 08:38:04 +00002347 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002348 if (!MFI->isFixedObjectIndex(FI))
2349 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002350 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002351}
2352
Dan Gohman98ca4f22009-08-05 01:29:28 +00002353/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2354/// for tail call optimization. Targets which want to do tail call
2355/// optimization should implement this function.
2356bool
2357X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002358 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002359 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002360 bool isCalleeStructRet,
2361 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002362 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002363 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002364 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002365 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002366 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002367 CalleeCC != CallingConv::C)
2368 return false;
2369
Evan Cheng7096ae42010-01-29 06:45:59 +00002370 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002371 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002372 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002373 CallingConv::ID CallerCC = CallerF->getCallingConv();
2374 bool CCMatch = CallerCC == CalleeCC;
2375
Dan Gohman1797ed52010-02-08 20:27:50 +00002376 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002377 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002378 return true;
2379 return false;
2380 }
2381
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002382 // Look for obvious safe cases to perform tail call optimization that do not
2383 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002384
Evan Cheng2c12cb42010-03-26 16:26:03 +00002385 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2386 // emit a special epilogue.
2387 if (RegInfo->needsStackRealignment(MF))
2388 return false;
2389
Eric Christopher90eb4022010-07-22 00:26:08 +00002390 // Do not sibcall optimize vararg calls unless the call site is not passing
2391 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002392 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002393 return false;
2394
Evan Chenga375d472010-03-15 18:54:48 +00002395 // Also avoid sibcall optimization if either caller or callee uses struct
2396 // return semantics.
2397 if (isCalleeStructRet || isCallerStructRet)
2398 return false;
2399
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002400 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2401 // Therefore if it's not used by the call it is not safe to optimize this into
2402 // a sibcall.
2403 bool Unused = false;
2404 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2405 if (!Ins[i].Used) {
2406 Unused = true;
2407 break;
2408 }
2409 }
2410 if (Unused) {
2411 SmallVector<CCValAssign, 16> RVLocs;
2412 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2413 RVLocs, *DAG.getContext());
2414 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002415 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002416 CCValAssign &VA = RVLocs[i];
2417 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2418 return false;
2419 }
2420 }
2421
Evan Cheng13617962010-04-30 01:12:32 +00002422 // If the calling conventions do not match, then we'd better make sure the
2423 // results are returned in the same way as what the caller expects.
2424 if (!CCMatch) {
2425 SmallVector<CCValAssign, 16> RVLocs1;
2426 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2427 RVLocs1, *DAG.getContext());
2428 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2429
2430 SmallVector<CCValAssign, 16> RVLocs2;
2431 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2432 RVLocs2, *DAG.getContext());
2433 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2434
2435 if (RVLocs1.size() != RVLocs2.size())
2436 return false;
2437 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2438 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2439 return false;
2440 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2441 return false;
2442 if (RVLocs1[i].isRegLoc()) {
2443 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2444 return false;
2445 } else {
2446 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2447 return false;
2448 }
2449 }
2450 }
2451
Evan Chenga6bff982010-01-30 01:22:00 +00002452 // If the callee takes no arguments then go on to check the results of the
2453 // call.
2454 if (!Outs.empty()) {
2455 // Check if stack adjustment is needed. For now, do not do this if any
2456 // argument is passed on the stack.
2457 SmallVector<CCValAssign, 16> ArgLocs;
2458 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2459 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00002460 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002461 if (CCInfo.getNextStackOffset()) {
2462 MachineFunction &MF = DAG.getMachineFunction();
2463 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2464 return false;
2465 if (Subtarget->isTargetWin64())
2466 // Win64 ABI has additional complications.
2467 return false;
2468
2469 // Check if the arguments are already laid out in the right way as
2470 // the caller's fixed stack objects.
2471 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002472 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2473 const X86InstrInfo *TII =
2474 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002475 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2476 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002477 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002478 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002479 if (VA.getLocInfo() == CCValAssign::Indirect)
2480 return false;
2481 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002482 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2483 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002484 return false;
2485 }
2486 }
2487 }
Evan Cheng9c044672010-05-29 01:35:22 +00002488
2489 // If the tailcall address may be in a register, then make sure it's
2490 // possible to register allocate for it. In 32-bit, the call address can
2491 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002492 // callee-saved registers are restored. These happen to be the same
2493 // registers used to pass 'inreg' arguments so watch out for those.
2494 if (!Subtarget->is64Bit() &&
2495 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002496 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002497 unsigned NumInRegs = 0;
2498 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2499 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002500 if (!VA.isRegLoc())
2501 continue;
2502 unsigned Reg = VA.getLocReg();
2503 switch (Reg) {
2504 default: break;
2505 case X86::EAX: case X86::EDX: case X86::ECX:
2506 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002507 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002508 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002509 }
2510 }
2511 }
Evan Chenga6bff982010-01-30 01:22:00 +00002512 }
Evan Chengb1712452010-01-27 06:25:16 +00002513
Dale Johannesend155d7e2010-10-25 22:17:05 +00002514 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002515 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002516 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2517 return false;
2518
Evan Cheng86809cc2010-02-03 03:28:02 +00002519 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002520}
2521
Dan Gohman3df24e62008-09-03 23:12:08 +00002522FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002523X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2524 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002525}
2526
2527
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002528//===----------------------------------------------------------------------===//
2529// Other Lowering Hooks
2530//===----------------------------------------------------------------------===//
2531
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002532static bool MayFoldLoad(SDValue Op) {
2533 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2534}
2535
2536static bool MayFoldIntoStore(SDValue Op) {
2537 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2538}
2539
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002540static bool isTargetShuffle(unsigned Opcode) {
2541 switch(Opcode) {
2542 default: return false;
2543 case X86ISD::PSHUFD:
2544 case X86ISD::PSHUFHW:
2545 case X86ISD::PSHUFLW:
2546 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002547 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002548 case X86ISD::SHUFPS:
2549 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002550 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002551 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002552 case X86ISD::MOVLPS:
2553 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002554 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002555 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002556 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002557 case X86ISD::MOVSS:
2558 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002559 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002560 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002561 case X86ISD::PUNPCKLWD:
2562 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002563 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002564 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002565 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002566 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002567 case X86ISD::PUNPCKHWD:
2568 case X86ISD::PUNPCKHBW:
2569 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002570 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002571 return true;
2572 }
2573 return false;
2574}
2575
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002576static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002577 SDValue V1, SelectionDAG &DAG) {
2578 switch(Opc) {
2579 default: llvm_unreachable("Unknown x86 shuffle node");
2580 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002581 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002582 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002583 return DAG.getNode(Opc, dl, VT, V1);
2584 }
2585
2586 return SDValue();
2587}
2588
2589static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002590 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002591 switch(Opc) {
2592 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002593 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002594 case X86ISD::PSHUFHW:
2595 case X86ISD::PSHUFLW:
2596 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2597 }
2598
2599 return SDValue();
2600}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002601
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002602static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2603 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2604 switch(Opc) {
2605 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002606 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002607 case X86ISD::SHUFPD:
2608 case X86ISD::SHUFPS:
2609 return DAG.getNode(Opc, dl, VT, V1, V2,
2610 DAG.getConstant(TargetMask, MVT::i8));
2611 }
2612 return SDValue();
2613}
2614
2615static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2616 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2617 switch(Opc) {
2618 default: llvm_unreachable("Unknown x86 shuffle node");
2619 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002620 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002621 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002622 case X86ISD::MOVLPS:
2623 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002624 case X86ISD::MOVSS:
2625 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002626 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002627 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002628 case X86ISD::PUNPCKLWD:
2629 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002630 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002631 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002632 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002633 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002634 case X86ISD::PUNPCKHWD:
2635 case X86ISD::PUNPCKHBW:
2636 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002637 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002638 return DAG.getNode(Opc, dl, VT, V1, V2);
2639 }
2640 return SDValue();
2641}
2642
Dan Gohmand858e902010-04-17 15:26:15 +00002643SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002644 MachineFunction &MF = DAG.getMachineFunction();
2645 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2646 int ReturnAddrIndex = FuncInfo->getRAIndex();
2647
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002648 if (ReturnAddrIndex == 0) {
2649 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002650 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002651 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002652 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002653 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002654 }
2655
Evan Cheng25ab6902006-09-08 06:48:29 +00002656 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002657}
2658
2659
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002660bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2661 bool hasSymbolicDisplacement) {
2662 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002663 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002664 return false;
2665
2666 // If we don't have a symbolic displacement - we don't have any extra
2667 // restrictions.
2668 if (!hasSymbolicDisplacement)
2669 return true;
2670
2671 // FIXME: Some tweaks might be needed for medium code model.
2672 if (M != CodeModel::Small && M != CodeModel::Kernel)
2673 return false;
2674
2675 // For small code model we assume that latest object is 16MB before end of 31
2676 // bits boundary. We may also accept pretty large negative constants knowing
2677 // that all objects are in the positive half of address space.
2678 if (M == CodeModel::Small && Offset < 16*1024*1024)
2679 return true;
2680
2681 // For kernel code model we know that all object resist in the negative half
2682 // of 32bits address space. We may not accept negative offsets, since they may
2683 // be just off and we may accept pretty large positive ones.
2684 if (M == CodeModel::Kernel && Offset > 0)
2685 return true;
2686
2687 return false;
2688}
2689
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002690/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2691/// specific condition code, returning the condition code and the LHS/RHS of the
2692/// comparison to make.
2693static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2694 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002695 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002696 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2697 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2698 // X > -1 -> X == 0, jump !sign.
2699 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002700 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002701 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2702 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002703 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002704 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002705 // X < 1 -> X <= 0
2706 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002707 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002708 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002709 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002710
Evan Chengd9558e02006-01-06 00:43:03 +00002711 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002712 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002713 case ISD::SETEQ: return X86::COND_E;
2714 case ISD::SETGT: return X86::COND_G;
2715 case ISD::SETGE: return X86::COND_GE;
2716 case ISD::SETLT: return X86::COND_L;
2717 case ISD::SETLE: return X86::COND_LE;
2718 case ISD::SETNE: return X86::COND_NE;
2719 case ISD::SETULT: return X86::COND_B;
2720 case ISD::SETUGT: return X86::COND_A;
2721 case ISD::SETULE: return X86::COND_BE;
2722 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002723 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002724 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002725
Chris Lattner4c78e022008-12-23 23:42:27 +00002726 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002727
Chris Lattner4c78e022008-12-23 23:42:27 +00002728 // If LHS is a foldable load, but RHS is not, flip the condition.
2729 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2730 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2731 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2732 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002733 }
2734
Chris Lattner4c78e022008-12-23 23:42:27 +00002735 switch (SetCCOpcode) {
2736 default: break;
2737 case ISD::SETOLT:
2738 case ISD::SETOLE:
2739 case ISD::SETUGT:
2740 case ISD::SETUGE:
2741 std::swap(LHS, RHS);
2742 break;
2743 }
2744
2745 // On a floating point condition, the flags are set as follows:
2746 // ZF PF CF op
2747 // 0 | 0 | 0 | X > Y
2748 // 0 | 0 | 1 | X < Y
2749 // 1 | 0 | 0 | X == Y
2750 // 1 | 1 | 1 | unordered
2751 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002752 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002753 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002754 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002755 case ISD::SETOLT: // flipped
2756 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002757 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002758 case ISD::SETOLE: // flipped
2759 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002760 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002761 case ISD::SETUGT: // flipped
2762 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002763 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002764 case ISD::SETUGE: // flipped
2765 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002766 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002767 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002768 case ISD::SETNE: return X86::COND_NE;
2769 case ISD::SETUO: return X86::COND_P;
2770 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002771 case ISD::SETOEQ:
2772 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002773 }
Evan Chengd9558e02006-01-06 00:43:03 +00002774}
2775
Evan Cheng4a460802006-01-11 00:33:36 +00002776/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2777/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002778/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002779static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002780 switch (X86CC) {
2781 default:
2782 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002783 case X86::COND_B:
2784 case X86::COND_BE:
2785 case X86::COND_E:
2786 case X86::COND_P:
2787 case X86::COND_A:
2788 case X86::COND_AE:
2789 case X86::COND_NE:
2790 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002791 return true;
2792 }
2793}
2794
Evan Chengeb2f9692009-10-27 19:56:55 +00002795/// isFPImmLegal - Returns true if the target can instruction select the
2796/// specified FP immediate natively. If false, the legalizer will
2797/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002798bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002799 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2800 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2801 return true;
2802 }
2803 return false;
2804}
2805
Nate Begeman9008ca62009-04-27 18:41:29 +00002806/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2807/// the specified range (L, H].
2808static bool isUndefOrInRange(int Val, int Low, int Hi) {
2809 return (Val < 0) || (Val >= Low && Val < Hi);
2810}
2811
2812/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2813/// specified value.
2814static bool isUndefOrEqual(int Val, int CmpVal) {
2815 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002816 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002817 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002818}
2819
Nate Begeman9008ca62009-04-27 18:41:29 +00002820/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2821/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2822/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002823static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002824 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002825 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002826 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002827 return (Mask[0] < 2 && Mask[1] < 2);
2828 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002829}
2830
Nate Begeman9008ca62009-04-27 18:41:29 +00002831bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002832 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002833 N->getMask(M);
2834 return ::isPSHUFDMask(M, N->getValueType(0));
2835}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002836
Nate Begeman9008ca62009-04-27 18:41:29 +00002837/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2838/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002839static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002840 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002841 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002842
Nate Begeman9008ca62009-04-27 18:41:29 +00002843 // Lower quadword copied in order or undef.
2844 for (int i = 0; i != 4; ++i)
2845 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002846 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002847
Evan Cheng506d3df2006-03-29 23:07:14 +00002848 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002849 for (int i = 4; i != 8; ++i)
2850 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002851 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002852
Evan Cheng506d3df2006-03-29 23:07:14 +00002853 return true;
2854}
2855
Nate Begeman9008ca62009-04-27 18:41:29 +00002856bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002857 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002858 N->getMask(M);
2859 return ::isPSHUFHWMask(M, N->getValueType(0));
2860}
Evan Cheng506d3df2006-03-29 23:07:14 +00002861
Nate Begeman9008ca62009-04-27 18:41:29 +00002862/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2863/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002864static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002865 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002866 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002867
Rafael Espindola15684b22009-04-24 12:40:33 +00002868 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 for (int i = 4; i != 8; ++i)
2870 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002871 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002872
Rafael Espindola15684b22009-04-24 12:40:33 +00002873 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002874 for (int i = 0; i != 4; ++i)
2875 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002876 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002877
Rafael Espindola15684b22009-04-24 12:40:33 +00002878 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002879}
2880
Nate Begeman9008ca62009-04-27 18:41:29 +00002881bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002882 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002883 N->getMask(M);
2884 return ::isPSHUFLWMask(M, N->getValueType(0));
2885}
2886
Nate Begemana09008b2009-10-19 02:17:23 +00002887/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2888/// is suitable for input to PALIGNR.
2889static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2890 bool hasSSSE3) {
2891 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002892
Nate Begemana09008b2009-10-19 02:17:23 +00002893 // Do not handle v2i64 / v2f64 shuffles with palignr.
2894 if (e < 4 || !hasSSSE3)
2895 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002896
Nate Begemana09008b2009-10-19 02:17:23 +00002897 for (i = 0; i != e; ++i)
2898 if (Mask[i] >= 0)
2899 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002900
Nate Begemana09008b2009-10-19 02:17:23 +00002901 // All undef, not a palignr.
2902 if (i == e)
2903 return false;
2904
2905 // Determine if it's ok to perform a palignr with only the LHS, since we
2906 // don't have access to the actual shuffle elements to see if RHS is undef.
2907 bool Unary = Mask[i] < (int)e;
2908 bool NeedsUnary = false;
2909
2910 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002911
Nate Begemana09008b2009-10-19 02:17:23 +00002912 // Check the rest of the elements to see if they are consecutive.
2913 for (++i; i != e; ++i) {
2914 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002915 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002916 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002917
Nate Begemana09008b2009-10-19 02:17:23 +00002918 Unary = Unary && (m < (int)e);
2919 NeedsUnary = NeedsUnary || (m < s);
2920
2921 if (NeedsUnary && !Unary)
2922 return false;
2923 if (Unary && m != ((s+i) & (e-1)))
2924 return false;
2925 if (!Unary && m != (s+i))
2926 return false;
2927 }
2928 return true;
2929}
2930
2931bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2932 SmallVector<int, 8> M;
2933 N->getMask(M);
2934 return ::isPALIGNRMask(M, N->getValueType(0), true);
2935}
2936
Evan Cheng14aed5e2006-03-24 01:18:28 +00002937/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2938/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002939static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002940 int NumElems = VT.getVectorNumElements();
2941 if (NumElems != 2 && NumElems != 4)
2942 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 int Half = NumElems / 2;
2945 for (int i = 0; i < Half; ++i)
2946 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002947 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002948 for (int i = Half; i < NumElems; ++i)
2949 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002950 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002951
Evan Cheng14aed5e2006-03-24 01:18:28 +00002952 return true;
2953}
2954
Nate Begeman9008ca62009-04-27 18:41:29 +00002955bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2956 SmallVector<int, 8> M;
2957 N->getMask(M);
2958 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002959}
2960
Evan Cheng213d2cf2007-05-17 18:45:50 +00002961/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002962/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2963/// half elements to come from vector 1 (which would equal the dest.) and
2964/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002965static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002966 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002967
2968 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002969 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002970
Nate Begeman9008ca62009-04-27 18:41:29 +00002971 int Half = NumElems / 2;
2972 for (int i = 0; i < Half; ++i)
2973 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002974 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002975 for (int i = Half; i < NumElems; ++i)
2976 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002977 return false;
2978 return true;
2979}
2980
Nate Begeman9008ca62009-04-27 18:41:29 +00002981static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2982 SmallVector<int, 8> M;
2983 N->getMask(M);
2984 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002985}
2986
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002987/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2988/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002989bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2990 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002991 return false;
2992
Evan Cheng2064a2b2006-03-28 06:50:32 +00002993 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002994 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2995 isUndefOrEqual(N->getMaskElt(1), 7) &&
2996 isUndefOrEqual(N->getMaskElt(2), 2) &&
2997 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002998}
2999
Nate Begeman0b10b912009-11-07 23:17:15 +00003000/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3001/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3002/// <2, 3, 2, 3>
3003bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3004 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003005
Nate Begeman0b10b912009-11-07 23:17:15 +00003006 if (NumElems != 4)
3007 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003008
Nate Begeman0b10b912009-11-07 23:17:15 +00003009 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3010 isUndefOrEqual(N->getMaskElt(1), 3) &&
3011 isUndefOrEqual(N->getMaskElt(2), 2) &&
3012 isUndefOrEqual(N->getMaskElt(3), 3);
3013}
3014
Evan Cheng5ced1d82006-04-06 23:23:56 +00003015/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3016/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003017bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3018 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003019
Evan Cheng5ced1d82006-04-06 23:23:56 +00003020 if (NumElems != 2 && NumElems != 4)
3021 return false;
3022
Evan Chengc5cdff22006-04-07 21:53:05 +00003023 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003024 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003025 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003026
Evan Chengc5cdff22006-04-07 21:53:05 +00003027 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003029 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003030
3031 return true;
3032}
3033
Nate Begeman0b10b912009-11-07 23:17:15 +00003034/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3035/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3036bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003037 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003038
Evan Cheng5ced1d82006-04-06 23:23:56 +00003039 if (NumElems != 2 && NumElems != 4)
3040 return false;
3041
Evan Chengc5cdff22006-04-07 21:53:05 +00003042 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003044 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003045
Nate Begeman9008ca62009-04-27 18:41:29 +00003046 for (unsigned i = 0; i < NumElems/2; ++i)
3047 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003048 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003049
3050 return true;
3051}
3052
Evan Cheng0038e592006-03-28 00:39:58 +00003053/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3054/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003055static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003056 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003057 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003058 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003059 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003060
Nate Begeman9008ca62009-04-27 18:41:29 +00003061 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3062 int BitI = Mask[i];
3063 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003064 if (!isUndefOrEqual(BitI, j))
3065 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003066 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003067 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003068 return false;
3069 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003070 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003071 return false;
3072 }
Evan Cheng0038e592006-03-28 00:39:58 +00003073 }
Evan Cheng0038e592006-03-28 00:39:58 +00003074 return true;
3075}
3076
Nate Begeman9008ca62009-04-27 18:41:29 +00003077bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3078 SmallVector<int, 8> M;
3079 N->getMask(M);
3080 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003081}
3082
Evan Cheng4fcb9222006-03-28 02:43:26 +00003083/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3084/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003085static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003086 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003087 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003088 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003089 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003090
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3092 int BitI = Mask[i];
3093 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003094 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003095 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003096 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003097 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003098 return false;
3099 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003100 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003101 return false;
3102 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003103 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003104 return true;
3105}
3106
Nate Begeman9008ca62009-04-27 18:41:29 +00003107bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3108 SmallVector<int, 8> M;
3109 N->getMask(M);
3110 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003111}
3112
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003113/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3114/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3115/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003116static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003117 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003118 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003119 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003120
Nate Begeman9008ca62009-04-27 18:41:29 +00003121 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3122 int BitI = Mask[i];
3123 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003124 if (!isUndefOrEqual(BitI, j))
3125 return false;
3126 if (!isUndefOrEqual(BitI1, j))
3127 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003128 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003129 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003130}
3131
Nate Begeman9008ca62009-04-27 18:41:29 +00003132bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3133 SmallVector<int, 8> M;
3134 N->getMask(M);
3135 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3136}
3137
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003138/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3139/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3140/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003141static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003143 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3144 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003145
Nate Begeman9008ca62009-04-27 18:41:29 +00003146 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3147 int BitI = Mask[i];
3148 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003149 if (!isUndefOrEqual(BitI, j))
3150 return false;
3151 if (!isUndefOrEqual(BitI1, j))
3152 return false;
3153 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003154 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003155}
3156
Nate Begeman9008ca62009-04-27 18:41:29 +00003157bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3158 SmallVector<int, 8> M;
3159 N->getMask(M);
3160 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3161}
3162
Evan Cheng017dcc62006-04-21 01:05:10 +00003163/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3164/// specifies a shuffle of elements that is suitable for input to MOVSS,
3165/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003166static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003167 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003168 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003169
3170 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003171
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003173 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003174
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 for (int i = 1; i < NumElts; ++i)
3176 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003177 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003178
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003179 return true;
3180}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003181
Nate Begeman9008ca62009-04-27 18:41:29 +00003182bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3183 SmallVector<int, 8> M;
3184 N->getMask(M);
3185 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003186}
3187
Evan Cheng017dcc62006-04-21 01:05:10 +00003188/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3189/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003190/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003191static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 bool V2IsSplat = false, bool V2IsUndef = false) {
3193 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003194 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003195 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003196
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003198 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003199
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 for (int i = 1; i < NumOps; ++i)
3201 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3202 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3203 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003204 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003205
Evan Cheng39623da2006-04-20 08:58:49 +00003206 return true;
3207}
3208
Nate Begeman9008ca62009-04-27 18:41:29 +00003209static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003210 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 SmallVector<int, 8> M;
3212 N->getMask(M);
3213 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003214}
3215
Evan Chengd9539472006-04-14 21:59:03 +00003216/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3217/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003218bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3219 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003220 return false;
3221
3222 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003223 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 int Elt = N->getMaskElt(i);
3225 if (Elt >= 0 && Elt != 1)
3226 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003227 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003228
3229 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003230 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003231 int Elt = N->getMaskElt(i);
3232 if (Elt >= 0 && Elt != 3)
3233 return false;
3234 if (Elt == 3)
3235 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003236 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003237 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003238 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003239 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003240}
3241
3242/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3243/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003244bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3245 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003246 return false;
3247
3248 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 for (unsigned i = 0; i < 2; ++i)
3250 if (N->getMaskElt(i) > 0)
3251 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003252
3253 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003254 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 int Elt = N->getMaskElt(i);
3256 if (Elt >= 0 && Elt != 2)
3257 return false;
3258 if (Elt == 2)
3259 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003260 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003261 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003262 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003263}
3264
Evan Cheng0b457f02008-09-25 20:50:48 +00003265/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3266/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003267bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3268 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003269
Nate Begeman9008ca62009-04-27 18:41:29 +00003270 for (int i = 0; i < e; ++i)
3271 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003272 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 for (int i = 0; i < e; ++i)
3274 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003275 return false;
3276 return true;
3277}
3278
Evan Cheng63d33002006-03-22 08:01:21 +00003279/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003280/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003281unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003282 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3283 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3284
Evan Chengb9df0ca2006-03-22 02:53:00 +00003285 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3286 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 for (int i = 0; i < NumOperands; ++i) {
3288 int Val = SVOp->getMaskElt(NumOperands-i-1);
3289 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003290 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003291 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003292 if (i != NumOperands - 1)
3293 Mask <<= Shift;
3294 }
Evan Cheng63d33002006-03-22 08:01:21 +00003295 return Mask;
3296}
3297
Evan Cheng506d3df2006-03-29 23:07:14 +00003298/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003299/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003300unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003301 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003302 unsigned Mask = 0;
3303 // 8 nodes, but we only care about the last 4.
3304 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003305 int Val = SVOp->getMaskElt(i);
3306 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003307 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003308 if (i != 4)
3309 Mask <<= 2;
3310 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003311 return Mask;
3312}
3313
3314/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003315/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003316unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003317 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003318 unsigned Mask = 0;
3319 // 8 nodes, but we only care about the first 4.
3320 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003321 int Val = SVOp->getMaskElt(i);
3322 if (Val >= 0)
3323 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003324 if (i != 0)
3325 Mask <<= 2;
3326 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003327 return Mask;
3328}
3329
Nate Begemana09008b2009-10-19 02:17:23 +00003330/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3331/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3332unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3333 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3334 EVT VVT = N->getValueType(0);
3335 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3336 int Val = 0;
3337
3338 unsigned i, e;
3339 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3340 Val = SVOp->getMaskElt(i);
3341 if (Val >= 0)
3342 break;
3343 }
3344 return (Val - i) * EltSize;
3345}
3346
Evan Cheng37b73872009-07-30 08:33:02 +00003347/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3348/// constant +0.0.
3349bool X86::isZeroNode(SDValue Elt) {
3350 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003351 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003352 (isa<ConstantFPSDNode>(Elt) &&
3353 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3354}
3355
Nate Begeman9008ca62009-04-27 18:41:29 +00003356/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3357/// their permute mask.
3358static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3359 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003360 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003361 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003363
Nate Begeman5a5ca152009-04-29 05:20:52 +00003364 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003365 int idx = SVOp->getMaskElt(i);
3366 if (idx < 0)
3367 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003368 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003370 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003371 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003372 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003373 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3374 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003375}
3376
Evan Cheng779ccea2007-12-07 21:30:01 +00003377/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3378/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003379static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003380 unsigned NumElems = VT.getVectorNumElements();
3381 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 int idx = Mask[i];
3383 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003384 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003385 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003387 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003389 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003390}
3391
Evan Cheng533a0aa2006-04-19 20:35:22 +00003392/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3393/// match movhlps. The lower half elements should come from upper half of
3394/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003395/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003396static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3397 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003398 return false;
3399 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003401 return false;
3402 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003403 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003404 return false;
3405 return true;
3406}
3407
Evan Cheng5ced1d82006-04-06 23:23:56 +00003408/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003409/// is promoted to a vector. It also returns the LoadSDNode by reference if
3410/// required.
3411static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003412 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3413 return false;
3414 N = N->getOperand(0).getNode();
3415 if (!ISD::isNON_EXTLoad(N))
3416 return false;
3417 if (LD)
3418 *LD = cast<LoadSDNode>(N);
3419 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003420}
3421
Evan Cheng533a0aa2006-04-19 20:35:22 +00003422/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3423/// match movlp{s|d}. The lower half elements should come from lower half of
3424/// V1 (and in order), and the upper half elements should come from the upper
3425/// half of V2 (and in order). And since V1 will become the source of the
3426/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003427static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3428 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003429 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003430 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003431 // Is V2 is a vector load, don't do this transformation. We will try to use
3432 // load folding shufps op.
3433 if (ISD::isNON_EXTLoad(V2))
3434 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003435
Nate Begeman5a5ca152009-04-29 05:20:52 +00003436 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003437
Evan Cheng533a0aa2006-04-19 20:35:22 +00003438 if (NumElems != 2 && NumElems != 4)
3439 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003440 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003442 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003443 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003444 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003445 return false;
3446 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447}
3448
Evan Cheng39623da2006-04-20 08:58:49 +00003449/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3450/// all the same.
3451static bool isSplatVector(SDNode *N) {
3452 if (N->getOpcode() != ISD::BUILD_VECTOR)
3453 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003454
Dan Gohman475871a2008-07-27 21:46:04 +00003455 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003456 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3457 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458 return false;
3459 return true;
3460}
3461
Evan Cheng213d2cf2007-05-17 18:45:50 +00003462/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003463/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003464/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003465static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003466 SDValue V1 = N->getOperand(0);
3467 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003468 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3469 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003470 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003471 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003473 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3474 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003475 if (Opc != ISD::BUILD_VECTOR ||
3476 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 return false;
3478 } else if (Idx >= 0) {
3479 unsigned Opc = V1.getOpcode();
3480 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3481 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003482 if (Opc != ISD::BUILD_VECTOR ||
3483 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003484 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003485 }
3486 }
3487 return true;
3488}
3489
3490/// getZeroVector - Returns a vector of specified type with all zero elements.
3491///
Owen Andersone50ed302009-08-10 22:56:29 +00003492static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003493 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003494 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003495
Dale Johannesen0488fb62010-09-30 23:57:10 +00003496 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003497 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003498 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003499 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003500 if (HasSSE2) { // SSE2
3501 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3502 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3503 } else { // SSE1
3504 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3505 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3506 }
3507 } else if (VT.getSizeInBits() == 256) { // AVX
3508 // 256-bit logic and arithmetic instructions in AVX are
3509 // all floating-point, no support for integer ops. Default
3510 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003512 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3513 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003514 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003515 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003516}
3517
Chris Lattner8a594482007-11-25 00:24:49 +00003518/// getOnesVector - Returns a vector of specified type with all bits set.
3519///
Owen Andersone50ed302009-08-10 22:56:29 +00003520static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003521 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003522
Chris Lattner8a594482007-11-25 00:24:49 +00003523 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3524 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003526 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003527 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003528 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003529}
3530
3531
Evan Cheng39623da2006-04-20 08:58:49 +00003532/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3533/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003534static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003535 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003536 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003537
Evan Cheng39623da2006-04-20 08:58:49 +00003538 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003539 SmallVector<int, 8> MaskVec;
3540 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003541
Nate Begeman5a5ca152009-04-29 05:20:52 +00003542 for (unsigned i = 0; i != NumElems; ++i) {
3543 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003544 MaskVec[i] = NumElems;
3545 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003546 }
Evan Cheng39623da2006-04-20 08:58:49 +00003547 }
Evan Cheng39623da2006-04-20 08:58:49 +00003548 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003549 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3550 SVOp->getOperand(1), &MaskVec[0]);
3551 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003552}
3553
Evan Cheng017dcc62006-04-21 01:05:10 +00003554/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3555/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003556static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003557 SDValue V2) {
3558 unsigned NumElems = VT.getVectorNumElements();
3559 SmallVector<int, 8> Mask;
3560 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003561 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003562 Mask.push_back(i);
3563 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003564}
3565
Nate Begeman9008ca62009-04-27 18:41:29 +00003566/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003567static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003568 SDValue V2) {
3569 unsigned NumElems = VT.getVectorNumElements();
3570 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003571 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003572 Mask.push_back(i);
3573 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003574 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003575 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003576}
3577
Nate Begeman9008ca62009-04-27 18:41:29 +00003578/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003579static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003580 SDValue V2) {
3581 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003582 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003584 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003585 Mask.push_back(i + Half);
3586 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003587 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003588 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003589}
3590
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003591/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3592static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003593 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003594 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 DebugLoc dl = SV->getDebugLoc();
3596 SDValue V1 = SV->getOperand(0);
3597 int NumElems = VT.getVectorNumElements();
3598 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003599
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 // unpack elements to the correct location
3601 while (NumElems > 4) {
3602 if (EltNo < NumElems/2) {
3603 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3604 } else {
3605 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3606 EltNo -= NumElems/2;
3607 }
3608 NumElems >>= 1;
3609 }
Eric Christopherfd179292009-08-27 18:07:15 +00003610
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 // Perform the splat.
3612 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003613 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003615 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003616}
3617
Evan Chengba05f722006-04-21 23:03:30 +00003618/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003619/// vector of zero or undef vector. This produces a shuffle where the low
3620/// element of V2 is swizzled into the zero/undef vector, landing at element
3621/// 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 +00003622static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003623 bool isZero, bool HasSSE2,
3624 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003625 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003626 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003627 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3628 unsigned NumElems = VT.getVectorNumElements();
3629 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003630 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003631 // If this is the insertion idx, put the low elt of V2 here.
3632 MaskVec.push_back(i == Idx ? NumElems : i);
3633 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003634}
3635
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003636/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3637/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003638SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3639 unsigned Depth) {
3640 if (Depth == 6)
3641 return SDValue(); // Limit search depth.
3642
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003643 SDValue V = SDValue(N, 0);
3644 EVT VT = V.getValueType();
3645 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003646
3647 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3648 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3649 Index = SV->getMaskElt(Index);
3650
3651 if (Index < 0)
3652 return DAG.getUNDEF(VT.getVectorElementType());
3653
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003654 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003655 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003656 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003657 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003658
3659 // Recurse into target specific vector shuffles to find scalars.
3660 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003661 int NumElems = VT.getVectorNumElements();
3662 SmallVector<unsigned, 16> ShuffleMask;
3663 SDValue ImmN;
3664
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003665 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003666 case X86ISD::SHUFPS:
3667 case X86ISD::SHUFPD:
3668 ImmN = N->getOperand(N->getNumOperands()-1);
3669 DecodeSHUFPSMask(NumElems,
3670 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3671 ShuffleMask);
3672 break;
3673 case X86ISD::PUNPCKHBW:
3674 case X86ISD::PUNPCKHWD:
3675 case X86ISD::PUNPCKHDQ:
3676 case X86ISD::PUNPCKHQDQ:
3677 DecodePUNPCKHMask(NumElems, ShuffleMask);
3678 break;
3679 case X86ISD::UNPCKHPS:
3680 case X86ISD::UNPCKHPD:
3681 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3682 break;
3683 case X86ISD::PUNPCKLBW:
3684 case X86ISD::PUNPCKLWD:
3685 case X86ISD::PUNPCKLDQ:
3686 case X86ISD::PUNPCKLQDQ:
3687 DecodePUNPCKLMask(NumElems, ShuffleMask);
3688 break;
3689 case X86ISD::UNPCKLPS:
3690 case X86ISD::UNPCKLPD:
3691 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3692 break;
3693 case X86ISD::MOVHLPS:
3694 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3695 break;
3696 case X86ISD::MOVLHPS:
3697 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3698 break;
3699 case X86ISD::PSHUFD:
3700 ImmN = N->getOperand(N->getNumOperands()-1);
3701 DecodePSHUFMask(NumElems,
3702 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3703 ShuffleMask);
3704 break;
3705 case X86ISD::PSHUFHW:
3706 ImmN = N->getOperand(N->getNumOperands()-1);
3707 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3708 ShuffleMask);
3709 break;
3710 case X86ISD::PSHUFLW:
3711 ImmN = N->getOperand(N->getNumOperands()-1);
3712 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3713 ShuffleMask);
3714 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003715 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003716 case X86ISD::MOVSD: {
3717 // The index 0 always comes from the first element of the second source,
3718 // this is why MOVSS and MOVSD are used in the first place. The other
3719 // elements come from the other positions of the first source vector.
3720 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003721 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3722 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003723 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003724 default:
3725 assert("not implemented for target shuffle node");
3726 return SDValue();
3727 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003728
3729 Index = ShuffleMask[Index];
3730 if (Index < 0)
3731 return DAG.getUNDEF(VT.getVectorElementType());
3732
3733 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3734 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3735 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003736 }
3737
3738 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003739 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003740 V = V.getOperand(0);
3741 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003742 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003743
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003744 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003745 return SDValue();
3746 }
3747
3748 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3749 return (Index == 0) ? V.getOperand(0)
3750 : DAG.getUNDEF(VT.getVectorElementType());
3751
3752 if (V.getOpcode() == ISD::BUILD_VECTOR)
3753 return V.getOperand(Index);
3754
3755 return SDValue();
3756}
3757
3758/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3759/// shuffle operation which come from a consecutively from a zero. The
3760/// search can start in two diferent directions, from left or right.
3761static
3762unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3763 bool ZerosFromLeft, SelectionDAG &DAG) {
3764 int i = 0;
3765
3766 while (i < NumElems) {
3767 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003768 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003769 if (!(Elt.getNode() &&
3770 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3771 break;
3772 ++i;
3773 }
3774
3775 return i;
3776}
3777
3778/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3779/// MaskE correspond consecutively to elements from one of the vector operands,
3780/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3781static
3782bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3783 int OpIdx, int NumElems, unsigned &OpNum) {
3784 bool SeenV1 = false;
3785 bool SeenV2 = false;
3786
3787 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3788 int Idx = SVOp->getMaskElt(i);
3789 // Ignore undef indicies
3790 if (Idx < 0)
3791 continue;
3792
3793 if (Idx < NumElems)
3794 SeenV1 = true;
3795 else
3796 SeenV2 = true;
3797
3798 // Only accept consecutive elements from the same vector
3799 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3800 return false;
3801 }
3802
3803 OpNum = SeenV1 ? 0 : 1;
3804 return true;
3805}
3806
3807/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3808/// logical left shift of a vector.
3809static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3810 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3811 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3812 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3813 false /* check zeros from right */, DAG);
3814 unsigned OpSrc;
3815
3816 if (!NumZeros)
3817 return false;
3818
3819 // Considering the elements in the mask that are not consecutive zeros,
3820 // check if they consecutively come from only one of the source vectors.
3821 //
3822 // V1 = {X, A, B, C} 0
3823 // \ \ \ /
3824 // vector_shuffle V1, V2 <1, 2, 3, X>
3825 //
3826 if (!isShuffleMaskConsecutive(SVOp,
3827 0, // Mask Start Index
3828 NumElems-NumZeros-1, // Mask End Index
3829 NumZeros, // Where to start looking in the src vector
3830 NumElems, // Number of elements in vector
3831 OpSrc)) // Which source operand ?
3832 return false;
3833
3834 isLeft = false;
3835 ShAmt = NumZeros;
3836 ShVal = SVOp->getOperand(OpSrc);
3837 return true;
3838}
3839
3840/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3841/// logical left shift of a vector.
3842static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3843 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3844 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3845 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3846 true /* check zeros from left */, DAG);
3847 unsigned OpSrc;
3848
3849 if (!NumZeros)
3850 return false;
3851
3852 // Considering the elements in the mask that are not consecutive zeros,
3853 // check if they consecutively come from only one of the source vectors.
3854 //
3855 // 0 { A, B, X, X } = V2
3856 // / \ / /
3857 // vector_shuffle V1, V2 <X, X, 4, 5>
3858 //
3859 if (!isShuffleMaskConsecutive(SVOp,
3860 NumZeros, // Mask Start Index
3861 NumElems-1, // Mask End Index
3862 0, // Where to start looking in the src vector
3863 NumElems, // Number of elements in vector
3864 OpSrc)) // Which source operand ?
3865 return false;
3866
3867 isLeft = true;
3868 ShAmt = NumZeros;
3869 ShVal = SVOp->getOperand(OpSrc);
3870 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003871}
3872
3873/// isVectorShift - Returns true if the shuffle can be implemented as a
3874/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003875static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003876 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003877 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3878 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3879 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003880
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003881 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003882}
3883
Evan Chengc78d3b42006-04-24 18:01:45 +00003884/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3885///
Dan Gohman475871a2008-07-27 21:46:04 +00003886static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003887 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003888 SelectionDAG &DAG,
3889 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003890 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003891 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003892
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003893 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003894 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003895 bool First = true;
3896 for (unsigned i = 0; i < 16; ++i) {
3897 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3898 if (ThisIsNonZero && First) {
3899 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003900 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003901 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003902 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003903 First = false;
3904 }
3905
3906 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003907 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003908 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3909 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003910 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003911 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003912 }
3913 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003914 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3915 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3916 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003917 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003919 } else
3920 ThisElt = LastElt;
3921
Gabor Greifba36cb52008-08-28 21:40:38 +00003922 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003924 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003925 }
3926 }
3927
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003928 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003929}
3930
Bill Wendlinga348c562007-03-22 18:42:45 +00003931/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003932///
Dan Gohman475871a2008-07-27 21:46:04 +00003933static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003934 unsigned NumNonZero, unsigned NumZero,
3935 SelectionDAG &DAG,
3936 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003937 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003938 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003939
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003940 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003941 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003942 bool First = true;
3943 for (unsigned i = 0; i < 8; ++i) {
3944 bool isNonZero = (NonZeros & (1 << i)) != 0;
3945 if (isNonZero) {
3946 if (First) {
3947 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003948 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003949 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003950 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003951 First = false;
3952 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003953 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003955 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003956 }
3957 }
3958
3959 return V;
3960}
3961
Evan Chengf26ffe92008-05-29 08:22:04 +00003962/// getVShift - Return a vector logical shift node.
3963///
Owen Andersone50ed302009-08-10 22:56:29 +00003964static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003965 unsigned NumBits, SelectionDAG &DAG,
3966 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003967 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003968 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003969 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
3970 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003971 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003972 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003973}
3974
Dan Gohman475871a2008-07-27 21:46:04 +00003975SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003976X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003977 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00003978
Evan Chengc3630942009-12-09 21:00:30 +00003979 // Check if the scalar load can be widened into a vector load. And if
3980 // the address is "base + cst" see if the cst can be "absorbed" into
3981 // the shuffle mask.
3982 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3983 SDValue Ptr = LD->getBasePtr();
3984 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3985 return SDValue();
3986 EVT PVT = LD->getValueType(0);
3987 if (PVT != MVT::i32 && PVT != MVT::f32)
3988 return SDValue();
3989
3990 int FI = -1;
3991 int64_t Offset = 0;
3992 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3993 FI = FINode->getIndex();
3994 Offset = 0;
3995 } else if (Ptr.getOpcode() == ISD::ADD &&
3996 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3997 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3998 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3999 Offset = Ptr.getConstantOperandVal(1);
4000 Ptr = Ptr.getOperand(0);
4001 } else {
4002 return SDValue();
4003 }
4004
4005 SDValue Chain = LD->getChain();
4006 // Make sure the stack object alignment is at least 16.
4007 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4008 if (DAG.InferPtrAlignment(Ptr) < 16) {
4009 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004010 // Can't change the alignment. FIXME: It's possible to compute
4011 // the exact stack offset and reference FI + adjust offset instead.
4012 // If someone *really* cares about this. That's the way to implement it.
4013 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004014 } else {
4015 MFI->setObjectAlignment(FI, 16);
4016 }
4017 }
4018
4019 // (Offset % 16) must be multiple of 4. Then address is then
4020 // Ptr + (Offset & ~15).
4021 if (Offset < 0)
4022 return SDValue();
4023 if ((Offset % 16) & 3)
4024 return SDValue();
4025 int64_t StartOffset = Offset & ~15;
4026 if (StartOffset)
4027 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4028 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4029
4030 int EltNo = (Offset - StartOffset) >> 2;
4031 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4032 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004033 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4034 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004035 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004036 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004037 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4038 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004039 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004040 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004041 }
4042
4043 return SDValue();
4044}
4045
Michael J. Spencerec38de22010-10-10 22:04:20 +00004046/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4047/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004048/// load which has the same value as a build_vector whose operands are 'elts'.
4049///
4050/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004051///
Nate Begeman1449f292010-03-24 22:19:06 +00004052/// FIXME: we'd also like to handle the case where the last elements are zero
4053/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4054/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004055static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004056 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004057 EVT EltVT = VT.getVectorElementType();
4058 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004059
Nate Begemanfdea31a2010-03-24 20:49:50 +00004060 LoadSDNode *LDBase = NULL;
4061 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004062
Nate Begeman1449f292010-03-24 22:19:06 +00004063 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004064 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004065 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004066 for (unsigned i = 0; i < NumElems; ++i) {
4067 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004068
Nate Begemanfdea31a2010-03-24 20:49:50 +00004069 if (!Elt.getNode() ||
4070 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4071 return SDValue();
4072 if (!LDBase) {
4073 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4074 return SDValue();
4075 LDBase = cast<LoadSDNode>(Elt.getNode());
4076 LastLoadedElt = i;
4077 continue;
4078 }
4079 if (Elt.getOpcode() == ISD::UNDEF)
4080 continue;
4081
4082 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4083 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4084 return SDValue();
4085 LastLoadedElt = i;
4086 }
Nate Begeman1449f292010-03-24 22:19:06 +00004087
4088 // If we have found an entire vector of loads and undefs, then return a large
4089 // load of the entire vector width starting at the base pointer. If we found
4090 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004091 if (LastLoadedElt == NumElems - 1) {
4092 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004093 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004094 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004095 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004096 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004097 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004098 LDBase->isVolatile(), LDBase->isNonTemporal(),
4099 LDBase->getAlignment());
4100 } else if (NumElems == 4 && LastLoadedElt == 1) {
4101 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4102 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004103 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4104 Ops, 2, MVT::i32,
4105 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004106 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004107 }
4108 return SDValue();
4109}
4110
Evan Chengc3630942009-12-09 21:00:30 +00004111SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004112X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004113 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004114 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4115 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004116 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4117 // is present, so AllOnes is ignored.
4118 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4119 (Op.getValueType().getSizeInBits() != 256 &&
4120 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004121 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004122 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4123 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004124 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004125 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004126
Gabor Greifba36cb52008-08-28 21:40:38 +00004127 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004128 return getOnesVector(Op.getValueType(), DAG, dl);
4129 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004130 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004131
Owen Andersone50ed302009-08-10 22:56:29 +00004132 EVT VT = Op.getValueType();
4133 EVT ExtVT = VT.getVectorElementType();
4134 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004135
4136 unsigned NumElems = Op.getNumOperands();
4137 unsigned NumZero = 0;
4138 unsigned NumNonZero = 0;
4139 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004140 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004141 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004142 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004143 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004144 if (Elt.getOpcode() == ISD::UNDEF)
4145 continue;
4146 Values.insert(Elt);
4147 if (Elt.getOpcode() != ISD::Constant &&
4148 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004149 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004150 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004151 NumZero++;
4152 else {
4153 NonZeros |= (1 << i);
4154 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004155 }
4156 }
4157
Chris Lattner97a2a562010-08-26 05:24:29 +00004158 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4159 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004160 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004161
Chris Lattner67f453a2008-03-09 05:42:06 +00004162 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004163 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004164 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004165 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004166
Chris Lattner62098042008-03-09 01:05:04 +00004167 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4168 // the value are obviously zero, truncate the value to i32 and do the
4169 // insertion that way. Only do this if the value is non-constant or if the
4170 // value is a constant being inserted into element 0. It is cheaper to do
4171 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004173 (!IsAllConstants || Idx == 0)) {
4174 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004175 // Handle SSE only.
4176 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4177 EVT VecVT = MVT::v4i32;
4178 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004179
Chris Lattner62098042008-03-09 01:05:04 +00004180 // Truncate the value (which may itself be a constant) to i32, and
4181 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004183 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004184 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4185 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004186
Chris Lattner62098042008-03-09 01:05:04 +00004187 // Now we have our 32-bit value zero extended in the low element of
4188 // a vector. If Idx != 0, swizzle it into place.
4189 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004190 SmallVector<int, 4> Mask;
4191 Mask.push_back(Idx);
4192 for (unsigned i = 1; i != VecElts; ++i)
4193 Mask.push_back(i);
4194 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004195 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004196 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004197 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004198 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004199 }
4200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004201
Chris Lattner19f79692008-03-08 22:59:52 +00004202 // If we have a constant or non-constant insertion into the low element of
4203 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4204 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004205 // depending on what the source datatype is.
4206 if (Idx == 0) {
4207 if (NumZero == 0) {
4208 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004209 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4210 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004211 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4212 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4213 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4214 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004215 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4216 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004217 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4218 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004219 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4220 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4221 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004222 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004223 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004224 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004225
4226 // Is it a vector logical left shift?
4227 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004228 X86::isZeroNode(Op.getOperand(0)) &&
4229 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004230 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004231 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004232 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004233 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004234 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004235 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004236
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004237 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004238 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004239
Chris Lattner19f79692008-03-08 22:59:52 +00004240 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4241 // is a non-constant being inserted into an element other than the low one,
4242 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4243 // movd/movss) to move this into the low element, then shuffle it into
4244 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004245 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004246 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004247
Evan Cheng0db9fe62006-04-25 20:13:52 +00004248 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004249 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4250 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004251 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004252 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004253 MaskVec.push_back(i == Idx ? 0 : 1);
4254 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004255 }
4256 }
4257
Chris Lattner67f453a2008-03-09 05:42:06 +00004258 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004259 if (Values.size() == 1) {
4260 if (EVTBits == 32) {
4261 // Instead of a shuffle like this:
4262 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4263 // Check if it's possible to issue this instead.
4264 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4265 unsigned Idx = CountTrailingZeros_32(NonZeros);
4266 SDValue Item = Op.getOperand(Idx);
4267 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4268 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4269 }
Dan Gohman475871a2008-07-27 21:46:04 +00004270 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004272
Dan Gohmana3941172007-07-24 22:55:08 +00004273 // A vector full of immediates; various special cases are already
4274 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004275 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004276 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004277
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004278 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004279 if (EVTBits == 64) {
4280 if (NumNonZero == 1) {
4281 // One half is zero or undef.
4282 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004283 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004284 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004285 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4286 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004287 }
Dan Gohman475871a2008-07-27 21:46:04 +00004288 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004289 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004290
4291 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004292 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004293 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004294 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004295 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004296 }
4297
Bill Wendling826f36f2007-03-28 00:57:11 +00004298 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004299 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004300 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004301 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004302 }
4303
4304 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004305 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004306 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004307 if (NumElems == 4 && NumZero > 0) {
4308 for (unsigned i = 0; i < 4; ++i) {
4309 bool isZero = !(NonZeros & (1 << i));
4310 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004311 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004312 else
Dale Johannesenace16102009-02-03 19:33:06 +00004313 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004314 }
4315
4316 for (unsigned i = 0; i < 2; ++i) {
4317 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4318 default: break;
4319 case 0:
4320 V[i] = V[i*2]; // Must be a zero vector.
4321 break;
4322 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004323 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004324 break;
4325 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004327 break;
4328 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004329 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 break;
4331 }
4332 }
4333
Nate Begeman9008ca62009-04-27 18:41:29 +00004334 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004335 bool Reverse = (NonZeros & 0x3) == 2;
4336 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4339 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4341 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004342 }
4343
Nate Begemanfdea31a2010-03-24 20:49:50 +00004344 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4345 // Check for a build vector of consecutive loads.
4346 for (unsigned i = 0; i < NumElems; ++i)
4347 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004348
Nate Begemanfdea31a2010-03-24 20:49:50 +00004349 // Check for elements which are consecutive loads.
4350 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4351 if (LD.getNode())
4352 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004353
4354 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004355 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004356 SDValue Result;
4357 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4358 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4359 else
4360 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004361
Chris Lattner24faf612010-08-28 17:59:08 +00004362 for (unsigned i = 1; i < NumElems; ++i) {
4363 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4364 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004366 }
4367 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004369
Chris Lattner6e80e442010-08-28 17:15:43 +00004370 // Otherwise, expand into a number of unpckl*, start by extending each of
4371 // our (non-undef) elements to the full vector width with the element in the
4372 // bottom slot of the vector (which generates no code for SSE).
4373 for (unsigned i = 0; i < NumElems; ++i) {
4374 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4375 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4376 else
4377 V[i] = DAG.getUNDEF(VT);
4378 }
4379
4380 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004381 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4382 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4383 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004384 unsigned EltStride = NumElems >> 1;
4385 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004386 for (unsigned i = 0; i < EltStride; ++i) {
4387 // If V[i+EltStride] is undef and this is the first round of mixing,
4388 // then it is safe to just drop this shuffle: V[i] is already in the
4389 // right place, the one element (since it's the first round) being
4390 // inserted as undef can be dropped. This isn't safe for successive
4391 // rounds because they will permute elements within both vectors.
4392 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4393 EltStride == NumElems/2)
4394 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004395
Chris Lattner6e80e442010-08-28 17:15:43 +00004396 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004397 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004398 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004399 }
4400 return V[0];
4401 }
Dan Gohman475871a2008-07-27 21:46:04 +00004402 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004403}
4404
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004405SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004406X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004407 // We support concatenate two MMX registers and place them in a MMX
4408 // register. This is better than doing a stack convert.
4409 DebugLoc dl = Op.getDebugLoc();
4410 EVT ResVT = Op.getValueType();
4411 assert(Op.getNumOperands() == 2);
4412 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4413 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4414 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004415 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004416 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4417 InVec = Op.getOperand(1);
4418 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4419 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004420 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004421 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4422 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4423 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004424 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004425 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4426 Mask[0] = 0; Mask[1] = 2;
4427 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4428 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004429 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004430}
4431
Nate Begemanb9a47b82009-02-23 08:49:38 +00004432// v8i16 shuffles - Prefer shuffles in the following order:
4433// 1. [all] pshuflw, pshufhw, optional move
4434// 2. [ssse3] 1 x pshufb
4435// 3. [ssse3] 2 x pshufb + 1 x por
4436// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004437SDValue
4438X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4439 SelectionDAG &DAG) const {
4440 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 SDValue V1 = SVOp->getOperand(0);
4442 SDValue V2 = SVOp->getOperand(1);
4443 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004444 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004445
Nate Begemanb9a47b82009-02-23 08:49:38 +00004446 // Determine if more than 1 of the words in each of the low and high quadwords
4447 // of the result come from the same quadword of one of the two inputs. Undef
4448 // mask values count as coming from any quadword, for better codegen.
4449 SmallVector<unsigned, 4> LoQuad(4);
4450 SmallVector<unsigned, 4> HiQuad(4);
4451 BitVector InputQuads(4);
4452 for (unsigned i = 0; i < 8; ++i) {
4453 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004455 MaskVals.push_back(EltIdx);
4456 if (EltIdx < 0) {
4457 ++Quad[0];
4458 ++Quad[1];
4459 ++Quad[2];
4460 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004461 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004462 }
4463 ++Quad[EltIdx / 4];
4464 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004465 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004466
Nate Begemanb9a47b82009-02-23 08:49:38 +00004467 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004468 unsigned MaxQuad = 1;
4469 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004470 if (LoQuad[i] > MaxQuad) {
4471 BestLoQuad = i;
4472 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004473 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004474 }
4475
Nate Begemanb9a47b82009-02-23 08:49:38 +00004476 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004477 MaxQuad = 1;
4478 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004479 if (HiQuad[i] > MaxQuad) {
4480 BestHiQuad = i;
4481 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004482 }
4483 }
4484
Nate Begemanb9a47b82009-02-23 08:49:38 +00004485 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004486 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004487 // single pshufb instruction is necessary. If There are more than 2 input
4488 // quads, disable the next transformation since it does not help SSSE3.
4489 bool V1Used = InputQuads[0] || InputQuads[1];
4490 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004491 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004492 if (InputQuads.count() == 2 && V1Used && V2Used) {
4493 BestLoQuad = InputQuads.find_first();
4494 BestHiQuad = InputQuads.find_next(BestLoQuad);
4495 }
4496 if (InputQuads.count() > 2) {
4497 BestLoQuad = -1;
4498 BestHiQuad = -1;
4499 }
4500 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004501
Nate Begemanb9a47b82009-02-23 08:49:38 +00004502 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4503 // the shuffle mask. If a quad is scored as -1, that means that it contains
4504 // words from all 4 input quadwords.
4505 SDValue NewV;
4506 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004507 SmallVector<int, 8> MaskV;
4508 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4509 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004510 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004511 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4512 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4513 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004514
Nate Begemanb9a47b82009-02-23 08:49:38 +00004515 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4516 // source words for the shuffle, to aid later transformations.
4517 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004518 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004519 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004520 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004521 if (idx != (int)i)
4522 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004523 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004524 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004525 AllWordsInNewV = false;
4526 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004527 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004528
Nate Begemanb9a47b82009-02-23 08:49:38 +00004529 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4530 if (AllWordsInNewV) {
4531 for (int i = 0; i != 8; ++i) {
4532 int idx = MaskVals[i];
4533 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004534 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004535 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004536 if ((idx != i) && idx < 4)
4537 pshufhw = false;
4538 if ((idx != i) && idx > 3)
4539 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004540 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004541 V1 = NewV;
4542 V2Used = false;
4543 BestLoQuad = 0;
4544 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004545 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004546
Nate Begemanb9a47b82009-02-23 08:49:38 +00004547 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4548 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004549 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004550 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4551 unsigned TargetMask = 0;
4552 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004553 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004554 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4555 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4556 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004557 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004558 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004559 }
Eric Christopherfd179292009-08-27 18:07:15 +00004560
Nate Begemanb9a47b82009-02-23 08:49:38 +00004561 // If we have SSSE3, and all words of the result are from 1 input vector,
4562 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4563 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004564 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004565 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004566
Nate Begemanb9a47b82009-02-23 08:49:38 +00004567 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004568 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004569 // mask, and elements that come from V1 in the V2 mask, so that the two
4570 // results can be OR'd together.
4571 bool TwoInputs = V1Used && V2Used;
4572 for (unsigned i = 0; i != 8; ++i) {
4573 int EltIdx = MaskVals[i] * 2;
4574 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004575 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4576 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004577 continue;
4578 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004579 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4580 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004581 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004582 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004583 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004584 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004585 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004586 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004587 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004588
Nate Begemanb9a47b82009-02-23 08:49:38 +00004589 // Calculate the shuffle mask for the second input, shuffle it, and
4590 // OR it with the first shuffled input.
4591 pshufbMask.clear();
4592 for (unsigned i = 0; i != 8; ++i) {
4593 int EltIdx = MaskVals[i] * 2;
4594 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004595 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4596 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004597 continue;
4598 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004599 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4600 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004601 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004602 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004603 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004604 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004605 MVT::v16i8, &pshufbMask[0], 16));
4606 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004607 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004608 }
4609
4610 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4611 // and update MaskVals with new element order.
4612 BitVector InOrder(8);
4613 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004614 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004615 for (int i = 0; i != 4; ++i) {
4616 int idx = MaskVals[i];
4617 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004618 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004619 InOrder.set(i);
4620 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004621 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004622 InOrder.set(i);
4623 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004624 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004625 }
4626 }
4627 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004628 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004629 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004630 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004631
4632 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4633 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4634 NewV.getOperand(0),
4635 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4636 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004637 }
Eric Christopherfd179292009-08-27 18:07:15 +00004638
Nate Begemanb9a47b82009-02-23 08:49:38 +00004639 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4640 // and update MaskVals with the new element order.
4641 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004642 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004643 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004644 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004645 for (unsigned i = 4; i != 8; ++i) {
4646 int idx = MaskVals[i];
4647 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004648 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004649 InOrder.set(i);
4650 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004651 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004652 InOrder.set(i);
4653 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004655 }
4656 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004657 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004658 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004659
4660 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4661 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4662 NewV.getOperand(0),
4663 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4664 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004665 }
Eric Christopherfd179292009-08-27 18:07:15 +00004666
Nate Begemanb9a47b82009-02-23 08:49:38 +00004667 // In case BestHi & BestLo were both -1, which means each quadword has a word
4668 // from each of the four input quadwords, calculate the InOrder bitvector now
4669 // before falling through to the insert/extract cleanup.
4670 if (BestLoQuad == -1 && BestHiQuad == -1) {
4671 NewV = V1;
4672 for (int i = 0; i != 8; ++i)
4673 if (MaskVals[i] < 0 || MaskVals[i] == i)
4674 InOrder.set(i);
4675 }
Eric Christopherfd179292009-08-27 18:07:15 +00004676
Nate Begemanb9a47b82009-02-23 08:49:38 +00004677 // The other elements are put in the right place using pextrw and pinsrw.
4678 for (unsigned i = 0; i != 8; ++i) {
4679 if (InOrder[i])
4680 continue;
4681 int EltIdx = MaskVals[i];
4682 if (EltIdx < 0)
4683 continue;
4684 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004685 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004686 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004687 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004688 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004689 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004690 DAG.getIntPtrConstant(i));
4691 }
4692 return NewV;
4693}
4694
4695// v16i8 shuffles - Prefer shuffles in the following order:
4696// 1. [ssse3] 1 x pshufb
4697// 2. [ssse3] 2 x pshufb + 1 x por
4698// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4699static
Nate Begeman9008ca62009-04-27 18:41:29 +00004700SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004701 SelectionDAG &DAG,
4702 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004703 SDValue V1 = SVOp->getOperand(0);
4704 SDValue V2 = SVOp->getOperand(1);
4705 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004706 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004707 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004708
Nate Begemanb9a47b82009-02-23 08:49:38 +00004709 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004710 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004711 // present, fall back to case 3.
4712 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4713 bool V1Only = true;
4714 bool V2Only = true;
4715 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004716 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004717 if (EltIdx < 0)
4718 continue;
4719 if (EltIdx < 16)
4720 V2Only = false;
4721 else
4722 V1Only = false;
4723 }
Eric Christopherfd179292009-08-27 18:07:15 +00004724
Nate Begemanb9a47b82009-02-23 08:49:38 +00004725 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4726 if (TLI.getSubtarget()->hasSSSE3()) {
4727 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004728
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004730 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004731 //
4732 // Otherwise, we have elements from both input vectors, and must zero out
4733 // elements that come from V2 in the first mask, and V1 in the second mask
4734 // so that we can OR them together.
4735 bool TwoInputs = !(V1Only || V2Only);
4736 for (unsigned i = 0; i != 16; ++i) {
4737 int EltIdx = MaskVals[i];
4738 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004740 continue;
4741 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004742 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004743 }
4744 // If all the elements are from V2, assign it to V1 and return after
4745 // building the first pshufb.
4746 if (V2Only)
4747 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004748 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004749 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004750 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004751 if (!TwoInputs)
4752 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004753
Nate Begemanb9a47b82009-02-23 08:49:38 +00004754 // Calculate the shuffle mask for the second input, shuffle it, and
4755 // OR it with the first shuffled input.
4756 pshufbMask.clear();
4757 for (unsigned i = 0; i != 16; ++i) {
4758 int EltIdx = MaskVals[i];
4759 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004760 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004761 continue;
4762 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004763 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004764 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004765 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004766 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004767 MVT::v16i8, &pshufbMask[0], 16));
4768 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004769 }
Eric Christopherfd179292009-08-27 18:07:15 +00004770
Nate Begemanb9a47b82009-02-23 08:49:38 +00004771 // No SSSE3 - Calculate in place words and then fix all out of place words
4772 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4773 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004774 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4775 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004776 SDValue NewV = V2Only ? V2 : V1;
4777 for (int i = 0; i != 8; ++i) {
4778 int Elt0 = MaskVals[i*2];
4779 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004780
Nate Begemanb9a47b82009-02-23 08:49:38 +00004781 // This word of the result is all undef, skip it.
4782 if (Elt0 < 0 && Elt1 < 0)
4783 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004784
Nate Begemanb9a47b82009-02-23 08:49:38 +00004785 // This word of the result is already in the correct place, skip it.
4786 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4787 continue;
4788 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4789 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004790
Nate Begemanb9a47b82009-02-23 08:49:38 +00004791 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4792 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4793 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004794
4795 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4796 // using a single extract together, load it and store it.
4797 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004798 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004799 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004800 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004801 DAG.getIntPtrConstant(i));
4802 continue;
4803 }
4804
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004806 // source byte is not also odd, shift the extracted word left 8 bits
4807 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004809 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004810 DAG.getIntPtrConstant(Elt1 / 2));
4811 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004814 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004815 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4816 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 }
4818 // If Elt0 is defined, extract it from the appropriate source. If the
4819 // source byte is not also even, shift the extracted word right 8 bits. If
4820 // Elt1 was also defined, OR the extracted values together before
4821 // inserting them in the result.
4822 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004823 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004824 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4825 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004828 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4830 DAG.getConstant(0x00FF, MVT::i16));
4831 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 : InsElt0;
4833 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 DAG.getIntPtrConstant(i));
4836 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004837 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004838}
4839
Evan Cheng7a831ce2007-12-15 03:00:47 +00004840/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004841/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004842/// done when every pair / quad of shuffle mask elements point to elements in
4843/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004844/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004845static
Nate Begeman9008ca62009-04-27 18:41:29 +00004846SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004847 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004848 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004849 SDValue V1 = SVOp->getOperand(0);
4850 SDValue V2 = SVOp->getOperand(1);
4851 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004852 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004853 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004854 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004855 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 case MVT::v4f32: NewVT = MVT::v2f64; break;
4857 case MVT::v4i32: NewVT = MVT::v2i64; break;
4858 case MVT::v8i16: NewVT = MVT::v4i32; break;
4859 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004860 }
4861
Nate Begeman9008ca62009-04-27 18:41:29 +00004862 int Scale = NumElems / NewWidth;
4863 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004864 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004865 int StartIdx = -1;
4866 for (int j = 0; j < Scale; ++j) {
4867 int EltIdx = SVOp->getMaskElt(i+j);
4868 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004869 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004870 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004871 StartIdx = EltIdx - (EltIdx % Scale);
4872 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004873 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004874 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004875 if (StartIdx == -1)
4876 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004877 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004878 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004879 }
4880
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004881 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4882 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004883 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004884}
4885
Evan Chengd880b972008-05-09 21:53:03 +00004886/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004887///
Owen Andersone50ed302009-08-10 22:56:29 +00004888static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004889 SDValue SrcOp, SelectionDAG &DAG,
4890 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004891 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004892 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004893 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004894 LD = dyn_cast<LoadSDNode>(SrcOp);
4895 if (!LD) {
4896 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4897 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004898 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004899 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004900 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004901 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004902 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004903 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004905 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004906 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4907 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4908 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004909 SrcOp.getOperand(0)
4910 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004911 }
4912 }
4913 }
4914
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004915 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004916 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004917 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004918 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004919}
4920
Evan Chengace3c172008-07-22 21:13:36 +00004921/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4922/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004923static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004924LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4925 SDValue V1 = SVOp->getOperand(0);
4926 SDValue V2 = SVOp->getOperand(1);
4927 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004928 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004929
Evan Chengace3c172008-07-22 21:13:36 +00004930 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004931 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004932 SmallVector<int, 8> Mask1(4U, -1);
4933 SmallVector<int, 8> PermMask;
4934 SVOp->getMask(PermMask);
4935
Evan Chengace3c172008-07-22 21:13:36 +00004936 unsigned NumHi = 0;
4937 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004938 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004939 int Idx = PermMask[i];
4940 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004941 Locs[i] = std::make_pair(-1, -1);
4942 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4944 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004945 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004946 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004947 NumLo++;
4948 } else {
4949 Locs[i] = std::make_pair(1, NumHi);
4950 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004951 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004952 NumHi++;
4953 }
4954 }
4955 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004956
Evan Chengace3c172008-07-22 21:13:36 +00004957 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004958 // If no more than two elements come from either vector. This can be
4959 // implemented with two shuffles. First shuffle gather the elements.
4960 // The second shuffle, which takes the first shuffle as both of its
4961 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004962 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004963
Nate Begeman9008ca62009-04-27 18:41:29 +00004964 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004965
Evan Chengace3c172008-07-22 21:13:36 +00004966 for (unsigned i = 0; i != 4; ++i) {
4967 if (Locs[i].first == -1)
4968 continue;
4969 else {
4970 unsigned Idx = (i < 2) ? 0 : 4;
4971 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004972 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004973 }
4974 }
4975
Nate Begeman9008ca62009-04-27 18:41:29 +00004976 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004977 } else if (NumLo == 3 || NumHi == 3) {
4978 // Otherwise, we must have three elements from one vector, call it X, and
4979 // one element from the other, call it Y. First, use a shufps to build an
4980 // intermediate vector with the one element from Y and the element from X
4981 // that will be in the same half in the final destination (the indexes don't
4982 // matter). Then, use a shufps to build the final vector, taking the half
4983 // containing the element from Y from the intermediate, and the other half
4984 // from X.
4985 if (NumHi == 3) {
4986 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004987 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004988 std::swap(V1, V2);
4989 }
4990
4991 // Find the element from V2.
4992 unsigned HiIndex;
4993 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004994 int Val = PermMask[HiIndex];
4995 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004996 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004997 if (Val >= 4)
4998 break;
4999 }
5000
Nate Begeman9008ca62009-04-27 18:41:29 +00005001 Mask1[0] = PermMask[HiIndex];
5002 Mask1[1] = -1;
5003 Mask1[2] = PermMask[HiIndex^1];
5004 Mask1[3] = -1;
5005 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005006
5007 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005008 Mask1[0] = PermMask[0];
5009 Mask1[1] = PermMask[1];
5010 Mask1[2] = HiIndex & 1 ? 6 : 4;
5011 Mask1[3] = HiIndex & 1 ? 4 : 6;
5012 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005013 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005014 Mask1[0] = HiIndex & 1 ? 2 : 0;
5015 Mask1[1] = HiIndex & 1 ? 0 : 2;
5016 Mask1[2] = PermMask[2];
5017 Mask1[3] = PermMask[3];
5018 if (Mask1[2] >= 0)
5019 Mask1[2] += 4;
5020 if (Mask1[3] >= 0)
5021 Mask1[3] += 4;
5022 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005023 }
Evan Chengace3c172008-07-22 21:13:36 +00005024 }
5025
5026 // Break it into (shuffle shuffle_hi, shuffle_lo).
5027 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005028 SmallVector<int,8> LoMask(4U, -1);
5029 SmallVector<int,8> HiMask(4U, -1);
5030
5031 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005032 unsigned MaskIdx = 0;
5033 unsigned LoIdx = 0;
5034 unsigned HiIdx = 2;
5035 for (unsigned i = 0; i != 4; ++i) {
5036 if (i == 2) {
5037 MaskPtr = &HiMask;
5038 MaskIdx = 1;
5039 LoIdx = 0;
5040 HiIdx = 2;
5041 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005042 int Idx = PermMask[i];
5043 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005044 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005045 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005046 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005047 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005048 LoIdx++;
5049 } else {
5050 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005051 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005052 HiIdx++;
5053 }
5054 }
5055
Nate Begeman9008ca62009-04-27 18:41:29 +00005056 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5057 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5058 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005059 for (unsigned i = 0; i != 4; ++i) {
5060 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005061 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005062 } else {
5063 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005064 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005065 }
5066 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005067 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005068}
5069
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005070static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005071 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005072 V = V.getOperand(0);
5073 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5074 V = V.getOperand(0);
5075 if (MayFoldLoad(V))
5076 return true;
5077 return false;
5078}
5079
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005080// FIXME: the version above should always be used. Since there's
5081// a bug where several vector shuffles can't be folded because the
5082// DAG is not updated during lowering and a node claims to have two
5083// uses while it only has one, use this version, and let isel match
5084// another instruction if the load really happens to have more than
5085// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005086// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005087static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005088 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005089 V = V.getOperand(0);
5090 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5091 V = V.getOperand(0);
5092 if (ISD::isNormalLoad(V.getNode()))
5093 return true;
5094 return false;
5095}
5096
5097/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5098/// a vector extract, and if both can be later optimized into a single load.
5099/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5100/// here because otherwise a target specific shuffle node is going to be
5101/// emitted for this shuffle, and the optimization not done.
5102/// FIXME: This is probably not the best approach, but fix the problem
5103/// until the right path is decided.
5104static
5105bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5106 const TargetLowering &TLI) {
5107 EVT VT = V.getValueType();
5108 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5109
5110 // Be sure that the vector shuffle is present in a pattern like this:
5111 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5112 if (!V.hasOneUse())
5113 return false;
5114
5115 SDNode *N = *V.getNode()->use_begin();
5116 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5117 return false;
5118
5119 SDValue EltNo = N->getOperand(1);
5120 if (!isa<ConstantSDNode>(EltNo))
5121 return false;
5122
5123 // If the bit convert changed the number of elements, it is unsafe
5124 // to examine the mask.
5125 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005126 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005127 EVT SrcVT = V.getOperand(0).getValueType();
5128 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5129 return false;
5130 V = V.getOperand(0);
5131 HasShuffleIntoBitcast = true;
5132 }
5133
5134 // Select the input vector, guarding against out of range extract vector.
5135 unsigned NumElems = VT.getVectorNumElements();
5136 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5137 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5138 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5139
5140 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005141 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005142 V = V.getOperand(0);
5143
5144 if (ISD::isNormalLoad(V.getNode())) {
5145 // Is the original load suitable?
5146 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5147
5148 // FIXME: avoid the multi-use bug that is preventing lots of
5149 // of foldings to be detected, this is still wrong of course, but
5150 // give the temporary desired behavior, and if it happens that
5151 // the load has real more uses, during isel it will not fold, and
5152 // will generate poor code.
5153 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5154 return false;
5155
5156 if (!HasShuffleIntoBitcast)
5157 return true;
5158
5159 // If there's a bitcast before the shuffle, check if the load type and
5160 // alignment is valid.
5161 unsigned Align = LN0->getAlignment();
5162 unsigned NewAlign =
5163 TLI.getTargetData()->getABITypeAlignment(
5164 VT.getTypeForEVT(*DAG.getContext()));
5165
5166 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5167 return false;
5168 }
5169
5170 return true;
5171}
5172
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005173static
Evan Cheng835580f2010-10-07 20:50:20 +00005174SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5175 EVT VT = Op.getValueType();
5176
5177 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005178 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5179 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005180 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5181 V1, DAG));
5182}
5183
5184static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005185SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5186 bool HasSSE2) {
5187 SDValue V1 = Op.getOperand(0);
5188 SDValue V2 = Op.getOperand(1);
5189 EVT VT = Op.getValueType();
5190
5191 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5192
5193 if (HasSSE2 && VT == MVT::v2f64)
5194 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5195
5196 // v4f32 or v4i32
5197 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5198}
5199
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005200static
5201SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5202 SDValue V1 = Op.getOperand(0);
5203 SDValue V2 = Op.getOperand(1);
5204 EVT VT = Op.getValueType();
5205
5206 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5207 "unsupported shuffle type");
5208
5209 if (V2.getOpcode() == ISD::UNDEF)
5210 V2 = V1;
5211
5212 // v4i32 or v4f32
5213 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5214}
5215
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005216static
5217SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5218 SDValue V1 = Op.getOperand(0);
5219 SDValue V2 = Op.getOperand(1);
5220 EVT VT = Op.getValueType();
5221 unsigned NumElems = VT.getVectorNumElements();
5222
5223 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5224 // operand of these instructions is only memory, so check if there's a
5225 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5226 // same masks.
5227 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005228
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005229 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005230 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005231 CanFoldLoad = true;
5232
5233 // When V1 is a load, it can be folded later into a store in isel, example:
5234 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5235 // turns into:
5236 // (MOVLPSmr addr:$src1, VR128:$src2)
5237 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005238 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005239 CanFoldLoad = true;
5240
5241 if (CanFoldLoad) {
5242 if (HasSSE2 && NumElems == 2)
5243 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5244
5245 if (NumElems == 4)
5246 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5247 }
5248
5249 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5250 // movl and movlp will both match v2i64, but v2i64 is never matched by
5251 // movl earlier because we make it strict to avoid messing with the movlp load
5252 // folding logic (see the code above getMOVLP call). Match it here then,
5253 // this is horrible, but will stay like this until we move all shuffle
5254 // matching to x86 specific nodes. Note that for the 1st condition all
5255 // types are matched with movsd.
5256 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5257 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5258 else if (HasSSE2)
5259 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5260
5261
5262 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5263
5264 // Invert the operand order and use SHUFPS to match it.
5265 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5266 X86::getShuffleSHUFImmediate(SVOp), DAG);
5267}
5268
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005269static inline unsigned getUNPCKLOpcode(EVT VT) {
5270 switch(VT.getSimpleVT().SimpleTy) {
5271 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5272 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5273 case MVT::v4f32: return X86ISD::UNPCKLPS;
5274 case MVT::v2f64: return X86ISD::UNPCKLPD;
5275 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5276 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5277 default:
5278 llvm_unreachable("Unknow type for unpckl");
5279 }
5280 return 0;
5281}
5282
5283static inline unsigned getUNPCKHOpcode(EVT VT) {
5284 switch(VT.getSimpleVT().SimpleTy) {
5285 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5286 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5287 case MVT::v4f32: return X86ISD::UNPCKHPS;
5288 case MVT::v2f64: return X86ISD::UNPCKHPD;
5289 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5290 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5291 default:
5292 llvm_unreachable("Unknow type for unpckh");
5293 }
5294 return 0;
5295}
5296
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005297static
5298SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005299 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005300 const X86Subtarget *Subtarget) {
5301 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5302 EVT VT = Op.getValueType();
5303 DebugLoc dl = Op.getDebugLoc();
5304 SDValue V1 = Op.getOperand(0);
5305 SDValue V2 = Op.getOperand(1);
5306
5307 if (isZeroShuffle(SVOp))
5308 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5309
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005310 // Handle splat operations
5311 if (SVOp->isSplat()) {
5312 // Special case, this is the only place now where it's
5313 // allowed to return a vector_shuffle operation without
5314 // using a target specific node, because *hopefully* it
5315 // will be optimized away by the dag combiner.
5316 if (VT.getVectorNumElements() <= 4 &&
5317 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5318 return Op;
5319
5320 // Handle splats by matching through known masks
5321 if (VT.getVectorNumElements() <= 4)
5322 return SDValue();
5323
Evan Cheng835580f2010-10-07 20:50:20 +00005324 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005325 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005326 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005327
5328 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5329 // do it!
5330 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5331 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5332 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005333 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005334 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5335 // FIXME: Figure out a cleaner way to do this.
5336 // Try to make use of movq to zero out the top part.
5337 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5338 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5339 if (NewOp.getNode()) {
5340 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5341 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5342 DAG, Subtarget, dl);
5343 }
5344 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5345 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5346 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5347 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5348 DAG, Subtarget, dl);
5349 }
5350 }
5351 return SDValue();
5352}
5353
Dan Gohman475871a2008-07-27 21:46:04 +00005354SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005355X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005356 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005357 SDValue V1 = Op.getOperand(0);
5358 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005359 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005360 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005361 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005362 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005363 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5364 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005365 bool V1IsSplat = false;
5366 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005367 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005368 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005369 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005370 MachineFunction &MF = DAG.getMachineFunction();
5371 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005372
Dale Johannesen0488fb62010-09-30 23:57:10 +00005373 // Shuffle operations on MMX not supported.
5374 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005375 return Op;
5376
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005377 // Vector shuffle lowering takes 3 steps:
5378 //
5379 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5380 // narrowing and commutation of operands should be handled.
5381 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5382 // shuffle nodes.
5383 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5384 // so the shuffle can be broken into other shuffles and the legalizer can
5385 // try the lowering again.
5386 //
5387 // The general ideia is that no vector_shuffle operation should be left to
5388 // be matched during isel, all of them must be converted to a target specific
5389 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005390
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005391 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5392 // narrowing and commutation of operands should be handled. The actual code
5393 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005394 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005395 if (NewOp.getNode())
5396 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005397
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005398 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5399 // unpckh_undef). Only use pshufd if speed is more important than size.
5400 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5401 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5402 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5403 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5404 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5405 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005406
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005407 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005408 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005409 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005410
Dale Johannesen0488fb62010-09-30 23:57:10 +00005411 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005412 return getMOVHighToLow(Op, dl, DAG);
5413
5414 // Use to match splats
5415 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5416 (VT == MVT::v2f64 || VT == MVT::v2i64))
5417 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5418
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005419 if (X86::isPSHUFDMask(SVOp)) {
5420 // The actual implementation will match the mask in the if above and then
5421 // during isel it can match several different instructions, not only pshufd
5422 // as its name says, sad but true, emulate the behavior for now...
5423 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5424 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5425
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005426 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5427
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005428 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005429 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5430
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005431 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005432 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5433 TargetMask, DAG);
5434
5435 if (VT == MVT::v4f32)
5436 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5437 TargetMask, DAG);
5438 }
Eric Christopherfd179292009-08-27 18:07:15 +00005439
Evan Chengf26ffe92008-05-29 08:22:04 +00005440 // Check if this can be converted into a logical shift.
5441 bool isLeft = false;
5442 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005443 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005444 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005445 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005446 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005447 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005448 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005449 EVT EltVT = VT.getVectorElementType();
5450 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005451 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005452 }
Eric Christopherfd179292009-08-27 18:07:15 +00005453
Nate Begeman9008ca62009-04-27 18:41:29 +00005454 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005455 if (V1IsUndef)
5456 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005457 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005458 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005459 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005460 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005461 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5462
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005463 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005464 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5465 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005466 }
Eric Christopherfd179292009-08-27 18:07:15 +00005467
Nate Begeman9008ca62009-04-27 18:41:29 +00005468 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005469 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5470 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005471
Dale Johannesen0488fb62010-09-30 23:57:10 +00005472 if (X86::isMOVHLPSMask(SVOp))
5473 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005474
Dale Johannesen0488fb62010-09-30 23:57:10 +00005475 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5476 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005477
Dale Johannesen0488fb62010-09-30 23:57:10 +00005478 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5479 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005480
Dale Johannesen0488fb62010-09-30 23:57:10 +00005481 if (X86::isMOVLPMask(SVOp))
5482 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005483
Nate Begeman9008ca62009-04-27 18:41:29 +00005484 if (ShouldXformToMOVHLPS(SVOp) ||
5485 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5486 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487
Evan Chengf26ffe92008-05-29 08:22:04 +00005488 if (isShift) {
5489 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005490 EVT EltVT = VT.getVectorElementType();
5491 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005492 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005493 }
Eric Christopherfd179292009-08-27 18:07:15 +00005494
Evan Cheng9eca5e82006-10-25 21:49:50 +00005495 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005496 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5497 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005498 V1IsSplat = isSplatVector(V1.getNode());
5499 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005500
Chris Lattner8a594482007-11-25 00:24:49 +00005501 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005502 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005503 Op = CommuteVectorShuffle(SVOp, DAG);
5504 SVOp = cast<ShuffleVectorSDNode>(Op);
5505 V1 = SVOp->getOperand(0);
5506 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005507 std::swap(V1IsSplat, V2IsSplat);
5508 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005509 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005510 }
5511
Nate Begeman9008ca62009-04-27 18:41:29 +00005512 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5513 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005514 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 return V1;
5516 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5517 // the instruction selector will not match, so get a canonical MOVL with
5518 // swapped operands to undo the commute.
5519 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005520 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005521
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005522 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005523 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005524
5525 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005526 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005527
Evan Cheng9bbbb982006-10-25 20:48:19 +00005528 if (V2IsSplat) {
5529 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005530 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005531 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005532 SDValue NewMask = NormalizeMask(SVOp, DAG);
5533 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5534 if (NSVOp != SVOp) {
5535 if (X86::isUNPCKLMask(NSVOp, true)) {
5536 return NewMask;
5537 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5538 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005539 }
5540 }
5541 }
5542
Evan Cheng9eca5e82006-10-25 21:49:50 +00005543 if (Commuted) {
5544 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005545 // FIXME: this seems wrong.
5546 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5547 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005548
5549 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005550 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005551
5552 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005553 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005554 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005555
Nate Begeman9008ca62009-04-27 18:41:29 +00005556 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005557 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005558 return CommuteVectorShuffle(SVOp, DAG);
5559
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005560 // The checks below are all present in isShuffleMaskLegal, but they are
5561 // inlined here right now to enable us to directly emit target specific
5562 // nodes, and remove one by one until they don't return Op anymore.
5563 SmallVector<int, 16> M;
5564 SVOp->getMask(M);
5565
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005566 if (isPALIGNRMask(M, VT, HasSSSE3))
5567 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5568 X86::getShufflePALIGNRImmediate(SVOp),
5569 DAG);
5570
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005571 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5572 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5573 if (VT == MVT::v2f64)
5574 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5575 if (VT == MVT::v2i64)
5576 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5577 }
5578
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005579 if (isPSHUFHWMask(M, VT))
5580 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5581 X86::getShufflePSHUFHWImmediate(SVOp),
5582 DAG);
5583
5584 if (isPSHUFLWMask(M, VT))
5585 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5586 X86::getShufflePSHUFLWImmediate(SVOp),
5587 DAG);
5588
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005589 if (isSHUFPMask(M, VT)) {
5590 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5591 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5592 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5593 TargetMask, DAG);
5594 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5595 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5596 TargetMask, DAG);
5597 }
5598
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005599 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5600 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5601 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5602 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5603 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5604 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5605
Evan Cheng14b32e12007-12-11 01:46:18 +00005606 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005607 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005608 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005609 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005610 return NewOp;
5611 }
5612
Owen Anderson825b72b2009-08-11 20:47:22 +00005613 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005614 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005615 if (NewOp.getNode())
5616 return NewOp;
5617 }
Eric Christopherfd179292009-08-27 18:07:15 +00005618
Dale Johannesen0488fb62010-09-30 23:57:10 +00005619 // Handle all 4 wide cases with a number of shuffles.
5620 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005621 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005622
Dan Gohman475871a2008-07-27 21:46:04 +00005623 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005624}
5625
Dan Gohman475871a2008-07-27 21:46:04 +00005626SDValue
5627X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005628 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005629 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005630 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005631 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005632 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005633 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005634 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005635 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005636 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005637 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005638 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5639 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5640 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005641 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5642 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005643 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005644 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005645 Op.getOperand(0)),
5646 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005647 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005648 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005649 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005650 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005651 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005652 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005653 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5654 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005655 // result has a single use which is a store or a bitcast to i32. And in
5656 // the case of a store, it's not worth it if the index is a constant 0,
5657 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005658 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005659 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005660 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005661 if ((User->getOpcode() != ISD::STORE ||
5662 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5663 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005664 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005665 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005666 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005667 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005668 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005669 Op.getOperand(0)),
5670 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005671 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005672 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005673 // ExtractPS works with constant index.
5674 if (isa<ConstantSDNode>(Op.getOperand(1)))
5675 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005676 }
Dan Gohman475871a2008-07-27 21:46:04 +00005677 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005678}
5679
5680
Dan Gohman475871a2008-07-27 21:46:04 +00005681SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005682X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5683 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005684 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005685 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005686
Evan Cheng62a3f152008-03-24 21:52:23 +00005687 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005688 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005689 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005690 return Res;
5691 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005692
Owen Andersone50ed302009-08-10 22:56:29 +00005693 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005694 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005695 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005696 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005697 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005698 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005699 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005700 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5701 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005702 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005703 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005704 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005705 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005706 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005707 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005708 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005709 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005710 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005711 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005712 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005713 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005714 if (Idx == 0)
5715 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005716
Evan Cheng0db9fe62006-04-25 20:13:52 +00005717 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005718 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005719 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005720 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005721 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005722 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005723 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005724 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005725 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5726 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5727 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005728 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005729 if (Idx == 0)
5730 return Op;
5731
5732 // UNPCKHPD the element to the lowest double word, then movsd.
5733 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5734 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005735 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005736 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005737 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005738 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005739 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005740 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005741 }
5742
Dan Gohman475871a2008-07-27 21:46:04 +00005743 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005744}
5745
Dan Gohman475871a2008-07-27 21:46:04 +00005746SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005747X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5748 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005749 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005750 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005751 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005752
Dan Gohman475871a2008-07-27 21:46:04 +00005753 SDValue N0 = Op.getOperand(0);
5754 SDValue N1 = Op.getOperand(1);
5755 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005756
Dan Gohman8a55ce42009-09-23 21:02:20 +00005757 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005758 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005759 unsigned Opc;
5760 if (VT == MVT::v8i16)
5761 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005762 else if (VT == MVT::v16i8)
5763 Opc = X86ISD::PINSRB;
5764 else
5765 Opc = X86ISD::PINSRB;
5766
Nate Begeman14d12ca2008-02-11 04:19:36 +00005767 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5768 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005769 if (N1.getValueType() != MVT::i32)
5770 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5771 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005772 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005773 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005774 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005775 // Bits [7:6] of the constant are the source select. This will always be
5776 // zero here. The DAG Combiner may combine an extract_elt index into these
5777 // bits. For example (insert (extract, 3), 2) could be matched by putting
5778 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005779 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005780 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005781 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005782 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005783 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005784 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005785 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005786 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005787 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005788 // PINSR* works with constant index.
5789 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005790 }
Dan Gohman475871a2008-07-27 21:46:04 +00005791 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005792}
5793
Dan Gohman475871a2008-07-27 21:46:04 +00005794SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005795X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005796 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005797 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005798
5799 if (Subtarget->hasSSE41())
5800 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5801
Dan Gohman8a55ce42009-09-23 21:02:20 +00005802 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005803 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005804
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005805 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005806 SDValue N0 = Op.getOperand(0);
5807 SDValue N1 = Op.getOperand(1);
5808 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005809
Dan Gohman8a55ce42009-09-23 21:02:20 +00005810 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005811 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5812 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005813 if (N1.getValueType() != MVT::i32)
5814 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5815 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005816 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005817 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005818 }
Dan Gohman475871a2008-07-27 21:46:04 +00005819 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005820}
5821
Dan Gohman475871a2008-07-27 21:46:04 +00005822SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005823X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005824 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005825
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005826 if (Op.getValueType() == MVT::v1i64 &&
5827 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005829
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005831 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5832 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005833 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005834 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005835}
5836
Bill Wendling056292f2008-09-16 21:48:12 +00005837// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5838// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5839// one of the above mentioned nodes. It has to be wrapped because otherwise
5840// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5841// be used to form addressing mode. These wrapped nodes will be selected
5842// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005843SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005844X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005845 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005846
Chris Lattner41621a22009-06-26 19:22:52 +00005847 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5848 // global base reg.
5849 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005850 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005851 CodeModel::Model M = getTargetMachine().getCodeModel();
5852
Chris Lattner4f066492009-07-11 20:29:19 +00005853 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005854 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005855 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005856 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005857 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005858 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005859 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005860
Evan Cheng1606e8e2009-03-13 07:51:59 +00005861 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005862 CP->getAlignment(),
5863 CP->getOffset(), OpFlag);
5864 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005865 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005866 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005867 if (OpFlag) {
5868 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005869 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005870 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005871 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005872 }
5873
5874 return Result;
5875}
5876
Dan Gohmand858e902010-04-17 15:26:15 +00005877SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005878 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005879
Chris Lattner18c59872009-06-27 04:16:01 +00005880 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5881 // global base reg.
5882 unsigned char OpFlag = 0;
5883 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005884 CodeModel::Model M = getTargetMachine().getCodeModel();
5885
Chris Lattner4f066492009-07-11 20:29:19 +00005886 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005887 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005888 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005889 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005890 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005891 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005892 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005893
Chris Lattner18c59872009-06-27 04:16:01 +00005894 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5895 OpFlag);
5896 DebugLoc DL = JT->getDebugLoc();
5897 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005898
Chris Lattner18c59872009-06-27 04:16:01 +00005899 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00005900 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00005901 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5902 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005903 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005904 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005905
Chris Lattner18c59872009-06-27 04:16:01 +00005906 return Result;
5907}
5908
5909SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005910X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005911 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005912
Chris Lattner18c59872009-06-27 04:16:01 +00005913 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5914 // global base reg.
5915 unsigned char OpFlag = 0;
5916 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005917 CodeModel::Model M = getTargetMachine().getCodeModel();
5918
Chris Lattner4f066492009-07-11 20:29:19 +00005919 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005920 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005921 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005922 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005923 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005924 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005925 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005926
Chris Lattner18c59872009-06-27 04:16:01 +00005927 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005928
Chris Lattner18c59872009-06-27 04:16:01 +00005929 DebugLoc DL = Op.getDebugLoc();
5930 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005931
5932
Chris Lattner18c59872009-06-27 04:16:01 +00005933 // With PIC, the address is actually $g + Offset.
5934 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005935 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005936 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5937 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005938 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005939 Result);
5940 }
Eric Christopherfd179292009-08-27 18:07:15 +00005941
Chris Lattner18c59872009-06-27 04:16:01 +00005942 return Result;
5943}
5944
Dan Gohman475871a2008-07-27 21:46:04 +00005945SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005946X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005947 // Create the TargetBlockAddressAddress node.
5948 unsigned char OpFlags =
5949 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005950 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005951 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005952 DebugLoc dl = Op.getDebugLoc();
5953 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5954 /*isTarget=*/true, OpFlags);
5955
Dan Gohmanf705adb2009-10-30 01:28:02 +00005956 if (Subtarget->isPICStyleRIPRel() &&
5957 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005958 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5959 else
5960 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005961
Dan Gohman29cbade2009-11-20 23:18:13 +00005962 // With PIC, the address is actually $g + Offset.
5963 if (isGlobalRelativeToPICBase(OpFlags)) {
5964 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5965 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5966 Result);
5967 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005968
5969 return Result;
5970}
5971
5972SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005973X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005974 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005975 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005976 // Create the TargetGlobalAddress node, folding in the constant
5977 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005978 unsigned char OpFlags =
5979 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005980 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005981 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005982 if (OpFlags == X86II::MO_NO_FLAG &&
5983 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005984 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00005985 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005986 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005987 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00005988 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005989 }
Eric Christopherfd179292009-08-27 18:07:15 +00005990
Chris Lattner4f066492009-07-11 20:29:19 +00005991 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005992 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005993 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5994 else
5995 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005996
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005997 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005998 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005999 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6000 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006001 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006002 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006003
Chris Lattner36c25012009-07-10 07:34:39 +00006004 // For globals that require a load from a stub to get the address, emit the
6005 // load.
6006 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006007 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006008 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006009
Dan Gohman6520e202008-10-18 02:06:02 +00006010 // If there was a non-zero offset that we didn't fold, create an explicit
6011 // addition for it.
6012 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006013 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006014 DAG.getConstant(Offset, getPointerTy()));
6015
Evan Cheng0db9fe62006-04-25 20:13:52 +00006016 return Result;
6017}
6018
Evan Chengda43bcf2008-09-24 00:05:32 +00006019SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006020X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006021 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006022 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006023 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006024}
6025
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006026static SDValue
6027GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006028 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006029 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006030 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00006031 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006032 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006033 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006034 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006035 GA->getOffset(),
6036 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006037 if (InFlag) {
6038 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006039 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006040 } else {
6041 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006042 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006043 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006044
6045 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006046 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006047
Rafael Espindola15f1b662009-04-24 12:59:40 +00006048 SDValue Flag = Chain.getValue(1);
6049 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006050}
6051
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006052// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006053static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006054LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006055 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006056 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006057 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6058 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006059 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006060 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006061 InFlag = Chain.getValue(1);
6062
Chris Lattnerb903bed2009-06-26 21:20:29 +00006063 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006064}
6065
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006066// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006067static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006068LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006069 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006070 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6071 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006072}
6073
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006074// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6075// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006076static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006077 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006078 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006079 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006080
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006081 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6082 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6083 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006084
Michael J. Spencerec38de22010-10-10 22:04:20 +00006085 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006086 DAG.getIntPtrConstant(0),
6087 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006088
Chris Lattnerb903bed2009-06-26 21:20:29 +00006089 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006090 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6091 // initialexec.
6092 unsigned WrapperKind = X86ISD::Wrapper;
6093 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006094 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006095 } else if (is64Bit) {
6096 assert(model == TLSModel::InitialExec);
6097 OperandFlags = X86II::MO_GOTTPOFF;
6098 WrapperKind = X86ISD::WrapperRIP;
6099 } else {
6100 assert(model == TLSModel::InitialExec);
6101 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006102 }
Eric Christopherfd179292009-08-27 18:07:15 +00006103
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006104 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6105 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006106 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006107 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006108 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006109 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006110
Rafael Espindola9a580232009-02-27 13:37:18 +00006111 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006112 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006113 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006114
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006115 // The address of the thread local variable is the add of the thread
6116 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006117 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006118}
6119
Dan Gohman475871a2008-07-27 21:46:04 +00006120SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006121X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006122
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006123 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006124 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006125
Eric Christopher30ef0e52010-06-03 04:07:48 +00006126 if (Subtarget->isTargetELF()) {
6127 // TODO: implement the "local dynamic" model
6128 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006129
Eric Christopher30ef0e52010-06-03 04:07:48 +00006130 // If GV is an alias then use the aliasee for determining
6131 // thread-localness.
6132 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6133 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006134
6135 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006136 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006137
Eric Christopher30ef0e52010-06-03 04:07:48 +00006138 switch (model) {
6139 case TLSModel::GeneralDynamic:
6140 case TLSModel::LocalDynamic: // not implemented
6141 if (Subtarget->is64Bit())
6142 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6143 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006144
Eric Christopher30ef0e52010-06-03 04:07:48 +00006145 case TLSModel::InitialExec:
6146 case TLSModel::LocalExec:
6147 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6148 Subtarget->is64Bit());
6149 }
6150 } else if (Subtarget->isTargetDarwin()) {
6151 // Darwin only has one model of TLS. Lower to that.
6152 unsigned char OpFlag = 0;
6153 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6154 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006155
Eric Christopher30ef0e52010-06-03 04:07:48 +00006156 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6157 // global base reg.
6158 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6159 !Subtarget->is64Bit();
6160 if (PIC32)
6161 OpFlag = X86II::MO_TLVP_PIC_BASE;
6162 else
6163 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006164 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006165 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006166 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006167 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006168 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006169
Eric Christopher30ef0e52010-06-03 04:07:48 +00006170 // With PIC32, the address is actually $g + Offset.
6171 if (PIC32)
6172 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6173 DAG.getNode(X86ISD::GlobalBaseReg,
6174 DebugLoc(), getPointerTy()),
6175 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006176
Eric Christopher30ef0e52010-06-03 04:07:48 +00006177 // Lowering the machine isd will make sure everything is in the right
6178 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006179 SDValue Chain = DAG.getEntryNode();
6180 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
6181 SDValue Args[] = { Chain, Offset };
6182 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006183
Eric Christopher30ef0e52010-06-03 04:07:48 +00006184 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6185 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6186 MFI->setAdjustsStack(true);
Eric Christopher8bce7cc2010-12-09 00:27:58 +00006187
Eric Christopher30ef0e52010-06-03 04:07:48 +00006188 // And our return value (tls address) is in the standard call return value
6189 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006190 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6191 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006192 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006193
Eric Christopher30ef0e52010-06-03 04:07:48 +00006194 assert(false &&
6195 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006196
Torok Edwinc23197a2009-07-14 16:55:14 +00006197 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006198 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006199}
6200
Evan Cheng0db9fe62006-04-25 20:13:52 +00006201
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006202/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006203/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006204SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006205 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006206 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006207 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006208 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006209 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006210 SDValue ShOpLo = Op.getOperand(0);
6211 SDValue ShOpHi = Op.getOperand(1);
6212 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006213 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006214 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006215 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006216
Dan Gohman475871a2008-07-27 21:46:04 +00006217 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006218 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006219 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6220 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006221 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006222 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6223 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006224 }
Evan Chenge3413162006-01-09 18:33:28 +00006225
Owen Anderson825b72b2009-08-11 20:47:22 +00006226 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6227 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006228 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006229 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006230
Dan Gohman475871a2008-07-27 21:46:04 +00006231 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006232 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006233 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6234 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006235
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006236 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006237 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6238 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006239 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006240 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6241 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006242 }
6243
Dan Gohman475871a2008-07-27 21:46:04 +00006244 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006245 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006246}
Evan Chenga3195e82006-01-12 22:54:21 +00006247
Dan Gohmand858e902010-04-17 15:26:15 +00006248SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6249 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006250 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006251
Dale Johannesen0488fb62010-09-30 23:57:10 +00006252 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006253 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006254
Owen Anderson825b72b2009-08-11 20:47:22 +00006255 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006256 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006257
Eli Friedman36df4992009-05-27 00:47:34 +00006258 // These are really Legal; return the operand so the caller accepts it as
6259 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006260 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006261 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006262 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006263 Subtarget->is64Bit()) {
6264 return Op;
6265 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006266
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006267 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006268 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006269 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006270 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006271 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006272 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006273 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006274 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006275 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006276 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6277}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006278
Owen Andersone50ed302009-08-10 22:56:29 +00006279SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006280 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006281 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006282 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006283 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006284 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006285 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006286 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00006287 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00006288 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006289 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006290
Chris Lattner492a43e2010-09-22 01:28:21 +00006291 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006292
Chris Lattner492a43e2010-09-22 01:28:21 +00006293 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6294 MachineMemOperand *MMO =
6295 DAG.getMachineFunction()
6296 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6297 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006298
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006299 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006300 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6301 X86ISD::FILD, DL,
6302 Tys, Ops, array_lengthof(Ops),
6303 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006304
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006305 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006306 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006307 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006308
6309 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6310 // shouldn't be necessary except that RFP cannot be live across
6311 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006312 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006313 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6314 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006315 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006316 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006317 SDValue Ops[] = {
6318 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6319 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006320 MachineMemOperand *MMO =
6321 DAG.getMachineFunction()
6322 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006323 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006324
Chris Lattner492a43e2010-09-22 01:28:21 +00006325 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6326 Ops, array_lengthof(Ops),
6327 Op.getValueType(), MMO);
6328 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006329 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006330 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006331 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006332
Evan Cheng0db9fe62006-04-25 20:13:52 +00006333 return Result;
6334}
6335
Bill Wendling8b8a6362009-01-17 03:56:04 +00006336// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006337SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6338 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006339 // This algorithm is not obvious. Here it is in C code, more or less:
6340 /*
6341 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6342 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6343 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006344
Bill Wendling8b8a6362009-01-17 03:56:04 +00006345 // Copy ints to xmm registers.
6346 __m128i xh = _mm_cvtsi32_si128( hi );
6347 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006348
Bill Wendling8b8a6362009-01-17 03:56:04 +00006349 // Combine into low half of a single xmm register.
6350 __m128i x = _mm_unpacklo_epi32( xh, xl );
6351 __m128d d;
6352 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006353
Bill Wendling8b8a6362009-01-17 03:56:04 +00006354 // Merge in appropriate exponents to give the integer bits the right
6355 // magnitude.
6356 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006357
Bill Wendling8b8a6362009-01-17 03:56:04 +00006358 // Subtract away the biases to deal with the IEEE-754 double precision
6359 // implicit 1.
6360 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006361
Bill Wendling8b8a6362009-01-17 03:56:04 +00006362 // All conversions up to here are exact. The correctly rounded result is
6363 // calculated using the current rounding mode using the following
6364 // horizontal add.
6365 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6366 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6367 // store doesn't really need to be here (except
6368 // maybe to zero the other double)
6369 return sd;
6370 }
6371 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006372
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006373 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006374 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006375
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006376 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006377 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006378 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6379 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6380 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6381 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006382 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006383 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006384
Bill Wendling8b8a6362009-01-17 03:56:04 +00006385 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006386 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006387 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006388 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006389 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006390 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006391 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006392
Owen Anderson825b72b2009-08-11 20:47:22 +00006393 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6394 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006395 Op.getOperand(0),
6396 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006397 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6398 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006399 Op.getOperand(0),
6400 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006401 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6402 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006403 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006404 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006405 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006406 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006407 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006408 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006409 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006410 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006411
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006412 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006413 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006414 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6415 DAG.getUNDEF(MVT::v2f64), ShufMask);
6416 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6417 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006418 DAG.getIntPtrConstant(0));
6419}
6420
Bill Wendling8b8a6362009-01-17 03:56:04 +00006421// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006422SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6423 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006424 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006425 // FP constant to bias correct the final result.
6426 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006427 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006428
6429 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006430 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6431 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006432 Op.getOperand(0),
6433 DAG.getIntPtrConstant(0)));
6434
Owen Anderson825b72b2009-08-11 20:47:22 +00006435 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006436 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006437 DAG.getIntPtrConstant(0));
6438
6439 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006440 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006441 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006442 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006443 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006444 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006445 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006446 MVT::v2f64, Bias)));
6447 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006448 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006449 DAG.getIntPtrConstant(0));
6450
6451 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006452 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006453
6454 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006455 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006456
Owen Anderson825b72b2009-08-11 20:47:22 +00006457 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006458 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006459 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006460 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006461 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006462 }
6463
6464 // Handle final rounding.
6465 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006466}
6467
Dan Gohmand858e902010-04-17 15:26:15 +00006468SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6469 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006470 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006471 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006472
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006473 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006474 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6475 // the optimization here.
6476 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006477 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006478
Owen Andersone50ed302009-08-10 22:56:29 +00006479 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006480 EVT DstVT = Op.getValueType();
6481 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006482 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006483 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006484 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006485
6486 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006487 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006488 if (SrcVT == MVT::i32) {
6489 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6490 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6491 getPointerTy(), StackSlot, WordOff);
6492 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006493 StackSlot, MachinePointerInfo(),
6494 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006495 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006496 OffsetSlot, MachinePointerInfo(),
6497 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006498 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6499 return Fild;
6500 }
6501
6502 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6503 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006504 StackSlot, MachinePointerInfo(),
6505 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006506 // For i64 source, we need to add the appropriate power of 2 if the input
6507 // was negative. This is the same as the optimization in
6508 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6509 // we must be careful to do the computation in x87 extended precision, not
6510 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006511 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6512 MachineMemOperand *MMO =
6513 DAG.getMachineFunction()
6514 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6515 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006516
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006517 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6518 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006519 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6520 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006521
6522 APInt FF(32, 0x5F800000ULL);
6523
6524 // Check whether the sign bit is set.
6525 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6526 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6527 ISD::SETLT);
6528
6529 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6530 SDValue FudgePtr = DAG.getConstantPool(
6531 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6532 getPointerTy());
6533
6534 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6535 SDValue Zero = DAG.getIntPtrConstant(0);
6536 SDValue Four = DAG.getIntPtrConstant(4);
6537 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6538 Zero, Four);
6539 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6540
6541 // Load the value out, extending it from f32 to f80.
6542 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006543 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006544 FudgePtr, MachinePointerInfo::getConstantPool(),
6545 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006546 // Extend everything to 80 bits to force it to be done on x87.
6547 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6548 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006549}
6550
Dan Gohman475871a2008-07-27 21:46:04 +00006551std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006552FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006553 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006554
Owen Andersone50ed302009-08-10 22:56:29 +00006555 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006556
6557 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006558 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6559 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006560 }
6561
Owen Anderson825b72b2009-08-11 20:47:22 +00006562 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6563 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006564 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006565
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006566 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006567 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006568 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006569 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006570 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006571 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006572 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006573 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006574
Evan Cheng87c89352007-10-15 20:11:21 +00006575 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6576 // stack slot.
6577 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006578 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006579 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006580 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006581
Michael J. Spencerec38de22010-10-10 22:04:20 +00006582
6583
Evan Cheng0db9fe62006-04-25 20:13:52 +00006584 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006585 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006586 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006587 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6588 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6589 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006590 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006591
Dan Gohman475871a2008-07-27 21:46:04 +00006592 SDValue Chain = DAG.getEntryNode();
6593 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006594 EVT TheVT = Op.getOperand(0).getValueType();
6595 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006596 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006597 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006598 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006599 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006600 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006601 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006602 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006603 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006604
Chris Lattner492a43e2010-09-22 01:28:21 +00006605 MachineMemOperand *MMO =
6606 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6607 MachineMemOperand::MOLoad, MemSize, MemSize);
6608 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6609 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006610 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006611 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006612 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6613 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006614
Chris Lattner07290932010-09-22 01:05:16 +00006615 MachineMemOperand *MMO =
6616 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6617 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006618
Evan Cheng0db9fe62006-04-25 20:13:52 +00006619 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006620 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006621 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6622 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006623
Chris Lattner27a6c732007-11-24 07:07:01 +00006624 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006625}
6626
Dan Gohmand858e902010-04-17 15:26:15 +00006627SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6628 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006629 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006630 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006631
Eli Friedman948e95a2009-05-23 09:59:16 +00006632 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006633 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006634 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6635 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006636
Chris Lattner27a6c732007-11-24 07:07:01 +00006637 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006638 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006639 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006640}
6641
Dan Gohmand858e902010-04-17 15:26:15 +00006642SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6643 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006644 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6645 SDValue FIST = Vals.first, StackSlot = Vals.second;
6646 assert(FIST.getNode() && "Unexpected failure");
6647
6648 // Load the result.
6649 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006650 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006651}
6652
Dan Gohmand858e902010-04-17 15:26:15 +00006653SDValue X86TargetLowering::LowerFABS(SDValue Op,
6654 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006655 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006656 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006657 EVT VT = Op.getValueType();
6658 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006659 if (VT.isVector())
6660 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006661 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006662 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006663 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006664 CV.push_back(C);
6665 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006666 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006667 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006668 CV.push_back(C);
6669 CV.push_back(C);
6670 CV.push_back(C);
6671 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006672 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006673 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006674 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006675 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006676 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006677 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006678 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006679}
6680
Dan Gohmand858e902010-04-17 15:26:15 +00006681SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006682 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006683 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006684 EVT VT = Op.getValueType();
6685 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006686 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006687 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006688 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006689 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006690 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006691 CV.push_back(C);
6692 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006693 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006694 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006695 CV.push_back(C);
6696 CV.push_back(C);
6697 CV.push_back(C);
6698 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006699 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006700 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006701 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006702 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006703 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006704 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006705 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006706 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006707 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006708 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006709 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006710 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006711 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006712 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006713 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006714}
6715
Dan Gohmand858e902010-04-17 15:26:15 +00006716SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006717 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006718 SDValue Op0 = Op.getOperand(0);
6719 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006720 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006721 EVT VT = Op.getValueType();
6722 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006723
6724 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006725 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006726 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006727 SrcVT = VT;
6728 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006729 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006730 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006731 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006732 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006733 }
6734
6735 // At this point the operands and the result should have the same
6736 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006737
Evan Cheng68c47cb2007-01-05 07:55:56 +00006738 // First get the sign bit of second operand.
6739 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006740 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006741 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6742 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006743 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006744 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6745 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6746 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6747 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006748 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006749 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006750 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006751 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006752 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006753 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006754 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006755
6756 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006757 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006758 // Op0 is MVT::f32, Op1 is MVT::f64.
6759 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6760 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6761 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006762 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006763 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006764 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006765 }
6766
Evan Cheng73d6cf12007-01-05 21:37:56 +00006767 // Clear first operand sign bit.
6768 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006769 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006770 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6771 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006772 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006773 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6774 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6775 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6776 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006777 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006778 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006779 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006780 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006781 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006782 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006783 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006784
6785 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006786 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006787}
6788
Dan Gohman076aee32009-03-04 19:44:21 +00006789/// Emit nodes that will be selected as "test Op0,Op0", or something
6790/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006791SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006792 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006793 DebugLoc dl = Op.getDebugLoc();
6794
Dan Gohman31125812009-03-07 01:58:32 +00006795 // CF and OF aren't always set the way we want. Determine which
6796 // of these we need.
6797 bool NeedCF = false;
6798 bool NeedOF = false;
6799 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006800 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006801 case X86::COND_A: case X86::COND_AE:
6802 case X86::COND_B: case X86::COND_BE:
6803 NeedCF = true;
6804 break;
6805 case X86::COND_G: case X86::COND_GE:
6806 case X86::COND_L: case X86::COND_LE:
6807 case X86::COND_O: case X86::COND_NO:
6808 NeedOF = true;
6809 break;
Dan Gohman31125812009-03-07 01:58:32 +00006810 }
6811
Dan Gohman076aee32009-03-04 19:44:21 +00006812 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006813 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6814 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006815 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6816 // Emit a CMP with 0, which is the TEST pattern.
6817 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6818 DAG.getConstant(0, Op.getValueType()));
6819
6820 unsigned Opcode = 0;
6821 unsigned NumOperands = 0;
6822 switch (Op.getNode()->getOpcode()) {
6823 case ISD::ADD:
6824 // Due to an isel shortcoming, be conservative if this add is likely to be
6825 // selected as part of a load-modify-store instruction. When the root node
6826 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6827 // uses of other nodes in the match, such as the ADD in this case. This
6828 // leads to the ADD being left around and reselected, with the result being
6829 // two adds in the output. Alas, even if none our users are stores, that
6830 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6831 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6832 // climbing the DAG back to the root, and it doesn't seem to be worth the
6833 // effort.
6834 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006835 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006836 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6837 goto default_case;
6838
6839 if (ConstantSDNode *C =
6840 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6841 // An add of one will be selected as an INC.
6842 if (C->getAPIntValue() == 1) {
6843 Opcode = X86ISD::INC;
6844 NumOperands = 1;
6845 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006846 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006847
6848 // An add of negative one (subtract of one) will be selected as a DEC.
6849 if (C->getAPIntValue().isAllOnesValue()) {
6850 Opcode = X86ISD::DEC;
6851 NumOperands = 1;
6852 break;
6853 }
Dan Gohman076aee32009-03-04 19:44:21 +00006854 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006855
6856 // Otherwise use a regular EFLAGS-setting add.
6857 Opcode = X86ISD::ADD;
6858 NumOperands = 2;
6859 break;
6860 case ISD::AND: {
6861 // If the primary and result isn't used, don't bother using X86ISD::AND,
6862 // because a TEST instruction will be better.
6863 bool NonFlagUse = false;
6864 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6865 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6866 SDNode *User = *UI;
6867 unsigned UOpNo = UI.getOperandNo();
6868 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6869 // Look pass truncate.
6870 UOpNo = User->use_begin().getOperandNo();
6871 User = *User->use_begin();
6872 }
6873
6874 if (User->getOpcode() != ISD::BRCOND &&
6875 User->getOpcode() != ISD::SETCC &&
6876 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6877 NonFlagUse = true;
6878 break;
6879 }
Dan Gohman076aee32009-03-04 19:44:21 +00006880 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006881
6882 if (!NonFlagUse)
6883 break;
6884 }
6885 // FALL THROUGH
6886 case ISD::SUB:
6887 case ISD::OR:
6888 case ISD::XOR:
6889 // Due to the ISEL shortcoming noted above, be conservative if this op is
6890 // likely to be selected as part of a load-modify-store instruction.
6891 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6892 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6893 if (UI->getOpcode() == ISD::STORE)
6894 goto default_case;
6895
6896 // Otherwise use a regular EFLAGS-setting instruction.
6897 switch (Op.getNode()->getOpcode()) {
6898 default: llvm_unreachable("unexpected operator!");
6899 case ISD::SUB: Opcode = X86ISD::SUB; break;
6900 case ISD::OR: Opcode = X86ISD::OR; break;
6901 case ISD::XOR: Opcode = X86ISD::XOR; break;
6902 case ISD::AND: Opcode = X86ISD::AND; break;
6903 }
6904
6905 NumOperands = 2;
6906 break;
6907 case X86ISD::ADD:
6908 case X86ISD::SUB:
6909 case X86ISD::INC:
6910 case X86ISD::DEC:
6911 case X86ISD::OR:
6912 case X86ISD::XOR:
6913 case X86ISD::AND:
6914 return SDValue(Op.getNode(), 1);
6915 default:
6916 default_case:
6917 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006918 }
6919
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006920 if (Opcode == 0)
6921 // Emit a CMP with 0, which is the TEST pattern.
6922 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6923 DAG.getConstant(0, Op.getValueType()));
6924
6925 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6926 SmallVector<SDValue, 4> Ops;
6927 for (unsigned i = 0; i != NumOperands; ++i)
6928 Ops.push_back(Op.getOperand(i));
6929
6930 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6931 DAG.ReplaceAllUsesWith(Op, New);
6932 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006933}
6934
6935/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6936/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006937SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006938 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006939 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6940 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006941 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006942
6943 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006944 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006945}
6946
Evan Chengd40d03e2010-01-06 19:38:29 +00006947/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6948/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006949SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6950 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006951 SDValue Op0 = And.getOperand(0);
6952 SDValue Op1 = And.getOperand(1);
6953 if (Op0.getOpcode() == ISD::TRUNCATE)
6954 Op0 = Op0.getOperand(0);
6955 if (Op1.getOpcode() == ISD::TRUNCATE)
6956 Op1 = Op1.getOperand(0);
6957
Evan Chengd40d03e2010-01-06 19:38:29 +00006958 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006959 if (Op1.getOpcode() == ISD::SHL)
6960 std::swap(Op0, Op1);
6961 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006962 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6963 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006964 // If we looked past a truncate, check that it's only truncating away
6965 // known zeros.
6966 unsigned BitWidth = Op0.getValueSizeInBits();
6967 unsigned AndBitWidth = And.getValueSizeInBits();
6968 if (BitWidth > AndBitWidth) {
6969 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6970 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6971 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6972 return SDValue();
6973 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006974 LHS = Op1;
6975 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006976 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006977 } else if (Op1.getOpcode() == ISD::Constant) {
6978 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6979 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006980 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6981 LHS = AndLHS.getOperand(0);
6982 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006983 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006984 }
Evan Cheng0488db92007-09-25 01:57:46 +00006985
Evan Chengd40d03e2010-01-06 19:38:29 +00006986 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006987 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006988 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006989 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006990 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006991 // Also promote i16 to i32 for performance / code size reason.
6992 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006993 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006994 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006995
Evan Chengd40d03e2010-01-06 19:38:29 +00006996 // If the operand types disagree, extend the shift amount to match. Since
6997 // BT ignores high bits (like shifts) we can use anyextend.
6998 if (LHS.getValueType() != RHS.getValueType())
6999 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007000
Evan Chengd40d03e2010-01-06 19:38:29 +00007001 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7002 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7003 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7004 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007005 }
7006
Evan Cheng54de3ea2010-01-05 06:52:31 +00007007 return SDValue();
7008}
7009
Dan Gohmand858e902010-04-17 15:26:15 +00007010SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007011 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7012 SDValue Op0 = Op.getOperand(0);
7013 SDValue Op1 = Op.getOperand(1);
7014 DebugLoc dl = Op.getDebugLoc();
7015 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7016
7017 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007018 // Lower (X & (1 << N)) == 0 to BT(X, N).
7019 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7020 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007021 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007022 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007023 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007024 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7025 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7026 if (NewSetCC.getNode())
7027 return NewSetCC;
7028 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007029
Chris Lattner481eebc2010-12-19 21:23:48 +00007030 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7031 // these.
7032 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007033 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7034 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7035 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Chris Lattner481eebc2010-12-19 21:23:48 +00007036
7037 // If the input is a setcc, then reuse the input setcc or use a new one with
7038 // the inverted condition.
7039 if (Op0.getOpcode() == X86ISD::SETCC) {
7040 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7041 bool Invert = (CC == ISD::SETNE) ^
7042 cast<ConstantSDNode>(Op1)->isNullValue();
7043 if (!Invert) return Op0;
7044
Evan Cheng2c755ba2010-02-27 07:36:59 +00007045 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007046 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7047 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7048 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007049 }
7050
Evan Chenge5b51ac2010-04-17 06:13:15 +00007051 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007052 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007053 if (X86CC == X86::COND_INVALID)
7054 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007055
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007056 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007057 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007058 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007059}
7060
Dan Gohmand858e902010-04-17 15:26:15 +00007061SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007062 SDValue Cond;
7063 SDValue Op0 = Op.getOperand(0);
7064 SDValue Op1 = Op.getOperand(1);
7065 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007066 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007067 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7068 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007069 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007070
7071 if (isFP) {
7072 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007073 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007074 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7075 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007076 bool Swap = false;
7077
7078 switch (SetCCOpcode) {
7079 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007080 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007081 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007082 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007083 case ISD::SETGT: Swap = true; // Fallthrough
7084 case ISD::SETLT:
7085 case ISD::SETOLT: SSECC = 1; break;
7086 case ISD::SETOGE:
7087 case ISD::SETGE: Swap = true; // Fallthrough
7088 case ISD::SETLE:
7089 case ISD::SETOLE: SSECC = 2; break;
7090 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007091 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007092 case ISD::SETNE: SSECC = 4; break;
7093 case ISD::SETULE: Swap = true;
7094 case ISD::SETUGE: SSECC = 5; break;
7095 case ISD::SETULT: Swap = true;
7096 case ISD::SETUGT: SSECC = 6; break;
7097 case ISD::SETO: SSECC = 7; break;
7098 }
7099 if (Swap)
7100 std::swap(Op0, Op1);
7101
Nate Begemanfb8ead02008-07-25 19:05:58 +00007102 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007103 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007104 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007105 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007106 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7107 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007108 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007109 }
7110 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007111 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007112 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7113 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007114 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007115 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007116 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007117 }
7118 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007119 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007121
Nate Begeman30a0de92008-07-17 16:51:19 +00007122 // We are handling one of the integer comparisons here. Since SSE only has
7123 // GT and EQ comparisons for integer, swapping operands and multiple
7124 // operations may be required for some comparisons.
7125 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7126 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007127
Owen Anderson825b72b2009-08-11 20:47:22 +00007128 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007129 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007130 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007131 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007132 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7133 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007134 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007135
Nate Begeman30a0de92008-07-17 16:51:19 +00007136 switch (SetCCOpcode) {
7137 default: break;
7138 case ISD::SETNE: Invert = true;
7139 case ISD::SETEQ: Opc = EQOpc; break;
7140 case ISD::SETLT: Swap = true;
7141 case ISD::SETGT: Opc = GTOpc; break;
7142 case ISD::SETGE: Swap = true;
7143 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7144 case ISD::SETULT: Swap = true;
7145 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7146 case ISD::SETUGE: Swap = true;
7147 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7148 }
7149 if (Swap)
7150 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007151
Nate Begeman30a0de92008-07-17 16:51:19 +00007152 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7153 // bits of the inputs before performing those operations.
7154 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007155 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007156 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7157 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007158 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007159 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7160 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007161 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7162 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007163 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007164
Dale Johannesenace16102009-02-03 19:33:06 +00007165 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007166
7167 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007168 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007169 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007170
Nate Begeman30a0de92008-07-17 16:51:19 +00007171 return Result;
7172}
Evan Cheng0488db92007-09-25 01:57:46 +00007173
Evan Cheng370e5342008-12-03 08:38:43 +00007174// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007175static bool isX86LogicalCmp(SDValue Op) {
7176 unsigned Opc = Op.getNode()->getOpcode();
7177 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7178 return true;
7179 if (Op.getResNo() == 1 &&
7180 (Opc == X86ISD::ADD ||
7181 Opc == X86ISD::SUB ||
7182 Opc == X86ISD::SMUL ||
7183 Opc == X86ISD::UMUL ||
7184 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007185 Opc == X86ISD::DEC ||
7186 Opc == X86ISD::OR ||
7187 Opc == X86ISD::XOR ||
7188 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007189 return true;
7190
Chris Lattner9637d5b2010-12-05 07:49:54 +00007191 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7192 return true;
7193
Dan Gohman076aee32009-03-04 19:44:21 +00007194 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007195}
7196
Chris Lattnera2b56002010-12-05 01:23:24 +00007197static bool isZero(SDValue V) {
7198 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7199 return C && C->isNullValue();
7200}
7201
Chris Lattner96908b12010-12-05 02:00:51 +00007202static bool isAllOnes(SDValue V) {
7203 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7204 return C && C->isAllOnesValue();
7205}
7206
Dan Gohmand858e902010-04-17 15:26:15 +00007207SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007208 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007209 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007210 SDValue Op1 = Op.getOperand(1);
7211 SDValue Op2 = Op.getOperand(2);
7212 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007213 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007214
Dan Gohman1a492952009-10-20 16:22:37 +00007215 if (Cond.getOpcode() == ISD::SETCC) {
7216 SDValue NewCond = LowerSETCC(Cond, DAG);
7217 if (NewCond.getNode())
7218 Cond = NewCond;
7219 }
Evan Cheng734503b2006-09-11 02:19:56 +00007220
Chris Lattnera2b56002010-12-05 01:23:24 +00007221 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007222 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007223 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007224 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007225 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007226 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7227 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007228 SDValue Cmp = Cond.getOperand(1);
Chris Lattnera2b56002010-12-05 01:23:24 +00007229
7230 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
7231
Chris Lattner96908b12010-12-05 02:00:51 +00007232 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
7233 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7234 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007235
7236 SDValue CmpOp0 = Cmp.getOperand(0);
7237 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7238 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
7239
Chris Lattner96908b12010-12-05 02:00:51 +00007240 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007241 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7242 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
Chris Lattner96908b12010-12-05 02:00:51 +00007243
7244 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7245 Res = DAG.getNOT(DL, Res, Res.getValueType());
7246
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007247 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007248 if (N2C == 0 || !N2C->isNullValue())
7249 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7250 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007251 }
7252 }
7253
Chris Lattnera2b56002010-12-05 01:23:24 +00007254 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007255 if (Cond.getOpcode() == ISD::AND &&
7256 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7257 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007258 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007259 Cond = Cond.getOperand(0);
7260 }
7261
Evan Cheng3f41d662007-10-08 22:16:29 +00007262 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7263 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007264 if (Cond.getOpcode() == X86ISD::SETCC ||
7265 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007266 CC = Cond.getOperand(0);
7267
Dan Gohman475871a2008-07-27 21:46:04 +00007268 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007269 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007270 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007271
Evan Cheng3f41d662007-10-08 22:16:29 +00007272 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007273 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007274 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007275 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007276
Chris Lattnerd1980a52009-03-12 06:52:53 +00007277 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7278 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007279 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007280 addTest = false;
7281 }
7282 }
7283
7284 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007285 // Look pass the truncate.
7286 if (Cond.getOpcode() == ISD::TRUNCATE)
7287 Cond = Cond.getOperand(0);
7288
7289 // We know the result of AND is compared against zero. Try to match
7290 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007291 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007292 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007293 if (NewSetCC.getNode()) {
7294 CC = NewSetCC.getOperand(0);
7295 Cond = NewSetCC.getOperand(1);
7296 addTest = false;
7297 }
7298 }
7299 }
7300
7301 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007302 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007303 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007304 }
7305
Evan Cheng0488db92007-09-25 01:57:46 +00007306 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7307 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007308 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
7309 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007310 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007311}
7312
Evan Cheng370e5342008-12-03 08:38:43 +00007313// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7314// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7315// from the AND / OR.
7316static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7317 Opc = Op.getOpcode();
7318 if (Opc != ISD::OR && Opc != ISD::AND)
7319 return false;
7320 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7321 Op.getOperand(0).hasOneUse() &&
7322 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7323 Op.getOperand(1).hasOneUse());
7324}
7325
Evan Cheng961d6d42009-02-02 08:19:07 +00007326// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7327// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007328static bool isXor1OfSetCC(SDValue Op) {
7329 if (Op.getOpcode() != ISD::XOR)
7330 return false;
7331 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7332 if (N1C && N1C->getAPIntValue() == 1) {
7333 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7334 Op.getOperand(0).hasOneUse();
7335 }
7336 return false;
7337}
7338
Dan Gohmand858e902010-04-17 15:26:15 +00007339SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007340 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007341 SDValue Chain = Op.getOperand(0);
7342 SDValue Cond = Op.getOperand(1);
7343 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007344 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007345 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007346
Dan Gohman1a492952009-10-20 16:22:37 +00007347 if (Cond.getOpcode() == ISD::SETCC) {
7348 SDValue NewCond = LowerSETCC(Cond, DAG);
7349 if (NewCond.getNode())
7350 Cond = NewCond;
7351 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007352#if 0
7353 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007354 else if (Cond.getOpcode() == X86ISD::ADD ||
7355 Cond.getOpcode() == X86ISD::SUB ||
7356 Cond.getOpcode() == X86ISD::SMUL ||
7357 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007358 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007359#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007360
Evan Chengad9c0a32009-12-15 00:53:42 +00007361 // Look pass (and (setcc_carry (cmp ...)), 1).
7362 if (Cond.getOpcode() == ISD::AND &&
7363 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7364 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007365 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007366 Cond = Cond.getOperand(0);
7367 }
7368
Evan Cheng3f41d662007-10-08 22:16:29 +00007369 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7370 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007371 if (Cond.getOpcode() == X86ISD::SETCC ||
7372 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007373 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007374
Dan Gohman475871a2008-07-27 21:46:04 +00007375 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007376 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007377 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007378 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007379 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007380 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007381 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007382 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007383 default: break;
7384 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007385 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007386 // These can only come from an arithmetic instruction with overflow,
7387 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007388 Cond = Cond.getNode()->getOperand(1);
7389 addTest = false;
7390 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007391 }
Evan Cheng0488db92007-09-25 01:57:46 +00007392 }
Evan Cheng370e5342008-12-03 08:38:43 +00007393 } else {
7394 unsigned CondOpc;
7395 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7396 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007397 if (CondOpc == ISD::OR) {
7398 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7399 // two branches instead of an explicit OR instruction with a
7400 // separate test.
7401 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007402 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007403 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007404 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007405 Chain, Dest, CC, Cmp);
7406 CC = Cond.getOperand(1).getOperand(0);
7407 Cond = Cmp;
7408 addTest = false;
7409 }
7410 } else { // ISD::AND
7411 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7412 // two branches instead of an explicit AND instruction with a
7413 // separate test. However, we only do this if this block doesn't
7414 // have a fall-through edge, because this requires an explicit
7415 // jmp when the condition is false.
7416 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007417 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007418 Op.getNode()->hasOneUse()) {
7419 X86::CondCode CCode =
7420 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7421 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007422 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007423 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007424 // Look for an unconditional branch following this conditional branch.
7425 // We need this because we need to reverse the successors in order
7426 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007427 if (User->getOpcode() == ISD::BR) {
7428 SDValue FalseBB = User->getOperand(1);
7429 SDNode *NewBR =
7430 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007431 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007432 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007433 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007434
Dale Johannesene4d209d2009-02-03 20:21:25 +00007435 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007436 Chain, Dest, CC, Cmp);
7437 X86::CondCode CCode =
7438 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7439 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007440 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007441 Cond = Cmp;
7442 addTest = false;
7443 }
7444 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007445 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007446 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7447 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7448 // It should be transformed during dag combiner except when the condition
7449 // is set by a arithmetics with overflow node.
7450 X86::CondCode CCode =
7451 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7452 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007453 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007454 Cond = Cond.getOperand(0).getOperand(1);
7455 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007456 }
Evan Cheng0488db92007-09-25 01:57:46 +00007457 }
7458
7459 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007460 // Look pass the truncate.
7461 if (Cond.getOpcode() == ISD::TRUNCATE)
7462 Cond = Cond.getOperand(0);
7463
7464 // We know the result of AND is compared against zero. Try to match
7465 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007466 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007467 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7468 if (NewSetCC.getNode()) {
7469 CC = NewSetCC.getOperand(0);
7470 Cond = NewSetCC.getOperand(1);
7471 addTest = false;
7472 }
7473 }
7474 }
7475
7476 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007477 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007478 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007479 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007480 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007481 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007482}
7483
Anton Korobeynikove060b532007-04-17 19:34:00 +00007484
7485// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7486// Calls to _alloca is needed to probe the stack when allocating more than 4k
7487// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7488// that the guard pages used by the OS virtual memory manager are allocated in
7489// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007490SDValue
7491X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007492 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007493 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007494 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007495 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007496
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007497 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007498 SDValue Chain = Op.getOperand(0);
7499 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007500 // FIXME: Ensure alignment here
7501
Dan Gohman475871a2008-07-27 21:46:04 +00007502 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007503
Owen Anderson825b72b2009-08-11 20:47:22 +00007504 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007505
Dale Johannesendd64c412009-02-04 00:33:20 +00007506 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007507 Flag = Chain.getValue(1);
7508
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007509 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007510
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007511 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007512 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007513
Dale Johannesendd64c412009-02-04 00:33:20 +00007514 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007515
Dan Gohman475871a2008-07-27 21:46:04 +00007516 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007517 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007518}
7519
Dan Gohmand858e902010-04-17 15:26:15 +00007520SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007521 MachineFunction &MF = DAG.getMachineFunction();
7522 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7523
Dan Gohman69de1932008-02-06 22:27:42 +00007524 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007525 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007526
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007527 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007528 // vastart just stores the address of the VarArgsFrameIndex slot into the
7529 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007530 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7531 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007532 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7533 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007534 }
7535
7536 // __va_list_tag:
7537 // gp_offset (0 - 6 * 8)
7538 // fp_offset (48 - 48 + 8 * 16)
7539 // overflow_arg_area (point to parameters coming in memory).
7540 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007541 SmallVector<SDValue, 8> MemOps;
7542 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007543 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007544 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007545 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7546 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007547 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007548 MemOps.push_back(Store);
7549
7550 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007551 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007552 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007553 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007554 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7555 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007556 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007557 MemOps.push_back(Store);
7558
7559 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007560 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007561 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007562 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7563 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007564 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7565 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007566 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007567 MemOps.push_back(Store);
7568
7569 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007570 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007571 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007572 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7573 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007574 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7575 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007576 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007577 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007578 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007579}
7580
Dan Gohmand858e902010-04-17 15:26:15 +00007581SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007582 assert(Subtarget->is64Bit() &&
7583 "LowerVAARG only handles 64-bit va_arg!");
7584 assert((Subtarget->isTargetLinux() ||
7585 Subtarget->isTargetDarwin()) &&
7586 "Unhandled target in LowerVAARG");
7587 assert(Op.getNode()->getNumOperands() == 4);
7588 SDValue Chain = Op.getOperand(0);
7589 SDValue SrcPtr = Op.getOperand(1);
7590 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7591 unsigned Align = Op.getConstantOperandVal(3);
7592 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007593
Dan Gohman320afb82010-10-12 18:00:49 +00007594 EVT ArgVT = Op.getNode()->getValueType(0);
7595 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7596 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7597 uint8_t ArgMode;
7598
7599 // Decide which area this value should be read from.
7600 // TODO: Implement the AMD64 ABI in its entirety. This simple
7601 // selection mechanism works only for the basic types.
7602 if (ArgVT == MVT::f80) {
7603 llvm_unreachable("va_arg for f80 not yet implemented");
7604 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7605 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7606 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7607 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7608 } else {
7609 llvm_unreachable("Unhandled argument type in LowerVAARG");
7610 }
7611
7612 if (ArgMode == 2) {
7613 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007614 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007615 !(DAG.getMachineFunction()
7616 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007617 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007618 }
7619
7620 // Insert VAARG_64 node into the DAG
7621 // VAARG_64 returns two values: Variable Argument Address, Chain
7622 SmallVector<SDValue, 11> InstOps;
7623 InstOps.push_back(Chain);
7624 InstOps.push_back(SrcPtr);
7625 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7626 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7627 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7628 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7629 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7630 VTs, &InstOps[0], InstOps.size(),
7631 MVT::i64,
7632 MachinePointerInfo(SV),
7633 /*Align=*/0,
7634 /*Volatile=*/false,
7635 /*ReadMem=*/true,
7636 /*WriteMem=*/true);
7637 Chain = VAARG.getValue(1);
7638
7639 // Load the next argument and return it
7640 return DAG.getLoad(ArgVT, dl,
7641 Chain,
7642 VAARG,
7643 MachinePointerInfo(),
7644 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007645}
7646
Dan Gohmand858e902010-04-17 15:26:15 +00007647SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007648 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007649 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007650 SDValue Chain = Op.getOperand(0);
7651 SDValue DstPtr = Op.getOperand(1);
7652 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007653 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7654 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007655 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007656
Chris Lattnere72f2022010-09-21 05:40:29 +00007657 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007658 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007659 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007660 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007661}
7662
Dan Gohman475871a2008-07-27 21:46:04 +00007663SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007664X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007665 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007666 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007667 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007668 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007669 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007670 case Intrinsic::x86_sse_comieq_ss:
7671 case Intrinsic::x86_sse_comilt_ss:
7672 case Intrinsic::x86_sse_comile_ss:
7673 case Intrinsic::x86_sse_comigt_ss:
7674 case Intrinsic::x86_sse_comige_ss:
7675 case Intrinsic::x86_sse_comineq_ss:
7676 case Intrinsic::x86_sse_ucomieq_ss:
7677 case Intrinsic::x86_sse_ucomilt_ss:
7678 case Intrinsic::x86_sse_ucomile_ss:
7679 case Intrinsic::x86_sse_ucomigt_ss:
7680 case Intrinsic::x86_sse_ucomige_ss:
7681 case Intrinsic::x86_sse_ucomineq_ss:
7682 case Intrinsic::x86_sse2_comieq_sd:
7683 case Intrinsic::x86_sse2_comilt_sd:
7684 case Intrinsic::x86_sse2_comile_sd:
7685 case Intrinsic::x86_sse2_comigt_sd:
7686 case Intrinsic::x86_sse2_comige_sd:
7687 case Intrinsic::x86_sse2_comineq_sd:
7688 case Intrinsic::x86_sse2_ucomieq_sd:
7689 case Intrinsic::x86_sse2_ucomilt_sd:
7690 case Intrinsic::x86_sse2_ucomile_sd:
7691 case Intrinsic::x86_sse2_ucomigt_sd:
7692 case Intrinsic::x86_sse2_ucomige_sd:
7693 case Intrinsic::x86_sse2_ucomineq_sd: {
7694 unsigned Opc = 0;
7695 ISD::CondCode CC = ISD::SETCC_INVALID;
7696 switch (IntNo) {
7697 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007698 case Intrinsic::x86_sse_comieq_ss:
7699 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007700 Opc = X86ISD::COMI;
7701 CC = ISD::SETEQ;
7702 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007703 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007704 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007705 Opc = X86ISD::COMI;
7706 CC = ISD::SETLT;
7707 break;
7708 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007709 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007710 Opc = X86ISD::COMI;
7711 CC = ISD::SETLE;
7712 break;
7713 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007714 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007715 Opc = X86ISD::COMI;
7716 CC = ISD::SETGT;
7717 break;
7718 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007719 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007720 Opc = X86ISD::COMI;
7721 CC = ISD::SETGE;
7722 break;
7723 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007724 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007725 Opc = X86ISD::COMI;
7726 CC = ISD::SETNE;
7727 break;
7728 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007729 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007730 Opc = X86ISD::UCOMI;
7731 CC = ISD::SETEQ;
7732 break;
7733 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007734 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007735 Opc = X86ISD::UCOMI;
7736 CC = ISD::SETLT;
7737 break;
7738 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007739 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007740 Opc = X86ISD::UCOMI;
7741 CC = ISD::SETLE;
7742 break;
7743 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007744 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007745 Opc = X86ISD::UCOMI;
7746 CC = ISD::SETGT;
7747 break;
7748 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007749 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007750 Opc = X86ISD::UCOMI;
7751 CC = ISD::SETGE;
7752 break;
7753 case Intrinsic::x86_sse_ucomineq_ss:
7754 case Intrinsic::x86_sse2_ucomineq_sd:
7755 Opc = X86ISD::UCOMI;
7756 CC = ISD::SETNE;
7757 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007758 }
Evan Cheng734503b2006-09-11 02:19:56 +00007759
Dan Gohman475871a2008-07-27 21:46:04 +00007760 SDValue LHS = Op.getOperand(1);
7761 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007762 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007763 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007764 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7765 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7766 DAG.getConstant(X86CC, MVT::i8), Cond);
7767 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007768 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007769 // ptest and testp intrinsics. The intrinsic these come from are designed to
7770 // return an integer value, not just an instruction so lower it to the ptest
7771 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007772 case Intrinsic::x86_sse41_ptestz:
7773 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007774 case Intrinsic::x86_sse41_ptestnzc:
7775 case Intrinsic::x86_avx_ptestz_256:
7776 case Intrinsic::x86_avx_ptestc_256:
7777 case Intrinsic::x86_avx_ptestnzc_256:
7778 case Intrinsic::x86_avx_vtestz_ps:
7779 case Intrinsic::x86_avx_vtestc_ps:
7780 case Intrinsic::x86_avx_vtestnzc_ps:
7781 case Intrinsic::x86_avx_vtestz_pd:
7782 case Intrinsic::x86_avx_vtestc_pd:
7783 case Intrinsic::x86_avx_vtestnzc_pd:
7784 case Intrinsic::x86_avx_vtestz_ps_256:
7785 case Intrinsic::x86_avx_vtestc_ps_256:
7786 case Intrinsic::x86_avx_vtestnzc_ps_256:
7787 case Intrinsic::x86_avx_vtestz_pd_256:
7788 case Intrinsic::x86_avx_vtestc_pd_256:
7789 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7790 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007791 unsigned X86CC = 0;
7792 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007793 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007794 case Intrinsic::x86_avx_vtestz_ps:
7795 case Intrinsic::x86_avx_vtestz_pd:
7796 case Intrinsic::x86_avx_vtestz_ps_256:
7797 case Intrinsic::x86_avx_vtestz_pd_256:
7798 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007799 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007800 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007801 // ZF = 1
7802 X86CC = X86::COND_E;
7803 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007804 case Intrinsic::x86_avx_vtestc_ps:
7805 case Intrinsic::x86_avx_vtestc_pd:
7806 case Intrinsic::x86_avx_vtestc_ps_256:
7807 case Intrinsic::x86_avx_vtestc_pd_256:
7808 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007809 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007810 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007811 // CF = 1
7812 X86CC = X86::COND_B;
7813 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007814 case Intrinsic::x86_avx_vtestnzc_ps:
7815 case Intrinsic::x86_avx_vtestnzc_pd:
7816 case Intrinsic::x86_avx_vtestnzc_ps_256:
7817 case Intrinsic::x86_avx_vtestnzc_pd_256:
7818 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007819 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007820 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007821 // ZF and CF = 0
7822 X86CC = X86::COND_A;
7823 break;
7824 }
Eric Christopherfd179292009-08-27 18:07:15 +00007825
Eric Christopher71c67532009-07-29 00:28:05 +00007826 SDValue LHS = Op.getOperand(1);
7827 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007828 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7829 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007830 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7831 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7832 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007833 }
Evan Cheng5759f972008-05-04 09:15:50 +00007834
7835 // Fix vector shift instructions where the last operand is a non-immediate
7836 // i32 value.
7837 case Intrinsic::x86_sse2_pslli_w:
7838 case Intrinsic::x86_sse2_pslli_d:
7839 case Intrinsic::x86_sse2_pslli_q:
7840 case Intrinsic::x86_sse2_psrli_w:
7841 case Intrinsic::x86_sse2_psrli_d:
7842 case Intrinsic::x86_sse2_psrli_q:
7843 case Intrinsic::x86_sse2_psrai_w:
7844 case Intrinsic::x86_sse2_psrai_d:
7845 case Intrinsic::x86_mmx_pslli_w:
7846 case Intrinsic::x86_mmx_pslli_d:
7847 case Intrinsic::x86_mmx_pslli_q:
7848 case Intrinsic::x86_mmx_psrli_w:
7849 case Intrinsic::x86_mmx_psrli_d:
7850 case Intrinsic::x86_mmx_psrli_q:
7851 case Intrinsic::x86_mmx_psrai_w:
7852 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007853 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007854 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007855 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007856
7857 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007858 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007859 switch (IntNo) {
7860 case Intrinsic::x86_sse2_pslli_w:
7861 NewIntNo = Intrinsic::x86_sse2_psll_w;
7862 break;
7863 case Intrinsic::x86_sse2_pslli_d:
7864 NewIntNo = Intrinsic::x86_sse2_psll_d;
7865 break;
7866 case Intrinsic::x86_sse2_pslli_q:
7867 NewIntNo = Intrinsic::x86_sse2_psll_q;
7868 break;
7869 case Intrinsic::x86_sse2_psrli_w:
7870 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7871 break;
7872 case Intrinsic::x86_sse2_psrli_d:
7873 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7874 break;
7875 case Intrinsic::x86_sse2_psrli_q:
7876 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7877 break;
7878 case Intrinsic::x86_sse2_psrai_w:
7879 NewIntNo = Intrinsic::x86_sse2_psra_w;
7880 break;
7881 case Intrinsic::x86_sse2_psrai_d:
7882 NewIntNo = Intrinsic::x86_sse2_psra_d;
7883 break;
7884 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007885 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007886 switch (IntNo) {
7887 case Intrinsic::x86_mmx_pslli_w:
7888 NewIntNo = Intrinsic::x86_mmx_psll_w;
7889 break;
7890 case Intrinsic::x86_mmx_pslli_d:
7891 NewIntNo = Intrinsic::x86_mmx_psll_d;
7892 break;
7893 case Intrinsic::x86_mmx_pslli_q:
7894 NewIntNo = Intrinsic::x86_mmx_psll_q;
7895 break;
7896 case Intrinsic::x86_mmx_psrli_w:
7897 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7898 break;
7899 case Intrinsic::x86_mmx_psrli_d:
7900 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7901 break;
7902 case Intrinsic::x86_mmx_psrli_q:
7903 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7904 break;
7905 case Intrinsic::x86_mmx_psrai_w:
7906 NewIntNo = Intrinsic::x86_mmx_psra_w;
7907 break;
7908 case Intrinsic::x86_mmx_psrai_d:
7909 NewIntNo = Intrinsic::x86_mmx_psra_d;
7910 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007911 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007912 }
7913 break;
7914 }
7915 }
Mon P Wangefa42202009-09-03 19:56:25 +00007916
7917 // The vector shift intrinsics with scalars uses 32b shift amounts but
7918 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7919 // to be zero.
7920 SDValue ShOps[4];
7921 ShOps[0] = ShAmt;
7922 ShOps[1] = DAG.getConstant(0, MVT::i32);
7923 if (ShAmtVT == MVT::v4i32) {
7924 ShOps[2] = DAG.getUNDEF(MVT::i32);
7925 ShOps[3] = DAG.getUNDEF(MVT::i32);
7926 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7927 } else {
7928 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00007929// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00007930 }
7931
Owen Andersone50ed302009-08-10 22:56:29 +00007932 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007933 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007934 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007935 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007936 Op.getOperand(1), ShAmt);
7937 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007938 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007939}
Evan Cheng72261582005-12-20 06:22:03 +00007940
Dan Gohmand858e902010-04-17 15:26:15 +00007941SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7942 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007943 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7944 MFI->setReturnAddressIsTaken(true);
7945
Bill Wendling64e87322009-01-16 19:25:27 +00007946 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007947 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007948
7949 if (Depth > 0) {
7950 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7951 SDValue Offset =
7952 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007953 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007954 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007955 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007956 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00007957 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007958 }
7959
7960 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007961 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007962 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007963 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007964}
7965
Dan Gohmand858e902010-04-17 15:26:15 +00007966SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007967 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7968 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007969
Owen Andersone50ed302009-08-10 22:56:29 +00007970 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007971 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007972 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7973 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007974 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007975 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00007976 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
7977 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00007978 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007979 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007980}
7981
Dan Gohman475871a2008-07-27 21:46:04 +00007982SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007983 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007984 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007985}
7986
Dan Gohmand858e902010-04-17 15:26:15 +00007987SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007988 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007989 SDValue Chain = Op.getOperand(0);
7990 SDValue Offset = Op.getOperand(1);
7991 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007992 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007993
Dan Gohmand8816272010-08-11 18:14:00 +00007994 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
7995 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7996 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007997 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007998
Dan Gohmand8816272010-08-11 18:14:00 +00007999 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8000 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008001 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008002 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8003 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008004 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008005 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008006
Dale Johannesene4d209d2009-02-03 20:21:25 +00008007 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008008 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008009 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008010}
8011
Dan Gohman475871a2008-07-27 21:46:04 +00008012SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008013 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008014 SDValue Root = Op.getOperand(0);
8015 SDValue Trmp = Op.getOperand(1); // trampoline
8016 SDValue FPtr = Op.getOperand(2); // nested function
8017 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008018 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008019
Dan Gohman69de1932008-02-06 22:27:42 +00008020 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008021
8022 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008023 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008024
8025 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008026 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8027 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008028
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008029 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8030 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008031
8032 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8033
8034 // Load the pointer to the nested function into R11.
8035 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008036 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008037 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008038 Addr, MachinePointerInfo(TrmpAddr),
8039 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008040
Owen Anderson825b72b2009-08-11 20:47:22 +00008041 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8042 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008043 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8044 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008045 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008046
8047 // Load the 'nest' parameter value into R10.
8048 // R10 is specified in X86CallingConv.td
8049 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008050 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8051 DAG.getConstant(10, MVT::i64));
8052 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008053 Addr, MachinePointerInfo(TrmpAddr, 10),
8054 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008055
Owen Anderson825b72b2009-08-11 20:47:22 +00008056 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8057 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008058 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8059 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008060 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008061
8062 // Jump to the nested function.
8063 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008064 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8065 DAG.getConstant(20, MVT::i64));
8066 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008067 Addr, MachinePointerInfo(TrmpAddr, 20),
8068 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008069
8070 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008071 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8072 DAG.getConstant(22, MVT::i64));
8073 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008074 MachinePointerInfo(TrmpAddr, 22),
8075 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008076
Dan Gohman475871a2008-07-27 21:46:04 +00008077 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008078 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008079 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008080 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008081 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008082 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008083 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008084 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008085
8086 switch (CC) {
8087 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008088 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008089 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008090 case CallingConv::X86_StdCall: {
8091 // Pass 'nest' parameter in ECX.
8092 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008093 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008094
8095 // Check that ECX wasn't needed by an 'inreg' parameter.
8096 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008097 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008098
Chris Lattner58d74912008-03-12 17:45:29 +00008099 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008100 unsigned InRegCount = 0;
8101 unsigned Idx = 1;
8102
8103 for (FunctionType::param_iterator I = FTy->param_begin(),
8104 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008105 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008106 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008107 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008108
8109 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008110 report_fatal_error("Nest register in use - reduce number of inreg"
8111 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008112 }
8113 }
8114 break;
8115 }
8116 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008117 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008118 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008119 // Pass 'nest' parameter in EAX.
8120 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008121 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008122 break;
8123 }
8124
Dan Gohman475871a2008-07-27 21:46:04 +00008125 SDValue OutChains[4];
8126 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008127
Owen Anderson825b72b2009-08-11 20:47:22 +00008128 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8129 DAG.getConstant(10, MVT::i32));
8130 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008131
Chris Lattnera62fe662010-02-05 19:20:30 +00008132 // This is storing the opcode for MOV32ri.
8133 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008134 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008135 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008136 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008137 Trmp, MachinePointerInfo(TrmpAddr),
8138 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008139
Owen Anderson825b72b2009-08-11 20:47:22 +00008140 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8141 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008142 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8143 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008144 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008145
Chris Lattnera62fe662010-02-05 19:20:30 +00008146 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008147 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8148 DAG.getConstant(5, MVT::i32));
8149 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008150 MachinePointerInfo(TrmpAddr, 5),
8151 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008152
Owen Anderson825b72b2009-08-11 20:47:22 +00008153 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8154 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008155 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8156 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008157 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008158
Dan Gohman475871a2008-07-27 21:46:04 +00008159 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008160 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008161 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008162 }
8163}
8164
Dan Gohmand858e902010-04-17 15:26:15 +00008165SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8166 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008167 /*
8168 The rounding mode is in bits 11:10 of FPSR, and has the following
8169 settings:
8170 00 Round to nearest
8171 01 Round to -inf
8172 10 Round to +inf
8173 11 Round to 0
8174
8175 FLT_ROUNDS, on the other hand, expects the following:
8176 -1 Undefined
8177 0 Round to 0
8178 1 Round to nearest
8179 2 Round to +inf
8180 3 Round to -inf
8181
8182 To perform the conversion, we do:
8183 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8184 */
8185
8186 MachineFunction &MF = DAG.getMachineFunction();
8187 const TargetMachine &TM = MF.getTarget();
8188 const TargetFrameInfo &TFI = *TM.getFrameInfo();
8189 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008190 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008191 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008192
8193 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008194 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008195 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008196
Michael J. Spencerec38de22010-10-10 22:04:20 +00008197
Chris Lattner2156b792010-09-22 01:11:26 +00008198 MachineMemOperand *MMO =
8199 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8200 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008201
Chris Lattner2156b792010-09-22 01:11:26 +00008202 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8203 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8204 DAG.getVTList(MVT::Other),
8205 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008206
8207 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008208 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008209 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008210
8211 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008212 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008213 DAG.getNode(ISD::SRL, DL, MVT::i16,
8214 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008215 CWD, DAG.getConstant(0x800, MVT::i16)),
8216 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008217 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008218 DAG.getNode(ISD::SRL, DL, MVT::i16,
8219 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008220 CWD, DAG.getConstant(0x400, MVT::i16)),
8221 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008222
Dan Gohman475871a2008-07-27 21:46:04 +00008223 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008224 DAG.getNode(ISD::AND, DL, MVT::i16,
8225 DAG.getNode(ISD::ADD, DL, MVT::i16,
8226 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008227 DAG.getConstant(1, MVT::i16)),
8228 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008229
8230
Duncan Sands83ec4b62008-06-06 12:08:01 +00008231 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008232 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008233}
8234
Dan Gohmand858e902010-04-17 15:26:15 +00008235SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008236 EVT VT = Op.getValueType();
8237 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008238 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008239 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008240
8241 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008242 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008243 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008244 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008245 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008246 }
Evan Cheng18efe262007-12-14 02:13:44 +00008247
Evan Cheng152804e2007-12-14 08:30:15 +00008248 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008249 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008250 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008251
8252 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008253 SDValue Ops[] = {
8254 Op,
8255 DAG.getConstant(NumBits+NumBits-1, OpVT),
8256 DAG.getConstant(X86::COND_E, MVT::i8),
8257 Op.getValue(1)
8258 };
8259 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008260
8261 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008262 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008263
Owen Anderson825b72b2009-08-11 20:47:22 +00008264 if (VT == MVT::i8)
8265 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008266 return Op;
8267}
8268
Dan Gohmand858e902010-04-17 15:26:15 +00008269SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008270 EVT VT = Op.getValueType();
8271 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008272 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008273 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008274
8275 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 if (VT == MVT::i8) {
8277 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008278 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008279 }
Evan Cheng152804e2007-12-14 08:30:15 +00008280
8281 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008282 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008283 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008284
8285 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008286 SDValue Ops[] = {
8287 Op,
8288 DAG.getConstant(NumBits, OpVT),
8289 DAG.getConstant(X86::COND_E, MVT::i8),
8290 Op.getValue(1)
8291 };
8292 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008293
Owen Anderson825b72b2009-08-11 20:47:22 +00008294 if (VT == MVT::i8)
8295 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008296 return Op;
8297}
8298
Dan Gohmand858e902010-04-17 15:26:15 +00008299SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008300 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008301 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008302 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008303
Mon P Wangaf9b9522008-12-18 21:42:19 +00008304 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8305 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8306 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8307 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8308 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8309 //
8310 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8311 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8312 // return AloBlo + AloBhi + AhiBlo;
8313
8314 SDValue A = Op.getOperand(0);
8315 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008316
Dale Johannesene4d209d2009-02-03 20:21:25 +00008317 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008318 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8319 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008320 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008321 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8322 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008323 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008324 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008325 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008326 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008327 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008328 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008329 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008330 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008331 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008332 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008333 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8334 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008335 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008336 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8337 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008338 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8339 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008340 return Res;
8341}
8342
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008343SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8344 EVT VT = Op.getValueType();
8345 DebugLoc dl = Op.getDebugLoc();
8346 SDValue R = Op.getOperand(0);
8347
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008348 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008349
Nate Begeman51409212010-07-28 00:21:48 +00008350 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8351
8352 if (VT == MVT::v4i32) {
8353 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8354 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8355 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8356
8357 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008358
Nate Begeman51409212010-07-28 00:21:48 +00008359 std::vector<Constant*> CV(4, CI);
8360 Constant *C = ConstantVector::get(CV);
8361 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8362 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008363 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008364 false, false, 16);
8365
8366 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008367 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008368 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8369 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8370 }
8371 if (VT == MVT::v16i8) {
8372 // a = a << 5;
8373 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8374 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8375 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8376
8377 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8378 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8379
8380 std::vector<Constant*> CVM1(16, CM1);
8381 std::vector<Constant*> CVM2(16, CM2);
8382 Constant *C = ConstantVector::get(CVM1);
8383 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8384 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008385 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008386 false, false, 16);
8387
8388 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8389 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8390 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8391 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8392 DAG.getConstant(4, MVT::i32));
8393 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8394 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8395 R, M, Op);
8396 // a += a
8397 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008398
Nate Begeman51409212010-07-28 00:21:48 +00008399 C = ConstantVector::get(CVM2);
8400 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8401 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008402 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008403 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008404
Nate Begeman51409212010-07-28 00:21:48 +00008405 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8406 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8407 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8408 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8409 DAG.getConstant(2, MVT::i32));
8410 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8411 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8412 R, M, Op);
8413 // a += a
8414 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008415
Nate Begeman51409212010-07-28 00:21:48 +00008416 // return pblendv(r, r+r, a);
8417 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8418 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8419 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8420 return R;
8421 }
8422 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008423}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008424
Dan Gohmand858e902010-04-17 15:26:15 +00008425SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008426 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8427 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008428 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8429 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008430 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008431 SDValue LHS = N->getOperand(0);
8432 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008433 unsigned BaseOp = 0;
8434 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008435 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008436 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008437 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008438 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008439 // A subtract of one will be selected as a INC. Note that INC doesn't
8440 // set CF, so we can't do this for UADDO.
8441 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8442 if (C->getAPIntValue() == 1) {
8443 BaseOp = X86ISD::INC;
8444 Cond = X86::COND_O;
8445 break;
8446 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008447 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008448 Cond = X86::COND_O;
8449 break;
8450 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008451 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008452 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008453 break;
8454 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008455 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8456 // set CF, so we can't do this for USUBO.
8457 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8458 if (C->getAPIntValue() == 1) {
8459 BaseOp = X86ISD::DEC;
8460 Cond = X86::COND_O;
8461 break;
8462 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008463 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008464 Cond = X86::COND_O;
8465 break;
8466 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008467 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008468 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008469 break;
8470 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008471 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008472 Cond = X86::COND_O;
8473 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008474 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8475 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8476 MVT::i32);
8477 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
8478
8479 SDValue SetCC =
8480 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8481 DAG.getConstant(X86::COND_O, MVT::i32),
8482 SDValue(Sum.getNode(), 2));
8483
8484 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8485 return Sum;
8486 }
Bill Wendling74c37652008-12-09 22:08:41 +00008487 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008488
Bill Wendling61edeb52008-12-02 01:06:39 +00008489 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008490 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008491 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008492
Bill Wendling61edeb52008-12-02 01:06:39 +00008493 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008494 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8495 DAG.getConstant(Cond, MVT::i32),
8496 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008497
Bill Wendling61edeb52008-12-02 01:06:39 +00008498 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8499 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008500}
8501
Eric Christopher9a9d2752010-07-22 02:48:34 +00008502SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8503 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008504
Eric Christopherb6729dc2010-08-04 23:03:04 +00008505 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008506 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008507 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008508 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008509 SDValue Ops[] = {
8510 DAG.getRegister(X86::ESP, MVT::i32), // Base
8511 DAG.getTargetConstant(1, MVT::i8), // Scale
8512 DAG.getRegister(0, MVT::i32), // Index
8513 DAG.getTargetConstant(0, MVT::i32), // Disp
8514 DAG.getRegister(0, MVT::i32), // Segment.
8515 Zero,
8516 Chain
8517 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008518 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008519 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8520 array_lengthof(Ops));
8521 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008522 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008523
Eric Christopher9a9d2752010-07-22 02:48:34 +00008524 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008525 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008526 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008527
Chris Lattner132929a2010-08-14 17:26:09 +00008528 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8529 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8530 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8531 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008532
Chris Lattner132929a2010-08-14 17:26:09 +00008533 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8534 if (!Op1 && !Op2 && !Op3 && Op4)
8535 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008536
Chris Lattner132929a2010-08-14 17:26:09 +00008537 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8538 if (Op1 && !Op2 && !Op3 && !Op4)
8539 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008540
8541 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008542 // (MFENCE)>;
8543 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008544}
8545
Dan Gohmand858e902010-04-17 15:26:15 +00008546SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008547 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008548 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008549 unsigned Reg = 0;
8550 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008551 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008552 default:
8553 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008554 case MVT::i8: Reg = X86::AL; size = 1; break;
8555 case MVT::i16: Reg = X86::AX; size = 2; break;
8556 case MVT::i32: Reg = X86::EAX; size = 4; break;
8557 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008558 assert(Subtarget->is64Bit() && "Node not type legal!");
8559 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008560 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008561 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008562 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008563 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008564 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008565 Op.getOperand(1),
8566 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008567 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008568 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008569 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008570 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8571 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8572 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008573 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008574 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008575 return cpOut;
8576}
8577
Duncan Sands1607f052008-12-01 11:39:25 +00008578SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008579 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008580 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00008581 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008582 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008583 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008584 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008585 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8586 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008587 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008588 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8589 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008590 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008592 rdx.getValue(1)
8593 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008594 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008595}
8596
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008597SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008598 SelectionDAG &DAG) const {
8599 EVT SrcVT = Op.getOperand(0).getValueType();
8600 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008601 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8602 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008603 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008604 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008605 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008606 // i64 <=> MMX conversions are Legal.
8607 if (SrcVT==MVT::i64 && DstVT.isVector())
8608 return Op;
8609 if (DstVT==MVT::i64 && SrcVT.isVector())
8610 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008611 // MMX <=> MMX conversions are Legal.
8612 if (SrcVT.isVector() && DstVT.isVector())
8613 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008614 // All other conversions need to be expanded.
8615 return SDValue();
8616}
Dan Gohmand858e902010-04-17 15:26:15 +00008617SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008618 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008619 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008620 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008621 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008622 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008623 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008624 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008625 Node->getOperand(0),
8626 Node->getOperand(1), negOp,
8627 cast<AtomicSDNode>(Node)->getSrcValue(),
8628 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008629}
8630
Evan Cheng0db9fe62006-04-25 20:13:52 +00008631/// LowerOperation - Provide custom lowering hooks for some operations.
8632///
Dan Gohmand858e902010-04-17 15:26:15 +00008633SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008634 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008635 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008636 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008637 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8638 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008639 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008640 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008641 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8642 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8643 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
8644 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8645 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8646 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008647 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008648 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008649 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008650 case ISD::SHL_PARTS:
8651 case ISD::SRA_PARTS:
8652 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8653 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008654 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008655 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008656 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008657 case ISD::FABS: return LowerFABS(Op, DAG);
8658 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008659 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008660 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008661 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008662 case ISD::SELECT: return LowerSELECT(Op, DAG);
8663 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008664 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008665 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008666 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008667 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008668 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008669 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8670 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008671 case ISD::FRAME_TO_ARGS_OFFSET:
8672 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008673 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008674 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008675 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008676 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008677 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8678 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008679 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008680 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008681 case ISD::SADDO:
8682 case ISD::UADDO:
8683 case ISD::SSUBO:
8684 case ISD::USUBO:
8685 case ISD::SMULO:
8686 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008687 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008688 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008689 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008690}
8691
Duncan Sands1607f052008-12-01 11:39:25 +00008692void X86TargetLowering::
8693ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008694 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008695 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008696 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008697 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008698
8699 SDValue Chain = Node->getOperand(0);
8700 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008701 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008702 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008703 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008704 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008705 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008706 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008707 SDValue Result =
8708 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8709 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008710 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008711 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008712 Results.push_back(Result.getValue(2));
8713}
8714
Duncan Sands126d9072008-07-04 11:47:58 +00008715/// ReplaceNodeResults - Replace a node with an illegal result type
8716/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008717void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8718 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008719 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008720 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008721 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008722 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008723 assert(false && "Do not know how to custom type legalize this operation!");
8724 return;
8725 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008726 std::pair<SDValue,SDValue> Vals =
8727 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008728 SDValue FIST = Vals.first, StackSlot = Vals.second;
8729 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008730 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008731 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008732 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8733 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008734 }
8735 return;
8736 }
8737 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008738 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008739 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008740 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008741 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008742 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008743 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008744 eax.getValue(2));
8745 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8746 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008747 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008748 Results.push_back(edx.getValue(1));
8749 return;
8750 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008751 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008752 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008753 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008754 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008755 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8756 DAG.getConstant(0, MVT::i32));
8757 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8758 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008759 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8760 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008761 cpInL.getValue(1));
8762 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008763 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8764 DAG.getConstant(0, MVT::i32));
8765 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8766 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008767 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008768 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008769 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008770 swapInL.getValue(1));
8771 SDValue Ops[] = { swapInH.getValue(0),
8772 N->getOperand(1),
8773 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008774 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008775 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8776 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8777 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008778 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008779 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008780 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008781 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008782 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008783 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008784 Results.push_back(cpOutH.getValue(1));
8785 return;
8786 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008787 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008788 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8789 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008790 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008791 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8792 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008793 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008794 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8795 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008796 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008797 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8798 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008799 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008800 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8801 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008802 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008803 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8804 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008805 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008806 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8807 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008808 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008809}
8810
Evan Cheng72261582005-12-20 06:22:03 +00008811const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8812 switch (Opcode) {
8813 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008814 case X86ISD::BSF: return "X86ISD::BSF";
8815 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008816 case X86ISD::SHLD: return "X86ISD::SHLD";
8817 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008818 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008819 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008820 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008821 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008822 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008823 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008824 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8825 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8826 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008827 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008828 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008829 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008830 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008831 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008832 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008833 case X86ISD::COMI: return "X86ISD::COMI";
8834 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008835 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008836 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008837 case X86ISD::CMOV: return "X86ISD::CMOV";
8838 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008839 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008840 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8841 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008842 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008843 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008844 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008845 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008846 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008847 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8848 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008849 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008850 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00008851 case X86ISD::PANDN: return "X86ISD::PANDN";
8852 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
8853 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
8854 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +00008855 case X86ISD::FMAX: return "X86ISD::FMAX";
8856 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008857 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8858 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008859 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008860 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008861 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008862 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008863 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008864 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8865 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008866 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8867 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8868 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8869 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8870 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8871 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008872 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8873 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008874 case X86ISD::VSHL: return "X86ISD::VSHL";
8875 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008876 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8877 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8878 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8879 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8880 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8881 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8882 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8883 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8884 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8885 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008886 case X86ISD::ADD: return "X86ISD::ADD";
8887 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008888 case X86ISD::SMUL: return "X86ISD::SMUL";
8889 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008890 case X86ISD::INC: return "X86ISD::INC";
8891 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008892 case X86ISD::OR: return "X86ISD::OR";
8893 case X86ISD::XOR: return "X86ISD::XOR";
8894 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008895 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008896 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008897 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008898 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8899 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8900 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8901 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
8902 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
8903 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
8904 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
8905 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
8906 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008907 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00008908 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008909 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00008910 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
8911 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008912 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
8913 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
8914 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
8915 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
8916 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
8917 case X86ISD::MOVSD: return "X86ISD::MOVSD";
8918 case X86ISD::MOVSS: return "X86ISD::MOVSS";
8919 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
8920 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
8921 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
8922 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
8923 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
8924 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
8925 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
8926 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
8927 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
8928 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
8929 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
8930 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008931 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00008932 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008933 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008934 }
8935}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008936
Chris Lattnerc9addb72007-03-30 23:15:24 +00008937// isLegalAddressingMode - Return true if the addressing mode represented
8938// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008939bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008940 const Type *Ty) const {
8941 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008942 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00008943 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008944
Chris Lattnerc9addb72007-03-30 23:15:24 +00008945 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008946 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008947 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008948
Chris Lattnerc9addb72007-03-30 23:15:24 +00008949 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008950 unsigned GVFlags =
8951 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008952
Chris Lattnerdfed4132009-07-10 07:38:24 +00008953 // If a reference to this global requires an extra load, we can't fold it.
8954 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008955 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008956
Chris Lattnerdfed4132009-07-10 07:38:24 +00008957 // If BaseGV requires a register for the PIC base, we cannot also have a
8958 // BaseReg specified.
8959 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00008960 return false;
Evan Cheng52787842007-08-01 23:46:47 +00008961
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008962 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00008963 if ((M != CodeModel::Small || R != Reloc::Static) &&
8964 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008965 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00008966 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008967
Chris Lattnerc9addb72007-03-30 23:15:24 +00008968 switch (AM.Scale) {
8969 case 0:
8970 case 1:
8971 case 2:
8972 case 4:
8973 case 8:
8974 // These scales always work.
8975 break;
8976 case 3:
8977 case 5:
8978 case 9:
8979 // These scales are formed with basereg+scalereg. Only accept if there is
8980 // no basereg yet.
8981 if (AM.HasBaseReg)
8982 return false;
8983 break;
8984 default: // Other stuff never works.
8985 return false;
8986 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008987
Chris Lattnerc9addb72007-03-30 23:15:24 +00008988 return true;
8989}
8990
8991
Evan Cheng2bd122c2007-10-26 01:56:11 +00008992bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008993 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00008994 return false;
Evan Chenge127a732007-10-29 07:57:50 +00008995 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8996 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008997 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00008998 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008999 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009000}
9001
Owen Andersone50ed302009-08-10 22:56:29 +00009002bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009003 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009004 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009005 unsigned NumBits1 = VT1.getSizeInBits();
9006 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009007 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009008 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009009 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009010}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009011
Dan Gohman97121ba2009-04-08 00:15:30 +00009012bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009013 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009014 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009015}
9016
Owen Andersone50ed302009-08-10 22:56:29 +00009017bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009018 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009019 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009020}
9021
Owen Andersone50ed302009-08-10 22:56:29 +00009022bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009023 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009024 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009025}
9026
Evan Cheng60c07e12006-07-05 22:17:51 +00009027/// isShuffleMaskLegal - Targets can use this to indicate that they only
9028/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9029/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9030/// are assumed to be legal.
9031bool
Eric Christopherfd179292009-08-27 18:07:15 +00009032X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009033 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009034 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009035 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009036 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009037
Nate Begemana09008b2009-10-19 02:17:23 +00009038 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009039 return (VT.getVectorNumElements() == 2 ||
9040 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9041 isMOVLMask(M, VT) ||
9042 isSHUFPMask(M, VT) ||
9043 isPSHUFDMask(M, VT) ||
9044 isPSHUFHWMask(M, VT) ||
9045 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009046 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009047 isUNPCKLMask(M, VT) ||
9048 isUNPCKHMask(M, VT) ||
9049 isUNPCKL_v_undef_Mask(M, VT) ||
9050 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009051}
9052
Dan Gohman7d8143f2008-04-09 20:09:42 +00009053bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009054X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009055 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009056 unsigned NumElts = VT.getVectorNumElements();
9057 // FIXME: This collection of masks seems suspect.
9058 if (NumElts == 2)
9059 return true;
9060 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9061 return (isMOVLMask(Mask, VT) ||
9062 isCommutedMOVLMask(Mask, VT, true) ||
9063 isSHUFPMask(Mask, VT) ||
9064 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009065 }
9066 return false;
9067}
9068
9069//===----------------------------------------------------------------------===//
9070// X86 Scheduler Hooks
9071//===----------------------------------------------------------------------===//
9072
Mon P Wang63307c32008-05-05 19:05:59 +00009073// private utility function
9074MachineBasicBlock *
9075X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9076 MachineBasicBlock *MBB,
9077 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009078 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009079 unsigned LoadOpc,
9080 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009081 unsigned notOpc,
9082 unsigned EAXreg,
9083 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009084 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009085 // For the atomic bitwise operator, we generate
9086 // thisMBB:
9087 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009088 // ld t1 = [bitinstr.addr]
9089 // op t2 = t1, [bitinstr.val]
9090 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009091 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9092 // bz newMBB
9093 // fallthrough -->nextMBB
9094 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9095 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009096 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009097 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009098
Mon P Wang63307c32008-05-05 19:05:59 +00009099 /// First build the CFG
9100 MachineFunction *F = MBB->getParent();
9101 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009102 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9103 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9104 F->insert(MBBIter, newMBB);
9105 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009106
Dan Gohman14152b42010-07-06 20:24:04 +00009107 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9108 nextMBB->splice(nextMBB->begin(), thisMBB,
9109 llvm::next(MachineBasicBlock::iterator(bInstr)),
9110 thisMBB->end());
9111 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009112
Mon P Wang63307c32008-05-05 19:05:59 +00009113 // Update thisMBB to fall through to newMBB
9114 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009115
Mon P Wang63307c32008-05-05 19:05:59 +00009116 // newMBB jumps to itself and fall through to nextMBB
9117 newMBB->addSuccessor(nextMBB);
9118 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009119
Mon P Wang63307c32008-05-05 19:05:59 +00009120 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009121 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009122 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009123 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009124 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009125 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009126 int numArgs = bInstr->getNumOperands() - 1;
9127 for (int i=0; i < numArgs; ++i)
9128 argOpers[i] = &bInstr->getOperand(i+1);
9129
9130 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009131 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009132 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009133
Dale Johannesen140be2d2008-08-19 18:47:28 +00009134 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009135 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009136 for (int i=0; i <= lastAddrIndx; ++i)
9137 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009138
Dale Johannesen140be2d2008-08-19 18:47:28 +00009139 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009140 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009141 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009143 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009144 tt = t1;
9145
Dale Johannesen140be2d2008-08-19 18:47:28 +00009146 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009147 assert((argOpers[valArgIndx]->isReg() ||
9148 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009149 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009150 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009151 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009152 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009153 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009154 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009155 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009156
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009157 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009158 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009159
Dale Johannesene4d209d2009-02-03 20:21:25 +00009160 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009161 for (int i=0; i <= lastAddrIndx; ++i)
9162 (*MIB).addOperand(*argOpers[i]);
9163 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009164 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009165 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9166 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009167
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009168 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009169 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009170
Mon P Wang63307c32008-05-05 19:05:59 +00009171 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009172 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009173
Dan Gohman14152b42010-07-06 20:24:04 +00009174 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009175 return nextMBB;
9176}
9177
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009178// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009179MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009180X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9181 MachineBasicBlock *MBB,
9182 unsigned regOpcL,
9183 unsigned regOpcH,
9184 unsigned immOpcL,
9185 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009186 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009187 // For the atomic bitwise operator, we generate
9188 // thisMBB (instructions are in pairs, except cmpxchg8b)
9189 // ld t1,t2 = [bitinstr.addr]
9190 // newMBB:
9191 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9192 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009193 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009194 // mov ECX, EBX <- t5, t6
9195 // mov EAX, EDX <- t1, t2
9196 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9197 // mov t3, t4 <- EAX, EDX
9198 // bz newMBB
9199 // result in out1, out2
9200 // fallthrough -->nextMBB
9201
9202 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9203 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009204 const unsigned NotOpc = X86::NOT32r;
9205 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9206 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9207 MachineFunction::iterator MBBIter = MBB;
9208 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009209
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009210 /// First build the CFG
9211 MachineFunction *F = MBB->getParent();
9212 MachineBasicBlock *thisMBB = MBB;
9213 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9214 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9215 F->insert(MBBIter, newMBB);
9216 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009217
Dan Gohman14152b42010-07-06 20:24:04 +00009218 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9219 nextMBB->splice(nextMBB->begin(), thisMBB,
9220 llvm::next(MachineBasicBlock::iterator(bInstr)),
9221 thisMBB->end());
9222 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009223
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009224 // Update thisMBB to fall through to newMBB
9225 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009226
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009227 // newMBB jumps to itself and fall through to nextMBB
9228 newMBB->addSuccessor(nextMBB);
9229 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009230
Dale Johannesene4d209d2009-02-03 20:21:25 +00009231 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009232 // Insert instructions into newMBB based on incoming instruction
9233 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009234 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009235 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009236 MachineOperand& dest1Oper = bInstr->getOperand(0);
9237 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009238 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9239 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009240 argOpers[i] = &bInstr->getOperand(i+2);
9241
Dan Gohman71ea4e52010-05-14 21:01:44 +00009242 // We use some of the operands multiple times, so conservatively just
9243 // clear any kill flags that might be present.
9244 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9245 argOpers[i]->setIsKill(false);
9246 }
9247
Evan Chengad5b52f2010-01-08 19:14:57 +00009248 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009249 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009250
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009251 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009252 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009253 for (int i=0; i <= lastAddrIndx; ++i)
9254 (*MIB).addOperand(*argOpers[i]);
9255 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009256 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009257 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009258 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009259 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009260 MachineOperand newOp3 = *(argOpers[3]);
9261 if (newOp3.isImm())
9262 newOp3.setImm(newOp3.getImm()+4);
9263 else
9264 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009265 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009266 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009267
9268 // t3/4 are defined later, at the bottom of the loop
9269 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9270 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009271 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009272 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009273 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009274 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9275
Evan Cheng306b4ca2010-01-08 23:41:50 +00009276 // The subsequent operations should be using the destination registers of
9277 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009278 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009279 t1 = F->getRegInfo().createVirtualRegister(RC);
9280 t2 = F->getRegInfo().createVirtualRegister(RC);
9281 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9282 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009283 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009284 t1 = dest1Oper.getReg();
9285 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009286 }
9287
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009288 int valArgIndx = lastAddrIndx + 1;
9289 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009290 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009291 "invalid operand");
9292 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9293 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009294 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009295 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009296 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009297 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009298 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009299 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009300 (*MIB).addOperand(*argOpers[valArgIndx]);
9301 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009302 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009303 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009304 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009305 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009306 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009307 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009308 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009309 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009310 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009311 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009312
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009313 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009314 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009315 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009316 MIB.addReg(t2);
9317
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009318 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009319 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009320 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009321 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009322
Dale Johannesene4d209d2009-02-03 20:21:25 +00009323 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009324 for (int i=0; i <= lastAddrIndx; ++i)
9325 (*MIB).addOperand(*argOpers[i]);
9326
9327 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009328 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9329 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009330
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009331 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009332 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009333 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009334 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009335
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009336 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009337 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009338
Dan Gohman14152b42010-07-06 20:24:04 +00009339 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009340 return nextMBB;
9341}
9342
9343// private utility function
9344MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009345X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9346 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009347 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009348 // For the atomic min/max operator, we generate
9349 // thisMBB:
9350 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009351 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009352 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009353 // cmp t1, t2
9354 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009355 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009356 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9357 // bz newMBB
9358 // fallthrough -->nextMBB
9359 //
9360 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9361 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009362 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009363 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009364
Mon P Wang63307c32008-05-05 19:05:59 +00009365 /// First build the CFG
9366 MachineFunction *F = MBB->getParent();
9367 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009368 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9369 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9370 F->insert(MBBIter, newMBB);
9371 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009372
Dan Gohman14152b42010-07-06 20:24:04 +00009373 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9374 nextMBB->splice(nextMBB->begin(), thisMBB,
9375 llvm::next(MachineBasicBlock::iterator(mInstr)),
9376 thisMBB->end());
9377 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009378
Mon P Wang63307c32008-05-05 19:05:59 +00009379 // Update thisMBB to fall through to newMBB
9380 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009381
Mon P Wang63307c32008-05-05 19:05:59 +00009382 // newMBB jumps to newMBB and fall through to nextMBB
9383 newMBB->addSuccessor(nextMBB);
9384 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009385
Dale Johannesene4d209d2009-02-03 20:21:25 +00009386 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009387 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009388 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009389 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009390 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009391 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009392 int numArgs = mInstr->getNumOperands() - 1;
9393 for (int i=0; i < numArgs; ++i)
9394 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009395
Mon P Wang63307c32008-05-05 19:05:59 +00009396 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009397 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009398 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009399
Mon P Wangab3e7472008-05-05 22:56:23 +00009400 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009401 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009402 for (int i=0; i <= lastAddrIndx; ++i)
9403 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009404
Mon P Wang63307c32008-05-05 19:05:59 +00009405 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009406 assert((argOpers[valArgIndx]->isReg() ||
9407 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009408 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009409
9410 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009411 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009412 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009413 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009414 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009415 (*MIB).addOperand(*argOpers[valArgIndx]);
9416
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009417 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009418 MIB.addReg(t1);
9419
Dale Johannesene4d209d2009-02-03 20:21:25 +00009420 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009421 MIB.addReg(t1);
9422 MIB.addReg(t2);
9423
9424 // Generate movc
9425 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009426 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009427 MIB.addReg(t2);
9428 MIB.addReg(t1);
9429
9430 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009431 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009432 for (int i=0; i <= lastAddrIndx; ++i)
9433 (*MIB).addOperand(*argOpers[i]);
9434 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009435 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009436 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9437 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009438
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009439 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009440 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009441
Mon P Wang63307c32008-05-05 19:05:59 +00009442 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009443 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009444
Dan Gohman14152b42010-07-06 20:24:04 +00009445 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009446 return nextMBB;
9447}
9448
Eric Christopherf83a5de2009-08-27 18:08:16 +00009449// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009450// or XMM0_V32I8 in AVX all of this code can be replaced with that
9451// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009452MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009453X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009454 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009455 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9456 "Target must have SSE4.2 or AVX features enabled");
9457
Eric Christopherb120ab42009-08-18 22:50:32 +00009458 DebugLoc dl = MI->getDebugLoc();
9459 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009460 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009461 if (!Subtarget->hasAVX()) {
9462 if (memArg)
9463 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9464 else
9465 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9466 } else {
9467 if (memArg)
9468 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9469 else
9470 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9471 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009472
Eric Christopher41c902f2010-11-30 08:20:21 +00009473 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009474 for (unsigned i = 0; i < numArgs; ++i) {
9475 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009476 if (!(Op.isReg() && Op.isImplicit()))
9477 MIB.addOperand(Op);
9478 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009479 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009480 .addReg(X86::XMM0);
9481
Dan Gohman14152b42010-07-06 20:24:04 +00009482 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009483 return BB;
9484}
9485
9486MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009487X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009488 DebugLoc dl = MI->getDebugLoc();
9489 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9490
9491 // Address into RAX/EAX, other two args into ECX, EDX.
9492 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9493 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9494 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9495 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009496 MIB.addOperand(MI->getOperand(i));
Eric Christopher228232b2010-11-30 07:20:12 +00009497
9498 unsigned ValOps = X86::AddrNumOperands;
9499 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9500 .addReg(MI->getOperand(ValOps).getReg());
9501 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9502 .addReg(MI->getOperand(ValOps+1).getReg());
9503
9504 // The instruction doesn't actually take any operands though.
9505 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
9506
9507 MI->eraseFromParent(); // The pseudo is gone now.
9508 return BB;
9509}
9510
9511MachineBasicBlock *
9512X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009513 DebugLoc dl = MI->getDebugLoc();
9514 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9515
9516 // First arg in ECX, the second in EAX.
9517 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9518 .addReg(MI->getOperand(0).getReg());
9519 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9520 .addReg(MI->getOperand(1).getReg());
9521
9522 // The instruction doesn't actually take any operands though.
9523 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
9524
9525 MI->eraseFromParent(); // The pseudo is gone now.
9526 return BB;
9527}
9528
9529MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009530X86TargetLowering::EmitVAARG64WithCustomInserter(
9531 MachineInstr *MI,
9532 MachineBasicBlock *MBB) const {
9533 // Emit va_arg instruction on X86-64.
9534
9535 // Operands to this pseudo-instruction:
9536 // 0 ) Output : destination address (reg)
9537 // 1-5) Input : va_list address (addr, i64mem)
9538 // 6 ) ArgSize : Size (in bytes) of vararg type
9539 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9540 // 8 ) Align : Alignment of type
9541 // 9 ) EFLAGS (implicit-def)
9542
9543 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9544 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9545
9546 unsigned DestReg = MI->getOperand(0).getReg();
9547 MachineOperand &Base = MI->getOperand(1);
9548 MachineOperand &Scale = MI->getOperand(2);
9549 MachineOperand &Index = MI->getOperand(3);
9550 MachineOperand &Disp = MI->getOperand(4);
9551 MachineOperand &Segment = MI->getOperand(5);
9552 unsigned ArgSize = MI->getOperand(6).getImm();
9553 unsigned ArgMode = MI->getOperand(7).getImm();
9554 unsigned Align = MI->getOperand(8).getImm();
9555
9556 // Memory Reference
9557 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9558 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9559 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9560
9561 // Machine Information
9562 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9563 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9564 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9565 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9566 DebugLoc DL = MI->getDebugLoc();
9567
9568 // struct va_list {
9569 // i32 gp_offset
9570 // i32 fp_offset
9571 // i64 overflow_area (address)
9572 // i64 reg_save_area (address)
9573 // }
9574 // sizeof(va_list) = 24
9575 // alignment(va_list) = 8
9576
9577 unsigned TotalNumIntRegs = 6;
9578 unsigned TotalNumXMMRegs = 8;
9579 bool UseGPOffset = (ArgMode == 1);
9580 bool UseFPOffset = (ArgMode == 2);
9581 unsigned MaxOffset = TotalNumIntRegs * 8 +
9582 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9583
9584 /* Align ArgSize to a multiple of 8 */
9585 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9586 bool NeedsAlign = (Align > 8);
9587
9588 MachineBasicBlock *thisMBB = MBB;
9589 MachineBasicBlock *overflowMBB;
9590 MachineBasicBlock *offsetMBB;
9591 MachineBasicBlock *endMBB;
9592
9593 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9594 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9595 unsigned OffsetReg = 0;
9596
9597 if (!UseGPOffset && !UseFPOffset) {
9598 // If we only pull from the overflow region, we don't create a branch.
9599 // We don't need to alter control flow.
9600 OffsetDestReg = 0; // unused
9601 OverflowDestReg = DestReg;
9602
9603 offsetMBB = NULL;
9604 overflowMBB = thisMBB;
9605 endMBB = thisMBB;
9606 } else {
9607 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9608 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9609 // If not, pull from overflow_area. (branch to overflowMBB)
9610 //
9611 // thisMBB
9612 // | .
9613 // | .
9614 // offsetMBB overflowMBB
9615 // | .
9616 // | .
9617 // endMBB
9618
9619 // Registers for the PHI in endMBB
9620 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9621 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9622
9623 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9624 MachineFunction *MF = MBB->getParent();
9625 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9626 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9627 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9628
9629 MachineFunction::iterator MBBIter = MBB;
9630 ++MBBIter;
9631
9632 // Insert the new basic blocks
9633 MF->insert(MBBIter, offsetMBB);
9634 MF->insert(MBBIter, overflowMBB);
9635 MF->insert(MBBIter, endMBB);
9636
9637 // Transfer the remainder of MBB and its successor edges to endMBB.
9638 endMBB->splice(endMBB->begin(), thisMBB,
9639 llvm::next(MachineBasicBlock::iterator(MI)),
9640 thisMBB->end());
9641 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9642
9643 // Make offsetMBB and overflowMBB successors of thisMBB
9644 thisMBB->addSuccessor(offsetMBB);
9645 thisMBB->addSuccessor(overflowMBB);
9646
9647 // endMBB is a successor of both offsetMBB and overflowMBB
9648 offsetMBB->addSuccessor(endMBB);
9649 overflowMBB->addSuccessor(endMBB);
9650
9651 // Load the offset value into a register
9652 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9653 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9654 .addOperand(Base)
9655 .addOperand(Scale)
9656 .addOperand(Index)
9657 .addDisp(Disp, UseFPOffset ? 4 : 0)
9658 .addOperand(Segment)
9659 .setMemRefs(MMOBegin, MMOEnd);
9660
9661 // Check if there is enough room left to pull this argument.
9662 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9663 .addReg(OffsetReg)
9664 .addImm(MaxOffset + 8 - ArgSizeA8);
9665
9666 // Branch to "overflowMBB" if offset >= max
9667 // Fall through to "offsetMBB" otherwise
9668 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9669 .addMBB(overflowMBB);
9670 }
9671
9672 // In offsetMBB, emit code to use the reg_save_area.
9673 if (offsetMBB) {
9674 assert(OffsetReg != 0);
9675
9676 // Read the reg_save_area address.
9677 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9678 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9679 .addOperand(Base)
9680 .addOperand(Scale)
9681 .addOperand(Index)
9682 .addDisp(Disp, 16)
9683 .addOperand(Segment)
9684 .setMemRefs(MMOBegin, MMOEnd);
9685
9686 // Zero-extend the offset
9687 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9688 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9689 .addImm(0)
9690 .addReg(OffsetReg)
9691 .addImm(X86::sub_32bit);
9692
9693 // Add the offset to the reg_save_area to get the final address.
9694 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9695 .addReg(OffsetReg64)
9696 .addReg(RegSaveReg);
9697
9698 // Compute the offset for the next argument
9699 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9700 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9701 .addReg(OffsetReg)
9702 .addImm(UseFPOffset ? 16 : 8);
9703
9704 // Store it back into the va_list.
9705 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9706 .addOperand(Base)
9707 .addOperand(Scale)
9708 .addOperand(Index)
9709 .addDisp(Disp, UseFPOffset ? 4 : 0)
9710 .addOperand(Segment)
9711 .addReg(NextOffsetReg)
9712 .setMemRefs(MMOBegin, MMOEnd);
9713
9714 // Jump to endMBB
9715 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9716 .addMBB(endMBB);
9717 }
9718
9719 //
9720 // Emit code to use overflow area
9721 //
9722
9723 // Load the overflow_area address into a register.
9724 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9725 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9726 .addOperand(Base)
9727 .addOperand(Scale)
9728 .addOperand(Index)
9729 .addDisp(Disp, 8)
9730 .addOperand(Segment)
9731 .setMemRefs(MMOBegin, MMOEnd);
9732
9733 // If we need to align it, do so. Otherwise, just copy the address
9734 // to OverflowDestReg.
9735 if (NeedsAlign) {
9736 // Align the overflow address
9737 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9738 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9739
9740 // aligned_addr = (addr + (align-1)) & ~(align-1)
9741 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9742 .addReg(OverflowAddrReg)
9743 .addImm(Align-1);
9744
9745 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9746 .addReg(TmpReg)
9747 .addImm(~(uint64_t)(Align-1));
9748 } else {
9749 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9750 .addReg(OverflowAddrReg);
9751 }
9752
9753 // Compute the next overflow address after this argument.
9754 // (the overflow address should be kept 8-byte aligned)
9755 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9756 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9757 .addReg(OverflowDestReg)
9758 .addImm(ArgSizeA8);
9759
9760 // Store the new overflow address.
9761 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9762 .addOperand(Base)
9763 .addOperand(Scale)
9764 .addOperand(Index)
9765 .addDisp(Disp, 8)
9766 .addOperand(Segment)
9767 .addReg(NextAddrReg)
9768 .setMemRefs(MMOBegin, MMOEnd);
9769
9770 // If we branched, emit the PHI to the front of endMBB.
9771 if (offsetMBB) {
9772 BuildMI(*endMBB, endMBB->begin(), DL,
9773 TII->get(X86::PHI), DestReg)
9774 .addReg(OffsetDestReg).addMBB(offsetMBB)
9775 .addReg(OverflowDestReg).addMBB(overflowMBB);
9776 }
9777
9778 // Erase the pseudo instruction
9779 MI->eraseFromParent();
9780
9781 return endMBB;
9782}
9783
9784MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009785X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9786 MachineInstr *MI,
9787 MachineBasicBlock *MBB) const {
9788 // Emit code to save XMM registers to the stack. The ABI says that the
9789 // number of registers to save is given in %al, so it's theoretically
9790 // possible to do an indirect jump trick to avoid saving all of them,
9791 // however this code takes a simpler approach and just executes all
9792 // of the stores if %al is non-zero. It's less code, and it's probably
9793 // easier on the hardware branch predictor, and stores aren't all that
9794 // expensive anyway.
9795
9796 // Create the new basic blocks. One block contains all the XMM stores,
9797 // and one block is the final destination regardless of whether any
9798 // stores were performed.
9799 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9800 MachineFunction *F = MBB->getParent();
9801 MachineFunction::iterator MBBIter = MBB;
9802 ++MBBIter;
9803 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9804 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9805 F->insert(MBBIter, XMMSaveMBB);
9806 F->insert(MBBIter, EndMBB);
9807
Dan Gohman14152b42010-07-06 20:24:04 +00009808 // Transfer the remainder of MBB and its successor edges to EndMBB.
9809 EndMBB->splice(EndMBB->begin(), MBB,
9810 llvm::next(MachineBasicBlock::iterator(MI)),
9811 MBB->end());
9812 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9813
Dan Gohmand6708ea2009-08-15 01:38:56 +00009814 // The original block will now fall through to the XMM save block.
9815 MBB->addSuccessor(XMMSaveMBB);
9816 // The XMMSaveMBB will fall through to the end block.
9817 XMMSaveMBB->addSuccessor(EndMBB);
9818
9819 // Now add the instructions.
9820 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9821 DebugLoc DL = MI->getDebugLoc();
9822
9823 unsigned CountReg = MI->getOperand(0).getReg();
9824 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9825 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9826
9827 if (!Subtarget->isTargetWin64()) {
9828 // If %al is 0, branch around the XMM save block.
9829 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009830 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009831 MBB->addSuccessor(EndMBB);
9832 }
9833
9834 // In the XMM save block, save all the XMM argument registers.
9835 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9836 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009837 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009838 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009839 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009840 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009841 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009842 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9843 .addFrameIndex(RegSaveFrameIndex)
9844 .addImm(/*Scale=*/1)
9845 .addReg(/*IndexReg=*/0)
9846 .addImm(/*Disp=*/Offset)
9847 .addReg(/*Segment=*/0)
9848 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009849 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009850 }
9851
Dan Gohman14152b42010-07-06 20:24:04 +00009852 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009853
9854 return EndMBB;
9855}
Mon P Wang63307c32008-05-05 19:05:59 +00009856
Evan Cheng60c07e12006-07-05 22:17:51 +00009857MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009858X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009859 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009860 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9861 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009862
Chris Lattner52600972009-09-02 05:57:00 +00009863 // To "insert" a SELECT_CC instruction, we actually have to insert the
9864 // diamond control-flow pattern. The incoming instruction knows the
9865 // destination vreg to set, the condition code register to branch on, the
9866 // true/false values to select between, and a branch opcode to use.
9867 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9868 MachineFunction::iterator It = BB;
9869 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009870
Chris Lattner52600972009-09-02 05:57:00 +00009871 // thisMBB:
9872 // ...
9873 // TrueVal = ...
9874 // cmpTY ccX, r1, r2
9875 // bCC copy1MBB
9876 // fallthrough --> copy0MBB
9877 MachineBasicBlock *thisMBB = BB;
9878 MachineFunction *F = BB->getParent();
9879 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9880 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009881 F->insert(It, copy0MBB);
9882 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009883
Bill Wendling730c07e2010-06-25 20:48:10 +00009884 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9885 // live into the sink and copy blocks.
9886 const MachineFunction *MF = BB->getParent();
9887 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9888 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009889
Dan Gohman14152b42010-07-06 20:24:04 +00009890 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9891 const MachineOperand &MO = MI->getOperand(I);
9892 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009893 unsigned Reg = MO.getReg();
9894 if (Reg != X86::EFLAGS) continue;
9895 copy0MBB->addLiveIn(Reg);
9896 sinkMBB->addLiveIn(Reg);
9897 }
9898
Dan Gohman14152b42010-07-06 20:24:04 +00009899 // Transfer the remainder of BB and its successor edges to sinkMBB.
9900 sinkMBB->splice(sinkMBB->begin(), BB,
9901 llvm::next(MachineBasicBlock::iterator(MI)),
9902 BB->end());
9903 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9904
9905 // Add the true and fallthrough blocks as its successors.
9906 BB->addSuccessor(copy0MBB);
9907 BB->addSuccessor(sinkMBB);
9908
9909 // Create the conditional branch instruction.
9910 unsigned Opc =
9911 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
9912 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
9913
Chris Lattner52600972009-09-02 05:57:00 +00009914 // copy0MBB:
9915 // %FalseValue = ...
9916 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00009917 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00009918
Chris Lattner52600972009-09-02 05:57:00 +00009919 // sinkMBB:
9920 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9921 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00009922 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
9923 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00009924 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
9925 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
9926
Dan Gohman14152b42010-07-06 20:24:04 +00009927 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00009928 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00009929}
9930
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009931MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009932X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009933 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009934 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9935 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009936
9937 // The lowering is pretty easy: we're just emitting the call to _alloca. The
9938 // non-trivial part is impdef of ESP.
9939 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
9940 // mingw-w64.
9941
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009942 const char *StackProbeSymbol =
9943 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
9944
Dan Gohman14152b42010-07-06 20:24:04 +00009945 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009946 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009947 .addReg(X86::EAX, RegState::Implicit)
9948 .addReg(X86::ESP, RegState::Implicit)
9949 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +00009950 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
9951 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009952
Dan Gohman14152b42010-07-06 20:24:04 +00009953 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009954 return BB;
9955}
Chris Lattner52600972009-09-02 05:57:00 +00009956
9957MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00009958X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
9959 MachineBasicBlock *BB) const {
9960 // This is pretty easy. We're taking the value that we received from
9961 // our load from the relocation, sticking it in either RDI (x86-64)
9962 // or EAX and doing an indirect call. The return value will then
9963 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009964 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +00009965 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00009966 DebugLoc DL = MI->getDebugLoc();
9967 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +00009968
9969 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +00009970 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009971
Eric Christopher30ef0e52010-06-03 04:07:48 +00009972 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +00009973 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9974 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +00009975 .addReg(X86::RIP)
9976 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +00009977 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +00009978 MI->getOperand(3).getTargetFlags())
9979 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +00009980 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +00009981 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +00009982 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +00009983 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9984 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +00009985 .addReg(0)
9986 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +00009987 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +00009988 MI->getOperand(3).getTargetFlags())
9989 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009990 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009991 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009992 } else {
Dan Gohman14152b42010-07-06 20:24:04 +00009993 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9994 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +00009995 .addReg(TII->getGlobalBaseReg(F))
9996 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +00009997 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +00009998 MI->getOperand(3).getTargetFlags())
9999 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010000 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010001 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010002 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010003
Dan Gohman14152b42010-07-06 20:24:04 +000010004 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010005 return BB;
10006}
10007
10008MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010009X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010010 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010011 switch (MI->getOpcode()) {
10012 default: assert(false && "Unexpected instr type to insert");
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010013 case X86::WIN_ALLOCA:
10014 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010015 case X86::TLSCall_32:
10016 case X86::TLSCall_64:
10017 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010018 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010019 case X86::CMOV_FR32:
10020 case X86::CMOV_FR64:
10021 case X86::CMOV_V4F32:
10022 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010023 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010024 case X86::CMOV_GR16:
10025 case X86::CMOV_GR32:
10026 case X86::CMOV_RFP32:
10027 case X86::CMOV_RFP64:
10028 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010029 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010030
Dale Johannesen849f2142007-07-03 00:53:03 +000010031 case X86::FP32_TO_INT16_IN_MEM:
10032 case X86::FP32_TO_INT32_IN_MEM:
10033 case X86::FP32_TO_INT64_IN_MEM:
10034 case X86::FP64_TO_INT16_IN_MEM:
10035 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010036 case X86::FP64_TO_INT64_IN_MEM:
10037 case X86::FP80_TO_INT16_IN_MEM:
10038 case X86::FP80_TO_INT32_IN_MEM:
10039 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010040 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10041 DebugLoc DL = MI->getDebugLoc();
10042
Evan Cheng60c07e12006-07-05 22:17:51 +000010043 // Change the floating point control register to use "round towards zero"
10044 // mode when truncating to an integer value.
10045 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010046 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010047 addFrameReference(BuildMI(*BB, MI, DL,
10048 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010049
10050 // Load the old value of the high byte of the control word...
10051 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010052 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010053 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010054 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010055
10056 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010057 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010058 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010059
10060 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010061 addFrameReference(BuildMI(*BB, MI, DL,
10062 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010063
10064 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010065 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010066 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010067
10068 // Get the X86 opcode to use.
10069 unsigned Opc;
10070 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010071 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010072 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10073 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10074 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10075 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10076 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10077 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010078 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10079 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10080 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010081 }
10082
10083 X86AddressMode AM;
10084 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010085 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010086 AM.BaseType = X86AddressMode::RegBase;
10087 AM.Base.Reg = Op.getReg();
10088 } else {
10089 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010090 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010091 }
10092 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010093 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010094 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010095 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010096 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010097 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010098 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010099 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010100 AM.GV = Op.getGlobal();
10101 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010102 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010103 }
Dan Gohman14152b42010-07-06 20:24:04 +000010104 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010105 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010106
10107 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010108 addFrameReference(BuildMI(*BB, MI, DL,
10109 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010110
Dan Gohman14152b42010-07-06 20:24:04 +000010111 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010112 return BB;
10113 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010114 // String/text processing lowering.
10115 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010116 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010117 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10118 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010119 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010120 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10121 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010122 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010123 return EmitPCMP(MI, BB, 5, false /* in mem */);
10124 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010125 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010126 return EmitPCMP(MI, BB, 5, true /* in mem */);
10127
Eric Christopher228232b2010-11-30 07:20:12 +000010128 // Thread synchronization.
10129 case X86::MONITOR:
10130 return EmitMonitor(MI, BB);
10131 case X86::MWAIT:
10132 return EmitMwait(MI, BB);
10133
Eric Christopherb120ab42009-08-18 22:50:32 +000010134 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010135 case X86::ATOMAND32:
10136 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010137 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010138 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010139 X86::NOT32r, X86::EAX,
10140 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010141 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010142 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10143 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010144 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010145 X86::NOT32r, X86::EAX,
10146 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010147 case X86::ATOMXOR32:
10148 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010149 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010150 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010151 X86::NOT32r, X86::EAX,
10152 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010153 case X86::ATOMNAND32:
10154 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010155 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010156 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010157 X86::NOT32r, X86::EAX,
10158 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010159 case X86::ATOMMIN32:
10160 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10161 case X86::ATOMMAX32:
10162 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10163 case X86::ATOMUMIN32:
10164 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10165 case X86::ATOMUMAX32:
10166 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010167
10168 case X86::ATOMAND16:
10169 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10170 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010171 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010172 X86::NOT16r, X86::AX,
10173 X86::GR16RegisterClass);
10174 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010175 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010176 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010177 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010178 X86::NOT16r, X86::AX,
10179 X86::GR16RegisterClass);
10180 case X86::ATOMXOR16:
10181 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10182 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010183 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010184 X86::NOT16r, X86::AX,
10185 X86::GR16RegisterClass);
10186 case X86::ATOMNAND16:
10187 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10188 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010189 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010190 X86::NOT16r, X86::AX,
10191 X86::GR16RegisterClass, true);
10192 case X86::ATOMMIN16:
10193 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10194 case X86::ATOMMAX16:
10195 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10196 case X86::ATOMUMIN16:
10197 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10198 case X86::ATOMUMAX16:
10199 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10200
10201 case X86::ATOMAND8:
10202 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10203 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010204 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010205 X86::NOT8r, X86::AL,
10206 X86::GR8RegisterClass);
10207 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010208 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010209 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010210 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010211 X86::NOT8r, X86::AL,
10212 X86::GR8RegisterClass);
10213 case X86::ATOMXOR8:
10214 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10215 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010216 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010217 X86::NOT8r, X86::AL,
10218 X86::GR8RegisterClass);
10219 case X86::ATOMNAND8:
10220 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10221 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010222 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010223 X86::NOT8r, X86::AL,
10224 X86::GR8RegisterClass, true);
10225 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010226 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010227 case X86::ATOMAND64:
10228 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010229 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010230 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010231 X86::NOT64r, X86::RAX,
10232 X86::GR64RegisterClass);
10233 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010234 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10235 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010236 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010237 X86::NOT64r, X86::RAX,
10238 X86::GR64RegisterClass);
10239 case X86::ATOMXOR64:
10240 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010241 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010242 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010243 X86::NOT64r, X86::RAX,
10244 X86::GR64RegisterClass);
10245 case X86::ATOMNAND64:
10246 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10247 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010248 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010249 X86::NOT64r, X86::RAX,
10250 X86::GR64RegisterClass, true);
10251 case X86::ATOMMIN64:
10252 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10253 case X86::ATOMMAX64:
10254 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10255 case X86::ATOMUMIN64:
10256 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10257 case X86::ATOMUMAX64:
10258 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010259
10260 // This group does 64-bit operations on a 32-bit host.
10261 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010262 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010263 X86::AND32rr, X86::AND32rr,
10264 X86::AND32ri, X86::AND32ri,
10265 false);
10266 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010267 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010268 X86::OR32rr, X86::OR32rr,
10269 X86::OR32ri, X86::OR32ri,
10270 false);
10271 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010272 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010273 X86::XOR32rr, X86::XOR32rr,
10274 X86::XOR32ri, X86::XOR32ri,
10275 false);
10276 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010277 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010278 X86::AND32rr, X86::AND32rr,
10279 X86::AND32ri, X86::AND32ri,
10280 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010281 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010282 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010283 X86::ADD32rr, X86::ADC32rr,
10284 X86::ADD32ri, X86::ADC32ri,
10285 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010286 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010287 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010288 X86::SUB32rr, X86::SBB32rr,
10289 X86::SUB32ri, X86::SBB32ri,
10290 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010291 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010292 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010293 X86::MOV32rr, X86::MOV32rr,
10294 X86::MOV32ri, X86::MOV32ri,
10295 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010296 case X86::VASTART_SAVE_XMM_REGS:
10297 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010298
10299 case X86::VAARG_64:
10300 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010301 }
10302}
10303
10304//===----------------------------------------------------------------------===//
10305// X86 Optimization Hooks
10306//===----------------------------------------------------------------------===//
10307
Dan Gohman475871a2008-07-27 21:46:04 +000010308void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010309 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010310 APInt &KnownZero,
10311 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010312 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010313 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010314 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010315 assert((Opc >= ISD::BUILTIN_OP_END ||
10316 Opc == ISD::INTRINSIC_WO_CHAIN ||
10317 Opc == ISD::INTRINSIC_W_CHAIN ||
10318 Opc == ISD::INTRINSIC_VOID) &&
10319 "Should use MaskedValueIsZero if you don't know whether Op"
10320 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010321
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010322 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010323 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010324 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010325 case X86ISD::ADD:
10326 case X86ISD::SUB:
10327 case X86ISD::SMUL:
10328 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010329 case X86ISD::INC:
10330 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010331 case X86ISD::OR:
10332 case X86ISD::XOR:
10333 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010334 // These nodes' second result is a boolean.
10335 if (Op.getResNo() == 0)
10336 break;
10337 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010338 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010339 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10340 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010341 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010342 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010343}
Chris Lattner259e97c2006-01-31 19:43:35 +000010344
Owen Andersonbc146b02010-09-21 20:42:50 +000010345unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10346 unsigned Depth) const {
10347 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10348 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10349 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010350
Owen Andersonbc146b02010-09-21 20:42:50 +000010351 // Fallback case.
10352 return 1;
10353}
10354
Evan Cheng206ee9d2006-07-07 08:33:52 +000010355/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010356/// node is a GlobalAddress + offset.
10357bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010358 const GlobalValue* &GA,
10359 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010360 if (N->getOpcode() == X86ISD::Wrapper) {
10361 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010362 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010363 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010364 return true;
10365 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010366 }
Evan Chengad4196b2008-05-12 19:56:52 +000010367 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010368}
10369
Evan Cheng206ee9d2006-07-07 08:33:52 +000010370/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10371/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10372/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010373/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010374static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010375 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010376 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010377 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010378
Eli Friedman7a5e5552009-06-07 06:52:44 +000010379 if (VT.getSizeInBits() != 128)
10380 return SDValue();
10381
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010382 // Don't create instructions with illegal types after legalize types has run.
10383 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10384 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10385 return SDValue();
10386
Nate Begemanfdea31a2010-03-24 20:49:50 +000010387 SmallVector<SDValue, 16> Elts;
10388 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010389 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010390
Nate Begemanfdea31a2010-03-24 20:49:50 +000010391 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010392}
Evan Chengd880b972008-05-09 21:53:03 +000010393
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010394/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10395/// generation and convert it from being a bunch of shuffles and extracts
10396/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010397static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10398 const TargetLowering &TLI) {
10399 SDValue InputVector = N->getOperand(0);
10400
10401 // Only operate on vectors of 4 elements, where the alternative shuffling
10402 // gets to be more expensive.
10403 if (InputVector.getValueType() != MVT::v4i32)
10404 return SDValue();
10405
10406 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10407 // single use which is a sign-extend or zero-extend, and all elements are
10408 // used.
10409 SmallVector<SDNode *, 4> Uses;
10410 unsigned ExtractedElements = 0;
10411 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10412 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10413 if (UI.getUse().getResNo() != InputVector.getResNo())
10414 return SDValue();
10415
10416 SDNode *Extract = *UI;
10417 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10418 return SDValue();
10419
10420 if (Extract->getValueType(0) != MVT::i32)
10421 return SDValue();
10422 if (!Extract->hasOneUse())
10423 return SDValue();
10424 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10425 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10426 return SDValue();
10427 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10428 return SDValue();
10429
10430 // Record which element was extracted.
10431 ExtractedElements |=
10432 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10433
10434 Uses.push_back(Extract);
10435 }
10436
10437 // If not all the elements were used, this may not be worthwhile.
10438 if (ExtractedElements != 15)
10439 return SDValue();
10440
10441 // Ok, we've now decided to do the transformation.
10442 DebugLoc dl = InputVector.getDebugLoc();
10443
10444 // Store the value to a temporary stack slot.
10445 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010446 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10447 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010448
10449 // Replace each use (extract) with a load of the appropriate element.
10450 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10451 UE = Uses.end(); UI != UE; ++UI) {
10452 SDNode *Extract = *UI;
10453
10454 // Compute the element's address.
10455 SDValue Idx = Extract->getOperand(1);
10456 unsigned EltSize =
10457 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10458 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10459 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10460
Eric Christopher90eb4022010-07-22 00:26:08 +000010461 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010462 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010463
10464 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010465 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010466 ScalarAddr, MachinePointerInfo(),
10467 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010468
10469 // Replace the exact with the load.
10470 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10471 }
10472
10473 // The replacement was made in place; don't return anything.
10474 return SDValue();
10475}
10476
Chris Lattner83e6c992006-10-04 06:57:07 +000010477/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010478static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010479 const X86Subtarget *Subtarget) {
10480 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010481 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010482 // Get the LHS/RHS of the select.
10483 SDValue LHS = N->getOperand(1);
10484 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010485
Dan Gohman670e5392009-09-21 18:03:22 +000010486 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010487 // instructions match the semantics of the common C idiom x<y?x:y but not
10488 // x<=y?x:y, because of how they handle negative zero (which can be
10489 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010490 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010491 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010492 Cond.getOpcode() == ISD::SETCC) {
10493 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010494
Chris Lattner47b4ce82009-03-11 05:48:52 +000010495 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010496 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010497 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10498 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010499 switch (CC) {
10500 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010501 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010502 // Converting this to a min would handle NaNs incorrectly, and swapping
10503 // the operands would cause it to handle comparisons between positive
10504 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010505 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010506 if (!UnsafeFPMath &&
10507 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10508 break;
10509 std::swap(LHS, RHS);
10510 }
Dan Gohman670e5392009-09-21 18:03:22 +000010511 Opcode = X86ISD::FMIN;
10512 break;
10513 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010514 // Converting this to a min would handle comparisons between positive
10515 // and negative zero incorrectly.
10516 if (!UnsafeFPMath &&
10517 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10518 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010519 Opcode = X86ISD::FMIN;
10520 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010521 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010522 // Converting this to a min would handle both negative zeros and NaNs
10523 // incorrectly, but we can swap the operands to fix both.
10524 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010525 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010526 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010527 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010528 Opcode = X86ISD::FMIN;
10529 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010530
Dan Gohman670e5392009-09-21 18:03:22 +000010531 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010532 // Converting this to a max would handle comparisons between positive
10533 // and negative zero incorrectly.
10534 if (!UnsafeFPMath &&
10535 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10536 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010537 Opcode = X86ISD::FMAX;
10538 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010539 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010540 // Converting this to a max would handle NaNs incorrectly, and swapping
10541 // the operands would cause it to handle comparisons between positive
10542 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010543 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010544 if (!UnsafeFPMath &&
10545 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10546 break;
10547 std::swap(LHS, RHS);
10548 }
Dan Gohman670e5392009-09-21 18:03:22 +000010549 Opcode = X86ISD::FMAX;
10550 break;
10551 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010552 // Converting this to a max would handle both negative zeros and NaNs
10553 // incorrectly, but we can swap the operands to fix both.
10554 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010555 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010556 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010557 case ISD::SETGE:
10558 Opcode = X86ISD::FMAX;
10559 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010560 }
Dan Gohman670e5392009-09-21 18:03:22 +000010561 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010562 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10563 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010564 switch (CC) {
10565 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010566 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010567 // Converting this to a min would handle comparisons between positive
10568 // and negative zero incorrectly, and swapping the operands would
10569 // cause it to handle NaNs incorrectly.
10570 if (!UnsafeFPMath &&
10571 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010572 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010573 break;
10574 std::swap(LHS, RHS);
10575 }
Dan Gohman670e5392009-09-21 18:03:22 +000010576 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010577 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010578 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010579 // Converting this to a min would handle NaNs incorrectly.
10580 if (!UnsafeFPMath &&
10581 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10582 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010583 Opcode = X86ISD::FMIN;
10584 break;
10585 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010586 // Converting this to a min would handle both negative zeros and NaNs
10587 // incorrectly, but we can swap the operands to fix both.
10588 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010589 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010590 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010591 case ISD::SETGE:
10592 Opcode = X86ISD::FMIN;
10593 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010594
Dan Gohman670e5392009-09-21 18:03:22 +000010595 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010596 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010597 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010598 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010599 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010600 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010601 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010602 // Converting this to a max would handle comparisons between positive
10603 // and negative zero incorrectly, and swapping the operands would
10604 // cause it to handle NaNs incorrectly.
10605 if (!UnsafeFPMath &&
10606 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010607 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010608 break;
10609 std::swap(LHS, RHS);
10610 }
Dan Gohman670e5392009-09-21 18:03:22 +000010611 Opcode = X86ISD::FMAX;
10612 break;
10613 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010614 // Converting this to a max would handle both negative zeros and NaNs
10615 // incorrectly, but we can swap the operands to fix both.
10616 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010617 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010618 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010619 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010620 Opcode = X86ISD::FMAX;
10621 break;
10622 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010623 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010624
Chris Lattner47b4ce82009-03-11 05:48:52 +000010625 if (Opcode)
10626 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010627 }
Eric Christopherfd179292009-08-27 18:07:15 +000010628
Chris Lattnerd1980a52009-03-12 06:52:53 +000010629 // If this is a select between two integer constants, try to do some
10630 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010631 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10632 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010633 // Don't do this for crazy integer types.
10634 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10635 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010636 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010637 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010638
Chris Lattnercee56e72009-03-13 05:53:31 +000010639 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010640 // Efficiently invertible.
10641 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10642 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10643 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10644 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010645 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010646 }
Eric Christopherfd179292009-08-27 18:07:15 +000010647
Chris Lattnerd1980a52009-03-12 06:52:53 +000010648 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010649 if (FalseC->getAPIntValue() == 0 &&
10650 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010651 if (NeedsCondInvert) // Invert the condition if needed.
10652 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10653 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010654
Chris Lattnerd1980a52009-03-12 06:52:53 +000010655 // Zero extend the condition if needed.
10656 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010657
Chris Lattnercee56e72009-03-13 05:53:31 +000010658 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010659 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010660 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010661 }
Eric Christopherfd179292009-08-27 18:07:15 +000010662
Chris Lattner97a29a52009-03-13 05:22:11 +000010663 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010664 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010665 if (NeedsCondInvert) // Invert the condition if needed.
10666 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10667 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010668
Chris Lattner97a29a52009-03-13 05:22:11 +000010669 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010670 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10671 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010672 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010673 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010674 }
Eric Christopherfd179292009-08-27 18:07:15 +000010675
Chris Lattnercee56e72009-03-13 05:53:31 +000010676 // Optimize cases that will turn into an LEA instruction. This requires
10677 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010678 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010679 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010680 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010681
Chris Lattnercee56e72009-03-13 05:53:31 +000010682 bool isFastMultiplier = false;
10683 if (Diff < 10) {
10684 switch ((unsigned char)Diff) {
10685 default: break;
10686 case 1: // result = add base, cond
10687 case 2: // result = lea base( , cond*2)
10688 case 3: // result = lea base(cond, cond*2)
10689 case 4: // result = lea base( , cond*4)
10690 case 5: // result = lea base(cond, cond*4)
10691 case 8: // result = lea base( , cond*8)
10692 case 9: // result = lea base(cond, cond*8)
10693 isFastMultiplier = true;
10694 break;
10695 }
10696 }
Eric Christopherfd179292009-08-27 18:07:15 +000010697
Chris Lattnercee56e72009-03-13 05:53:31 +000010698 if (isFastMultiplier) {
10699 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10700 if (NeedsCondInvert) // Invert the condition if needed.
10701 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10702 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010703
Chris Lattnercee56e72009-03-13 05:53:31 +000010704 // Zero extend the condition if needed.
10705 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10706 Cond);
10707 // Scale the condition by the difference.
10708 if (Diff != 1)
10709 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10710 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010711
Chris Lattnercee56e72009-03-13 05:53:31 +000010712 // Add the base if non-zero.
10713 if (FalseC->getAPIntValue() != 0)
10714 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10715 SDValue(FalseC, 0));
10716 return Cond;
10717 }
Eric Christopherfd179292009-08-27 18:07:15 +000010718 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010719 }
10720 }
Eric Christopherfd179292009-08-27 18:07:15 +000010721
Dan Gohman475871a2008-07-27 21:46:04 +000010722 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010723}
10724
Chris Lattnerd1980a52009-03-12 06:52:53 +000010725/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10726static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10727 TargetLowering::DAGCombinerInfo &DCI) {
10728 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010729
Chris Lattnerd1980a52009-03-12 06:52:53 +000010730 // If the flag operand isn't dead, don't touch this CMOV.
10731 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10732 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010733
Chris Lattnerd1980a52009-03-12 06:52:53 +000010734 // If this is a select between two integer constants, try to do some
10735 // optimizations. Note that the operands are ordered the opposite of SELECT
10736 // operands.
10737 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10738 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10739 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10740 // larger than FalseC (the false value).
10741 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010742
Chris Lattnerd1980a52009-03-12 06:52:53 +000010743 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10744 CC = X86::GetOppositeBranchCondition(CC);
10745 std::swap(TrueC, FalseC);
10746 }
Eric Christopherfd179292009-08-27 18:07:15 +000010747
Chris Lattnerd1980a52009-03-12 06:52:53 +000010748 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010749 // This is efficient for any integer data type (including i8/i16) and
10750 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010751 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10752 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010753 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10754 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010755
Chris Lattnerd1980a52009-03-12 06:52:53 +000010756 // Zero extend the condition if needed.
10757 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010758
Chris Lattnerd1980a52009-03-12 06:52:53 +000010759 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10760 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010761 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010762 if (N->getNumValues() == 2) // Dead flag value?
10763 return DCI.CombineTo(N, Cond, SDValue());
10764 return Cond;
10765 }
Eric Christopherfd179292009-08-27 18:07:15 +000010766
Chris Lattnercee56e72009-03-13 05:53:31 +000010767 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10768 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010769 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10770 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010771 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10772 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010773
Chris Lattner97a29a52009-03-13 05:22:11 +000010774 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010775 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10776 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010777 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10778 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010779
Chris Lattner97a29a52009-03-13 05:22:11 +000010780 if (N->getNumValues() == 2) // Dead flag value?
10781 return DCI.CombineTo(N, Cond, SDValue());
10782 return Cond;
10783 }
Eric Christopherfd179292009-08-27 18:07:15 +000010784
Chris Lattnercee56e72009-03-13 05:53:31 +000010785 // Optimize cases that will turn into an LEA instruction. This requires
10786 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010787 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010788 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010789 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010790
Chris Lattnercee56e72009-03-13 05:53:31 +000010791 bool isFastMultiplier = false;
10792 if (Diff < 10) {
10793 switch ((unsigned char)Diff) {
10794 default: break;
10795 case 1: // result = add base, cond
10796 case 2: // result = lea base( , cond*2)
10797 case 3: // result = lea base(cond, cond*2)
10798 case 4: // result = lea base( , cond*4)
10799 case 5: // result = lea base(cond, cond*4)
10800 case 8: // result = lea base( , cond*8)
10801 case 9: // result = lea base(cond, cond*8)
10802 isFastMultiplier = true;
10803 break;
10804 }
10805 }
Eric Christopherfd179292009-08-27 18:07:15 +000010806
Chris Lattnercee56e72009-03-13 05:53:31 +000010807 if (isFastMultiplier) {
10808 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10809 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010810 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10811 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010812 // Zero extend the condition if needed.
10813 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10814 Cond);
10815 // Scale the condition by the difference.
10816 if (Diff != 1)
10817 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10818 DAG.getConstant(Diff, Cond.getValueType()));
10819
10820 // Add the base if non-zero.
10821 if (FalseC->getAPIntValue() != 0)
10822 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10823 SDValue(FalseC, 0));
10824 if (N->getNumValues() == 2) // Dead flag value?
10825 return DCI.CombineTo(N, Cond, SDValue());
10826 return Cond;
10827 }
Eric Christopherfd179292009-08-27 18:07:15 +000010828 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010829 }
10830 }
10831 return SDValue();
10832}
10833
10834
Evan Cheng0b0cd912009-03-28 05:57:29 +000010835/// PerformMulCombine - Optimize a single multiply with constant into two
10836/// in order to implement it with two cheaper instructions, e.g.
10837/// LEA + SHL, LEA + LEA.
10838static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10839 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010840 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10841 return SDValue();
10842
Owen Andersone50ed302009-08-10 22:56:29 +000010843 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010844 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010845 return SDValue();
10846
10847 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10848 if (!C)
10849 return SDValue();
10850 uint64_t MulAmt = C->getZExtValue();
10851 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10852 return SDValue();
10853
10854 uint64_t MulAmt1 = 0;
10855 uint64_t MulAmt2 = 0;
10856 if ((MulAmt % 9) == 0) {
10857 MulAmt1 = 9;
10858 MulAmt2 = MulAmt / 9;
10859 } else if ((MulAmt % 5) == 0) {
10860 MulAmt1 = 5;
10861 MulAmt2 = MulAmt / 5;
10862 } else if ((MulAmt % 3) == 0) {
10863 MulAmt1 = 3;
10864 MulAmt2 = MulAmt / 3;
10865 }
10866 if (MulAmt2 &&
10867 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10868 DebugLoc DL = N->getDebugLoc();
10869
10870 if (isPowerOf2_64(MulAmt2) &&
10871 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10872 // If second multiplifer is pow2, issue it first. We want the multiply by
10873 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10874 // is an add.
10875 std::swap(MulAmt1, MulAmt2);
10876
10877 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000010878 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010879 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010880 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000010881 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010882 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000010883 DAG.getConstant(MulAmt1, VT));
10884
Eric Christopherfd179292009-08-27 18:07:15 +000010885 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010886 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000010887 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000010888 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010889 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000010890 DAG.getConstant(MulAmt2, VT));
10891
10892 // Do not add new nodes to DAG combiner worklist.
10893 DCI.CombineTo(N, NewMul, false);
10894 }
10895 return SDValue();
10896}
10897
Evan Chengad9c0a32009-12-15 00:53:42 +000010898static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
10899 SDValue N0 = N->getOperand(0);
10900 SDValue N1 = N->getOperand(1);
10901 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
10902 EVT VT = N0.getValueType();
10903
10904 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
10905 // since the result of setcc_c is all zero's or all ones.
10906 if (N1C && N0.getOpcode() == ISD::AND &&
10907 N0.getOperand(1).getOpcode() == ISD::Constant) {
10908 SDValue N00 = N0.getOperand(0);
10909 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
10910 ((N00.getOpcode() == ISD::ANY_EXTEND ||
10911 N00.getOpcode() == ISD::ZERO_EXTEND) &&
10912 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
10913 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
10914 APInt ShAmt = N1C->getAPIntValue();
10915 Mask = Mask.shl(ShAmt);
10916 if (Mask != 0)
10917 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
10918 N00, DAG.getConstant(Mask, VT));
10919 }
10920 }
10921
10922 return SDValue();
10923}
Evan Cheng0b0cd912009-03-28 05:57:29 +000010924
Nate Begeman740ab032009-01-26 00:52:55 +000010925/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
10926/// when possible.
10927static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
10928 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000010929 EVT VT = N->getValueType(0);
10930 if (!VT.isVector() && VT.isInteger() &&
10931 N->getOpcode() == ISD::SHL)
10932 return PerformSHLCombine(N, DAG);
10933
Nate Begeman740ab032009-01-26 00:52:55 +000010934 // On X86 with SSE2 support, we can transform this to a vector shift if
10935 // all elements are shifted by the same amount. We can't do this in legalize
10936 // because the a constant vector is typically transformed to a constant pool
10937 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010938 if (!Subtarget->hasSSE2())
10939 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010940
Owen Anderson825b72b2009-08-11 20:47:22 +000010941 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010942 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010943
Mon P Wang3becd092009-01-28 08:12:05 +000010944 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000010945 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000010946 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000010947 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000010948 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
10949 unsigned NumElts = VT.getVectorNumElements();
10950 unsigned i = 0;
10951 for (; i != NumElts; ++i) {
10952 SDValue Arg = ShAmtOp.getOperand(i);
10953 if (Arg.getOpcode() == ISD::UNDEF) continue;
10954 BaseShAmt = Arg;
10955 break;
10956 }
10957 for (; i != NumElts; ++i) {
10958 SDValue Arg = ShAmtOp.getOperand(i);
10959 if (Arg.getOpcode() == ISD::UNDEF) continue;
10960 if (Arg != BaseShAmt) {
10961 return SDValue();
10962 }
10963 }
10964 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000010965 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000010966 SDValue InVec = ShAmtOp.getOperand(0);
10967 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
10968 unsigned NumElts = InVec.getValueType().getVectorNumElements();
10969 unsigned i = 0;
10970 for (; i != NumElts; ++i) {
10971 SDValue Arg = InVec.getOperand(i);
10972 if (Arg.getOpcode() == ISD::UNDEF) continue;
10973 BaseShAmt = Arg;
10974 break;
10975 }
10976 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
10977 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000010978 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000010979 if (C->getZExtValue() == SplatIdx)
10980 BaseShAmt = InVec.getOperand(1);
10981 }
10982 }
10983 if (BaseShAmt.getNode() == 0)
10984 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
10985 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000010986 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010987 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000010988
Mon P Wangefa42202009-09-03 19:56:25 +000010989 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000010990 if (EltVT.bitsGT(MVT::i32))
10991 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
10992 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000010993 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000010994
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010995 // The shift amount is identical so we can do a vector shift.
10996 SDValue ValOp = N->getOperand(0);
10997 switch (N->getOpcode()) {
10998 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010999 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011000 break;
11001 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011002 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011003 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011004 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011005 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011006 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011007 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011008 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011009 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011010 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011011 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011012 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011013 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011014 break;
11015 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011016 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011017 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011018 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011019 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011020 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011021 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011022 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011023 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011024 break;
11025 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011026 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011027 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011028 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011029 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011030 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011031 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011032 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011033 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011034 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011035 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011036 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011037 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011038 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011039 }
11040 return SDValue();
11041}
11042
Nate Begemanb65c1752010-12-17 22:55:37 +000011043
11044static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11045 TargetLowering::DAGCombinerInfo &DCI,
11046 const X86Subtarget *Subtarget) {
11047 if (DCI.isBeforeLegalizeOps())
11048 return SDValue();
11049
11050 // Want to form PANDN nodes, in the hopes of then easily combining them with
11051 // OR and AND nodes to form PBLEND/PSIGN.
11052 EVT VT = N->getValueType(0);
11053 if (VT != MVT::v2i64)
11054 return SDValue();
11055
11056 SDValue N0 = N->getOperand(0);
11057 SDValue N1 = N->getOperand(1);
11058 DebugLoc DL = N->getDebugLoc();
11059
11060 // Check LHS for vnot
11061 if (N0.getOpcode() == ISD::XOR &&
11062 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11063 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11064
11065 // Check RHS for vnot
11066 if (N1.getOpcode() == ISD::XOR &&
11067 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11068 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
11069
11070 return SDValue();
11071}
11072
Evan Cheng760d1942010-01-04 21:22:48 +000011073static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011074 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011075 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011076 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011077 return SDValue();
11078
Evan Cheng760d1942010-01-04 21:22:48 +000011079 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011080 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011081 return SDValue();
11082
Evan Cheng760d1942010-01-04 21:22:48 +000011083 SDValue N0 = N->getOperand(0);
11084 SDValue N1 = N->getOperand(1);
Nate Begemanb65c1752010-12-17 22:55:37 +000011085
11086 // look for psign/blend
11087 if (Subtarget->hasSSSE3()) {
11088 if (VT == MVT::v2i64) {
11089 // Canonicalize pandn to RHS
11090 if (N0.getOpcode() == X86ISD::PANDN)
11091 std::swap(N0, N1);
11092 // or (and (m, x), (pandn m, y))
11093 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11094 SDValue Mask = N1.getOperand(0);
11095 SDValue X = N1.getOperand(1);
11096 SDValue Y;
11097 if (N0.getOperand(0) == Mask)
11098 Y = N0.getOperand(1);
11099 if (N0.getOperand(1) == Mask)
11100 Y = N0.getOperand(0);
11101
11102 // Check to see if the mask appeared in both the AND and PANDN and
11103 if (!Y.getNode())
11104 return SDValue();
11105
11106 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11107 if (Mask.getOpcode() != ISD::BITCAST ||
11108 X.getOpcode() != ISD::BITCAST ||
11109 Y.getOpcode() != ISD::BITCAST)
11110 return SDValue();
11111
11112 // Look through mask bitcast.
11113 Mask = Mask.getOperand(0);
11114 EVT MaskVT = Mask.getValueType();
11115
11116 // Validate that the Mask operand is a vector sra node. The sra node
11117 // will be an intrinsic.
11118 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11119 return SDValue();
11120
11121 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11122 // there is no psrai.b
11123 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11124 case Intrinsic::x86_sse2_psrai_w:
11125 case Intrinsic::x86_sse2_psrai_d:
11126 break;
11127 default: return SDValue();
11128 }
11129
11130 // Check that the SRA is all signbits.
11131 SDValue SraC = Mask.getOperand(2);
11132 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11133 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11134 if ((SraAmt + 1) != EltBits)
11135 return SDValue();
11136
11137 DebugLoc DL = N->getDebugLoc();
11138
11139 // Now we know we at least have a plendvb with the mask val. See if
11140 // we can form a psignb/w/d.
11141 // psign = x.type == y.type == mask.type && y = sub(0, x);
11142 X = X.getOperand(0);
11143 Y = Y.getOperand(0);
11144 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11145 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11146 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11147 unsigned Opc = 0;
11148 switch (EltBits) {
11149 case 8: Opc = X86ISD::PSIGNB; break;
11150 case 16: Opc = X86ISD::PSIGNW; break;
11151 case 32: Opc = X86ISD::PSIGND; break;
11152 default: break;
11153 }
11154 if (Opc) {
11155 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11156 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11157 }
11158 }
11159 // PBLENDVB only available on SSE 4.1
11160 if (!Subtarget->hasSSE41())
11161 return SDValue();
11162
11163 unsigned IID = Intrinsic::x86_sse41_pblendvb;
11164 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11165 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11166 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
11167 Mask = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, MVT::v16i8,
11168 DAG.getConstant(IID, MVT::i32), X, Y, Mask);
11169 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11170 }
11171 }
11172 }
11173
11174 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011175 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11176 std::swap(N0, N1);
11177 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11178 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011179 if (!N0.hasOneUse() || !N1.hasOneUse())
11180 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011181
11182 SDValue ShAmt0 = N0.getOperand(1);
11183 if (ShAmt0.getValueType() != MVT::i8)
11184 return SDValue();
11185 SDValue ShAmt1 = N1.getOperand(1);
11186 if (ShAmt1.getValueType() != MVT::i8)
11187 return SDValue();
11188 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11189 ShAmt0 = ShAmt0.getOperand(0);
11190 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11191 ShAmt1 = ShAmt1.getOperand(0);
11192
11193 DebugLoc DL = N->getDebugLoc();
11194 unsigned Opc = X86ISD::SHLD;
11195 SDValue Op0 = N0.getOperand(0);
11196 SDValue Op1 = N1.getOperand(0);
11197 if (ShAmt0.getOpcode() == ISD::SUB) {
11198 Opc = X86ISD::SHRD;
11199 std::swap(Op0, Op1);
11200 std::swap(ShAmt0, ShAmt1);
11201 }
11202
Evan Cheng8b1190a2010-04-28 01:18:01 +000011203 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011204 if (ShAmt1.getOpcode() == ISD::SUB) {
11205 SDValue Sum = ShAmt1.getOperand(0);
11206 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011207 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11208 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11209 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11210 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011211 return DAG.getNode(Opc, DL, VT,
11212 Op0, Op1,
11213 DAG.getNode(ISD::TRUNCATE, DL,
11214 MVT::i8, ShAmt0));
11215 }
11216 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11217 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11218 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011219 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011220 return DAG.getNode(Opc, DL, VT,
11221 N0.getOperand(0), N1.getOperand(0),
11222 DAG.getNode(ISD::TRUNCATE, DL,
11223 MVT::i8, ShAmt0));
11224 }
Nate Begemanb65c1752010-12-17 22:55:37 +000011225
Evan Cheng760d1942010-01-04 21:22:48 +000011226 return SDValue();
11227}
11228
Chris Lattner149a4e52008-02-22 02:09:43 +000011229/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011230static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011231 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011232 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11233 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011234 // A preferable solution to the general problem is to figure out the right
11235 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011236
11237 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011238 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011239 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011240 if (VT.getSizeInBits() != 64)
11241 return SDValue();
11242
Devang Patel578efa92009-06-05 21:57:13 +000011243 const Function *F = DAG.getMachineFunction().getFunction();
11244 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011245 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011246 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011247 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011248 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011249 isa<LoadSDNode>(St->getValue()) &&
11250 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11251 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011252 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011253 LoadSDNode *Ld = 0;
11254 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011255 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011256 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011257 // Must be a store of a load. We currently handle two cases: the load
11258 // is a direct child, and it's under an intervening TokenFactor. It is
11259 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011260 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011261 Ld = cast<LoadSDNode>(St->getChain());
11262 else if (St->getValue().hasOneUse() &&
11263 ChainVal->getOpcode() == ISD::TokenFactor) {
11264 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011265 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011266 TokenFactorIndex = i;
11267 Ld = cast<LoadSDNode>(St->getValue());
11268 } else
11269 Ops.push_back(ChainVal->getOperand(i));
11270 }
11271 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011272
Evan Cheng536e6672009-03-12 05:59:15 +000011273 if (!Ld || !ISD::isNormalLoad(Ld))
11274 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011275
Evan Cheng536e6672009-03-12 05:59:15 +000011276 // If this is not the MMX case, i.e. we are just turning i64 load/store
11277 // into f64 load/store, avoid the transformation if there are multiple
11278 // uses of the loaded value.
11279 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11280 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011281
Evan Cheng536e6672009-03-12 05:59:15 +000011282 DebugLoc LdDL = Ld->getDebugLoc();
11283 DebugLoc StDL = N->getDebugLoc();
11284 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11285 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11286 // pair instead.
11287 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011288 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011289 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11290 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011291 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011292 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011293 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011294 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011295 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011296 Ops.size());
11297 }
Evan Cheng536e6672009-03-12 05:59:15 +000011298 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011299 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011300 St->isVolatile(), St->isNonTemporal(),
11301 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011302 }
Evan Cheng536e6672009-03-12 05:59:15 +000011303
11304 // Otherwise, lower to two pairs of 32-bit loads / stores.
11305 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011306 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11307 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011308
Owen Anderson825b72b2009-08-11 20:47:22 +000011309 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011310 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011311 Ld->isVolatile(), Ld->isNonTemporal(),
11312 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011313 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011314 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011315 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011316 MinAlign(Ld->getAlignment(), 4));
11317
11318 SDValue NewChain = LoLd.getValue(1);
11319 if (TokenFactorIndex != -1) {
11320 Ops.push_back(LoLd);
11321 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011322 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011323 Ops.size());
11324 }
11325
11326 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011327 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11328 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011329
11330 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011331 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011332 St->isVolatile(), St->isNonTemporal(),
11333 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011334 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011335 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011336 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011337 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011338 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011339 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011340 }
Dan Gohman475871a2008-07-27 21:46:04 +000011341 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011342}
11343
Chris Lattner6cf73262008-01-25 06:14:17 +000011344/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11345/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011346static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011347 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11348 // F[X]OR(0.0, x) -> x
11349 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011350 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11351 if (C->getValueAPF().isPosZero())
11352 return N->getOperand(1);
11353 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11354 if (C->getValueAPF().isPosZero())
11355 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011356 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011357}
11358
11359/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011360static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011361 // FAND(0.0, x) -> 0.0
11362 // FAND(x, 0.0) -> 0.0
11363 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11364 if (C->getValueAPF().isPosZero())
11365 return N->getOperand(0);
11366 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11367 if (C->getValueAPF().isPosZero())
11368 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011369 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011370}
11371
Dan Gohmane5af2d32009-01-29 01:59:02 +000011372static SDValue PerformBTCombine(SDNode *N,
11373 SelectionDAG &DAG,
11374 TargetLowering::DAGCombinerInfo &DCI) {
11375 // BT ignores high bits in the bit index operand.
11376 SDValue Op1 = N->getOperand(1);
11377 if (Op1.hasOneUse()) {
11378 unsigned BitWidth = Op1.getValueSizeInBits();
11379 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11380 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011381 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11382 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011383 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011384 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11385 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11386 DCI.CommitTargetLoweringOpt(TLO);
11387 }
11388 return SDValue();
11389}
Chris Lattner83e6c992006-10-04 06:57:07 +000011390
Eli Friedman7a5e5552009-06-07 06:52:44 +000011391static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11392 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011393 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011394 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011395 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011396 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011397 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011398 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011399 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011400 }
11401 return SDValue();
11402}
11403
Evan Cheng2e489c42009-12-16 00:53:11 +000011404static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11405 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11406 // (and (i32 x86isd::setcc_carry), 1)
11407 // This eliminates the zext. This transformation is necessary because
11408 // ISD::SETCC is always legalized to i8.
11409 DebugLoc dl = N->getDebugLoc();
11410 SDValue N0 = N->getOperand(0);
11411 EVT VT = N->getValueType(0);
11412 if (N0.getOpcode() == ISD::AND &&
11413 N0.hasOneUse() &&
11414 N0.getOperand(0).hasOneUse()) {
11415 SDValue N00 = N0.getOperand(0);
11416 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11417 return SDValue();
11418 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11419 if (!C || C->getZExtValue() != 1)
11420 return SDValue();
11421 return DAG.getNode(ISD::AND, dl, VT,
11422 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11423 N00.getOperand(0), N00.getOperand(1)),
11424 DAG.getConstant(1, VT));
11425 }
11426
11427 return SDValue();
11428}
11429
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011430// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11431static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11432 unsigned X86CC = N->getConstantOperandVal(0);
11433 SDValue EFLAG = N->getOperand(1);
11434 DebugLoc DL = N->getDebugLoc();
11435
11436 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11437 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11438 // cases.
11439 if (X86CC == X86::COND_B)
11440 return DAG.getNode(ISD::AND, DL, MVT::i8,
11441 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11442 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11443 DAG.getConstant(1, MVT::i8));
11444
11445 return SDValue();
11446}
11447
Dan Gohman475871a2008-07-27 21:46:04 +000011448SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011449 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011450 SelectionDAG &DAG = DCI.DAG;
11451 switch (N->getOpcode()) {
11452 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011453 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011454 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011455 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011456 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011457 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011458 case ISD::SHL:
11459 case ISD::SRA:
11460 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011461 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011462 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011463 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011464 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011465 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11466 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011467 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011468 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011469 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011470 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011471 case X86ISD::SHUFPS: // Handle all target specific shuffles
11472 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011473 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011474 case X86ISD::PUNPCKHBW:
11475 case X86ISD::PUNPCKHWD:
11476 case X86ISD::PUNPCKHDQ:
11477 case X86ISD::PUNPCKHQDQ:
11478 case X86ISD::UNPCKHPS:
11479 case X86ISD::UNPCKHPD:
11480 case X86ISD::PUNPCKLBW:
11481 case X86ISD::PUNPCKLWD:
11482 case X86ISD::PUNPCKLDQ:
11483 case X86ISD::PUNPCKLQDQ:
11484 case X86ISD::UNPCKLPS:
11485 case X86ISD::UNPCKLPD:
11486 case X86ISD::MOVHLPS:
11487 case X86ISD::MOVLHPS:
11488 case X86ISD::PSHUFD:
11489 case X86ISD::PSHUFHW:
11490 case X86ISD::PSHUFLW:
11491 case X86ISD::MOVSS:
11492 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011493 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011494 }
11495
Dan Gohman475871a2008-07-27 21:46:04 +000011496 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011497}
11498
Evan Chenge5b51ac2010-04-17 06:13:15 +000011499/// isTypeDesirableForOp - Return true if the target has native support for
11500/// the specified value type and it is 'desirable' to use the type for the
11501/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11502/// instruction encodings are longer and some i16 instructions are slow.
11503bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11504 if (!isTypeLegal(VT))
11505 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011506 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011507 return true;
11508
11509 switch (Opc) {
11510 default:
11511 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011512 case ISD::LOAD:
11513 case ISD::SIGN_EXTEND:
11514 case ISD::ZERO_EXTEND:
11515 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011516 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011517 case ISD::SRL:
11518 case ISD::SUB:
11519 case ISD::ADD:
11520 case ISD::MUL:
11521 case ISD::AND:
11522 case ISD::OR:
11523 case ISD::XOR:
11524 return false;
11525 }
11526}
11527
11528/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011529/// beneficial for dag combiner to promote the specified node. If true, it
11530/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011531bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011532 EVT VT = Op.getValueType();
11533 if (VT != MVT::i16)
11534 return false;
11535
Evan Cheng4c26e932010-04-19 19:29:22 +000011536 bool Promote = false;
11537 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011538 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011539 default: break;
11540 case ISD::LOAD: {
11541 LoadSDNode *LD = cast<LoadSDNode>(Op);
11542 // If the non-extending load has a single use and it's not live out, then it
11543 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011544 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11545 Op.hasOneUse()*/) {
11546 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11547 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11548 // The only case where we'd want to promote LOAD (rather then it being
11549 // promoted as an operand is when it's only use is liveout.
11550 if (UI->getOpcode() != ISD::CopyToReg)
11551 return false;
11552 }
11553 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011554 Promote = true;
11555 break;
11556 }
11557 case ISD::SIGN_EXTEND:
11558 case ISD::ZERO_EXTEND:
11559 case ISD::ANY_EXTEND:
11560 Promote = true;
11561 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011562 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011563 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011564 SDValue N0 = Op.getOperand(0);
11565 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011566 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011567 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011568 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011569 break;
11570 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011571 case ISD::ADD:
11572 case ISD::MUL:
11573 case ISD::AND:
11574 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011575 case ISD::XOR:
11576 Commute = true;
11577 // fallthrough
11578 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011579 SDValue N0 = Op.getOperand(0);
11580 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011581 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011582 return false;
11583 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011584 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011585 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011586 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011587 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011588 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011589 }
11590 }
11591
11592 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011593 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011594}
11595
Evan Cheng60c07e12006-07-05 22:17:51 +000011596//===----------------------------------------------------------------------===//
11597// X86 Inline Assembly Support
11598//===----------------------------------------------------------------------===//
11599
Chris Lattnerb8105652009-07-20 17:51:36 +000011600static bool LowerToBSwap(CallInst *CI) {
11601 // FIXME: this should verify that we are targetting a 486 or better. If not,
11602 // we will turn this bswap into something that will be lowered to logical ops
11603 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11604 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000011605
Chris Lattnerb8105652009-07-20 17:51:36 +000011606 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000011607 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011608 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011609 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000011610 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011611
Chris Lattnerb8105652009-07-20 17:51:36 +000011612 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11613 if (!Ty || Ty->getBitWidth() % 16 != 0)
11614 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011615
Chris Lattnerb8105652009-07-20 17:51:36 +000011616 // Okay, we can do this xform, do so now.
11617 const Type *Tys[] = { Ty };
11618 Module *M = CI->getParent()->getParent()->getParent();
11619 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000011620
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011621 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000011622 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000011623
Chris Lattnerb8105652009-07-20 17:51:36 +000011624 CI->replaceAllUsesWith(Op);
11625 CI->eraseFromParent();
11626 return true;
11627}
11628
11629bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11630 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
John Thompson44ab89e2010-10-29 17:29:13 +000011631 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
Chris Lattnerb8105652009-07-20 17:51:36 +000011632
11633 std::string AsmStr = IA->getAsmString();
11634
11635 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011636 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011637 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011638
11639 switch (AsmPieces.size()) {
11640 default: return false;
11641 case 1:
11642 AsmStr = AsmPieces[0];
11643 AsmPieces.clear();
11644 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11645
11646 // bswap $0
11647 if (AsmPieces.size() == 2 &&
11648 (AsmPieces[0] == "bswap" ||
11649 AsmPieces[0] == "bswapq" ||
11650 AsmPieces[0] == "bswapl") &&
11651 (AsmPieces[1] == "$0" ||
11652 AsmPieces[1] == "${0:q}")) {
11653 // No need to check constraints, nothing other than the equivalent of
11654 // "=r,0" would be valid here.
11655 return LowerToBSwap(CI);
11656 }
11657 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011658 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011659 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011660 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011661 AsmPieces[1] == "$$8," &&
11662 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011663 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11664 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000011665 const std::string &Constraints = IA->getConstraintString();
11666 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011667 std::sort(AsmPieces.begin(), AsmPieces.end());
11668 if (AsmPieces.size() == 4 &&
11669 AsmPieces[0] == "~{cc}" &&
11670 AsmPieces[1] == "~{dirflag}" &&
11671 AsmPieces[2] == "~{flags}" &&
11672 AsmPieces[3] == "~{fpsr}") {
11673 return LowerToBSwap(CI);
11674 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011675 }
11676 break;
11677 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011678 if (CI->getType()->isIntegerTy(32) &&
11679 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11680 SmallVector<StringRef, 4> Words;
11681 SplitString(AsmPieces[0], Words, " \t,");
11682 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11683 Words[2] == "${0:w}") {
11684 Words.clear();
11685 SplitString(AsmPieces[1], Words, " \t,");
11686 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11687 Words[2] == "$0") {
11688 Words.clear();
11689 SplitString(AsmPieces[2], Words, " \t,");
11690 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11691 Words[2] == "${0:w}") {
11692 AsmPieces.clear();
11693 const std::string &Constraints = IA->getConstraintString();
11694 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
11695 std::sort(AsmPieces.begin(), AsmPieces.end());
11696 if (AsmPieces.size() == 4 &&
11697 AsmPieces[0] == "~{cc}" &&
11698 AsmPieces[1] == "~{dirflag}" &&
11699 AsmPieces[2] == "~{flags}" &&
11700 AsmPieces[3] == "~{fpsr}") {
11701 return LowerToBSwap(CI);
11702 }
11703 }
11704 }
11705 }
11706 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011707 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000011708 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011709 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11710 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11711 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011712 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000011713 SplitString(AsmPieces[0], Words, " \t");
11714 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
11715 Words.clear();
11716 SplitString(AsmPieces[1], Words, " \t");
11717 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11718 Words.clear();
11719 SplitString(AsmPieces[2], Words, " \t,");
11720 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11721 Words[2] == "%edx") {
11722 return LowerToBSwap(CI);
11723 }
11724 }
11725 }
11726 }
11727 break;
11728 }
11729 return false;
11730}
11731
11732
11733
Chris Lattnerf4dff842006-07-11 02:54:03 +000011734/// getConstraintType - Given a constraint letter, return the type of
11735/// constraint it is for this target.
11736X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011737X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11738 if (Constraint.size() == 1) {
11739 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011740 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011741 case 'q':
11742 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011743 case 'f':
11744 case 't':
11745 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011746 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011747 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011748 case 'Y':
11749 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011750 case 'a':
11751 case 'b':
11752 case 'c':
11753 case 'd':
11754 case 'S':
11755 case 'D':
11756 case 'A':
11757 return C_Register;
11758 case 'I':
11759 case 'J':
11760 case 'K':
11761 case 'L':
11762 case 'M':
11763 case 'N':
11764 case 'G':
11765 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000011766 case 'e':
11767 case 'Z':
11768 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011769 default:
11770 break;
11771 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011772 }
Chris Lattner4234f572007-03-25 02:14:49 +000011773 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011774}
11775
John Thompson44ab89e2010-10-29 17:29:13 +000011776/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000011777/// This object must already have been set up with the operand type
11778/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000011779TargetLowering::ConstraintWeight
11780 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000011781 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000011782 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011783 Value *CallOperandVal = info.CallOperandVal;
11784 // If we don't have a value, we can't do a match,
11785 // but allow it at the lowest weight.
11786 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000011787 return CW_Default;
11788 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000011789 // Look at the constraint type.
11790 switch (*constraint) {
11791 default:
John Thompson44ab89e2010-10-29 17:29:13 +000011792 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11793 case 'R':
11794 case 'q':
11795 case 'Q':
11796 case 'a':
11797 case 'b':
11798 case 'c':
11799 case 'd':
11800 case 'S':
11801 case 'D':
11802 case 'A':
11803 if (CallOperandVal->getType()->isIntegerTy())
11804 weight = CW_SpecificReg;
11805 break;
11806 case 'f':
11807 case 't':
11808 case 'u':
11809 if (type->isFloatingPointTy())
11810 weight = CW_SpecificReg;
11811 break;
11812 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000011813 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000011814 weight = CW_SpecificReg;
11815 break;
11816 case 'x':
11817 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011818 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000011819 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011820 break;
11821 case 'I':
11822 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
11823 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000011824 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011825 }
11826 break;
John Thompson44ab89e2010-10-29 17:29:13 +000011827 case 'J':
11828 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11829 if (C->getZExtValue() <= 63)
11830 weight = CW_Constant;
11831 }
11832 break;
11833 case 'K':
11834 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11835 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
11836 weight = CW_Constant;
11837 }
11838 break;
11839 case 'L':
11840 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11841 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
11842 weight = CW_Constant;
11843 }
11844 break;
11845 case 'M':
11846 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11847 if (C->getZExtValue() <= 3)
11848 weight = CW_Constant;
11849 }
11850 break;
11851 case 'N':
11852 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11853 if (C->getZExtValue() <= 0xff)
11854 weight = CW_Constant;
11855 }
11856 break;
11857 case 'G':
11858 case 'C':
11859 if (dyn_cast<ConstantFP>(CallOperandVal)) {
11860 weight = CW_Constant;
11861 }
11862 break;
11863 case 'e':
11864 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11865 if ((C->getSExtValue() >= -0x80000000LL) &&
11866 (C->getSExtValue() <= 0x7fffffffLL))
11867 weight = CW_Constant;
11868 }
11869 break;
11870 case 'Z':
11871 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11872 if (C->getZExtValue() <= 0xffffffff)
11873 weight = CW_Constant;
11874 }
11875 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011876 }
11877 return weight;
11878}
11879
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011880/// LowerXConstraint - try to replace an X constraint, which matches anything,
11881/// with another that has more specific requirements based on the type of the
11882/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000011883const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000011884LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000011885 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
11886 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000011887 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011888 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000011889 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011890 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000011891 return "x";
11892 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011893
Chris Lattner5e764232008-04-26 23:02:14 +000011894 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011895}
11896
Chris Lattner48884cd2007-08-25 00:47:38 +000011897/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11898/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000011899void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000011900 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000011901 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000011902 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011903 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000011904
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011905 switch (Constraint) {
11906 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000011907 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000011908 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011909 if (C->getZExtValue() <= 31) {
11910 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011911 break;
11912 }
Devang Patel84f7fd22007-03-17 00:13:28 +000011913 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011914 return;
Evan Cheng364091e2008-09-22 23:57:37 +000011915 case 'J':
11916 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011917 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000011918 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11919 break;
11920 }
11921 }
11922 return;
11923 case 'K':
11924 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011925 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000011926 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11927 break;
11928 }
11929 }
11930 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000011931 case 'N':
11932 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011933 if (C->getZExtValue() <= 255) {
11934 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011935 break;
11936 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000011937 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011938 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011939 case 'e': {
11940 // 32-bit signed value
11941 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011942 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11943 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011944 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011945 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000011946 break;
11947 }
11948 // FIXME gcc accepts some relocatable values here too, but only in certain
11949 // memory models; it's complicated.
11950 }
11951 return;
11952 }
11953 case 'Z': {
11954 // 32-bit unsigned value
11955 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011956 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11957 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011958 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11959 break;
11960 }
11961 }
11962 // FIXME gcc accepts some relocatable values here too, but only in certain
11963 // memory models; it's complicated.
11964 return;
11965 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011966 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011967 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000011968 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011969 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011970 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000011971 break;
11972 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011973
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011974 // In any sort of PIC mode addresses need to be computed at runtime by
11975 // adding in a register or some sort of table lookup. These can't
11976 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000011977 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011978 return;
11979
Chris Lattnerdc43a882007-05-03 16:52:29 +000011980 // If we are in non-pic codegen mode, we allow the address of a global (with
11981 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000011982 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011983 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000011984
Chris Lattner49921962009-05-08 18:23:14 +000011985 // Match either (GA), (GA+C), (GA+C1+C2), etc.
11986 while (1) {
11987 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
11988 Offset += GA->getOffset();
11989 break;
11990 } else if (Op.getOpcode() == ISD::ADD) {
11991 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11992 Offset += C->getZExtValue();
11993 Op = Op.getOperand(0);
11994 continue;
11995 }
11996 } else if (Op.getOpcode() == ISD::SUB) {
11997 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11998 Offset += -C->getZExtValue();
11999 Op = Op.getOperand(0);
12000 continue;
12001 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012002 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012003
Chris Lattner49921962009-05-08 18:23:14 +000012004 // Otherwise, this isn't something we can handle, reject it.
12005 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012006 }
Eric Christopherfd179292009-08-27 18:07:15 +000012007
Dan Gohman46510a72010-04-15 01:51:59 +000012008 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012009 // If we require an extra load to get this address, as in PIC mode, we
12010 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012011 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12012 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012013 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012014
Devang Patel0d881da2010-07-06 22:08:15 +000012015 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12016 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012017 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012018 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012019 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012020
Gabor Greifba36cb52008-08-28 21:40:38 +000012021 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012022 Ops.push_back(Result);
12023 return;
12024 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012025 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012026}
12027
Chris Lattner259e97c2006-01-31 19:43:35 +000012028std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012029getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012030 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012031 if (Constraint.size() == 1) {
12032 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012033 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012034 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012035 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12036 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012037 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012038 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12039 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12040 X86::R10D,X86::R11D,X86::R12D,
12041 X86::R13D,X86::R14D,X86::R15D,
12042 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012043 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012044 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12045 X86::SI, X86::DI, X86::R8W,X86::R9W,
12046 X86::R10W,X86::R11W,X86::R12W,
12047 X86::R13W,X86::R14W,X86::R15W,
12048 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012049 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012050 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12051 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12052 X86::R10B,X86::R11B,X86::R12B,
12053 X86::R13B,X86::R14B,X86::R15B,
12054 X86::BPL, X86::SPL, 0);
12055
Owen Anderson825b72b2009-08-11 20:47:22 +000012056 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012057 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12058 X86::RSI, X86::RDI, X86::R8, X86::R9,
12059 X86::R10, X86::R11, X86::R12,
12060 X86::R13, X86::R14, X86::R15,
12061 X86::RBP, X86::RSP, 0);
12062
12063 break;
12064 }
Eric Christopherfd179292009-08-27 18:07:15 +000012065 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012066 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012067 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012068 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012069 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012070 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012071 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012072 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012073 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012074 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12075 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012076 }
12077 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012078
Chris Lattner1efa40f2006-02-22 00:56:39 +000012079 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012080}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012081
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012082std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012083X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012084 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012085 // First, see if this is a constraint that directly corresponds to an LLVM
12086 // register class.
12087 if (Constraint.size() == 1) {
12088 // GCC Constraint Letters
12089 switch (Constraint[0]) {
12090 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012091 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012092 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012093 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012094 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012095 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012096 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012097 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012098 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012099 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012100 case 'R': // LEGACY_REGS
12101 if (VT == MVT::i8)
12102 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12103 if (VT == MVT::i16)
12104 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12105 if (VT == MVT::i32 || !Subtarget->is64Bit())
12106 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12107 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012108 case 'f': // FP Stack registers.
12109 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12110 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012111 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012112 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012113 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012114 return std::make_pair(0U, X86::RFP64RegisterClass);
12115 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012116 case 'y': // MMX_REGS if MMX allowed.
12117 if (!Subtarget->hasMMX()) break;
12118 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012119 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012120 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012121 // FALL THROUGH.
12122 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012123 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012124
Owen Anderson825b72b2009-08-11 20:47:22 +000012125 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012126 default: break;
12127 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012128 case MVT::f32:
12129 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012130 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012131 case MVT::f64:
12132 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012133 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012134 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012135 case MVT::v16i8:
12136 case MVT::v8i16:
12137 case MVT::v4i32:
12138 case MVT::v2i64:
12139 case MVT::v4f32:
12140 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012141 return std::make_pair(0U, X86::VR128RegisterClass);
12142 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012143 break;
12144 }
12145 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012146
Chris Lattnerf76d1802006-07-31 23:26:50 +000012147 // Use the default implementation in TargetLowering to convert the register
12148 // constraint into a member of a register class.
12149 std::pair<unsigned, const TargetRegisterClass*> Res;
12150 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012151
12152 // Not found as a standard register?
12153 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012154 // Map st(0) -> st(7) -> ST0
12155 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12156 tolower(Constraint[1]) == 's' &&
12157 tolower(Constraint[2]) == 't' &&
12158 Constraint[3] == '(' &&
12159 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12160 Constraint[5] == ')' &&
12161 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012162
Chris Lattner56d77c72009-09-13 22:41:48 +000012163 Res.first = X86::ST0+Constraint[4]-'0';
12164 Res.second = X86::RFP80RegisterClass;
12165 return Res;
12166 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012167
Chris Lattner56d77c72009-09-13 22:41:48 +000012168 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012169 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012170 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012171 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012172 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012173 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012174
12175 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012176 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012177 Res.first = X86::EFLAGS;
12178 Res.second = X86::CCRRegisterClass;
12179 return Res;
12180 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012181
Dale Johannesen330169f2008-11-13 21:52:36 +000012182 // 'A' means EAX + EDX.
12183 if (Constraint == "A") {
12184 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012185 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012186 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012187 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012188 return Res;
12189 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012190
Chris Lattnerf76d1802006-07-31 23:26:50 +000012191 // Otherwise, check to see if this is a register class of the wrong value
12192 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12193 // turn into {ax},{dx}.
12194 if (Res.second->hasType(VT))
12195 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012196
Chris Lattnerf76d1802006-07-31 23:26:50 +000012197 // All of the single-register GCC register classes map their values onto
12198 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12199 // really want an 8-bit or 32-bit register, map to the appropriate register
12200 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012201 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012202 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012203 unsigned DestReg = 0;
12204 switch (Res.first) {
12205 default: break;
12206 case X86::AX: DestReg = X86::AL; break;
12207 case X86::DX: DestReg = X86::DL; break;
12208 case X86::CX: DestReg = X86::CL; break;
12209 case X86::BX: DestReg = X86::BL; break;
12210 }
12211 if (DestReg) {
12212 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012213 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012214 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012215 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012216 unsigned DestReg = 0;
12217 switch (Res.first) {
12218 default: break;
12219 case X86::AX: DestReg = X86::EAX; break;
12220 case X86::DX: DestReg = X86::EDX; break;
12221 case X86::CX: DestReg = X86::ECX; break;
12222 case X86::BX: DestReg = X86::EBX; break;
12223 case X86::SI: DestReg = X86::ESI; break;
12224 case X86::DI: DestReg = X86::EDI; break;
12225 case X86::BP: DestReg = X86::EBP; break;
12226 case X86::SP: DestReg = X86::ESP; break;
12227 }
12228 if (DestReg) {
12229 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012230 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012231 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012232 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012233 unsigned DestReg = 0;
12234 switch (Res.first) {
12235 default: break;
12236 case X86::AX: DestReg = X86::RAX; break;
12237 case X86::DX: DestReg = X86::RDX; break;
12238 case X86::CX: DestReg = X86::RCX; break;
12239 case X86::BX: DestReg = X86::RBX; break;
12240 case X86::SI: DestReg = X86::RSI; break;
12241 case X86::DI: DestReg = X86::RDI; break;
12242 case X86::BP: DestReg = X86::RBP; break;
12243 case X86::SP: DestReg = X86::RSP; break;
12244 }
12245 if (DestReg) {
12246 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012247 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012248 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012249 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012250 } else if (Res.second == X86::FR32RegisterClass ||
12251 Res.second == X86::FR64RegisterClass ||
12252 Res.second == X86::VR128RegisterClass) {
12253 // Handle references to XMM physical registers that got mapped into the
12254 // wrong class. This can happen with constraints like {xmm0} where the
12255 // target independent register mapper will just pick the first match it can
12256 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012257 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012258 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012259 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012260 Res.second = X86::FR64RegisterClass;
12261 else if (X86::VR128RegisterClass->hasType(VT))
12262 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012263 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012264
Chris Lattnerf76d1802006-07-31 23:26:50 +000012265 return Res;
12266}